Chromium Code Reviews| 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 // Custom bindings for the extension API. | 5 // Custom bindings for the extension API. |
| 6 | 6 |
| 7 var extensionNatives = requireNative('extension'); | 7 var extensionNatives = requireNative('extension'); |
| 8 var GetExtensionViews = extensionNatives.GetExtensionViews; | 8 var GetExtensionViews = extensionNatives.GetExtensionViews; |
| 9 var OpenChannelToExtension = extensionNatives.OpenChannelToExtension; | 9 var OpenChannelToExtension = extensionNatives.OpenChannelToExtension; |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 return GetExtensionViews(windowId, 'TAB'); | 45 return GetExtensionViews(windowId, 'TAB'); |
| 46 }); | 46 }); |
| 47 | 47 |
| 48 apiFunctions.setHandleRequest('getURL', function(path) { | 48 apiFunctions.setHandleRequest('getURL', function(path) { |
| 49 path = String(path); | 49 path = String(path); |
| 50 if (!path.length || path[0] != '/') | 50 if (!path.length || path[0] != '/') |
| 51 path = '/' + path; | 51 path = '/' + path; |
| 52 return 'chrome-extension://' + extensionId + path; | 52 return 'chrome-extension://' + extensionId + path; |
| 53 }); | 53 }); |
| 54 | 54 |
| 55 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', function() { | 55 function sendMessageUpdateArguments() { |
| 56 // Align missing (optional) function arguments with the arguments that | 56 // Align missing (optional) function arguments with the arguments that |
| 57 // schema validation is expecting, e.g. | 57 // schema validation is expecting, e.g. |
| 58 // extension.sendRequest(req) -> extension.sendRequest(null, req) | 58 // extension.sendRequest(req) -> extension.sendRequest(null, req) |
| 59 // extension.sendRequest(req, cb) -> extension.sendRequest(null, req, cb) | 59 // extension.sendRequest(req, cb) -> extension.sendRequest(null, req, cb) |
| 60 var lastArg = arguments.length - 1; | 60 var lastArg = arguments.length - 1; |
| 61 | 61 |
| 62 // responseCallback (last argument) is optional. | 62 // responseCallback (last argument) is optional. |
| 63 var responseCallback = null; | 63 var responseCallback = null; |
| 64 if (typeof(arguments[lastArg]) == 'function') | 64 if (typeof(arguments[lastArg]) == 'function') |
| 65 responseCallback = arguments[lastArg--]; | 65 responseCallback = arguments[lastArg--]; |
| 66 | 66 |
| 67 // request (second argument) is required. | 67 // request (second argument) is required. |
| 68 var request = arguments[lastArg--]; | 68 var request = arguments[lastArg--]; |
| 69 | 69 |
| 70 // targetId (first argument, extensionId in the manfiest) is optional. | 70 // targetId (first argument, extensionId in the manfiest) is optional. |
| 71 var targetId = null; | 71 var targetId = null; |
| 72 if (lastArg >= 0) | 72 if (lastArg >= 0) |
| 73 targetId = arguments[lastArg--]; | 73 targetId = arguments[lastArg--]; |
| 74 | 74 |
| 75 if (lastArg != -1) | 75 if (lastArg != -1) |
| 76 throw new Error('Invalid arguments to sendRequest.'); | 76 throw new Error('Invalid arguments to sendMessage.'); |
| 77 return [targetId, request, responseCallback]; | 77 return [targetId, request, responseCallback]; |
| 78 }); | 78 } |
| 79 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', | |
| 80 sendMessageUpdateArguments); | |
|
Aaron Boodman
2012/03/30 21:28:00
You could use sendMessageUpdateArguments.bind(null
Matt Perry
2012/03/30 23:02:48
Done.
| |
| 81 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', | |
| 82 sendMessageUpdateArguments); | |
| 79 | 83 |
| 80 apiFunctions.setHandleRequest('sendRequest', | 84 apiFunctions.setHandleRequest('sendRequest', |
| 81 function(targetId, request, responseCallback) { | 85 function(targetId, request, responseCallback) { |
| 82 if (!targetId) | 86 var port = chrome.extension.connect(targetId || extensionId, |
| 83 targetId = extensionId; | 87 {name: chromeHidden.kRequestChannel}); |
| 84 if (!responseCallback) | 88 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); |
| 85 responseCallback = function() {}; | 89 }); |
| 86 | 90 |
| 87 var connectInfo = { name: chromeHidden.kRequestChannel }; | 91 apiFunctions.setHandleRequest('sendMessage', |
| 88 var port = chrome.extension.connect(targetId, connectInfo); | 92 function(targetId, message, responseCallback) { |
| 89 | 93 var port = chrome.extension.connect(targetId || extensionId, |
| 90 port.postMessage(request); | 94 {name: chromeHidden.kMessageChannel}); |
| 91 port.onDisconnect.addListener(function() { | 95 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
| 92 // For onDisconnects, we only notify the callback if there was an error | |
| 93 try { | |
| 94 if (chrome.extension.lastError) | |
| 95 responseCallback(); | |
| 96 } finally { | |
| 97 port = null; | |
| 98 } | |
| 99 }); | |
| 100 port.onMessage.addListener(function(response) { | |
| 101 try { | |
| 102 responseCallback(response); | |
| 103 } finally { | |
| 104 port.disconnect(); | |
| 105 port = null; | |
| 106 } | |
| 107 }); | |
| 108 }); | 96 }); |
| 109 | 97 |
| 110 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { | 98 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { |
| 111 // Align missing (optional) function arguments with the arguments that | 99 // Align missing (optional) function arguments with the arguments that |
| 112 // schema validation is expecting, e.g. | 100 // schema validation is expecting, e.g. |
| 113 // extension.connect() -> extension.connect(null, null) | 101 // extension.connect() -> extension.connect(null, null) |
| 114 // extension.connect({}) -> extension.connect(null, {}) | 102 // extension.connect({}) -> extension.connect(null, {}) |
| 115 var nextArg = 0; | 103 var nextArg = 0; |
| 116 | 104 |
| 117 // targetId (first argument) is optional. | 105 // targetId (first argument) is optional. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 135 var name = ''; | 123 var name = ''; |
| 136 if (connectInfo && connectInfo.name) | 124 if (connectInfo && connectInfo.name) |
| 137 name = connectInfo.name; | 125 name = connectInfo.name; |
| 138 | 126 |
| 139 var portId = OpenChannelToExtension(extensionId, targetId, name); | 127 var portId = OpenChannelToExtension(extensionId, targetId, name); |
| 140 if (portId >= 0) | 128 if (portId >= 0) |
| 141 return chromeHidden.Port.createPort(portId, name); | 129 return chromeHidden.Port.createPort(portId, name); |
| 142 throw new Error('Error connecting to extension ' + targetId); | 130 throw new Error('Error connecting to extension ' + targetId); |
| 143 }); | 131 }); |
| 144 }); | 132 }); |
| OLD | NEW |