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