Home | History | Annotate | Download | only in custom
      1 /*
      2  * Copyright (C) 2012 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "core/inspector/InjectedScriptManager.h"
     33 
     34 #include "V8InjectedScriptHost.h"
     35 #include "V8Window.h"
     36 #include "bindings/v8/BindingSecurity.h"
     37 #include "bindings/v8/ScopedPersistent.h"
     38 #include "bindings/v8/ScriptDebugServer.h"
     39 #include "bindings/v8/ScriptObject.h"
     40 #include "bindings/v8/V8Binding.h"
     41 #include "bindings/v8/V8ObjectConstructor.h"
     42 #include "bindings/v8/V8ScriptRunner.h"
     43 #include "core/inspector/InjectedScriptHost.h"
     44 #include "core/frame/DOMWindow.h"
     45 #include "wtf/RefPtr.h"
     46 
     47 namespace WebCore {
     48 
     49 struct InjectedScriptManager::CallbackData {
     50     ScopedPersistent<v8::Object> handle;
     51     RefPtr<InjectedScriptHost> host;
     52 };
     53 
     54 static v8::Local<v8::Object> createInjectedScriptHostV8Wrapper(InjectedScriptHost* host, v8::Isolate* isolate)
     55 {
     56     v8::Local<v8::Function> function = V8InjectedScriptHost::domTemplate(isolate, MainWorld)->GetFunction();
     57     if (function.IsEmpty()) {
     58         // Return if allocation failed.
     59         return v8::Local<v8::Object>();
     60     }
     61     v8::Local<v8::Object> instanceTemplate = V8ObjectConstructor::newInstance(function);
     62     if (instanceTemplate.IsEmpty()) {
     63         // Avoid setting the wrapper if allocation failed.
     64         return v8::Local<v8::Object>();
     65     }
     66     V8DOMWrapper::setNativeInfo(instanceTemplate, &V8InjectedScriptHost::wrapperTypeInfo, host);
     67     // Create a weak reference to the v8 wrapper of InspectorBackend to deref
     68     // InspectorBackend when the wrapper is garbage collected.
     69     InjectedScriptManager::CallbackData* data = new InjectedScriptManager::CallbackData;
     70     data->host = host;
     71     data->handle.set(isolate, instanceTemplate);
     72     data->handle.setWeak(data, &InjectedScriptManager::setWeakCallback);
     73     return instanceTemplate;
     74 }
     75 
     76 ScriptObject InjectedScriptManager::createInjectedScript(const String& scriptSource, ScriptState* inspectedScriptState, int id)
     77 {
     78     v8::Isolate* isolate = inspectedScriptState->isolate();
     79     v8::HandleScope handleScope(isolate);
     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 toV8() 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(m_injectedScriptHost.get(), inspectedContext->GetIsolate());
     89     if (scriptHostWrapper.IsEmpty())
     90         return ScriptObject();
     91 
     92     // Inject javascript into the context. The compiled script is supposed to evaluate into
     93     // a single anonymous function(it's anonymous to avoid cluttering the global object with
     94     // inspector's stuff) the function is called a few lines below with InjectedScriptHost wrapper,
     95     // injected script id and explicit reference to the inspected global object. The function is expected
     96     // to create and configure InjectedScript instance that is going to be used by the inspector.
     97     v8::Local<v8::Value> value = V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, scriptSource), isolate);
     98     ASSERT(!value.IsEmpty());
     99     ASSERT(value->IsFunction());
    100 
    101     v8::Local<v8::Object> windowGlobal = inspectedContext->Global();
    102     v8::Handle<v8::Value> info[] = { scriptHostWrapper, windowGlobal, v8::Number::New(inspectedContext->GetIsolate(), id) };
    103     v8::Local<v8::Value> injectedScriptValue = V8ScriptRunner::callInternalFunction(v8::Local<v8::Function>::Cast(value), windowGlobal, WTF_ARRAY_LENGTH(info), info, inspectedContext->GetIsolate());
    104     return ScriptObject(inspectedScriptState, v8::Handle<v8::Object>::Cast(injectedScriptValue));
    105 }
    106 
    107 bool InjectedScriptManager::canAccessInspectedWindow(ScriptState* scriptState)
    108 {
    109     v8::HandleScope handleScope(scriptState->isolate());
    110     v8::Local<v8::Context> context = scriptState->context();
    111     v8::Local<v8::Object> global = context->Global();
    112     if (global.IsEmpty())
    113         return false;
    114     v8::Handle<v8::Object> holder = global->FindInstanceInPrototypeChain(V8Window::domTemplate(context->GetIsolate(), MainWorld));
    115     if (holder.IsEmpty())
    116         holder = global->FindInstanceInPrototypeChain(V8Window::domTemplate(context->GetIsolate(), IsolatedWorld));
    117     if (holder.IsEmpty())
    118         return false;
    119     Frame* frame = V8Window::toNative(holder)->frame();
    120 
    121     v8::Context::Scope contextScope(context);
    122     return BindingSecurity::shouldAllowAccessToFrame(frame, DoNotReportSecurityError);
    123 }
    124 
    125 void InjectedScriptManager::setWeakCallback(const v8::WeakCallbackData<v8::Object, InjectedScriptManager::CallbackData>& data)
    126 {
    127     data.GetParameter()->handle.clear();
    128     data.GetParameter()->host.clear();
    129     delete data.GetParameter();
    130 }
    131 
    132 } // namespace WebCore
    133