Changeset 53135 in webkit


Ignore:
Timestamp:
Jan 12, 2010 4:41:42 AM (14 years ago)
Author:
yurys@chromium.org
Message:

2010-01-12 Yury Semikhatsky <yurys@chromium.org>

Reviewed by Pavel Feldman.

Support injection of inspector scripts into the inspected context.

https://bugs.webkit.org/show_bug.cgi?id=33523

  • bindings/v8/custom/V8InjectedScriptHostCustom.cpp: (WebCore::WeakReferenceCallback): (WebCore::createInjectedScriptHostV8Wrapper): (WebCore::createInjectedScript): (WebCore::InjectedScriptHost::injectedScriptFor):
Location:
trunk/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r53132 r53135  
     12010-01-12  Yury Semikhatsky  <yurys@chromium.org>
     2
     3        Reviewed by Pavel Feldman.
     4
     5        Support injection of inspector scripts into the inspected context.
     6
     7        https://bugs.webkit.org/show_bug.cgi?id=33523
     8
     9        * bindings/v8/custom/V8InjectedScriptHostCustom.cpp:
     10        (WebCore::WeakReferenceCallback):
     11        (WebCore::createInjectedScriptHostV8Wrapper):
     12        (WebCore::createInjectedScript):
     13        (WebCore::InjectedScriptHost::injectedScriptFor):
     14
    1152010-01-12  Ben Murdoch  <benm@google.com>
    216
  • trunk/WebCore/bindings/v8/custom/V8InjectedScriptHostCustom.cpp

    r52556 r53135  
    3232#include "V8InjectedScriptHost.h"
    3333
     34#include "DOMWindow.h"
    3435#include "Database.h"
    35 #include "DOMWindow.h"
    3636#include "Frame.h"
    3737#include "InjectedScriptHost.h"
     
    4646namespace WebCore {
    4747
     48static void WeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter)
     49{
     50    InjectedScriptHost* nativeObject = static_cast<InjectedScriptHost*>(parameter);
     51    nativeObject->deref();
     52    object.Dispose();
     53}
     54
     55static v8::Local<v8::Object> createInjectedScriptHostV8Wrapper(InjectedScriptHost* host)
     56{
     57    V8ClassIndex::V8WrapperType descriptorType = V8ClassIndex::INJECTEDSCRIPTHOST;
     58    v8::Local<v8::Function> function = V8DOMWrapper::getTemplate(descriptorType)->GetFunction();
     59    if (function.IsEmpty()) {
     60        // Return if allocation failed.
     61        return v8::Local<v8::Object>();
     62    }
     63    v8::Local<v8::Object> instance = SafeAllocation::newInstance(function);
     64    if (instance.IsEmpty()) {
     65        // Avoid setting the wrapper if allocation failed.
     66        return v8::Local<v8::Object>();
     67    }
     68    V8DOMWrapper::setDOMWrapper(instance, V8ClassIndex::ToInt(descriptorType), host);
     69    // Create a weak reference to the v8 wrapper of InspectorBackend to deref
     70    // InspectorBackend when the wrapper is garbage collected.
     71    host->ref();
     72    v8::Persistent<v8::Object> weakHandle = v8::Persistent<v8::Object>::New(instance);
     73    weakHandle.MakeWeak(host, &WeakReferenceCallback);
     74    return instance;
     75}
     76
     77static ScriptObject createInjectedScript(const String& scriptSource, InjectedScriptHost* injectedScriptHost, ScriptState* inspectedScriptState, long id)
     78{
     79    v8::HandleScope scope;
     80
     81    v8::Local<v8::Context> inspectedContext = inspectedScriptState->context();
     82    v8::Context::Scope contextScope(inspectedContext);
     83
     84    // Call custom code to create InjectedScripHost wrapper specific for the context
     85    // instead of calling V8DOMWrapper::convertToV8Object that would create the
     86    // wrapper in the current context.
     87    // FIXME: make it possible to use generic bindings factory for InjectedScriptHost.
     88    v8::Local<v8::Object> scriptHostWrapper = createInjectedScriptHostV8Wrapper(injectedScriptHost);
     89    if (scriptHostWrapper.IsEmpty())
     90        return ScriptObject();
     91
     92    v8::Local<v8::Object> windowGlobal = inspectedContext->Global();
     93
     94    // Inject javascript into the context. The compiled script is supposed to evaluate into
     95    // a single anonymous function(it's anonymous to avoid cluttering the global object with
     96    // inspector's stuff) the function is called a few lines below with InjectedScriptHost wrapper,
     97    // injected script id and explicit reference to the inspected global object. The function is expected
     98    // to create and configure InjectedScript instance that is going to be used by the inspector.
     99    v8::Local<v8::Script> script = v8::Script::Compile(v8String(scriptSource));
     100    v8::Local<v8::Value> v = script->Run();
     101    ASSERT(!v.IsEmpty());
     102    ASSERT(v->IsFunction());
     103
     104    v8::Handle<v8::Value> args[] = {
     105      scriptHostWrapper,
     106      windowGlobal,
     107      v8::Number::New(id)
     108    };
     109    v8::Local<v8::Value> injectedScriptValue = v8::Function::Cast(*v)->Call(windowGlobal, 3, args);
     110    v8::Local<v8::Object> injectedScript(v8::Object::Cast(*injectedScriptValue));
     111    return ScriptObject(inspectedScriptState, injectedScript);
     112}
     113
    48114v8::Handle<v8::Value> V8InjectedScriptHost::inspectedWindowCallback(const v8::Arguments& args)
    49115{
     
    163229#endif
    164230
     231ScriptObject InjectedScriptHost::injectedScriptFor(ScriptState* inspectedScriptState)
     232{
     233    v8::HandleScope handleScope;
     234    v8::Local<v8::Context> context = inspectedScriptState->context();
     235    v8::Context::Scope contextScope(context);
     236
     237    v8::Local<v8::Object> global = context->Global();
     238    // Skip proxy object. The proxy object will survive page navigation while we need
     239    // an object whose lifetime consides with that of the inspected context.
     240    global = v8::Local<v8::Object>::Cast(global->GetPrototype());
     241
     242    v8::Local<v8::String> key = v8::String::New("Devtools_InjectedScript");
     243    v8::Local<v8::Value> val = global->GetHiddenValue(key);
     244    if (!val.IsEmpty() && val->IsObject())
     245        return ScriptObject(inspectedScriptState, v8::Local<v8::Object>::Cast(val));
     246
     247    ASSERT(!m_injectedScriptSource.isEmpty());
     248    ScriptObject injectedScriptObject = createInjectedScript(m_injectedScriptSource, this, inspectedScriptState, m_nextInjectedScriptId);
     249    m_idToInjectedScript.set(m_nextInjectedScriptId, injectedScriptObject);
     250    ++m_nextInjectedScriptId;
     251    global->SetHiddenValue(key, injectedScriptObject.v8Object());
     252    return injectedScriptObject;
     253}
     254
    165255} // namespace WebCore
Note: See TracChangeset for help on using the changeset viewer.