1 /*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4 * Copyright (C) 2010-2011 Google Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "config.h"
34 #include "InjectedScriptManager.h"
35
36 #if ENABLE(INSPECTOR)
37
38 #include "ExceptionCode.h"
39 #include "InjectedScript.h"
40 #include "JSDOMWindow.h"
41 #include "JSDOMWindowCustom.h"
42 #include "JSInjectedScriptHost.h"
43 #include "JSMainThreadExecState.h"
44 #include <parser/SourceCode.h>
45 #include <runtime/JSLock.h>
46
47 using namespace JSC;
48
49 namespace WebCore {
50
createInjectedScript(const String & source,ScriptState * scriptState,long id)51 ScriptObject InjectedScriptManager::createInjectedScript(const String& source, ScriptState* scriptState, long id)
52 {
53 SourceCode sourceCode = makeSource(stringToUString(source));
54 JSLock lock(SilenceAssertionsOnly);
55 JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
56 JSValue globalThisValue = scriptState->globalThisValue();
57 Completion comp = JSMainThreadExecState::evaluate(scriptState, globalObject->globalScopeChain(), sourceCode, globalThisValue);
58 if (comp.complType() != JSC::Normal && comp.complType() != JSC::ReturnValue)
59 return ScriptObject();
60 JSValue functionValue = comp.value();
61 CallData callData;
62 CallType callType = getCallData(functionValue, callData);
63 if (callType == CallTypeNone)
64 return ScriptObject();
65
66 MarkedArgumentBuffer args;
67 args.append(toJS(scriptState, globalObject, m_injectedScriptHost.get()));
68 args.append(globalThisValue);
69 args.append(jsNumber(id));
70 JSValue result = JSC::call(scriptState, functionValue, callType, callData, globalThisValue, args);
71 if (result.isObject())
72 return ScriptObject(scriptState, result.getObject());
73 return ScriptObject();
74 }
75
discardInjectedScript(ScriptState * scriptState)76 void InjectedScriptManager::discardInjectedScript(ScriptState* scriptState)
77 {
78 JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
79 globalObject->setInjectedScript(0);
80 }
81
injectedScriptFor(ScriptState * scriptState)82 InjectedScript InjectedScriptManager::injectedScriptFor(ScriptState* scriptState)
83 {
84 JSLock lock(SilenceAssertionsOnly);
85 JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
86 JSObject* injectedScript = globalObject->injectedScript();
87 if (injectedScript)
88 return InjectedScript(ScriptObject(scriptState, injectedScript), m_inspectedStateAccessCheck);
89
90 if (!m_inspectedStateAccessCheck(scriptState))
91 return InjectedScript();
92
93 pair<long, ScriptObject> injectedScriptObject = injectScript(injectedScriptSource(), scriptState);
94 globalObject->setInjectedScript(injectedScriptObject.second.jsObject());
95 InjectedScript result(injectedScriptObject.second, m_inspectedStateAccessCheck);
96 m_idToInjectedScript.set(injectedScriptObject.first, result);
97 return result;
98 }
99
canAccessInspectedWindow(ScriptState * scriptState)100 bool InjectedScriptManager::canAccessInspectedWindow(ScriptState* scriptState)
101 {
102 JSLock lock(SilenceAssertionsOnly);
103 JSDOMWindow* inspectedWindow = toJSDOMWindow(scriptState->lexicalGlobalObject());
104 if (!inspectedWindow)
105 return false;
106 return inspectedWindow->allowsAccessFromNoErrorMessage(scriptState);
107 }
108
109 } // namespace WebCore
110
111 #endif // ENABLE(INSPECTOR)
112