Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(498)

Side by Side Diff: chrome/renderer/resources/renderer_extension_bindings.js

Issue 147033: Refactor extension bindings to share code, avoid exposing hidden variables (Closed)
Patch Set: at head Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/renderer/resources/greasemonkey_api.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // ----------------------------------------------------------------------------- 5 // -----------------------------------------------------------------------------
6 // NOTE: If you change this file you need to touch renderer_resources.grd to 6 // NOTE: If you change this file you need to touch renderer_resources.grd to
7 // have your change take effect. 7 // have your change take effect.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 9
10 var chrome = chrome || {}; 10 var chrome = chrome || {};
11 (function () { 11 (function () {
12 native function OpenChannelToExtension(id); 12 native function OpenChannelToExtension(id);
13 native function PostMessage(portId, msg); 13 native function PostMessage(portId, msg);
14 native function GetChromeHidden();
15
16 var chromeHidden = GetChromeHidden();
17
18 // Map of port IDs to port object.
19 var ports = {};
14 20
15 // Port object. Represents a connection to another script context through 21 // Port object. Represents a connection to another script context through
16 // which messages can be passed. 22 // which messages can be passed.
17 chrome.Port = function(portId) { 23 chrome.Port = function(portId) {
18 if (chrome.Port.ports_[portId]) { 24 if (ports[portId]) {
19 throw new Error("Port '" + portId + "' already exists."); 25 throw new Error("Port '" + portId + "' already exists.");
20 } 26 }
21 this.portId_ = portId; // TODO(mpcomplete): readonly 27 this.portId_ = portId; // TODO(mpcomplete): readonly
22 this.onDisconnect = new chrome.Event(); 28 this.onDisconnect = new chrome.Event();
23 this.onMessage = new chrome.Event(); 29 this.onMessage = new chrome.Event();
24 chrome.Port.ports_[portId] = this; 30 ports[portId] = this;
31
32 chromeHidden.onUnload.addListener(function() {
33 this.disconnect();
34 });
25 }; 35 };
26 36
27 // Map of port IDs to port object. 37 chromeHidden.Port = {};
28 chrome.Port.ports_ = {};
29 38
30 // Called by native code when a channel has been opened to this context. 39 // Called by native code when a channel has been opened to this context.
31 chrome.Port.dispatchOnConnect_ = function(portId, tab) { 40 chromeHidden.Port.dispatchOnConnect = function(portId, tab) {
32 var port = new chrome.Port(portId); 41 var port = new chrome.Port(portId);
33 if (tab) { 42 if (tab) {
34 tab = JSON.parse(tab); 43 tab = JSON.parse(tab);
35 } 44 }
36 port.tab = tab; 45 port.tab = tab;
37 chrome.Event.dispatch_("channel-connect", [port]); 46 chromeHidden.Event.dispatch("channel-connect", [port]);
38 }; 47 };
39 48
40 // Called by native code when a channel has been closed. 49 // Called by native code when a channel has been closed.
41 chrome.Port.dispatchOnDisconnect_ = function(portId) { 50 chromeHidden.Port.dispatchOnDisconnect = function(portId) {
42 var port = chrome.Port.ports_[portId]; 51 var port = ports[portId];
43 if (port) { 52 if (port) {
44 port.onDisconnect.dispatch(port); 53 port.onDisconnect.dispatch(port);
45 delete chrome.Port.ports_[portId]; 54 delete ports[portId];
46 } 55 }
47 }; 56 };
48 57
49 // Called by native code when a message has been sent to the given port. 58 // Called by native code when a message has been sent to the given port.
50 chrome.Port.dispatchOnMessage_ = function(msg, portId) { 59 chromeHidden.Port.dispatchOnMessage = function(msg, portId) {
51 var port = chrome.Port.ports_[portId]; 60 var port = ports[portId];
52 if (port) { 61 if (port) {
53 if (msg) { 62 if (msg) {
54 msg = JSON.parse(msg); 63 msg = JSON.parse(msg);
55 } 64 }
56 port.onMessage.dispatch(msg, port); 65 port.onMessage.dispatch(msg, port);
57 } 66 }
58 }; 67 };
59 68
60 // Sends a message asynchronously to the context on the other end of this 69 // Sends a message asynchronously to the context on the other end of this
61 // port. 70 // port.
62 chrome.Port.prototype.postMessage = function(msg) { 71 chrome.Port.prototype.postMessage = function(msg) {
63 // JSON.stringify doesn't support a root object which is undefined. 72 // JSON.stringify doesn't support a root object which is undefined.
64 if (msg === undefined) 73 if (msg === undefined)
65 msg = null; 74 msg = null;
66 PostMessage(this.portId_, JSON.stringify(msg)); 75 PostMessage(this.portId_, JSON.stringify(msg));
67 }; 76 };
68 77
78 // Disconnects the port from the other end.
79 chrome.Port.prototype.disconnect = function() {
80 delete ports[this.portId_];
81 //CloseChannel(this.portId_); // TODO(mpcomplete)
82 }
83
69 // Extension object. 84 // Extension object.
70 chrome.Extension = function(id) { 85 chrome.Extension = function(id) {
71 this.id_ = id; 86 this.id_ = id;
72 }; 87 };
73 88
74 // Opens a message channel to the extension. Returns a Port for 89 // Opens a message channel to the extension. Returns a Port for
75 // message passing. 90 // message passing.
76 chrome.Extension.prototype.connect = function() { 91 chrome.Extension.prototype.connect = function() {
77 var portId = OpenChannelToExtension(this.id_); 92 var portId = OpenChannelToExtension(this.id_);
78 if (portId == -1) 93 if (portId == -1)
79 throw new Error("No such extension: '" + this.id_ + "'"); 94 throw new Error("No such extension: '" + this.id_ + "'");
80 return new chrome.Port(portId); 95 return new chrome.Port(portId);
81 }; 96 };
82 97
83 // Returns a resource URL that can be used to fetch a resource from this 98 // Returns a resource URL that can be used to fetch a resource from this
84 // extension. 99 // extension.
85 chrome.Extension.prototype.getURL = function(path) { 100 chrome.Extension.prototype.getURL = function(path) {
86 return "chrome-extension://" + this.id_ + "/" + path; 101 return "chrome-extension://" + this.id_ + "/" + path;
87 }; 102 };
88 })(); 103 })();
OLDNEW
« no previous file with comments | « chrome/renderer/resources/greasemonkey_api.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698