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 runtime API. | 5 // Custom bindings for the runtime API. |
6 | 6 |
| 7 var bindings = new (require('schema_binding_generator').Bindings)('runtime'); |
| 8 |
7 var runtimeNatives = requireNative('runtime'); | 9 var runtimeNatives = requireNative('runtime'); |
8 var extensionNatives = requireNative('extension'); | 10 var extensionNatives = requireNative('extension'); |
9 var GetExtensionViews = extensionNatives.GetExtensionViews; | 11 var GetExtensionViews = extensionNatives.GetExtensionViews; |
10 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; | 12 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; |
11 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; | 13 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; |
12 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 14 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
13 var sendMessageUpdateArguments = | 15 var sendMessageUpdateArguments = |
14 require('miscellaneous_bindings').sendMessageUpdateArguments; | 16 require('miscellaneous_bindings').sendMessageUpdateArguments; |
15 | 17 |
16 chromeHidden.registerCustomHook('runtime', function(bindings, id, contextType) { | 18 bindings.registerCustomHook(function(bindings, id, contextType) { |
17 var apiFunctions = bindings.apiFunctions; | 19 var apiFunctions = bindings.apiFunctions; |
| 20 var runtime = bindings.compiledApi; |
18 | 21 |
19 // | 22 // |
20 // Unprivileged APIs. | 23 // Unprivileged APIs. |
21 // | 24 // |
22 | 25 |
23 chrome.runtime.id = id; | 26 runtime.id = id; |
24 | 27 |
25 apiFunctions.setHandleRequest('getManifest', function() { | 28 apiFunctions.setHandleRequest('getManifest', function() { |
26 return runtimeNatives.GetManifest(); | 29 return runtimeNatives.GetManifest(); |
27 }); | 30 }); |
28 | 31 |
29 apiFunctions.setHandleRequest('getURL', function(path) { | 32 apiFunctions.setHandleRequest('getURL', function(path) { |
30 path = String(path); | 33 path = String(path); |
31 if (!path.length || path[0] != '/') | 34 if (!path.length || path[0] != '/') |
32 path = '/' + path; | 35 path = '/' + path; |
33 return 'chrome-extension://' + id + path; | 36 return 'chrome-extension://' + id + path; |
34 }); | 37 }); |
35 | 38 |
36 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', | 39 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', |
37 sendMessageUpdateArguments.bind(null, 'sendMessage')); | 40 sendMessageUpdateArguments.bind(null, 'sendMessage')); |
38 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', | 41 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', |
39 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); | 42 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); |
40 | 43 |
41 apiFunctions.setHandleRequest('sendMessage', | 44 apiFunctions.setHandleRequest('sendMessage', |
42 function(targetId, message, responseCallback) { | 45 function(targetId, message, responseCallback) { |
43 var port = chrome.runtime.connect(targetId || chrome.runtime.id, | 46 var port = runtime.connect(targetId || runtime.id, |
44 {name: chromeHidden.kMessageChannel}); | 47 {name: chromeHidden.kMessageChannel}); |
45 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 48 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
46 }); | 49 }); |
47 | 50 |
48 apiFunctions.setHandleRequest('sendNativeMessage', | 51 apiFunctions.setHandleRequest('sendNativeMessage', |
49 function(targetId, message, responseCallback) { | 52 function(targetId, message, responseCallback) { |
50 var port = chrome.runtime.connectNative(targetId); | 53 var port = runtime.connectNative(targetId); |
51 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 54 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
52 }); | 55 }); |
53 | 56 |
54 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { | 57 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { |
55 // Align missing (optional) function arguments with the arguments that | 58 // Align missing (optional) function arguments with the arguments that |
56 // schema validation is expecting, e.g. | 59 // schema validation is expecting, e.g. |
57 // runtime.connect() -> runtime.connect(null, null) | 60 // runtime.connect() -> runtime.connect(null, null) |
58 // runtime.connect({}) -> runtime.connect(null, {}) | 61 // runtime.connect({}) -> runtime.connect(null, {}) |
59 var nextArg = 0; | 62 var nextArg = 0; |
60 | 63 |
(...skipping 15 matching lines...) Expand all Loading... |
76 apiFunctions.setUpdateArgumentsPreValidate('connectNative', | 79 apiFunctions.setUpdateArgumentsPreValidate('connectNative', |
77 function(appName) { | 80 function(appName) { |
78 if (typeof(appName) !== 'string') { | 81 if (typeof(appName) !== 'string') { |
79 throw new Error('Invalid arguments to connectNative.'); | 82 throw new Error('Invalid arguments to connectNative.'); |
80 } | 83 } |
81 return [appName]; | 84 return [appName]; |
82 }); | 85 }); |
83 | 86 |
84 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { | 87 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { |
85 if (!targetId) | 88 if (!targetId) |
86 targetId = chrome.runtime.id; | 89 targetId = runtime.id; |
87 var name = ''; | 90 var name = ''; |
88 if (connectInfo && connectInfo.name) | 91 if (connectInfo && connectInfo.name) |
89 name = connectInfo.name; | 92 name = connectInfo.name; |
90 | 93 |
91 var portId = OpenChannelToExtension(chrome.runtime.id, targetId, name); | 94 var portId = OpenChannelToExtension(runtime.id, targetId, name); |
92 if (portId >= 0) | 95 if (portId >= 0) |
93 return chromeHidden.Port.createPort(portId, name); | 96 return chromeHidden.Port.createPort(portId, name); |
94 throw new Error('Error connecting to extension ' + targetId); | 97 throw new Error('Error connecting to extension ' + targetId); |
95 }); | 98 }); |
96 | 99 |
97 // | 100 // |
98 // Privileged APIs. | 101 // Privileged APIs. |
99 // | 102 // |
100 if (contextType != 'BLESSED_EXTENSION') | 103 if (contextType != 'BLESSED_EXTENSION') |
101 return; | 104 return; |
102 | 105 |
103 apiFunctions.setHandleRequest('connectNative', | 106 apiFunctions.setHandleRequest('connectNative', |
104 function(nativeAppName) { | 107 function(nativeAppName) { |
105 // Turn the object into a string here, because it eventually will be. | 108 // Turn the object into a string here, because it eventually will be. |
106 var portId = OpenChannelToNativeApp(chrome.runtime.id, nativeAppName); | 109 var portId = OpenChannelToNativeApp(runtime.id, nativeAppName); |
107 if (portId >= 0) { | 110 if (portId >= 0) { |
108 return chromeHidden.Port.createPort(portId, ''); | 111 return chromeHidden.Port.createPort(portId, ''); |
109 } | 112 } |
110 throw new Error('Error connecting to native app: ' + nativeAppName); | 113 throw new Error('Error connecting to native app: ' + nativeAppName); |
111 }); | 114 }); |
112 | 115 |
113 apiFunctions.setCustomCallback('getBackgroundPage', | 116 apiFunctions.setCustomCallback('getBackgroundPage', |
114 function(name, request, response) { | 117 function(name, request, response) { |
115 if (request.callback) { | 118 if (request.callback) { |
116 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; | 119 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; |
117 request.callback(bg); | 120 request.callback(bg); |
118 } | 121 } |
119 request.callback = null; | 122 request.callback = null; |
120 }); | 123 }); |
121 | 124 |
122 }); | 125 }); |
| 126 |
| 127 exports.bindings = bindings.generate(); |
OLD | NEW |