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"
34 #include "Completion.h"
35 #include "InjectedScriptHost.h"
36 #include "InjectedScriptSource.h"
37 #include "InspectorValues.h"
38 #include "JSInjectedScriptHost.h"
40 #include "ScriptObject.h"
41 #include "SourceCode.h"
47 InjectedScriptManager::InjectedScriptManager(InspectorEnvironment
& environment
, PassRefPtr
<InjectedScriptHost
> injectedScriptHost
)
48 : m_environment(environment
)
49 , m_injectedScriptHost(injectedScriptHost
)
50 , m_nextInjectedScriptId(1)
54 InjectedScriptManager::~InjectedScriptManager()
58 void InjectedScriptManager::disconnect()
60 discardInjectedScripts();
63 InjectedScriptHost
* InjectedScriptManager::injectedScriptHost()
65 return m_injectedScriptHost
.get();
68 InjectedScript
InjectedScriptManager::injectedScriptForId(int id
)
70 auto it
= m_idToInjectedScript
.find(id
);
71 if (it
!= m_idToInjectedScript
.end())
74 for (auto it
= m_scriptStateToId
.begin(); it
!= m_scriptStateToId
.end(); ++it
) {
76 return injectedScriptFor(it
->key
);
79 return InjectedScript();
82 int InjectedScriptManager::injectedScriptIdFor(ExecState
* scriptState
)
84 auto it
= m_scriptStateToId
.find(scriptState
);
85 if (it
!= m_scriptStateToId
.end())
88 int id
= m_nextInjectedScriptId
++;
89 m_scriptStateToId
.set(scriptState
, id
);
93 InjectedScript
InjectedScriptManager::injectedScriptForObjectId(const String
& objectId
)
95 RefPtr
<InspectorValue
> parsedObjectId
;
96 if (!InspectorValue::parseJSON(objectId
, parsedObjectId
))
97 return InjectedScript();
99 RefPtr
<InspectorObject
> resultObject
;
100 if (!parsedObjectId
->asObject(resultObject
))
101 return InjectedScript();
103 long injectedScriptId
= 0;
104 if (!resultObject
->getInteger(ASCIILiteral("injectedScriptId"), injectedScriptId
))
105 return InjectedScript();
107 return m_idToInjectedScript
.get(injectedScriptId
);
110 void InjectedScriptManager::discardInjectedScripts()
112 m_injectedScriptHost
->clearAllWrappers();
113 m_idToInjectedScript
.clear();
114 m_scriptStateToId
.clear();
117 void InjectedScriptManager::releaseObjectGroup(const String
& objectGroup
)
119 for (auto& injectedScript
: m_idToInjectedScript
.values())
120 injectedScript
.releaseObjectGroup(objectGroup
);
123 void InjectedScriptManager::clearExceptionValue()
125 for (auto& injectedScript
: m_idToInjectedScript
.values())
126 injectedScript
.clearExceptionValue();
129 String
InjectedScriptManager::injectedScriptSource()
131 return StringImpl::createWithoutCopying(InjectedScriptSource_js
, sizeof(InjectedScriptSource_js
));
134 Deprecated::ScriptObject
InjectedScriptManager::createInjectedScript(const String
& source
, ExecState
* scriptState
, int id
)
136 JSLockHolder
lock(scriptState
);
138 SourceCode sourceCode
= makeSource(source
);
139 JSGlobalObject
* globalObject
= scriptState
->lexicalGlobalObject();
140 JSValue globalThisValue
= scriptState
->globalThisValue();
142 NakedPtr
<Exception
> evaluationException
;
143 InspectorEvaluateHandler evaluateHandler
= m_environment
.evaluateHandler();
144 JSValue functionValue
= evaluateHandler(scriptState
, sourceCode
, globalThisValue
, evaluationException
);
145 if (evaluationException
)
146 return Deprecated::ScriptObject();
149 CallType callType
= getCallData(functionValue
, callData
);
150 if (callType
== CallTypeNone
)
151 return Deprecated::ScriptObject();
153 MarkedArgumentBuffer args
;
154 args
.append(m_injectedScriptHost
->jsWrapper(scriptState
, globalObject
));
155 args
.append(globalThisValue
);
156 args
.append(jsNumber(id
));
158 JSValue result
= JSC::call(scriptState
, functionValue
, callType
, callData
, globalThisValue
, args
);
159 scriptState
->clearException();
160 if (result
.isObject())
161 return Deprecated::ScriptObject(scriptState
, result
.getObject());
163 return Deprecated::ScriptObject();
166 InjectedScript
InjectedScriptManager::injectedScriptFor(ExecState
* inspectedExecState
)
168 auto it
= m_scriptStateToId
.find(inspectedExecState
);
169 if (it
!= m_scriptStateToId
.end()) {
170 auto it1
= m_idToInjectedScript
.find(it
->value
);
171 if (it1
!= m_idToInjectedScript
.end())
175 if (!m_environment
.canAccessInspectedScriptState(inspectedExecState
))
176 return InjectedScript();
178 int id
= injectedScriptIdFor(inspectedExecState
);
179 Deprecated::ScriptObject injectedScriptObject
= createInjectedScript(injectedScriptSource(), inspectedExecState
, id
);
180 InjectedScript
result(injectedScriptObject
, &m_environment
);
181 m_idToInjectedScript
.set(id
, result
);
182 didCreateInjectedScript(result
);
186 void InjectedScriptManager::didCreateInjectedScript(InjectedScript
)
188 // Intentionally empty. This allows for subclasses to inject additional scripts.
191 } // namespace Inspector