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 runtimeNatives = requireNative('runtime'); | 7 var runtimeNatives = requireNative('runtime'); |
8 var extensionNatives = requireNative('extension'); | 8 var extensionNatives = requireNative('extension'); |
9 var GetExtensionViews = extensionNatives.GetExtensionViews; | 9 var GetExtensionViews = extensionNatives.GetExtensionViews; |
10 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; | 10 var OpenChannelToExtension = runtimeNatives.OpenChannelToExtension; |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 apiFunctions.setHandleRequest('sendMessage', | 41 apiFunctions.setHandleRequest('sendMessage', |
42 function(targetId, message, responseCallback) { | 42 function(targetId, message, responseCallback) { |
43 var port = chrome.runtime.connect(targetId || chrome.runtime.id, | 43 var port = chrome.runtime.connect(targetId || chrome.runtime.id, |
44 {name: chromeHidden.kMessageChannel}); | 44 {name: chromeHidden.kMessageChannel}); |
45 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); | 45 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
46 }); | 46 }); |
47 | 47 |
48 apiFunctions.setHandleRequest('sendNativeMessage', | 48 apiFunctions.setHandleRequest('sendNativeMessage', |
49 function(targetId, message, responseCallback) { | 49 function(targetId, message, responseCallback) { |
50 var port = chrome.runtime.connectNative( | 50 var port = chrome.runtime.connectNative(targetId); |
51 targetId, message, chromeHidden.kNativeMessageChannel); | 51 chromeHidden.Port.sendMessageImpl(port, message, responseCallback); |
52 chromeHidden.Port.sendMessageImpl(port, '', responseCallback); | |
53 }); | 52 }); |
54 | 53 |
55 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { | 54 apiFunctions.setUpdateArgumentsPreValidate('connect', function() { |
56 // Align missing (optional) function arguments with the arguments that | 55 // Align missing (optional) function arguments with the arguments that |
57 // schema validation is expecting, e.g. | 56 // schema validation is expecting, e.g. |
58 // runtime.connect() -> runtime.connect(null, null) | 57 // runtime.connect() -> runtime.connect(null, null) |
59 // runtime.connect({}) -> runtime.connect(null, {}) | 58 // runtime.connect({}) -> runtime.connect(null, {}) |
60 var nextArg = 0; | 59 var nextArg = 0; |
61 | 60 |
62 // targetId (first argument) is optional. | 61 // targetId (first argument) is optional. |
63 var targetId = null; | 62 var targetId = null; |
64 if (typeof(arguments[nextArg]) == 'string') | 63 if (typeof(arguments[nextArg]) == 'string') |
65 targetId = arguments[nextArg++]; | 64 targetId = arguments[nextArg++]; |
66 | 65 |
67 // connectInfo (second argument) is optional. | 66 // connectInfo (second argument) is optional. |
68 var connectInfo = null; | 67 var connectInfo = null; |
69 if (typeof(arguments[nextArg]) == 'object') | 68 if (typeof(arguments[nextArg]) == 'object') |
70 connectInfo = arguments[nextArg++]; | 69 connectInfo = arguments[nextArg++]; |
71 | 70 |
72 if (nextArg != arguments.length) | 71 if (nextArg != arguments.length) |
73 throw new Error('Invalid arguments to connect.'); | 72 throw new Error('Invalid arguments to connect.'); |
74 return [targetId, connectInfo]; | 73 return [targetId, connectInfo]; |
75 }); | 74 }); |
76 | 75 |
77 apiFunctions.setUpdateArgumentsPreValidate('connectNative', function() { | 76 apiFunctions.setUpdateArgumentsPreValidate('connectNative', |
78 var nextArg = 0; | 77 function(appName) { |
79 | 78 if (typeof(appName) !== 'string') { |
80 // appName is required. | |
81 var appName = arguments[nextArg++]; | |
82 | |
83 // connectionMessage is required. | |
84 var connectMessage = arguments[nextArg++]; | |
85 | |
86 // channelName is only passed by sendMessage | |
87 var channelName = 'connectNative'; | |
88 if (typeof(arguments[nextArg]) == 'string') | |
89 channelName = arguments[nextArg++]; | |
90 | |
91 if (nextArg != arguments.length) | |
92 throw new Error('Invalid arguments to connectNative.'); | 79 throw new Error('Invalid arguments to connectNative.'); |
93 return [appName, {name: channelName, message: connectMessage}]; | 80 } |
| 81 return [appName]; |
94 }); | 82 }); |
95 | 83 |
96 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { | 84 apiFunctions.setHandleRequest('connect', function(targetId, connectInfo) { |
97 if (!targetId) | 85 if (!targetId) |
98 targetId = chrome.runtime.id; | 86 targetId = chrome.runtime.id; |
99 var name = ''; | 87 var name = ''; |
100 if (connectInfo && connectInfo.name) | 88 if (connectInfo && connectInfo.name) |
101 name = connectInfo.name; | 89 name = connectInfo.name; |
102 | 90 |
103 var portId = OpenChannelToExtension(chrome.runtime.id, targetId, name); | 91 var portId = OpenChannelToExtension(chrome.runtime.id, targetId, name); |
104 if (portId >= 0) | 92 if (portId >= 0) |
105 return chromeHidden.Port.createPort(portId, name); | 93 return chromeHidden.Port.createPort(portId, name); |
106 throw new Error('Error connecting to extension ' + targetId); | 94 throw new Error('Error connecting to extension ' + targetId); |
107 }); | 95 }); |
108 | 96 |
109 // | 97 // |
110 // Privileged APIs. | 98 // Privileged APIs. |
111 // | 99 // |
112 if (contextType != 'BLESSED_EXTENSION') | 100 if (contextType != 'BLESSED_EXTENSION') |
113 return; | 101 return; |
114 | 102 |
115 apiFunctions.setHandleRequest('connectNative', | 103 apiFunctions.setHandleRequest('connectNative', |
116 function(nativeAppName, connectInfo) { | 104 function(nativeAppName) { |
117 // Turn the object into a string here, because it eventually will be. | 105 // Turn the object into a string here, because it eventually will be. |
118 var portId = OpenChannelToNativeApp(chrome.runtime.id, | 106 var portId = OpenChannelToNativeApp(chrome.runtime.id, nativeAppName); |
119 nativeAppName, | |
120 connectInfo.name, | |
121 JSON.stringify(connectInfo.message)); | |
122 if (portId >= 0) { | 107 if (portId >= 0) { |
123 return chromeHidden.Port.createPort(portId, connectInfo.name); | 108 return chromeHidden.Port.createPort(portId, ''); |
124 } | 109 } |
125 throw new Error('Error connecting to native app: ' + nativeAppName); | 110 throw new Error('Error connecting to native app: ' + nativeAppName); |
126 }); | 111 }); |
127 | 112 |
128 apiFunctions.setCustomCallback('getBackgroundPage', | 113 apiFunctions.setCustomCallback('getBackgroundPage', |
129 function(name, request, response) { | 114 function(name, request, response) { |
130 if (request.callback) { | 115 if (request.callback) { |
131 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; | 116 var bg = GetExtensionViews(-1, 'BACKGROUND')[0] || null; |
132 request.callback(bg); | 117 request.callback(bg); |
133 } | 118 } |
134 request.callback = null; | 119 request.callback = null; |
135 }); | 120 }); |
136 | 121 |
137 }); | 122 }); |
OLD | NEW |