Commit c79ba53e authored by eric@webkit.org's avatar eric@webkit.org

2010-03-30 Andrey Kosyakov <caseq@chromium.org>

        Reviewed by Pavel Feldman.

        Support EventTarget interface in fake workers (both for Worker object
        and WorkerContext). Use MessagePort to implement message passing to
        support passing ports in PostMessage.
        https://bugs.webkit.org/show_bug.cgi?id=36763

        * inspector/front-end/InjectedFakeWorker.js:
        (InjectedFakeWorker.Worker.onmessageGetter):
        (InjectedFakeWorker.Worker.onmessageSetter):
        (InjectedFakeWorker.Worker):
        (InjectedFakeWorker.FakeWorker):
        (InjectedFakeWorker.FakeWorker.prototype.postMessage):
        (InjectedFakeWorker.FakeWorker.prototype.terminate):
        (InjectedFakeWorker.FakeWorker.prototype._onWorkerFrameLoaded):
        (InjectedFakeWorker.FakeWorker.prototype._setupWorkerContext.onmessageGetter):
        (InjectedFakeWorker.FakeWorker.prototype._setupWorkerContext.onmessageSetter):
        (InjectedFakeWorker.FakeWorker.prototype._setupWorkerContext):
        (InjectedFakeWorker.FakeWorker.prototype._addEventListener):
        (InjectedFakeWorker.FakeWorker.prototype._removeEventListener):
        (InjectedFakeWorker.FakeWorker.prototype._callbackWrapper):
        (InjectedFakeWorker.FakeWorker.prototype._handleException):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@56780 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 3fe41104
2010-03-30 Andrey Kosyakov <caseq@chromium.org>
Reviewed by Pavel Feldman.
Support EventTarget interface in fake workers (both for Worker object
and WorkerContext). Use MessagePort to implement message passing to
support passing ports in PostMessage.
https://bugs.webkit.org/show_bug.cgi?id=36763
* inspector/front-end/InjectedFakeWorker.js:
(InjectedFakeWorker.Worker.onmessageGetter):
(InjectedFakeWorker.Worker.onmessageSetter):
(InjectedFakeWorker.Worker):
(InjectedFakeWorker.FakeWorker):
(InjectedFakeWorker.FakeWorker.prototype.postMessage):
(InjectedFakeWorker.FakeWorker.prototype.terminate):
(InjectedFakeWorker.FakeWorker.prototype._onWorkerFrameLoaded):
(InjectedFakeWorker.FakeWorker.prototype._setupWorkerContext.onmessageGetter):
(InjectedFakeWorker.FakeWorker.prototype._setupWorkerContext.onmessageSetter):
(InjectedFakeWorker.FakeWorker.prototype._setupWorkerContext):
(InjectedFakeWorker.FakeWorker.prototype._addEventListener):
(InjectedFakeWorker.FakeWorker.prototype._removeEventListener):
(InjectedFakeWorker.FakeWorker.prototype._callbackWrapper):
(InjectedFakeWorker.FakeWorker.prototype._handleException):
2010-03-30 Kristian Monsen <kristianm@google.com>
Reviewed by David Levin.
......@@ -40,7 +40,20 @@ Worker = function(url)
this.isFake = true;
this.postMessage = bind(impl.postMessage, impl);
this.terminate = bind(impl.terminate, impl);
this.onmessage = noop;
function onmessageGetter()
{
return impl.channel.port1.onmessage;
}
function onmessageSetter(callback)
{
impl.channel.port1.onmessage = callback;
}
this.__defineGetter__("onmessage", onmessageGetter);
this.__defineSetter__("onmessage", onmessageSetter);
this.addEventListener = bind(impl.channel.port1.addEventListener, impl.channel.port1);
this.removeEventListener = bind(impl.channel.port1.removeEventListener, impl.channel.port1);
this.dispatchEvent = bind(impl.channel.port1.dispatchEvent, impl.channel.port1);
}
function FakeWorker(worker, url)
......@@ -48,73 +61,37 @@ function FakeWorker(worker, url)
var scriptURL = this._expandURLAndCheckOrigin(document.baseURI, location.href, url);
this._worker = worker;
this._buildWorker(scriptURL);
this._id = InjectedScriptHost.nextWorkerId();
this.channel = new MessageChannel();
this._listeners = [];
this._buildWorker(scriptURL);
InjectedScriptHost.didCreateWorker(this._id, scriptURL.url, false);
}
FakeWorker.prototype = {
postMessage: function(msg)
postMessage: function(msg, opt_ports)
{
if (this._frame != null)
this._dispatchMessage(this._frame, bind(this._onmessageWrapper, this), msg);
this.channel.port1.postMessage.apply(this.channel.port1, arguments);
else if (this._pendingMessages)
this._pendingMessages.push(msg)
this._pendingMessages.push(arguments)
else
this._pendingMessages = [ msg ];
this._pendingMessages = [ arguments ];
},
terminate: function()
{
InjectedScriptHost.didDestroyWorker(this._id);
if (this._frame != null) {
this._frame.onmessage = this._worker.onmessage = noop;
this.channel.port1.close();
this.channel.port2.close();
if (this._frame != null)
this._frame.frameElement.parentNode.removeChild(this._frame.frameElement);
}
this._frame = null;
this._worker = null; // Break reference loop.
},
_onmessageWrapper: function(msg)
{
// Shortcut -- if no exception handlers installed, avoid try/catch so as not to obscure line number.
if (!this._frame.onerror && !this._worker.onerror) {
this._frame.onmessage(msg);
return;
}
try {
this._frame.onmessage(msg);
} catch (e) {
this._handleException(e, this._frame.onerror, this._worker.onerror);
}
},
_dispatchMessage: function(targetWindow, handler, msg)
{
var event = this._document.createEvent("MessageEvent");
event.initMessageEvent("MessageEvent", false, false, msg);
targetWindow.setTimeout(handler, 0, event);
},
_handleException: function(e)
{
// NB: it should be an ErrorEvent, but creating it from script is not
// currently supported, so emulate it on top of plain vanilla Event.
var errorEvent = this._document.createEvent("Event");
errorEvent.initEvent("Event", false, false);
errorEvent.message = "Uncaught exception";
for (var i = 1; i < arguments.length; ++i) {
if (arguments[i] && arguments[i](errorEvent))
return;
}
throw e;
},
_buildWorker: function(url)
{
var code = this._loadScript(url.url);
......@@ -141,12 +118,12 @@ FakeWorker.prototype = {
this._frame = frame;
this._setupWorkerContext(frame, url);
var frameContents = '(function(location, window) { ' + code + '})(__devtools.location, undefined);\n' + '//@ sourceURL=' + url.url;
var frameContents = '(function() { var location = __devtools.location; var window; ' + code + '})();\n' + '//@ sourceURL=' + url.url;
frame.eval(frameContents);
if (this._pendingMessages) {
for (var msg in this._pendingMessages)
this.postMessage(this._pendingMessages[msg]);
for (var msg = 0; msg < this._pendingMessages.length; ++msg)
this.postMessage.apply(this, this._pendingMessages[msg]);
delete this._pendingMessages;
}
},
......@@ -157,19 +134,88 @@ FakeWorker.prototype = {
handleException: bind(this._handleException, this),
location: url.mockLocation()
};
var worker = this._worker;
function handler(event) // Late binding to onmessage desired, so no bind() here.
var self = this;
function onmessageGetter()
{
worker.onmessage(event);
return self.channel.port2.onmessage ? self.channel.port2.onmessage.originalCallback : null;
}
workerFrame.onmessage = noop;
workerFrame.postMessage = bind(this._dispatchMessage, this, window, handler);
function onmessageSetter(callback)
{
var wrappedCallback = bind(self._callbackWrapper, self, callback);
wrappedCallback.originalCallback = callback;
self.channel.port2.onmessage = wrappedCallback;
}
workerFrame.__defineGetter__("onmessage", onmessageGetter);
workerFrame.__defineSetter__("onmessage", onmessageSetter);
workerFrame.addEventListener = bind(this._addEventListener, this);
workerFrame.removeEventListener = bind(this._removeEventListener, this);
workerFrame.dispatchEvent = bind(this.channel.port2.dispatchEvent, this.channel.port2);
workerFrame.postMessage = bind(this.channel.port2.postMessage, this.channel.port2);
workerFrame.importScripts = bind(this._importScripts, this, workerFrame);
workerFrame.close = bind(this.terminate, this);
},
_addEventListener: function(type, callback, useCapture)
{
var wrappedCallback = bind(this._callbackWrapper, this, callback);
wrappedCallback.originalCallback = callback;
wrappedCallback.type = type;
wrappedCallback.useCapture = Boolean(useCapture);
this.channel.port2.addEventListener(type, wrappedCallback, useCapture);
this._listeners.push(wrappedCallback);
},
_removeEventListener: function(type, callback, useCapture)
{
var listeners = this._listeners;
for (var i = 0; i < listeners.length; ++i) {
if (listeners[i].originalCallback === callback &&
listeners[i].type === type &&
listeners[i].useCapture === Boolean(useCapture)) {
this.channel.port2.removeEventListener(type, listeners[i], useCapture);
listeners[i] = listeners[listeners.length - 1];
listeners.pop();
break;
}
}
},
_callbackWrapper: function(callback, msg)
{
// Shortcut -- if no exception handlers installed, avoid try/catch so as not to obscure line number.
if (!this._frame.onerror && !this._worker.onerror) {
callback(msg);
return;
}
try {
callback(msg);
} catch (e) {
this._handleException(e, this._frame.onerror, this._worker.onerror);
}
},
_handleException: function(e)
{
// NB: it should be an ErrorEvent, but creating it from script is not
// currently supported, so emulate it on top of plain vanilla Event.
var errorEvent = this._document.createEvent("Event");
errorEvent.initEvent("Event", false, false);
errorEvent.message = "Uncaught exception";
for (var i = 1; i < arguments.length; ++i) {
if (arguments[i] && arguments[i](errorEvent))
return;
}
throw e;
},
_importScripts: function(targetFrame)
{
for (var i = 1; i < arguments.length; ++i) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment