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 binding for the runtime API. | 5 // Custom binding for the runtime API. |
6 | 6 |
7 var binding = require('binding').Binding.create('runtime'); | 7 var binding = require('binding').Binding.create('runtime'); |
8 | 8 |
9 var runtimeNatives = requireNative('runtime'); | 9 var runtimeNatives = requireNative('runtime'); |
10 var extensionNatives = requireNative('extension'); | 10 var extensionNatives = requireNative('extension'); |
11 var GetExtensionViews = extensionNatives.GetExtensionViews; | 11 var GetExtensionViews = extensionNatives.GetExtensionViews; |
12 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; | 12 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; |
13 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; | 13 var OpenChannelToNativeApp = runtimeNatives.OpenChannelToNativeApp; |
14 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 14 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
15 var sendMessageUpdateArguments = | 15 var sendMessageUpdateArguments = |
16 require('miscellaneous_bindings').sendMessageUpdateArguments; | 16 require('miscellaneous_bindings').sendMessageUpdateArguments; |
17 | 17 |
18 binding.registerCustomHook(function(binding, id, contextType) { | 18 binding.registerCustomHook(function(binding, id, contextType) { |
19 var apiFunctions = binding.apiFunctions; | 19 var apiFunctions = binding.apiFunctions; |
20 var runtime = binding.compiledApi; | 20 var runtime = binding.compiledApi; |
21 | 21 |
22 // | 22 // |
23 // Unprivileged APIs. | 23 // Unprivileged APIs. |
24 // | 24 // |
25 | 25 |
26 runtime.id = id; | 26 runtime.id = id; |
27 | 27 |
28 apiFunctions.setHandleRequest('getManifest', function() { | 28 apiFunctions.setHandleRequest('getManifest', function() { |
29 return runtimeNatives.GetManifest(); | 29 return runtimeNatives.GetManifest(); |
30 }); | 30 }, false); |
31 | 31 |
32 apiFunctions.setHandleRequest('getURL', function(path) { | 32 apiFunctions.setHandleRequest('getURL', function(path) { |
33 path = String(path); | 33 path = String(path); |
34 if (!path.length || path[0] != '/') | 34 if (!path.length || path[0] != '/') |
35 path = '/' + path; | 35 path = '/' + path; |
36 return 'chrome-extension://' + id + path; | 36 return 'chrome-extension://' + id + path; |
37 }); | 37 }, false); |
38 | 38 |
39 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', | 39 apiFunctions.setUpdateArgumentsPreValidate('sendMessage', |
40 sendMessageUpdateArguments.bind(null, 'sendMessage')); | 40 sendMessageUpdateArguments.bind(null, 'sendMessage')); |
41 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', | 41 apiFunctions.setUpdateArgumentsPreValidate('sendNativeMessage', |
42 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); | 42 sendMessageUpdateArguments.bind(null, 'sendNativeMessage')); |
43 | 43 |
44 apiFunctions.setHandleRequest('sendMessage', | 44 apiFunctions.setHandleRequest('sendMessage', |
45 function(targetId, message, responseCallback) { | 45 function(targetId, message, responseCallback) { |
46 var port = runtime.connect(targetId || runtime.id, | 46 var port = runtime.connect(targetId || runtime.id, |
47 {name: chromeHidden.kMessageChannel}); | 47 {name: chromeHidden.kMessageChannel}); |
48 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 48 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
49 }); | 49 }, false); |
50 | 50 |
51 apiFunctions.setHandleRequest('sendNativeMessage', | 51 apiFunctions.setHandleRequest('sendNativeMessage', |
52 function(targetId, message, responseCallback) { | 52 function(targetId, message, responseCallback) { |
53 var port = runtime.connectNative(targetId); | 53 var port = runtime.connectNative(targetId); |
54 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 54 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
55 }); | 55 }, false); |
56 | 56 |
57 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { | 57 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { |
58 // Align missing (optional) function arguments with the arguments that | 58 // Align missing (optional) function arguments with the arguments that |
59 // schema validation is expecting, e.g. | 59 // schema validation is expecting, e.g. |
60 // runtime.connect() -> runtime.connect(null, null) | 60 // runtime.connect() -> runtime.connect(null, null) |
61 // runtime.connect({}) -> runtime.connect(null, {}) | 61 // runtime.connect({}) -> runtime.connect(null, {}) |
62 var nextArg = 0; | 62 var nextArg = 0; |
63 | 63 |
64 // targetId (first argument) is optional. | 64 // targetId (first argument) is optional. |
65 var targetId = null; | 65 var targetId = null; |
(...skipping 26 matching lines...) Expand all Loading... |
92 name = connectInfo.name; | 92 name = connectInfo.name; |
93 | 93 |
94 // Don't let orphaned content scripts communicate with their extension. | 94 // Don't let orphaned content scripts communicate with their extension. |
95 // http://crbug.com/168263 | 95 // http://crbug.com/168263 |
96 if (!chromeHidden.wasUnloaded) { | 96 if (!chromeHidden.wasUnloaded) { |
97 var portId = OpenChannelToExtension(runtime.id, targetId, name); | 97 var portId = OpenChannelToExtension(runtime.id, targetId, name); |
98 if (portId >= 0) | 98 if (portId >= 0) |
99 return chromeHidden.Port.createPort(portId, name); | 99 return chromeHidden.Port.createPort(portId, name); |
100 } | 100 } |
101 throw new Error('Error connecting to extension ' + targetId); | 101 throw new Error('Error connecting to extension ' + targetId); |
102 }); | 102 }, false); |
103 | 103 |
104 // | 104 // |
105 // Privileged APIs. | 105 // Privileged APIs. |
106 // | 106 // |
107 if (contextType != 'BLESSED_EXTENSION') | 107 if (contextType != 'BLESSED_EXTENSION') |
108 return; | 108 return; |
109 | 109 |
110 apiFunctions.setHandleRequest('connectNative', | 110 apiFunctions.setHandleRequest('connectNative', |
111 function(nativeAppName) { | 111 function(nativeAppName) { |
112 if (!chromeHidden.wasUnloaded) { | 112 if (!chromeHidden.wasUnloaded) { |
113 var portId = OpenChannelToNativeApp(runtime.id, nativeAppName); | 113 var portId = OpenChannelToNativeApp(runtime.id, nativeAppName); |
114 if (portId >= 0) | 114 if (portId >= 0) |
115 return chromeHidden.Port.createPort(portId, ''); | 115 return chromeHidden.Port.createPort(portId, ''); |
116 } | 116 } |
117 throw new Error('Error connecting to native app: ' + nativeAppName); | 117 throw new Error('Error connecting to native app: ' + nativeAppName); |
118 }); | 118 }, false); |
119 | 119 |
120 apiFunctions.setCustomCallback('getBackgroundPage', | 120 apiFunctions.setCustomCallback('getBackgroundPage', |
121 function(name, request, response) { | 121 function(name, request, response) { |
122 if (request.callback) { | 122 if (request.callback) { |
123 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; | 123 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; |
124 request.callback(bg); | 124 request.callback(bg); |
125 } | 125 } |
126 request.callback = null; | 126 request.callback = null; |
127 }); | 127 }); |
128 | 128 |
129 }); | 129 }); |
130 | 130 |
131 exports.binding = binding.generate(); | 131 exports.binding = binding.generate(); |
OLD | NEW |