2 * Copyright (C) 2007, 2008, 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4 * Copyright (C) 2012 Google Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
16 * its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "InjectedScriptManager.h"
36 #include "Completion.h"
37 #include "InjectedScriptHost.h"
38 #include "InjectedScriptSource.h"
39 #include "InspectorValues.h"
40 #include "JSInjectedScriptHost.h"
42 #include "ScriptObject.h"
43 #include "SourceCode.h"
49 InjectedScriptManager::InjectedScriptManager(InspectorEnvironment
& environment
, PassRefPtr
<InjectedScriptHost
> injectedScriptHost
)
50 : m_environment(environment
)
51 , m_injectedScriptHost(injectedScriptHost
)
52 , m_nextInjectedScriptId(1)
56 InjectedScriptManager::~InjectedScriptManager()
60 void InjectedScriptManager::disconnect()
62 discardInjectedScripts();
65 InjectedScriptHost
* InjectedScriptManager::injectedScriptHost()
67 return m_injectedScriptHost
.get();
70 InjectedScript
InjectedScriptManager::injectedScriptForId(int id
)
72 auto it
= m_idToInjectedScript
.find(id
);
73 if (it
!= m_idToInjectedScript
.end())
76 for (auto it
= m_scriptStateToId
.begin(); it
!= m_scriptStateToId
.end(); ++it
) {
78 return injectedScriptFor(it
->key
);
81 return InjectedScript();
84 int InjectedScriptManager::injectedScriptIdFor(ExecState
* scriptState
)
86 auto it
= m_scriptStateToId
.find(scriptState
);
87 if (it
!= m_scriptStateToId
.end())
90 int id
= m_nextInjectedScriptId
++;
91 m_scriptStateToId
.set(scriptState
, id
);
95 InjectedScript
InjectedScriptManager::injectedScriptForObjectId(const String
& objectId
)
97 RefPtr
<InspectorValue
> parsedObjectId
= InspectorValue::parseJSON(objectId
);
98 if (parsedObjectId
&& parsedObjectId
->type() == InspectorValue::TypeObject
) {
99 long injectedScriptId
= 0;
100 bool success
= parsedObjectId
->asObject()->getNumber(ASCIILiteral("injectedScriptId"), &injectedScriptId
);
102 return m_idToInjectedScript
.get(injectedScriptId
);
105 return InjectedScript();
108 void InjectedScriptManager::discardInjectedScripts()
110 m_injectedScriptHost
->clearAllWrappers();
111 m_idToInjectedScript
.clear();
112 m_scriptStateToId
.clear();
115 void InjectedScriptManager::releaseObjectGroup(const String
& objectGroup
)
117 for (auto it
= m_idToInjectedScript
.begin(); it
!= m_idToInjectedScript
.end(); ++it
)
118 it
->value
.releaseObjectGroup(objectGroup
);
121 String
InjectedScriptManager::injectedScriptSource()
123 return String(reinterpret_cast<const char*>(InjectedScriptSource_js
), sizeof(InjectedScriptSource_js
));
126 Deprecated::ScriptObject
InjectedScriptManager::createInjectedScript(const String
& source
, ExecState
* scriptState
, int id
)
128 JSLockHolder
lock(scriptState
);
130 SourceCode sourceCode
= makeSource(source
);
131 JSGlobalObject
* globalObject
= scriptState
->lexicalGlobalObject();
132 JSValue globalThisValue
= scriptState
->globalThisValue();
134 JSValue evaluationException
;
135 InspectorEvaluateHandler evaluateHandler
= m_environment
.evaluateHandler();
136 JSValue functionValue
= evaluateHandler(scriptState
, sourceCode
, globalThisValue
, &evaluationException
);
137 if (evaluationException
)
138 return Deprecated::ScriptObject();
141 CallType callType
= getCallData(functionValue
, callData
);
142 if (callType
== CallTypeNone
)
143 return Deprecated::ScriptObject();
145 MarkedArgumentBuffer args
;
146 args
.append(m_injectedScriptHost
->jsWrapper(scriptState
, globalObject
));
147 args
.append(globalThisValue
);
148 args
.append(jsNumber(id
));
150 JSValue result
= JSC::call(scriptState
, functionValue
, callType
, callData
, globalThisValue
, args
);
151 if (result
.isObject())
152 return Deprecated::ScriptObject(scriptState
, result
.getObject());
154 return Deprecated::ScriptObject();
157 InjectedScript
InjectedScriptManager::injectedScriptFor(ExecState
* inspectedExecState
)
159 auto it
= m_scriptStateToId
.find(inspectedExecState
);
160 if (it
!= m_scriptStateToId
.end()) {
161 auto it1
= m_idToInjectedScript
.find(it
->value
);
162 if (it1
!= m_idToInjectedScript
.end())
166 if (!m_environment
.canAccessInspectedScriptState(inspectedExecState
))
167 return InjectedScript();
169 int id
= injectedScriptIdFor(inspectedExecState
);
170 Deprecated::ScriptObject injectedScriptObject
= createInjectedScript(injectedScriptSource(), inspectedExecState
, id
);
171 InjectedScript
result(injectedScriptObject
, &m_environment
);
172 m_idToInjectedScript
.set(id
, result
);
173 didCreateInjectedScript(result
);
177 void InjectedScriptManager::didCreateInjectedScript(InjectedScript
)
179 // Intentionally empty. This allows for subclasses to inject additional scripts.
182 } // namespace Inspector
184 #endif // ENABLE(INSPECTOR)