15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2012 Google Inc. All rights reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h"
3253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/inspector/InjectedScriptManager.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
34d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "bindings/core/v8/V8InjectedScriptHost.h"
35d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "bindings/core/v8/V8Window.h"
36e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)#include "bindings/v8/BindingSecurity.h"
37a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "bindings/v8/ScopedPersistent.h"
3853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "bindings/v8/ScriptDebugServer.h"
39d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "bindings/v8/ScriptValue.h"
4053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "bindings/v8/V8Binding.h"
41e52495584422c5edb5b2944981473a2e208da323Torne (Richard Coles)#include "bindings/v8/V8ObjectConstructor.h"
4293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#include "bindings/v8/V8ScriptRunner.h"
4353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/inspector/InjectedScriptHost.h"
4476c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)#include "core/frame/LocalDOMWindow.h"
4553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "wtf/RefPtr.h"
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace WebCore {
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
49a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)struct InjectedScriptManager::CallbackData {
50a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    ScopedPersistent<v8::Object> handle;
51a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    RefPtr<InjectedScriptHost> host;
52a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)};
53a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
54926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)static v8::Local<v8::Object> createInjectedScriptHostV8Wrapper(InjectedScriptHost* host, v8::Isolate* isolate)
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
56d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    v8::Local<v8::Function> function = V8InjectedScriptHost::domTemplate(isolate)->GetFunction();
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (function.IsEmpty()) {
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Return if allocation failed.
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return v8::Local<v8::Object>();
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
61a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    v8::Local<v8::Object> instanceTemplate = V8ObjectConstructor::newInstance(isolate, function);
6251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (instanceTemplate.IsEmpty()) {
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        // Avoid setting the wrapper if allocation failed.
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return v8::Local<v8::Object>();
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
6651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    V8DOMWrapper::setNativeInfo(instanceTemplate, &V8InjectedScriptHost::wrapperTypeInfo, host);
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // Create a weak reference to the v8 wrapper of InspectorBackend to deref
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // InspectorBackend when the wrapper is garbage collected.
69a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    InjectedScriptManager::CallbackData* data = new InjectedScriptManager::CallbackData;
70a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    data->host = host;
71a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    data->handle.set(isolate, instanceTemplate);
72a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    data->handle.setWeak(data, &InjectedScriptManager::setWeakCallback);
7351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return instanceTemplate;
745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
76d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)ScriptValue InjectedScriptManager::createInjectedScript(const String& scriptSource, ScriptState* inspectedScriptState, int id)
775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
788abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    v8::Isolate* isolate = inspectedScriptState->isolate();
79f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    ScriptState::Scope scope(inspectedScriptState);
805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // Call custom code to create InjectedScripHost wrapper specific for the context
825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // instead of calling toV8() that would create the
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // wrapper in the current context.
845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // FIXME: make it possible to use generic bindings factory for InjectedScriptHost.
85f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    v8::Local<v8::Object> scriptHostWrapper = createInjectedScriptHostV8Wrapper(m_injectedScriptHost.get(), inspectedScriptState->isolate());
865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (scriptHostWrapper.IsEmpty())
87d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        return ScriptValue();
885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // Inject javascript into the context. The compiled script is supposed to evaluate into
905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // a single anonymous function(it's anonymous to avoid cluttering the global object with
915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // inspector's stuff) the function is called a few lines below with InjectedScriptHost wrapper,
925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // injected script id and explicit reference to the inspected global object. The function is expected
935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // to create and configure InjectedScript instance that is going to be used by the inspector.
94a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    v8::Local<v8::Value> value = V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, scriptSource), isolate);
9593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    ASSERT(!value.IsEmpty());
9693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    ASSERT(value->IsFunction());
975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
98f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    v8::Local<v8::Object> windowGlobal = inspectedScriptState->context()->Global();
99f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    v8::Handle<v8::Value> info[] = { scriptHostWrapper, windowGlobal, v8::Number::New(inspectedScriptState->isolate(), id) };
100f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    v8::Local<v8::Value> injectedScriptValue = V8ScriptRunner::callInternalFunction(v8::Local<v8::Function>::Cast(value), windowGlobal, WTF_ARRAY_LENGTH(info), info, inspectedScriptState->isolate());
101d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    return ScriptValue(inspectedScriptState, injectedScriptValue);
1025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool InjectedScriptManager::canAccessInspectedWindow(ScriptState* scriptState)
1055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
106f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    ScriptState::Scope scope(scriptState);
107f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    v8::Local<v8::Object> global = scriptState->context()->Global();
1085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (global.IsEmpty())
1095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return false;
110f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    v8::Handle<v8::Object> holder = V8Window::findInstanceInPrototypeChain(global, scriptState->isolate());
1115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (holder.IsEmpty())
1125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return false;
113d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    LocalFrame* frame = V8Window::toNative(holder)->frame();
1145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
11509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return BindingSecurity::shouldAllowAccessToFrame(scriptState->isolate(), frame, DoNotReportSecurityError);
1165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
118a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)void InjectedScriptManager::setWeakCallback(const v8::WeakCallbackData<v8::Object, InjectedScriptManager::CallbackData>& data)
11993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
120a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    data.GetParameter()->handle.clear();
121a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    data.GetParameter()->host.clear();
122a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    delete data.GetParameter();
12393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
12493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
1255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} // namespace WebCore
126