]> git.saurik.com Git - apple/javascriptcore.git/blob - inspector/InjectedScriptBase.cpp
JavaScriptCore-7600.1.4.9.tar.gz
[apple/javascriptcore.git] / inspector / InjectedScriptBase.cpp
1 /*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Google Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "config.h"
33 #include "InjectedScriptBase.h"
34
35 #if ENABLE(INSPECTOR)
36
37 #include "InspectorValues.h"
38 #include "JSCInlines.h"
39 #include "JSGlobalObject.h"
40 #include "ScriptFunctionCall.h"
41 #include <wtf/text/WTFString.h>
42
43 namespace Inspector {
44
45 InjectedScriptBase::InjectedScriptBase(const String& name)
46 : m_name(name)
47 , m_environment(nullptr)
48 {
49 }
50
51 InjectedScriptBase::InjectedScriptBase(const String& name, Deprecated::ScriptObject injectedScriptObject, InspectorEnvironment* environment)
52 : m_name(name)
53 , m_injectedScriptObject(injectedScriptObject)
54 , m_environment(environment)
55 {
56 }
57
58 InjectedScriptBase::~InjectedScriptBase()
59 {
60 }
61
62 void InjectedScriptBase::initialize(Deprecated::ScriptObject injectedScriptObject, InspectorEnvironment* environment)
63 {
64 m_injectedScriptObject = injectedScriptObject;
65 m_environment = environment;
66 }
67
68 bool InjectedScriptBase::hasAccessToInspectedScriptState() const
69 {
70 return m_environment && m_environment->canAccessInspectedScriptState(m_injectedScriptObject.scriptState());
71 }
72
73 const Deprecated::ScriptObject& InjectedScriptBase::injectedScriptObject() const
74 {
75 return m_injectedScriptObject;
76 }
77
78 Deprecated::ScriptValue InjectedScriptBase::callFunctionWithEvalEnabled(Deprecated::ScriptFunctionCall& function, bool& hadException) const
79 {
80 if (m_environment)
81 m_environment->willCallInjectedScriptFunction(m_injectedScriptObject.scriptState(), name(), 1);
82
83 JSC::ExecState* scriptState = m_injectedScriptObject.scriptState();
84 bool evalIsDisabled = false;
85 if (scriptState) {
86 evalIsDisabled = !scriptState->lexicalGlobalObject()->evalEnabled();
87 // Temporarily enable allow evals for inspector.
88 if (evalIsDisabled)
89 scriptState->lexicalGlobalObject()->setEvalEnabled(true);
90 }
91
92 Deprecated::ScriptValue resultValue = function.call(hadException);
93
94 if (evalIsDisabled)
95 scriptState->lexicalGlobalObject()->setEvalEnabled(false);
96
97 if (m_environment)
98 m_environment->didCallInjectedScriptFunction(m_injectedScriptObject.scriptState());
99
100 return resultValue;
101 }
102
103 void InjectedScriptBase::makeCall(Deprecated::ScriptFunctionCall& function, RefPtr<InspectorValue>* result)
104 {
105 if (hasNoValue() || !hasAccessToInspectedScriptState()) {
106 *result = InspectorValue::null();
107 return;
108 }
109
110 bool hadException = false;
111 Deprecated::ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
112
113 ASSERT(!hadException);
114 if (!hadException) {
115 *result = resultValue.toInspectorValue(m_injectedScriptObject.scriptState());
116 if (!*result)
117 *result = InspectorString::create(String::format("Object has too long reference chain (must not be longer than %d)", InspectorValue::maxDepth));
118 } else
119 *result = InspectorString::create("Exception while making a call.");
120 }
121
122 void InjectedScriptBase::makeEvalCall(ErrorString* errorString, Deprecated::ScriptFunctionCall& function, RefPtr<TypeBuilder::Runtime::RemoteObject>* objectResult, TypeBuilder::OptOutput<bool>* wasThrown)
123 {
124 RefPtr<InspectorValue> result;
125 makeCall(function, &result);
126 if (!result) {
127 *errorString = ASCIILiteral("Internal error: result value is empty");
128 return;
129 }
130
131 if (result->type() == InspectorValue::TypeString) {
132 result->asString(errorString);
133 ASSERT(errorString->length());
134 return;
135 }
136
137 RefPtr<InspectorObject> resultPair = result->asObject();
138 if (!resultPair) {
139 *errorString = ASCIILiteral("Internal error: result is not an Object");
140 return;
141 }
142
143 RefPtr<InspectorObject> resultObj = resultPair->getObject(ASCIILiteral("result"));
144 bool wasThrownVal = false;
145 if (!resultObj || !resultPair->getBoolean(ASCIILiteral("wasThrown"), &wasThrownVal)) {
146 *errorString = ASCIILiteral("Internal error: result is not a pair of value and wasThrown flag");
147 return;
148 }
149
150 *objectResult = TypeBuilder::Runtime::RemoteObject::runtimeCast(resultObj);
151 *wasThrown = wasThrownVal;
152 }
153
154 } // namespace Inspector
155
156 #endif // ENABLE(INSPECTOR)