Upstream version 8.37.180.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 "core/inspector/InspectorInstrumentation.h"
38 #include "core/inspector/InspectorTraceEvents.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, ScriptValue injectedScriptObject, InspectedStateAccessCheck accessCheck)
53     : m_name(name)
54     , m_injectedScriptObject(injectedScriptObject)
55     , m_inspectedStateAccessCheck(accessCheck)
56 {
57 }
58
59 void InjectedScriptBase::initialize(ScriptValue injectedScriptObject, InspectedStateAccessCheck accessCheck)
60 {
61     m_injectedScriptObject = injectedScriptObject;
62     m_inspectedStateAccessCheck = accessCheck;
63 }
64
65 bool InjectedScriptBase::canAccessInspectedWindow() const
66 {
67     ASSERT(!isEmpty());
68     return m_inspectedStateAccessCheck(m_injectedScriptObject.scriptState());
69 }
70
71 const ScriptValue& InjectedScriptBase::injectedScriptObject() const
72 {
73     return m_injectedScriptObject;
74 }
75
76 ScriptValue InjectedScriptBase::callFunctionWithEvalEnabled(ScriptFunctionCall& function, bool& hadException) const
77 {
78     ASSERT(!isEmpty());
79     ExecutionContext* executionContext = m_injectedScriptObject.scriptState()->executionContext();
80     TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "FunctionCall", "data", InspectorFunctionCallEvent::data(executionContext, 0, name(), 1));
81     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
82     // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
83     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willCallFunction(executionContext, 0, name(), 1);
84
85     ScriptState* scriptState = m_injectedScriptObject.scriptState();
86     bool evalIsDisabled = false;
87     if (scriptState) {
88         evalIsDisabled = !scriptState->evalEnabled();
89         // Temporarily enable allow evals for inspector.
90         if (evalIsDisabled)
91             scriptState->setEvalEnabled(true);
92     }
93
94     ScriptValue resultValue = function.call(hadException);
95
96     if (evalIsDisabled)
97         scriptState->setEvalEnabled(false);
98
99     InspectorInstrumentation::didCallFunction(cookie);
100     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "UpdateCounters", "data", InspectorUpdateCountersEvent::data());
101     return resultValue;
102 }
103
104 void InjectedScriptBase::makeCall(ScriptFunctionCall& function, RefPtr<JSONValue>* result)
105 {
106     if (isEmpty() || !canAccessInspectedWindow()) {
107         *result = JSONValue::null();
108         return;
109     }
110
111     bool hadException = false;
112     ScriptValue resultValue = callFunctionWithEvalEnabled(function, hadException);
113
114     ASSERT(!hadException);
115     if (!hadException) {
116         *result = resultValue.toJSONValue(m_injectedScriptObject.scriptState());
117         if (!*result)
118             *result = JSONString::create(String::format("Object has too long reference chain(must not be longer than %d)", JSONValue::maxDepth));
119     } else {
120         *result = JSONString::create("Exception while making a call.");
121     }
122 }
123
124 void InjectedScriptBase::makeEvalCall(ErrorString* errorString, ScriptFunctionCall& function, RefPtr<TypeBuilder::Runtime::RemoteObject>* objectResult, TypeBuilder::OptOutput<bool>* wasThrown)
125 {
126     RefPtr<JSONValue> result;
127     makeCall(function, &result);
128     if (!result) {
129         *errorString = "Internal error: result value is empty";
130         return;
131     }
132     if (result->type() == JSONValue::TypeString) {
133         result->asString(errorString);
134         ASSERT(errorString->length());
135         return;
136     }
137     RefPtr<JSONObject> resultPair = result->asObject();
138     if (!resultPair) {
139         *errorString = "Internal error: result is not an Object";
140         return;
141     }
142     RefPtr<JSONObject> resultObj = resultPair->getObject("result");
143     bool wasThrownVal = false;
144     if (!resultObj || !resultPair->getBoolean("wasThrown", &wasThrownVal)) {
145         *errorString = "Internal error: result is not a pair of value and wasThrown flag";
146         return;
147     }
148     *objectResult = TypeBuilder::Runtime::RemoteObject::runtimeCast(resultObj);
149     *wasThrown = wasThrownVal;
150 }
151
152 } // namespace WebCore
153