2009-04-09 Jian Li <jianli@chromium.org>
authordimich@chromium.org <dimich@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 9 Apr 2009 22:42:10 +0000 (22:42 +0000)
committerdimich@chromium.org <dimich@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 9 Apr 2009 22:42:10 +0000 (22:42 +0000)
        Reviewed by Dimitri Glazkov.

        https://bugs.webkit.org/show_bug.cgi?id=25084
        Upstream changes to V8 bindings for XHR so that it can work under either DOMWindow or WorkerContext.

        * bindings/v8/WorkerContextExecutionProxy.cpp:
        (WebCore::WorkerContextExecutionProxy::dispose):
        (WebCore::WorkerContextExecutionProxy::retrieve):
        (WebCore::WorkerContextExecutionProxy::initContextIfNeeded):
        (WebCore::WorkerContextExecutionProxy::findOrCreateEventListenerHelper):
        (WebCore::WorkerContextExecutionProxy::FindOrCreateEventListener):
        (WebCore::WorkerContextExecutionProxy::findOrCreateObjectEventListener):
        * bindings/v8/WorkerContextExecutionProxy.h:
        * bindings/v8/custom/V8XMLHttpRequestConstructor.cpp:
        (WebCore::CALLBACK_FUNC_DECL):
        * bindings/v8/custom/V8XMLHttpRequestCustom.cpp:
        (WebCore::getEventListener):
        (WebCore::ACCESSOR_SETTER):
        (WebCore::CALLBACK_FUNC_DECL):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@42373 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/bindings/v8/WorkerContextExecutionProxy.cpp
WebCore/bindings/v8/WorkerContextExecutionProxy.h
WebCore/bindings/v8/custom/V8XMLHttpRequestConstructor.cpp
WebCore/bindings/v8/custom/V8XMLHttpRequestCustom.cpp

index 24a3922..c78eba6 100644 (file)
@@ -1,3 +1,25 @@
+2009-04-09  Jian Li  <jianli@chromium.org>
+
+        Reviewed by Dimitri Glazkov.
+
+        https://bugs.webkit.org/show_bug.cgi?id=25084
+        Upstream changes to V8 bindings for XHR so that it can work under either DOMWindow or WorkerContext.
+
+        * bindings/v8/WorkerContextExecutionProxy.cpp:
+        (WebCore::WorkerContextExecutionProxy::dispose):
+        (WebCore::WorkerContextExecutionProxy::retrieve):
+        (WebCore::WorkerContextExecutionProxy::initContextIfNeeded):
+        (WebCore::WorkerContextExecutionProxy::findOrCreateEventListenerHelper):
+        (WebCore::WorkerContextExecutionProxy::FindOrCreateEventListener):
+        (WebCore::WorkerContextExecutionProxy::findOrCreateObjectEventListener):
+        * bindings/v8/WorkerContextExecutionProxy.h:
+        * bindings/v8/custom/V8XMLHttpRequestConstructor.cpp:
+        (WebCore::CALLBACK_FUNC_DECL):
+        * bindings/v8/custom/V8XMLHttpRequestCustom.cpp:
+        (WebCore::getEventListener):
+        (WebCore::ACCESSOR_SETTER):
+        (WebCore::CALLBACK_FUNC_DECL):
+
 2009-04-09  Darin Adler  <darin@apple.com>
 
         Reviewed by Anders Carlsson and Sam Weinig.
index d061ea2..6adfdbf 100644 (file)
@@ -39,6 +39,7 @@
 #include "V8Proxy.h"
 #include "Event.h"
 #include "V8WorkerContextEventListener.h"
+#include "V8WorkerContextObjectEventListener.h"
 #include "WorkerContext.h"
 #include "WorkerLocation.h"
 #include "WorkerNavigator.h"
@@ -106,7 +107,7 @@ void WorkerContextExecutionProxy::dispose()
     v8::HandleScope scope;
     v8::Persistent<v8::Object> wrapper = domObjectMap().get(m_workerContext);
     if (!wrapper.IsEmpty())
-        V8Proxy::SetDOMWrapper(wrapper, V8ClassIndex::INVALID_CLASS_INDEX, NULL);
+        V8Proxy::SetDOMWrapper(wrapper, V8ClassIndex::INVALID_CLASS_INDEX, 0);
     domObjectMap().forget(m_workerContext);
 }
 
@@ -115,7 +116,9 @@ WorkerContextExecutionProxy* WorkerContextExecutionProxy::retrieve()
     v8::Handle<v8::Context> context = v8::Context::GetCurrent();
     v8::Handle<v8::Object> global = context->Global();
     global = V8Proxy::LookupDOMWrapper(V8ClassIndex::WORKERCONTEXT, global);
-    ASSERT(!global.IsEmpty());
+    // Return 0 if the current executing context is not the worker context.
+    if (global.IsEmpty())
+        return 0;
     WorkerContext* workerContext = V8Proxy::ToNativeObject<WorkerContext>(V8ClassIndex::WORKERCONTEXT, global);
     return workerContext->script()->proxy();
 }
@@ -128,7 +131,7 @@ void WorkerContextExecutionProxy::initContextIfNeeded()
 
     // Create a new environment
     v8::Persistent<v8::ObjectTemplate> globalTemplate;
-    m_context = v8::Context::New(NULL, globalTemplate);
+    m_context = v8::Context::New(0, globalTemplate);
 
     // Starting from now, use local context only.
     v8::Local<v8::Context> context = v8::Local<v8::Context>::New(m_context);
@@ -330,7 +333,7 @@ v8::Local<v8::Value> WorkerContextExecutionProxy::runScript(v8::Handle<v8::Scrip
     return result;
 }
 
-PassRefPtr<V8EventListener> WorkerContextExecutionProxy::FindOrCreateEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly)
+PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateEventListenerHelper(v8::Local<v8::Value> object, bool isInline, bool findOnly, bool createObjectEventListener)
 {
     if (!object->IsObject())
         return 0;
@@ -340,7 +343,11 @@ PassRefPtr<V8EventListener> WorkerContextExecutionProxy::FindOrCreateEventListen
         return listener;
 
     // Create a new one, and add to cache.
-    RefPtr<V8WorkerContextEventListener> newListener = V8WorkerContextEventListener::create(this, v8::Local<v8::Object>::Cast(object), isInline);
+    RefPtr<V8EventListener> newListener;
+    if (createObjectEventListener)
+        newListener = V8WorkerContextObjectEventListener::create(this, v8::Local<v8::Object>::Cast(object), isInline);
+    else
+        newListener = V8WorkerContextEventListener::create(this, v8::Local<v8::Object>::Cast(object), isInline);
     {
         // Need to use lock since V8EventListenerList::add() creates HandleScope.
         v8::Locker locker;
@@ -350,6 +357,16 @@ PassRefPtr<V8EventListener> WorkerContextExecutionProxy::FindOrCreateEventListen
     return newListener.release();
 }
 
+PassRefPtr<V8EventListener> WorkerContextExecutionProxy::FindOrCreateEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly)
+{
+    return findOrCreateEventListenerHelper(object, isInline, findOnly, false);
+}
+
+PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateObjectEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly)
+{
+    return findOrCreateEventListenerHelper(object, isInline, findOnly, true);
+}
+
 void WorkerContextExecutionProxy::RemoveEventListener(V8EventListener* listener)
 {
     // Need to use lock since V8EventListenerList::remove() creates HandleScope.
index 70f9fc5..3f07377 100644 (file)
@@ -64,6 +64,9 @@ namespace WebCore {
         static v8::Handle<v8::Value> EventTargetToV8Object(EventTarget* target);
         static v8::Handle<v8::Value> WorkerContextToV8Object(WorkerContext* wc);
 
+        // Finds/creates event listener wrappers.
+        PassRefPtr<V8EventListener> findOrCreateObjectEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly);
+
         // Track the event so that we can detach it from the JS wrapper when a worker
         // terminates. This is needed because we need to be able to dispose these
         // events and releases references to their event targets: WorkerContext.
@@ -75,7 +78,7 @@ namespace WebCore {
         // Returns WorkerContext object.
         WorkerContext* workerContext() { return m_workerContext; }
 
-        // Returns WorkerContextExecutionProxy object of the currently executing context.
+        // Returns WorkerContextExecutionProxy object of the currently executing context. 0 will be returned if the current executing context is not the worker context.
         static WorkerContextExecutionProxy* retrieve();
 
         // Enables HTML5 worker support.
@@ -85,6 +88,7 @@ namespace WebCore {
     private:
         void initContextIfNeeded();
         void dispose();
+        PassRefPtr<V8EventListener> findOrCreateEventListenerHelper(v8::Local<v8::Value> object, bool isInline, bool findOnly, bool createObjectEventListener);
 
         // Run an already compiled script.
         v8::Local<v8::Value> runScript(v8::Handle<v8::Script>);
index 6b55238..c913c08 100644 (file)
@@ -36,6 +36,8 @@
 #include "V8ObjectEventListener.h"
 #include "V8Proxy.h"
 #include "XMLHttpRequest.h"
+#include "WorkerContext.h"
+#include "WorkerContextExecutionProxy.h"
 
 namespace WebCore {
 
@@ -48,8 +50,15 @@ CALLBACK_FUNC_DECL(XMLHttpRequestConstructor)
 
     // Expect no parameters.
     // Allocate a XMLHttpRequest object as its internal field.
-    Document* doc = V8Proxy::retrieveFrame()->document();
-    RefPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(doc);
+    ScriptExecutionContext* context = 0;
+#if ENABLE(WORKERS)
+    WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve();
+    if (proxy)
+        context = proxy->workerContext();
+    else
+#endif
+        context = V8Proxy::retrieveFrame()->document();
+    RefPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context);
     V8Proxy::SetDOMWrapper(args.Holder(), V8ClassIndex::ToInt(V8ClassIndex::XMLHTTPREQUEST), xmlHttpRequest.get());
 
     // Add object to the wrapper map.
index ea69f0b..a80cf51 100644 (file)
 #include "V8ObjectEventListener.h"
 #include "V8Proxy.h"
 #include "V8XMLHttpRequestUtilities.h"
+#include "WorkerContext.h"
+#include "WorkerContextExecutionProxy.h"
 
 namespace WebCore {
 
+PassRefPtr<EventListener> getEventListener(XMLHttpRequest* xmlHttpRequest, v8::Local<v8::Value> value, bool findOnly)
+{
+#if ENABLE(WORKERS)
+    WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve();
+    if (workerContextProxy)
+        return workerContextProxy->findOrCreateObjectEventListener(value, false, findOnly);
+#endif
+
+    V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext());
+    if (proxy)
+        return findOnly ? proxy->FindObjectEventListener(value, false) : proxy->FindOrCreateObjectEventListener(value, false);
+
+    return PassRefPtr<EventListener>();
+}
+
 ACCESSOR_GETTER(XMLHttpRequestOnabort)
 {
     INC_STATS("DOM.XMLHttpRequest.onabort._get");
@@ -68,11 +85,7 @@ ACCESSOR_SETTER(XMLHttpRequestOnabort)
         // Clear the listener.
         xmlHttpRequest->setOnabort(0);
     } else {
-        V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext());
-        if (!proxy)
-            return;
-
-        RefPtr<EventListener> listener = proxy->FindOrCreateObjectEventListener(value, false);
+        RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
         if (listener) {
             xmlHttpRequest->setOnabort(listener);
             createHiddenXHRDependency(info.Holder(), value);
@@ -106,11 +119,7 @@ ACCESSOR_SETTER(XMLHttpRequestOnerror)
         // Clear the listener.
         xmlHttpRequest->setOnerror(0);
     } else {
-        V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext());
-        if (!proxy)
-            return;
-
-        RefPtr<EventListener> listener = proxy->FindOrCreateObjectEventListener(value, false);
+        RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
         if (listener) {
             xmlHttpRequest->setOnerror(listener);
             createHiddenXHRDependency(info.Holder(), value);
@@ -144,11 +153,7 @@ ACCESSOR_SETTER(XMLHttpRequestOnload)
         xmlHttpRequest->setOnload(0);
 
     } else {
-        V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext());
-        if (!proxy)
-            return;
-
-        RefPtr<EventListener> listener = proxy->FindOrCreateObjectEventListener(value, false);
+        RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
         if (listener) {
             xmlHttpRequest->setOnload(listener.get());
             createHiddenXHRDependency(info.Holder(), value);
@@ -182,11 +187,7 @@ ACCESSOR_SETTER(XMLHttpRequestOnloadstart)
         // Clear the listener.
         xmlHttpRequest->setOnloadstart(0);
     } else {
-        V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext());
-        if (!proxy)
-            return;
-
-        RefPtr<EventListener> listener = proxy->FindOrCreateObjectEventListener(value, false);
+        RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
         if (listener) {
             xmlHttpRequest->setOnloadstart(listener);
             createHiddenXHRDependency(info.Holder(), value);
@@ -220,11 +221,7 @@ ACCESSOR_SETTER(XMLHttpRequestOnprogress)
         // Clear the listener.
         xmlHttpRequest->setOnprogress(0);
     } else {
-        V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext());
-        if (!proxy)
-            return;
-
-        RefPtr<EventListener> listener = proxy->FindOrCreateObjectEventListener(value, false);
+        RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
         if (listener) {
             xmlHttpRequest->setOnprogress(listener);
             createHiddenXHRDependency(info.Holder(), value);
@@ -258,11 +255,7 @@ ACCESSOR_SETTER(XMLHttpRequestOnreadystatechange)
         // Clear the listener.
         xmlHttpRequest->setOnreadystatechange(0);
     } else {
-        V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext());
-        if (!proxy)
-            return;
-
-        RefPtr<EventListener> listener = proxy->FindOrCreateObjectEventListener(value, false);
+        RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
         if (listener) {
             xmlHttpRequest->setOnreadystatechange(listener.get());
             createHiddenXHRDependency(info.Holder(), value);
@@ -284,11 +277,7 @@ CALLBACK_FUNC_DECL(XMLHttpRequestAddEventListener)
     INC_STATS("DOM.XMLHttpRequest.addEventListener()");
     XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
 
-    V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext());
-    if (!proxy)
-        return v8::Undefined();
-
-    RefPtr<EventListener> listener = proxy->FindOrCreateObjectEventListener(args[1], false);
+    RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, args[1], false);
     if (listener) {
         String type = toWebCoreString(args[0]);
         bool useCapture = args[2]->BooleanValue();
@@ -304,12 +293,7 @@ CALLBACK_FUNC_DECL(XMLHttpRequestRemoveEventListener)
     INC_STATS("DOM.XMLHttpRequest.removeEventListener()");
     XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
 
-    V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext());
-    if (!proxy)
-        return v8::Undefined(); // Probably leaked.
-
-    RefPtr<EventListener> listener = proxy->FindObjectEventListener(args[1], false);
-
+    RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, args[1], true);
     if (listener) {
         String type = toWebCoreString(args[0]);
         bool useCapture = args[2]->BooleanValue();
@@ -337,8 +321,15 @@ CALLBACK_FUNC_DECL(XMLHttpRequestOpen)
 
     String method = toWebCoreString(args[0]);
     String urlstring = toWebCoreString(args[1]);
-    V8Proxy* proxy = V8Proxy::retrieve();
-    KURL url = proxy->frame()->document()->completeURL(urlstring);
+    ScriptExecutionContext* context = 0;
+#if ENABLE(WORKERS)
+    WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve();
+    if (proxy)
+        context = proxy->workerContext();
+    else
+#endif
+        context = V8Proxy::retrieve()->frame()->document();
+    KURL url = context->completeURL(urlstring);
 
     bool async = (args.Length() < 3) ? true : args[2]->BooleanValue();