diff options
Diffstat (limited to 'chrome/renderer/resources')
-rw-r--r-- | chrome/renderer/resources/event_bindings.js | 98 | ||||
-rw-r--r-- | chrome/renderer/resources/extension_process_bindings.js | 55 | ||||
-rw-r--r-- | chrome/renderer/resources/greasemonkey_api.js | 4 | ||||
-rw-r--r-- | chrome/renderer/resources/renderer_extension_bindings.js | 37 |
4 files changed, 111 insertions, 83 deletions
diff --git a/chrome/renderer/resources/event_bindings.js b/chrome/renderer/resources/event_bindings.js index f11ec78..78a1ceb 100644 --- a/chrome/renderer/resources/event_bindings.js +++ b/chrome/renderer/resources/event_bindings.js @@ -1,5 +1,5 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ----------------------------------------------------------------------------- @@ -9,8 +9,12 @@ var chrome = chrome || {}; (function () { + native function GetChromeHidden(); native function AttachEvent(eventName); native function DetachEvent(eventName); + native function GetNextRequestId(); + + var chromeHidden = GetChromeHidden(); // Event object. If opt_eventName is provided, this object represents // the unique instance of that named event, and dispatching an event @@ -19,7 +23,7 @@ var chrome = chrome || {}; // Example: // chrome.tabs.onChanged = new chrome.Event("tab-changed"); // chrome.tabs.onChanged.addListener(function(data) { alert(data); }); - // chrome.Event.dispatch_("tab-changed", "hi"); + // chromeHidden.Event.dispatch("tab-changed", "hi"); // will result in an alert dialog that says 'hi'. chrome.Event = function(opt_eventName) { this.eventName_ = opt_eventName; @@ -27,26 +31,31 @@ var chrome = chrome || {}; }; // A map of event names to the event object that is registered to that name. - chrome.Event.attached_ = {}; + var attachedNamedEvents = {}; + + // An array of all attached event objects, used for detaching on unload. + var allAttachedEvents = []; + + chromeHidden.Event = {}; // Dispatches a named event with the given JSON array, which is deserialized // before dispatch. The JSON array is the list of arguments that will be // sent with the event callback. - chrome.Event.dispatchJSON_ = function(name, args) { - if (chrome.Event.attached_[name]) { + chromeHidden.Event.dispatchJSON = function(name, args) { + if (attachedNamedEvents[name]) { if (args) { args = JSON.parse(args); } - chrome.Event.attached_[name].dispatch.apply( - chrome.Event.attached_[name], args); + attachedNamedEvents[name].dispatch.apply( + attachedNamedEvents[name], args); } }; // Dispatches a named event with the given arguments, supplied as an array. - chrome.Event.dispatch_ = function(name, args) { - if (chrome.Event.attached_[name]) { - chrome.Event.attached_[name].dispatch.apply( - chrome.Event.attached_[name], args); + chromeHidden.Event.dispatch = function(name, args) { + if (attachedNamedEvents[name]) { + attachedNamedEvents[name].dispatch.apply( + attachedNamedEvents[name], args); } }; @@ -105,31 +114,82 @@ var chrome = chrome || {}; // name. chrome.Event.prototype.attach_ = function() { AttachEvent(this.eventName_); - this.unloadHandler_ = this.detach_.bind(this); - window.addEventListener('unload', this.unloadHandler_, false); + allAttachedEvents[allAttachedEvents.length] = this; if (!this.eventName_) return; - if (chrome.Event.attached_[this.eventName_]) { + if (attachedNamedEvents[this.eventName_]) { throw new Error("chrome.Event '" + this.eventName_ + "' is already attached."); } - chrome.Event.attached_[this.eventName_] = this; + attachedNamedEvents[this.eventName_] = this; }; // Detaches this event object from its name. chrome.Event.prototype.detach_ = function() { - window.removeEventListener('unload', this.unloadHandler_, false); + var i = allAttachedEvents.indexOf(this); + if (i >= 0) + delete allAttachedEvents[i]; DetachEvent(this.eventName_); if (!this.eventName_) return; - if (!chrome.Event.attached_[this.eventName_]) { + if (!attachedNamedEvents[this.eventName_]) { throw new Error("chrome.Event '" + this.eventName_ + "' is not attached."); } - delete chrome.Event.attached_[this.eventName_]; + delete attachedNamedEvents[this.eventName_]; }; + + // Callback handling. + var callbacks = []; + chromeHidden.handleResponse = function(requestId, name, + success, response, error) { + try { + if (!success) { + if (!error) + error = "Unknown error." + console.error("Error during " + name + ": " + error); + return; + } + + if (callbacks[requestId]) { + if (response) { + callbacks[requestId](JSON.parse(response)); + } else { + callbacks[requestId](); + } + } + } finally { + delete callbacks[requestId]; + } + }; + + // Send an API request and optionally register a callback. + chromeHidden.sendRequest = function(request, args, callback) { + // JSON.stringify doesn't support a root object which is undefined. + if (args === undefined) + args = null; + var sargs = JSON.stringify(args); + var requestId = GetNextRequestId(); + var hasCallback = false; + if (callback) { + hasCallback = true; + callbacks[requestId] = callback; + } + request(sargs, requestId, hasCallback); + } + + // Special unload event: we don't use the DOM unload because that slows + // down tab shutdown. On the other hand, this might not always fire, since + // Chrome will terminate renderers on shutdown (SuddenTermination). + chromeHidden.onUnload = new chrome.Event(); + + chromeHidden.dispatchOnUnload = function() { + chromeHidden.onUnload.dispatch(); + for (var i in allAttachedEvents) + allAttachedEvents[i].detach_(); + } })(); diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js index 2cdf661..0fbec3f 100644 --- a/chrome/renderer/resources/extension_process_bindings.js +++ b/chrome/renderer/resources/extension_process_bindings.js @@ -9,9 +9,6 @@ var chrome; (function() { - native function GetNextRequestId(); - native function RegisterExtension(); - native function UnregisterExtension(); native function GetViews(); native function GetWindow(); native function GetCurrentWindow(); @@ -37,10 +34,13 @@ var chrome; native function CreateBookmark(); native function MoveBookmark(); native function SetBookmarkTitle(); + native function GetChromeHidden(); if (!chrome) chrome = {}; + var chromeHidden = GetChromeHidden(); + // Validate arguments. function validate(args, schemas) { if (args.length > schemas.length) @@ -72,45 +72,7 @@ var chrome; } } - // Callback handling. - // TODO(aa): This function should not be publicly exposed. Pass it into V8 - // instead and hold one per-context. See the way event_bindings.js works. - var callbacks = []; - chrome.handleResponse_ = function(requestId, name, success, response, error) { - try { - if (!success) { - if (!error) - error = "Unknown error." - console.error("Error during " + name + ": " + error); - return; - } - - if (callbacks[requestId]) { - if (response) { - callbacks[requestId](JSON.parse(response)); - } else { - callbacks[requestId](); - } - } - } finally { - delete callbacks[requestId]; - } - }; - - // Send an API request and optionally register a callback. - function sendRequest(request, args, callback) { - // JSON.stringify doesn't support a root object which is undefined. - if (args === undefined) - args = null; - var sargs = JSON.stringify(args); - var requestId = GetNextRequestId(); - var hasCallback = false; - if (callback) { - hasCallback = true; - callbacks[requestId] = callback; - } - request(sargs, requestId, hasCallback); - } + var sendRequest = chromeHidden.sendRequest; //---------------------------------------------------------------------------- @@ -528,16 +490,7 @@ var chrome; chrome.self = chrome.self || {}; chrome.self.onConnect = new chrome.Event("channel-connect"); - // Register - chrome.self.register_ = function() { - var extensionId = RegisterExtension(); - window.addEventListener('unload', function() { - UnregisterExtension(extensionId); }, false); - delete chrome.self.register_; - } - chrome.self.getViews = function() { return GetViews(); } })(); - diff --git a/chrome/renderer/resources/greasemonkey_api.js b/chrome/renderer/resources/greasemonkey_api.js index 3ce1d38..0ad24f0 100644 --- a/chrome/renderer/resources/greasemonkey_api.js +++ b/chrome/renderer/resources/greasemonkey_api.js @@ -1,5 +1,5 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // ----------------------------------------------------------------------------- diff --git a/chrome/renderer/resources/renderer_extension_bindings.js b/chrome/renderer/resources/renderer_extension_bindings.js index 31b97ed..bf59671 100644 --- a/chrome/renderer/resources/renderer_extension_bindings.js +++ b/chrome/renderer/resources/renderer_extension_bindings.js @@ -11,44 +11,53 @@ var chrome = chrome || {}; (function () { native function OpenChannelToExtension(id); native function PostMessage(portId, msg); + native function GetChromeHidden(); + + var chromeHidden = GetChromeHidden(); + + // Map of port IDs to port object. + var ports = {}; // Port object. Represents a connection to another script context through // which messages can be passed. chrome.Port = function(portId) { - if (chrome.Port.ports_[portId]) { + if (ports[portId]) { throw new Error("Port '" + portId + "' already exists."); } this.portId_ = portId; // TODO(mpcomplete): readonly this.onDisconnect = new chrome.Event(); this.onMessage = new chrome.Event(); - chrome.Port.ports_[portId] = this; + ports[portId] = this; + + chromeHidden.onUnload.addListener(function() { + this.disconnect(); + }); }; - // Map of port IDs to port object. - chrome.Port.ports_ = {}; + chromeHidden.Port = {}; // Called by native code when a channel has been opened to this context. - chrome.Port.dispatchOnConnect_ = function(portId, tab) { + chromeHidden.Port.dispatchOnConnect = function(portId, tab) { var port = new chrome.Port(portId); if (tab) { tab = JSON.parse(tab); } port.tab = tab; - chrome.Event.dispatch_("channel-connect", [port]); + chromeHidden.Event.dispatch("channel-connect", [port]); }; // Called by native code when a channel has been closed. - chrome.Port.dispatchOnDisconnect_ = function(portId) { - var port = chrome.Port.ports_[portId]; + chromeHidden.Port.dispatchOnDisconnect = function(portId) { + var port = ports[portId]; if (port) { port.onDisconnect.dispatch(port); - delete chrome.Port.ports_[portId]; + delete ports[portId]; } }; // Called by native code when a message has been sent to the given port. - chrome.Port.dispatchOnMessage_ = function(msg, portId) { - var port = chrome.Port.ports_[portId]; + chromeHidden.Port.dispatchOnMessage = function(msg, portId) { + var port = ports[portId]; if (port) { if (msg) { msg = JSON.parse(msg); @@ -66,6 +75,12 @@ var chrome = chrome || {}; PostMessage(this.portId_, JSON.stringify(msg)); }; + // Disconnects the port from the other end. + chrome.Port.prototype.disconnect = function() { + delete ports[this.portId_]; + //CloseChannel(this.portId_); // TODO(mpcomplete) + } + // Extension object. chrome.Extension = function(id) { this.id_ = id; |