| OLD | NEW |
| (Empty) |
| 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 "sky/engine/config.h" | |
| 32 #include "sky/engine/v8_inspector/InjectedScriptManager.h" | |
| 33 | |
| 34 #include "bindings/core/v8/V8InjectedScriptHost.h" | |
| 35 #include "bindings/core/v8/V8Window.h" | |
| 36 #include "sky/engine/bindings/core/v8/BindingSecurity.h" | |
| 37 #include "sky/engine/bindings/core/v8/ScopedPersistent.h" | |
| 38 #include "sky/engine/bindings/core/v8/ScriptValue.h" | |
| 39 #include "sky/engine/bindings/core/v8/V8Binding.h" | |
| 40 #include "sky/engine/bindings/core/v8/V8ObjectConstructor.h" | |
| 41 #include "sky/engine/bindings/core/v8/V8ScriptRunner.h" | |
| 42 #include "sky/engine/core/frame/LocalDOMWindow.h" | |
| 43 #include "sky/engine/core/inspector/InjectedScriptHost.h" | |
| 44 #include "sky/engine/v8_inspector/ScriptDebugServer.h" | |
| 45 #include "sky/engine/wtf/RefPtr.h" | |
| 46 | |
| 47 namespace blink { | |
| 48 | |
| 49 InjectedScriptManager::CallbackData* InjectedScriptManager::createCallbackData(I
njectedScriptManager* injectedScriptManager) | |
| 50 { | |
| 51 OwnPtr<InjectedScriptManager::CallbackData> callbackData = adoptPtr(new Inje
ctedScriptManager::CallbackData()); | |
| 52 InjectedScriptManager::CallbackData* callbackDataPtr = callbackData.get(); | |
| 53 callbackData->injectedScriptManager = injectedScriptManager; | |
| 54 m_callbackDataSet.add(callbackData.release()); | |
| 55 return callbackDataPtr; | |
| 56 } | |
| 57 | |
| 58 void InjectedScriptManager::removeCallbackData(InjectedScriptManager::CallbackDa
ta* callbackData) | |
| 59 { | |
| 60 ASSERT(m_callbackDataSet.contains(callbackData)); | |
| 61 m_callbackDataSet.remove(callbackData); | |
| 62 } | |
| 63 | |
| 64 static v8::Local<v8::Object> createInjectedScriptHostV8Wrapper(PassRefPtr<Inject
edScriptHost> host, InjectedScriptManager* injectedScriptManager, v8::Handle<v8:
:Object> creationContext, v8::Isolate* isolate) | |
| 65 { | |
| 66 ASSERT(host); | |
| 67 | |
| 68 v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext
, &V8InjectedScriptHost::wrapperTypeInfo, V8InjectedScriptHost::toScriptWrappabl
eBase(host.get()), isolate); | |
| 69 if (UNLIKELY(wrapper.IsEmpty())) | |
| 70 return wrapper; | |
| 71 | |
| 72 // Create a weak reference to the v8 wrapper of InspectorBackend to deref | |
| 73 // InspectorBackend when the wrapper is garbage collected. | |
| 74 InjectedScriptManager::CallbackData* callbackData = injectedScriptManager->c
reateCallbackData(injectedScriptManager); | |
| 75 callbackData->host = host.get(); | |
| 76 callbackData->handle.set(isolate, wrapper); | |
| 77 callbackData->handle.setWeak(callbackData, &InjectedScriptManager::setWeakCa
llback); | |
| 78 | |
| 79 V8DOMWrapper::setNativeInfo(wrapper, &V8InjectedScriptHost::wrapperTypeInfo,
V8InjectedScriptHost::toScriptWrappableBase(host.get())); | |
| 80 ASSERT(V8DOMWrapper::isDOMWrapper(wrapper)); | |
| 81 return wrapper; | |
| 82 } | |
| 83 | |
| 84 ScriptValue InjectedScriptManager::createInjectedScript(const String& scriptSour
ce, ScriptState* inspectedScriptState, int id) | |
| 85 { | |
| 86 v8::Isolate* isolate = inspectedScriptState->isolate(); | |
| 87 ScriptState::Scope scope(inspectedScriptState); | |
| 88 | |
| 89 // Call custom code to create InjectedScripHost wrapper specific for the con
text | |
| 90 // instead of calling toV8() that would create the | |
| 91 // wrapper in the current context. | |
| 92 // FIXME: make it possible to use generic bindings factory for InjectedScrip
tHost. | |
| 93 v8::Local<v8::Object> scriptHostWrapper = createInjectedScriptHostV8Wrapper(
m_injectedScriptHost, this, inspectedScriptState->context()->Global(), inspected
ScriptState->isolate()); | |
| 94 if (scriptHostWrapper.IsEmpty()) | |
| 95 return ScriptValue(); | |
| 96 | |
| 97 // Inject javascript into the context. The compiled script is supposed to ev
aluate into | |
| 98 // a single anonymous function(it's anonymous to avoid cluttering the global
object with | |
| 99 // inspector's stuff) the function is called a few lines below with Injected
ScriptHost wrapper, | |
| 100 // injected script id and explicit reference to the inspected global object.
The function is expected | |
| 101 // to create and configure InjectedScript instance that is going to be used
by the inspector. | |
| 102 v8::Local<v8::Value> value = V8ScriptRunner::compileAndRunInternalScript(v8S
tring(isolate, scriptSource), isolate); | |
| 103 ASSERT(!value.IsEmpty()); | |
| 104 ASSERT(value->IsFunction()); | |
| 105 | |
| 106 v8::Local<v8::Object> windowGlobal = inspectedScriptState->context()->Global
(); | |
| 107 v8::Handle<v8::Value> info[] = { scriptHostWrapper, windowGlobal, v8::Number
::New(inspectedScriptState->isolate(), id) }; | |
| 108 v8::Local<v8::Value> injectedScriptValue = V8ScriptRunner::callInternalFunct
ion(v8::Local<v8::Function>::Cast(value), windowGlobal, WTF_ARRAY_LENGTH(info),
info, inspectedScriptState->isolate()); | |
| 109 return ScriptValue(inspectedScriptState, injectedScriptValue); | |
| 110 } | |
| 111 | |
| 112 void InjectedScriptManager::setWeakCallback(const v8::WeakCallbackData<v8::Objec
t, InjectedScriptManager::CallbackData>& data) | |
| 113 { | |
| 114 InjectedScriptManager::CallbackData* callbackData = data.GetParameter(); | |
| 115 callbackData->injectedScriptManager->removeCallbackData(callbackData); | |
| 116 } | |
| 117 | |
| 118 } // namespace blink | |
| OLD | NEW |