Changeset 53135 in webkit
- Timestamp:
- Jan 12, 2010 4:41:42 AM (14 years ago)
- Location:
- trunk/WebCore
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/WebCore/ChangeLog
r53132 r53135 1 2010-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 1 15 2010-01-12 Ben Murdoch <benm@google.com> 2 16 -
trunk/WebCore/bindings/v8/custom/V8InjectedScriptHostCustom.cpp
r52556 r53135 32 32 #include "V8InjectedScriptHost.h" 33 33 34 #include "DOMWindow.h" 34 35 #include "Database.h" 35 #include "DOMWindow.h"36 36 #include "Frame.h" 37 37 #include "InjectedScriptHost.h" … … 46 46 namespace WebCore { 47 47 48 static 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 55 static 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 77 static 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 48 114 v8::Handle<v8::Value> V8InjectedScriptHost::inspectedWindowCallback(const v8::Arguments& args) 49 115 { … … 163 229 #endif 164 230 231 ScriptObject 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 165 255 } // namespace WebCore
Note: See TracChangeset
for help on using the changeset viewer.