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 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()); 
    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;
}