| 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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 // Opens a message channel to the given target extension, or the current one | 161 // Opens a message channel to the given target extension, or the current one |
| 162 // if unspecified. Returns a Port for message passing. | 162 // if unspecified. Returns a Port for message passing. |
| 163 chrome.extension.connect = function(targetId_opt, connectInfo_opt) { | 163 chrome.extension.connect = function(targetId_opt, connectInfo_opt) { |
| 164 var name = ""; | 164 var name = ""; |
| 165 var targetId = extensionId; | 165 var targetId = extensionId; |
| 166 var nextArg = 0; | 166 var nextArg = 0; |
| 167 if (typeof(arguments[nextArg]) == "string") | 167 if (typeof(arguments[nextArg]) == "string") |
| 168 targetId = arguments[nextArg++]; | 168 targetId = arguments[nextArg++]; |
| 169 if (typeof(arguments[nextArg]) == "object") | 169 if (typeof(arguments[nextArg]) == "object") |
| 170 name = arguments[nextArg++].name || name; | 170 name = arguments[nextArg++].name || name; |
| 171 if (nextArg != arguments.length) |
| 172 throw new Error("Invalid arguments to connect."); |
| 171 | 173 |
| 172 var portId = OpenChannelToExtension(extensionId, targetId, name); | 174 var portId = OpenChannelToExtension(extensionId, targetId, name); |
| 173 if (portId >= 0) | 175 if (portId >= 0) |
| 174 return chromeHidden.Port.createPort(portId, name); | 176 return chromeHidden.Port.createPort(portId, name); |
| 175 throw new Error("Error connecting to extension '" + targetId + "'"); | 177 throw new Error("Error connecting to extension '" + targetId + "'"); |
| 176 }; | 178 }; |
| 177 | 179 |
| 178 chrome.extension.sendRequest = | 180 chrome.extension.sendRequest = |
| 179 function(targetId_opt, request, responseCallback_opt) { | 181 function(targetId_opt, request, responseCallback_opt) { |
| 180 var targetId = extensionId; | 182 var targetId = extensionId; |
| 181 var responseCallback = null; | 183 var responseCallback = null; |
| 182 var lastArg = arguments.length - 1; | 184 var lastArg = arguments.length - 1; |
| 183 if (typeof(arguments[lastArg]) == "function") | 185 if (typeof(arguments[lastArg]) == "function") |
| 184 responseCallback = arguments[lastArg--]; | 186 responseCallback = arguments[lastArg--]; |
| 185 request = arguments[lastArg--]; | 187 request = arguments[lastArg--]; |
| 186 if (lastArg >= 0) | 188 if (lastArg >= 0 && typeof(arguments[lastArg]) == "string") |
| 187 targetId = arguments[lastArg--]; | 189 targetId = arguments[lastArg--]; |
| 190 if (lastArg != -1) |
| 191 throw new Error("Invalid arguments to sendRequest."); |
| 188 | 192 |
| 189 var port = chrome.extension.connect(targetId, | 193 var port = chrome.extension.connect(targetId, |
| 190 {name: chromeHidden.kRequestChannel}); | 194 {name: chromeHidden.kRequestChannel}); |
| 191 port.postMessage(request); | 195 port.postMessage(request); |
| 192 port.onMessage.addListener(function(response) { | 196 port.onMessage.addListener(function(response) { |
| 193 if (responseCallback) | 197 if (responseCallback) |
| 194 responseCallback(response); | 198 responseCallback(response); |
| 195 port.disconnect(); | 199 port.disconnect(); |
| 196 }); | 200 }); |
| 197 }; | 201 }; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 "extension.onConnectExternal", | 278 "extension.onConnectExternal", |
| 275 "extension.onRequestExternal", | 279 "extension.onRequestExternal", |
| 276 "i18n.getAcceptLanguages" | 280 "i18n.getAcceptLanguages" |
| 277 ]; | 281 ]; |
| 278 for (var i = 0; i < privileged.length; i++) { | 282 for (var i = 0; i < privileged.length; i++) { |
| 279 createStub(privileged[i]); | 283 createStub(privileged[i]); |
| 280 } | 284 } |
| 281 } | 285 } |
| 282 | 286 |
| 283 })(); | 287 })(); |
| OLD | NEW |