OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 contains unprivileged javascript APIs for extensions and apps. It | 5 // This contains unprivileged javascript APIs for extensions and apps. It |
6 // can be loaded by any extension-related context, such as content scripts or | 6 // can be loaded by any extension-related context, such as content scripts or |
7 // background pages. See user_script_slave.cc for script that is loaded by | 7 // background pages. See user_script_slave.cc for script that is loaded by |
8 // content scripts only. | 8 // content scripts only. |
9 | 9 |
10 // TODO(kalman): factor requiring chrome out of here. | 10 // TODO(kalman): factor requiring chrome out of here. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 91 |
92 // Helper function for dispatchOnRequest. | 92 // Helper function for dispatchOnRequest. |
93 function handleSendRequestError(isSendMessage, | 93 function handleSendRequestError(isSendMessage, |
94 responseCallbackPreserved, | 94 responseCallbackPreserved, |
95 sourceExtensionId, | 95 sourceExtensionId, |
96 targetExtensionId, | 96 targetExtensionId, |
97 sourceUrl) { | 97 sourceUrl) { |
98 var errorMsg = []; | 98 var errorMsg = []; |
99 var eventName = isSendMessage ? "runtime.onMessage" : "extension.onRequest"; | 99 var eventName = isSendMessage ? "runtime.onMessage" : "extension.onRequest"; |
100 if (isSendMessage && !responseCallbackPreserved) { | 100 if (isSendMessage && !responseCallbackPreserved) { |
101 errorMsg.push( | 101 $Array.push(errorMsg, |
102 "The chrome." + eventName + " listener must return true if you " + | 102 "The chrome." + eventName + " listener must return true if you " + |
103 "want to send a response after the listener returns"); | 103 "want to send a response after the listener returns"); |
104 } else { | 104 } else { |
105 errorMsg.push( | 105 $Array.push(errorMsg, |
106 "Cannot send a response more than once per chrome." + eventName + | 106 "Cannot send a response more than once per chrome." + eventName + |
107 " listener per document"); | 107 " listener per document"); |
108 } | 108 } |
109 errorMsg.push("(message was sent by extension" + sourceExtensionId); | 109 $Array.push(errorMsg, "(message was sent by extension" + sourceExtensionId); |
110 if (sourceExtensionId != "" && sourceExtensionId != targetExtensionId) | 110 if (sourceExtensionId != "" && sourceExtensionId != targetExtensionId) |
111 errorMsg.push("for extension " + targetExtensionId); | 111 $Array.push(errorMsg, "for extension " + targetExtensionId); |
112 if (sourceUrl != "") | 112 if (sourceUrl != "") |
113 errorMsg.push("for URL " + sourceUrl); | 113 $Array.push(errorMsg, "for URL " + sourceUrl); |
114 lastError.set(eventName, errorMsg.join(" ") + ").", null, chrome); | 114 lastError.set(eventName, errorMsg.join(" ") + ").", null, chrome); |
115 } | 115 } |
116 | 116 |
117 // Helper function for dispatchOnConnect | 117 // Helper function for dispatchOnConnect |
118 function dispatchOnRequest(portId, channelName, sender, | 118 function dispatchOnRequest(portId, channelName, sender, |
119 sourceExtensionId, targetExtensionId, sourceUrl, | 119 sourceExtensionId, targetExtensionId, sourceUrl, |
120 isExternal) { | 120 isExternal) { |
121 var isSendMessage = channelName == kMessageChannel; | 121 var isSendMessage = channelName == kMessageChannel; |
122 var requestEvent = null; | 122 var requestEvent = null; |
123 if (isSendMessage) { | 123 if (isSendMessage) { |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 port = null; | 305 port = null; |
306 } | 306 } |
307 }); | 307 }); |
308 }; | 308 }; |
309 | 309 |
310 function sendMessageUpdateArguments(functionName) { | 310 function sendMessageUpdateArguments(functionName) { |
311 // Align missing (optional) function arguments with the arguments that | 311 // Align missing (optional) function arguments with the arguments that |
312 // schema validation is expecting, e.g. | 312 // schema validation is expecting, e.g. |
313 // extension.sendRequest(req) -> extension.sendRequest(null, req) | 313 // extension.sendRequest(req) -> extension.sendRequest(null, req) |
314 // extension.sendRequest(req, cb) -> extension.sendRequest(null, req, cb) | 314 // extension.sendRequest(req, cb) -> extension.sendRequest(null, req, cb) |
315 var args = Array.prototype.splice.call(arguments, 1); // skip functionName | 315 var args = $Array.splice(arguments, 1); // skip functionName |
316 var lastArg = args.length - 1; | 316 var lastArg = args.length - 1; |
317 | 317 |
318 // responseCallback (last argument) is optional. | 318 // responseCallback (last argument) is optional. |
319 var responseCallback = null; | 319 var responseCallback = null; |
320 if (typeof(args[lastArg]) == 'function') | 320 if (typeof(args[lastArg]) == 'function') |
321 responseCallback = args[lastArg--]; | 321 responseCallback = args[lastArg--]; |
322 | 322 |
323 // request (second argument) is required. | 323 // request (second argument) is required. |
324 var request = args[lastArg--]; | 324 var request = args[lastArg--]; |
325 | 325 |
(...skipping 13 matching lines...) Expand all Loading... |
339 exports.Port = Port; | 339 exports.Port = Port; |
340 exports.createPort = createPort; | 340 exports.createPort = createPort; |
341 exports.sendMessageImpl = sendMessageImpl; | 341 exports.sendMessageImpl = sendMessageImpl; |
342 exports.sendMessageUpdateArguments = sendMessageUpdateArguments; | 342 exports.sendMessageUpdateArguments = sendMessageUpdateArguments; |
343 | 343 |
344 // For C++ code to call. | 344 // For C++ code to call. |
345 exports.hasPort = hasPort; | 345 exports.hasPort = hasPort; |
346 exports.dispatchOnConnect = dispatchOnConnect; | 346 exports.dispatchOnConnect = dispatchOnConnect; |
347 exports.dispatchOnDisconnect = dispatchOnDisconnect; | 347 exports.dispatchOnDisconnect = dispatchOnDisconnect; |
348 exports.dispatchOnMessage = dispatchOnMessage; | 348 exports.dispatchOnMessage = dispatchOnMessage; |
OLD | NEW |