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(functionName) { |
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 args = Array.prototype.splice.call(arguments, 1); // skip functionName |
| 61 var lastArg = args.length - 1; |
61 | 62 |
62 // responseCallback (last argument) is optional. | 63 // responseCallback (last argument) is optional. |
63 var responseCallback = null; | 64 var responseCallback = null; |
64 if (typeof(arguments[lastArg]) == 'function') | 65 if (typeof(args[lastArg]) == 'function') |
65 responseCallback = arguments[lastArg--]; | 66 responseCallback = args[lastArg--]; |
66 | 67 |
67 // request (second argument) is required. | 68 // request (second argument) is required. |
68 var request = arguments[lastArg--]; | 69 var request = args[lastArg--]; |
69 | 70 |
70 // targetId (first argument, extensionId in the manfiest) is optional. | 71 // targetId (first argument, extensionId in the manfiest) is optional. |
71 var targetId = null; | 72 var targetId = null; |
72 if (lastArg >= 0) | 73 if (lastArg >= 0) |
73 targetId = arguments[lastArg--]; | 74 targetId = args[lastArg--]; |
74 | 75 |
75 if (lastArg != -1) | 76 if (lastArg != -1) |
76 throw new Error('Invalid arguments to sendRequest.'); | 77 throw new Error('Invalid arguments to ' + functionName + '.'); |
77 return [targetId, request, responseCallback]; | 78 return [targetId, request, responseCallback]; |
78 }); | 79 } |
| 80 apiFunctions.setUpdateArgumentsPreValidate('sendRequest', |
| 81 sendMessageUpdateArguments.bind(null, 'sendRequest')); |
| 82 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', |
| 83 sendMessageUpdateArguments.bind(null, 'sendMessage')); |
79 | 84 |
80 apiFunctions.setHandleRequest('sendRequest', | 85 apiFunctions.setHandleRequest('sendRequest', |
81 function(targetId, request, responseCallback) { | 86 function(targetId, request, responseCallback) { |
82 if (!targetId) | 87 var port = chrome.extension.connect(targetId || extensionId, |
83 targetId = extensionId; | 88 {name: chromeHidden.kRequestChannel}); |
84 if (!responseCallback) | 89 chromeHidden.Port.sendMessageImpl(port, request, responseCallback); |
85 responseCallback = function() {}; | 90 }); |
86 | 91 |
87 var connectInfo = { name: chromeHidden.kRequestChannel }; | 92 apiFunctions.setHandleRequest('sendMessage', |
88 var port = chrome.extension.connect(targetId, connectInfo); | 93 function(targetId, message, responseCallback) { |
89 | 94 var port = chrome.extension.connect(targetId || extensionId, |
90 port.postMessage(request); | 95 {name: chromeHidden.kMessageChannel}); |
91 port.onDisconnect.addListener(function() { | 96 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 }); | 97 }); |
109 | 98 |
110 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { | 99 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { |
111 // Align missing (optional) function arguments with the arguments that | 100 // Align missing (optional) function arguments with the arguments that |
112 // schema validation is expecting, e.g. | 101 // schema validation is expecting, e.g. |
113 // extension.connect() -> extension.connect(null, null) | 102 // extension.connect() -> extension.connect(null, null) |
114 // extension.connect({}) -> extension.connect(null, {}) | 103 // extension.connect({}) -> extension.connect(null, {}) |
115 var nextArg = 0; | 104 var nextArg = 0; |
116 | 105 |
117 // targetId (first argument) is optional. | 106 // targetId (first argument) is optional. |
(...skipping 17 matching lines...) Expand all Loading... |
135 var name = ''; | 124 var name = ''; |
136 if (connectInfo && connectInfo.name) | 125 if (connectInfo && connectInfo.name) |
137 name = connectInfo.name; | 126 name = connectInfo.name; |
138 | 127 |
139 var portId = OpenChannelToExtension(extensionId, targetId, name); | 128 var portId = OpenChannelToExtension(extensionId, targetId, name); |
140 if (portId >= 0) | 129 if (portId >= 0) |
141 return chromeHidden.Port.createPort(portId, name); | 130 return chromeHidden.Port.createPort(portId, name); |
142 throw new Error('Error connecting to extension ' + targetId); | 131 throw new Error('Error connecting to extension ' + targetId); |
143 }); | 132 }); |
144 }); | 133 }); |
OLD | NEW |