InjectedScript InjectedScriptManager::injectedScriptFor(ScriptState* inspectedScriptState) { v8::HandleScope handleScope; v8::Local<v8::Context> context = inspectedScriptState->context(); v8::Context::Scope contextScope(context); v8::Local<v8::Object> global = context->Global(); // Skip proxy object. The proxy object will survive page navigation while we need // an object whose lifetime consides with that of the inspected context. global = v8::Local<v8::Object>::Cast(global->GetPrototype()); v8::Handle<v8::String> key = V8HiddenPropertyName::devtoolsInjectedScript(); v8::Local<v8::Value> val = global->GetHiddenValue(key); if (!val.IsEmpty() && val->IsObject()) return InjectedScript(ScriptObject(inspectedScriptState, v8::Local<v8::Object>::Cast(val)), m_inspectedStateAccessCheck); if (!m_inspectedStateAccessCheck(inspectedScriptState)) return InjectedScript(); pair<long, ScriptObject> injectedScript = injectScript(injectedScriptSource(), inspectedScriptState); InjectedScript result(injectedScript.second, m_inspectedStateAccessCheck); m_idToInjectedScript.set(injectedScript.first, result); global->SetHiddenValue(key, injectedScript.second.v8Object()); return result; }
InjectedScript InjectedScriptManager::injectedScriptForObjectId(const String& objectId) { RefPtr<InspectorValue> parsedObjectId; if (!InspectorValue::parseJSON(objectId, parsedObjectId)) return InjectedScript(); RefPtr<InspectorObject> resultObject; if (!parsedObjectId->asObject(resultObject)) return InjectedScript(); long injectedScriptId = 0; if (!resultObject->getInteger(ASCIILiteral("injectedScriptId"), injectedScriptId)) return InjectedScript(); return m_idToInjectedScript.get(injectedScriptId); }
InjectedScript InjectedScriptManager::injectedScriptFor(ExecState* inspectedExecState) { auto it = m_scriptStateToId.find(inspectedExecState); if (it != m_scriptStateToId.end()) { auto it1 = m_idToInjectedScript.find(it->value); if (it1 != m_idToInjectedScript.end()) return it1->value; } if (!m_environment.canAccessInspectedScriptState(inspectedExecState)) return InjectedScript(); int id = injectedScriptIdFor(inspectedExecState); Deprecated::ScriptObject injectedScriptObject = createInjectedScript(injectedScriptSource(), inspectedExecState, id); if (injectedScriptObject.scriptState() != inspectedExecState) { WTFLogAlways("Failed to parse/execute InjectedScriptSource.js!"); WTFLogAlways("%s\n", injectedScriptSource().ascii().data()); RELEASE_ASSERT_NOT_REACHED(); } InjectedScript result(injectedScriptObject, &m_environment); m_idToInjectedScript.set(id, result); didCreateInjectedScript(result); return result; }
InjectedScript WorkerRuntimeAgent::injectedScriptForEval(ErrorString* error, const int* executionContextId) { if (executionContextId) { *error = "Execution context id is not supported for workers as there is only one execution context."; return InjectedScript(); } ScriptState* scriptState = scriptStateFromWorkerGlobalScope(m_workerGlobalScope); return injectedScriptManager()->injectedScriptFor(scriptState); }
InjectedScript InjectedScriptHost::injectedScriptFor(ScriptState* scriptState) { JSLock lock(SilenceAssertionsOnly); JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject()); JSObject* injectedScript = globalObject->injectedScript(); if (injectedScript) return InjectedScript(ScriptObject(scriptState, injectedScript)); if (!canAccessInspectedWindow(scriptState)) return InjectedScript(); ASSERT(!m_injectedScriptSource.isEmpty()); pair<long, ScriptObject> injectedScriptObject = injectScript(m_injectedScriptSource, scriptState); globalObject->setInjectedScript(injectedScriptObject.second.jsObject()); InjectedScript result(injectedScriptObject.second); m_idToInjectedScript.set(injectedScriptObject.first, result); return result; }
InjectedScript WorkerDebuggerAgent::injectedScriptForEval(ErrorString& error, const int* executionContextId) { if (executionContextId) { error = ASCIILiteral("Execution context id is not supported for workers as there is only one execution context."); return InjectedScript(); } JSC::ExecState* scriptState = execStateFromWorkerGlobalScope(&m_inspectedWorkerGlobalScope); return injectedScriptManager().injectedScriptFor(scriptState); }
InjectedScript JSGlobalObjectDebuggerAgent::injectedScriptForEval(ErrorString* error, const int* executionContextId) { if (executionContextId) { *error = ASCIILiteral("Execution context id is not supported for JSContext inspection as there is only one execution context."); return InjectedScript(); } ExecState* exec = m_scriptDebugServer.globalObject().globalExec(); return injectedScriptManager()->injectedScriptFor(exec); }
InjectedScript InjectedScriptManager::injectedScriptForObjectId(const String& objectId) { RefPtr<InspectorValue> parsedObjectId = InspectorValue::parseJSON(objectId); if (parsedObjectId && parsedObjectId->type() == InspectorValue::TypeObject) { long injectedScriptId = 0; bool success = parsedObjectId->asObject()->getNumber("injectedScriptId", &injectedScriptId); if (success) return m_idToInjectedScript.get(injectedScriptId); } return InjectedScript(); }
InjectedScript InjectedScriptManager::injectedScriptForId(int id) { IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id); if (it != m_idToInjectedScript.end()) return it->value; for (ExecStateToId::iterator it = m_scriptStateToId.begin(); it != m_scriptStateToId.end(); ++it) { if (it->value == id) return injectedScriptFor(it->key); } return InjectedScript(); }
InjectedScript InjectedScriptManager::injectedScriptForId(int id) { IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id); if (it != m_idToInjectedScript.end()) return it->second; for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scriptStateToId.end(); ++it) { if (it->second == id) return injectedScriptFor(it->first); } return InjectedScript(); }
InjectedScript InjectedScriptHost::injectedScriptFor(ScriptState* scriptState) { JSLock lock(SilenceAssertionsOnly); JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject()); JSObject* injectedScript = globalObject->injectedScript(); if (injectedScript) return InjectedScript(ScriptObject(scriptState, injectedScript)); ASSERT(!m_injectedScriptSource.isEmpty()); ScriptObject injectedScriptObject = createInjectedScript(m_injectedScriptSource, this, scriptState, m_nextInjectedScriptId); globalObject->setInjectedScript(injectedScriptObject.jsObject()); InjectedScript result(injectedScriptObject); m_idToInjectedScript.set(m_nextInjectedScriptId, result); m_nextInjectedScriptId++; return result; }
InjectedScript InjectedScriptManager::injectedScriptFor(JSC::ExecState* inspectedExecState) { ExecStateToId::iterator it = m_scriptStateToId.find(inspectedExecState); if (it != m_scriptStateToId.end()) { IdToInjectedScriptMap::iterator it1 = m_idToInjectedScript.find(it->value); if (it1 != m_idToInjectedScript.end()) return it1->value; } if (!m_inspectedStateAccessCheck(inspectedExecState)) return InjectedScript(); int id = injectedScriptIdFor(inspectedExecState); ScriptObject injectedScriptObject = createInjectedScript(injectedScriptSource(), inspectedExecState, id); InjectedScript result(injectedScriptObject, m_inspectedStateAccessCheck); m_idToInjectedScript.set(id, result); return result; }