| OLD | NEW |
| 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 // This script contains unprivileged javascript APIs related to chrome | 5 // This script contains unprivileged javascript APIs related to chrome |
| 6 // extensions. It is loaded by any extension-related context, such as content | 6 // extensions. It is loaded by any extension-related context, such as content |
| 7 // scripts or toolstrips. | 7 // scripts or toolstrips. |
| 8 // See user_script_slave.cc for script that is loaded by content scripts only. | 8 // See user_script_slave.cc for script that is loaded by content scripts only. |
| 9 // TODO(mpcomplete): we also load this in regular web pages, but don't need | 9 // TODO(mpcomplete): we also load this in regular web pages, but don't need |
| 10 // to. | 10 // to. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 if (targetExtensionId != chromeHidden.extensionId) | 68 if (targetExtensionId != chromeHidden.extensionId) |
| 69 return; // not for us | 69 return; // not for us |
| 70 if (ports[getOppositePortId(portId)]) | 70 if (ports[getOppositePortId(portId)]) |
| 71 return; // this channel was opened by us, so ignore it | 71 return; // this channel was opened by us, so ignore it |
| 72 | 72 |
| 73 // Determine whether this is coming from another extension, so we can use | 73 // Determine whether this is coming from another extension, so we can use |
| 74 // the right event. | 74 // the right event. |
| 75 var isExternal = sourceExtensionId != chromeHidden.extensionId; | 75 var isExternal = sourceExtensionId != chromeHidden.extensionId; |
| 76 | 76 |
| 77 if (tab) | 77 if (tab) |
| 78 tab = JSON.parse(tab); | 78 tab = chromeHidden.JSON.parse(tab); |
| 79 var sender = {tab: tab, id: sourceExtensionId}; | 79 var sender = {tab: tab, id: sourceExtensionId}; |
| 80 | 80 |
| 81 // Special case for sendRequest/onRequest. | 81 // Special case for sendRequest/onRequest. |
| 82 if (channelName == chromeHidden.kRequestChannel) { | 82 if (channelName == chromeHidden.kRequestChannel) { |
| 83 var requestEvent = (isExternal ? | 83 var requestEvent = (isExternal ? |
| 84 chrome.extension.onRequestExternal : chrome.extension.onRequest); | 84 chrome.extension.onRequestExternal : chrome.extension.onRequest); |
| 85 if (requestEvent.hasListeners()) { | 85 if (requestEvent.hasListeners()) { |
| 86 var port = chromeHidden.Port.createPort(portId, channelName); | 86 var port = chromeHidden.Port.createPort(portId, channelName); |
| 87 port.onMessage.addListener(function(request) { | 87 port.onMessage.addListener(function(request) { |
| 88 requestEvent.dispatch(request, sender, function(response) { | 88 requestEvent.dispatch(request, sender, function(response) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 112 port.onDisconnect.dispatch(port); | 112 port.onDisconnect.dispatch(port); |
| 113 delete ports[portId]; | 113 delete ports[portId]; |
| 114 } | 114 } |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 // Called by native code when a message has been sent to the given port. | 117 // Called by native code when a message has been sent to the given port. |
| 118 chromeHidden.Port.dispatchOnMessage = function(msg, portId) { | 118 chromeHidden.Port.dispatchOnMessage = function(msg, portId) { |
| 119 var port = ports[portId]; | 119 var port = ports[portId]; |
| 120 if (port) { | 120 if (port) { |
| 121 if (msg) { | 121 if (msg) { |
| 122 msg = JSON.parse(msg); | 122 msg = chromeHidden.JSON.parse(msg); |
| 123 } | 123 } |
| 124 port.onMessage.dispatch(msg, port); | 124 port.onMessage.dispatch(msg, port); |
| 125 } | 125 } |
| 126 }; | 126 }; |
| 127 | 127 |
| 128 // Sends a message asynchronously to the context on the other end of this | 128 // Sends a message asynchronously to the context on the other end of this |
| 129 // port. | 129 // port. |
| 130 chrome.Port.prototype.postMessage = function(msg) { | 130 chrome.Port.prototype.postMessage = function(msg) { |
| 131 // JSON.stringify doesn't support a root object which is undefined. | 131 // JSON.stringify doesn't support a root object which is undefined. |
| 132 if (msg === undefined) | 132 if (msg === undefined) |
| 133 msg = null; | 133 msg = null; |
| 134 PostMessage(this.portId_, JSON.stringify(msg)); | 134 PostMessage(this.portId_, chromeHidden.JSON.stringify(msg)); |
| 135 }; | 135 }; |
| 136 | 136 |
| 137 // Disconnects the port from the other end. | 137 // Disconnects the port from the other end. |
| 138 chrome.Port.prototype.disconnect = function() { | 138 chrome.Port.prototype.disconnect = function() { |
| 139 delete ports[this.portId_]; | 139 delete ports[this.portId_]; |
| 140 CloseChannel(this.portId_); | 140 CloseChannel(this.portId_); |
| 141 } | 141 } |
| 142 | 142 |
| 143 // This function is called on context initialization for both content scripts | 143 // This function is called on context initialization for both content scripts |
| 144 // and extension contexts. | 144 // and extension contexts. |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 "extension.onConnectExternal", | 271 "extension.onConnectExternal", |
| 272 "extension.onRequestExternal", | 272 "extension.onRequestExternal", |
| 273 "i18n.getAcceptLanguages" | 273 "i18n.getAcceptLanguages" |
| 274 ]; | 274 ]; |
| 275 for (var i = 0; i < privileged.length; i++) { | 275 for (var i = 0; i < privileged.length; i++) { |
| 276 createStub(privileged[i]); | 276 createStub(privileged[i]); |
| 277 } | 277 } |
| 278 } | 278 } |
| 279 | 279 |
| 280 })(); | 280 })(); |
| OLD | NEW |