OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 chrome.runtime.onMessageExternal.addListener( | 5 chrome.runtime.onMessageExternal.addListener(function( |
6 function(message, sender, sendResponse) { | 6 message, sender, sendResponse) { |
7 function doSendResponse(value, errorString) { | 7 function doSendResponse(value, errorString) { |
8 var error = null; | 8 var error = null; |
9 if (errorString) { | 9 if (errorString) { |
10 error = {}; | 10 error = {}; |
11 error['name'] = 'ComponentExtensionError'; | 11 error['name'] = 'ComponentExtensionError'; |
12 error['message'] = errorString; | 12 error['message'] = errorString; |
13 } | 13 } |
14 | 14 |
15 var errorMessage = error || chrome.extension.lastError; | 15 var errorMessage = error || chrome.extension.lastError; |
16 sendResponse({'value': value, 'error': errorMessage}); | 16 sendResponse({'value': value, 'error': errorMessage}); |
| 17 } |
| 18 |
| 19 function getHost(url) { |
| 20 if (!url) |
| 21 return ''; |
| 22 // Use the DOM to parse the URL. Since we don't add the anchor to |
| 23 // the page, this is the only reference to it and it will be |
| 24 // deleted once it's gone out of scope. |
| 25 var a = document.createElement('a'); |
| 26 a.href = url; |
| 27 var origin = a.protocol + '//' + a.hostname; |
| 28 if (a.port != '') |
| 29 origin = origin + ':' + a.port; |
| 30 origin = origin + '/'; |
| 31 return origin; |
| 32 } |
| 33 |
| 34 try { |
| 35 var requestInfo = {}; |
| 36 |
| 37 // Set the tab ID. If it's passed in the message, use that. |
| 38 // Otherwise use the sender information. |
| 39 if (message['tabId']) { |
| 40 requestInfo['tabId'] = +message['tabId']; |
| 41 if (isNaN(requestInfo['tabId'])) { |
| 42 throw new Error( |
| 43 'Cannot convert tab ID string to integer: ' + message['tabId']); |
17 } | 44 } |
18 | 45 } else if (sender.tab) { |
19 function getHost(url) { | 46 requestInfo['tabId'] = sender.tab.id; |
20 if (!url) | 47 } |
21 return ''; | 48 |
22 // Use the DOM to parse the URL. Since we don't add the anchor to | 49 if (sender.guestProcessId) { |
23 // the page, this is the only reference to it and it will be | 50 requestInfo['guestProcessId'] = sender.guestProcessId; |
24 // deleted once it's gone out of scope. | 51 } |
25 var a = document.createElement('a'); | 52 |
26 a.href = url; | 53 var method = message['method']; |
27 var origin = a.protocol + '//' + a.hostname; | 54 |
28 if (a.port != '') | 55 // Set the origin. If a URL is passed in the message, use that. |
29 origin = origin + ':' + a.port; | 56 // Otherwise use the sender information. |
30 origin = origin + '/'; | 57 var origin; |
31 return origin; | 58 if (message['winUrl']) { |
32 } | 59 origin = getHost(message['winUrl']); |
33 | 60 } else { |
34 try { | 61 origin = getHost(sender.url); |
35 var requestInfo = {}; | 62 } |
36 | 63 |
37 // Set the tab ID. If it's passed in the message, use that. | 64 if (method == 'cpu.getInfo') { |
38 // Otherwise use the sender information. | 65 chrome.system.cpu.getInfo(doSendResponse); |
39 if (message['tabId']) { | 66 return true; |
40 requestInfo['tabId'] = +message['tabId']; | 67 } else if (method == 'logging.setMetadata') { |
41 if (isNaN(requestInfo['tabId'])) { | 68 var metaData = message['metaData']; |
42 throw new Error('Cannot convert tab ID string to integer: ' + | 69 chrome.webrtcLoggingPrivate.setMetaData( |
43 message['tabId']); | 70 requestInfo, origin, metaData, doSendResponse); |
44 } | 71 return true; |
45 } else if (sender.tab) { | 72 } else if (method == 'logging.start') { |
46 requestInfo['tabId'] = sender.tab.id; | 73 chrome.webrtcLoggingPrivate.start(requestInfo, origin, doSendResponse); |
47 } | 74 return true; |
48 | 75 } else if (method == 'logging.uploadOnRenderClose') { |
49 if (sender.guestProcessId) { | 76 chrome.webrtcLoggingPrivate.setUploadOnRenderClose( |
50 requestInfo['guestProcessId'] = sender.guestProcessId; | 77 requestInfo, origin, true); |
51 } | 78 doSendResponse(); |
52 | 79 return false; |
53 var method = message['method']; | 80 } else if (method == 'logging.noUploadOnRenderClose') { |
54 | 81 chrome.webrtcLoggingPrivate.setUploadOnRenderClose( |
55 // Set the origin. If a URL is passed in the message, use that. | 82 requestInfo, origin, false); |
56 // Otherwise use the sender information. | 83 doSendResponse(); |
57 var origin; | 84 return false; |
58 if (message['winUrl']) { | 85 } else if (method == 'logging.stop') { |
59 origin = getHost(message['winUrl']); | 86 chrome.webrtcLoggingPrivate.stop(requestInfo, origin, doSendResponse); |
60 } else { | 87 return true; |
61 origin = getHost(sender.url); | 88 } else if (method == 'logging.upload') { |
62 } | 89 chrome.webrtcLoggingPrivate.upload(requestInfo, origin, doSendResponse); |
63 | 90 return true; |
64 if (method == 'cpu.getInfo') { | 91 } else if (method == 'logging.uploadStored') { |
65 chrome.system.cpu.getInfo(doSendResponse); | 92 var logId = message['logId']; |
66 return true; | 93 chrome.webrtcLoggingPrivate.uploadStored( |
67 } else if (method == 'logging.setMetadata') { | 94 requestInfo, origin, logId, doSendResponse); |
68 var metaData = message['metaData']; | 95 return true; |
69 chrome.webrtcLoggingPrivate.setMetaData( | 96 } else if (method == 'logging.stopAndUpload') { |
70 requestInfo, origin, metaData, doSendResponse); | 97 // Stop everything and upload. This is allowed to be called even if |
71 return true; | 98 // logs have already been stopped or not started. Therefore, ignore |
72 } else if (method == 'logging.start') { | 99 // any errors along the way, but store them, so that if upload fails |
73 chrome.webrtcLoggingPrivate.start( | 100 // they are all reported back. |
74 requestInfo, origin, doSendResponse); | 101 // Stop incoming and outgoing RTP dumps separately, otherwise |
75 return true; | 102 // stopRtpDump will fail and not stop anything if either type has not |
76 } else if (method == 'logging.uploadOnRenderClose') { | 103 // been started. |
77 chrome.webrtcLoggingPrivate.setUploadOnRenderClose( | 104 var errors = []; |
78 requestInfo, origin, true); | 105 chrome.webrtcLoggingPrivate.stopRtpDump( |
79 doSendResponse(); | 106 requestInfo, origin, true /* incoming */, false /* outgoing */, |
80 return false; | 107 function() { |
81 } else if (method == 'logging.noUploadOnRenderClose') { | |
82 chrome.webrtcLoggingPrivate.setUploadOnRenderClose( | |
83 requestInfo, origin, false); | |
84 doSendResponse(); | |
85 return false; | |
86 } else if (method == 'logging.stop') { | |
87 chrome.webrtcLoggingPrivate.stop( | |
88 requestInfo, origin, doSendResponse); | |
89 return true; | |
90 } else if (method == 'logging.upload') { | |
91 chrome.webrtcLoggingPrivate.upload( | |
92 requestInfo, origin, doSendResponse); | |
93 return true; | |
94 } else if (method == 'logging.uploadStored') { | |
95 var logId = message['logId']; | |
96 chrome.webrtcLoggingPrivate.uploadStored( | |
97 requestInfo, origin, logId, doSendResponse); | |
98 return true; | |
99 } else if (method == 'logging.stopAndUpload') { | |
100 // Stop everything and upload. This is allowed to be called even if | |
101 // logs have already been stopped or not started. Therefore, ignore | |
102 // any errors along the way, but store them, so that if upload fails | |
103 // they are all reported back. | |
104 // Stop incoming and outgoing RTP dumps separately, otherwise | |
105 // stopRtpDump will fail and not stop anything if either type has not | |
106 // been started. | |
107 var errors = []; | |
108 chrome.webrtcLoggingPrivate.stopRtpDump( | |
109 requestInfo, origin, true /* incoming */, false /* outgoing */, | |
110 function() { | |
111 appendLastErrorMessage(errors); | 108 appendLastErrorMessage(errors); |
112 chrome.webrtcLoggingPrivate.stopRtpDump( | 109 chrome.webrtcLoggingPrivate.stopRtpDump( |
113 requestInfo, origin, false /* incoming */, true /* outgoing */, | 110 requestInfo, origin, false /* incoming */, true /* outgoing */, |
114 function() { | 111 function() { |
115 appendLastErrorMessage(errors); | 112 appendLastErrorMessage(errors); |
116 chrome.webrtcLoggingPrivate.stop( | 113 chrome.webrtcLoggingPrivate.stop( |
117 requestInfo, origin, function() { | 114 requestInfo, origin, function() { |
118 appendLastErrorMessage(errors); | 115 appendLastErrorMessage(errors); |
119 chrome.webrtcLoggingPrivate.upload( | 116 chrome.webrtcLoggingPrivate.upload( |
120 requestInfo, origin, | 117 requestInfo, origin, function(uploadValue) { |
121 function(uploadValue) { | 118 var errorMessage = null; |
122 var errorMessage = null; | 119 // If upload fails, report all previous errors. |
123 // If upload fails, report all previous errors. Otherwise, | 120 // Otherwise, throw them away. |
124 // throw them away. | 121 if (chrome.extension.lastError !== undefined) { |
125 if (chrome.extension.lastError !== undefined) { | 122 appendLastErrorMessage(errors); |
126 appendLastErrorMessage(errors); | 123 errorMessage = errors.join('; '); |
127 errorMessage = errors.join('; '); | 124 } |
128 } | 125 doSendResponse(uploadValue, errorMessage); |
129 doSendResponse(uploadValue, errorMessage); | 126 }); |
| 127 }); |
130 }); | 128 }); |
131 }); | |
132 }); | |
133 }); | 129 }); |
134 return true; | 130 return true; |
135 } else if (method == 'logging.store') { | 131 } else if (method == 'logging.store') { |
136 var logId = message['logId']; | 132 var logId = message['logId']; |
137 chrome.webrtcLoggingPrivate.store( | 133 chrome.webrtcLoggingPrivate.store( |
138 requestInfo, origin, logId, doSendResponse); | 134 requestInfo, origin, logId, doSendResponse); |
139 return true; | 135 return true; |
140 } else if (method == 'logging.discard') { | 136 } else if (method == 'logging.discard') { |
141 chrome.webrtcLoggingPrivate.discard( | 137 chrome.webrtcLoggingPrivate.discard(requestInfo, origin, doSendResponse); |
142 requestInfo, origin, doSendResponse); | 138 return true; |
143 return true; | 139 } else if (method == 'getSinks') { |
144 } else if (method == 'getSinks') { | 140 chrome.webrtcAudioPrivate.getSinks(doSendResponse); |
145 chrome.webrtcAudioPrivate.getSinks(doSendResponse); | 141 return true; |
146 return true; | 142 } else if (method == 'getAssociatedSink') { |
147 } else if (method == 'getAssociatedSink') { | 143 var sourceId = message['sourceId']; |
148 var sourceId = message['sourceId']; | 144 chrome.webrtcAudioPrivate.getAssociatedSink( |
149 chrome.webrtcAudioPrivate.getAssociatedSink( | 145 origin, sourceId, doSendResponse); |
150 origin, sourceId, doSendResponse); | 146 return true; |
151 return true; | 147 } else if (method == 'isExtensionEnabled') { |
152 } else if (method == 'isExtensionEnabled') { | 148 // This method is necessary because there may be more than one |
153 // This method is necessary because there may be more than one | 149 // version of this extension, under different extension IDs. By |
154 // version of this extension, under different extension IDs. By | 150 // first calling this method on the extension ID, the client can |
155 // first calling this method on the extension ID, the client can | 151 // check if it's loaded; if it's not, the extension system will |
156 // check if it's loaded; if it's not, the extension system will | 152 // call the callback with no arguments and set |
157 // call the callback with no arguments and set | 153 // chrome.runtime.lastError. |
158 // chrome.runtime.lastError. | 154 doSendResponse(); |
159 doSendResponse(); | 155 return false; |
160 return false; | 156 } else if (method == 'getNaclArchitecture') { |
161 } else if (method == 'getNaclArchitecture') { | 157 chrome.runtime.getPlatformInfo(function(obj) { |
162 chrome.runtime.getPlatformInfo(function(obj) { | 158 doSendResponse(obj.nacl_arch); |
163 doSendResponse(obj.nacl_arch); | 159 }); |
164 }); | 160 return true; |
165 return true; | 161 } else if (method == 'logging.startRtpDump') { |
166 } else if (method == 'logging.startRtpDump') { | 162 var incoming = message['incoming'] || false; |
167 var incoming = message['incoming'] || false; | 163 var outgoing = message['outgoing'] || false; |
168 var outgoing = message['outgoing'] || false; | 164 chrome.webrtcLoggingPrivate.startRtpDump( |
169 chrome.webrtcLoggingPrivate.startRtpDump( | 165 requestInfo, origin, incoming, outgoing, doSendResponse); |
170 requestInfo, origin, incoming, outgoing, doSendResponse); | 166 return true; |
171 return true; | 167 } else if (method == 'logging.stopRtpDump') { |
172 } else if (method == 'logging.stopRtpDump') { | 168 var incoming = message['incoming'] || false; |
173 var incoming = message['incoming'] || false; | 169 var outgoing = message['outgoing'] || false; |
174 var outgoing = message['outgoing'] || false; | 170 chrome.webrtcLoggingPrivate.stopRtpDump( |
175 chrome.webrtcLoggingPrivate.stopRtpDump( | 171 requestInfo, origin, incoming, outgoing, doSendResponse); |
176 requestInfo, origin, incoming, outgoing, doSendResponse); | 172 return true; |
177 return true; | 173 } else if (method == 'logging.startAudioDebugRecordings') { |
178 } else if (method == 'logging.startAudioDebugRecordings') { | 174 var seconds = message['seconds'] || 0; |
179 var seconds = message['seconds'] || 0; | 175 chrome.webrtcLoggingPrivate.startAudioDebugRecordings( |
180 chrome.webrtcLoggingPrivate.startAudioDebugRecordings( | 176 requestInfo, origin, seconds, doSendResponse); |
181 requestInfo, origin, seconds, doSendResponse); | 177 return true; |
182 return true; | 178 } else if (method == 'logging.stopAudioDebugRecordings') { |
183 } else if (method == 'logging.stopAudioDebugRecordings') { | 179 chrome.webrtcLoggingPrivate.stopAudioDebugRecordings( |
184 chrome.webrtcLoggingPrivate.stopAudioDebugRecordings( | 180 requestInfo, origin, doSendResponse); |
185 requestInfo, origin, doSendResponse); | 181 return true; |
186 return true; | 182 } else if (method == 'logging.startWebRtcEventLogging') { |
187 } else if (method == 'logging.startWebRtcEventLogging') { | 183 var seconds = message['seconds'] || 0; |
188 var seconds = message['seconds'] || 0; | 184 chrome.webrtcLoggingPrivate.startWebRtcEventLogging( |
189 chrome.webrtcLoggingPrivate.startWebRtcEventLogging( | 185 requestInfo, origin, seconds, doSendResponse); |
190 requestInfo, origin, seconds, doSendResponse); | 186 return true; |
191 return true; | 187 } else if (method == 'logging.stopWebRtcEventLogging') { |
192 } else if (method == 'logging.stopWebRtcEventLogging') { | 188 chrome.webrtcLoggingPrivate.stopWebRtcEventLogging( |
193 chrome.webrtcLoggingPrivate.stopWebRtcEventLogging( | 189 requestInfo, origin, doSendResponse); |
194 requestInfo, origin, doSendResponse); | 190 return true; |
195 return true; | 191 } else if (method == 'setAudioExperiments') { |
196 } else if (method == 'setAudioExperiments') { | 192 var experiments = message['experiments']; |
197 var experiments = message['experiments']; | 193 chrome.webrtcAudioPrivate.setAudioExperiments( |
198 chrome.webrtcAudioPrivate.setAudioExperiments( | 194 requestInfo, origin, experiments); |
199 requestInfo, origin, experiments); | 195 doSendResponse(); |
200 doSendResponse(); | 196 return false; |
201 return false; | 197 } |
202 } | 198 |
203 | 199 throw new Error('Unknown method: ' + method); |
204 throw new Error('Unknown method: ' + method); | 200 } catch (e) { |
205 } catch (e) { | 201 doSendResponse(null, e.name + ': ' + e.message); |
206 doSendResponse(null, e.name + ': ' + e.message); | 202 } |
207 } | 203 }); |
208 } | |
209 ); | |
210 | 204 |
211 // If Hangouts connects with a port named 'onSinksChangedListener', we | 205 // If Hangouts connects with a port named 'onSinksChangedListener', we |
212 // will register a listener and send it a message {'eventName': | 206 // will register a listener and send it a message {'eventName': |
213 // 'onSinksChanged'} whenever the event fires. | 207 // 'onSinksChanged'} whenever the event fires. |
214 function onSinksChangedPort(port) { | 208 function onSinksChangedPort(port) { |
215 function clientListener() { | 209 function clientListener() { |
216 port.postMessage({'eventName': 'onSinksChanged'}); | 210 port.postMessage({'eventName': 'onSinksChanged'}); |
217 } | 211 } |
218 chrome.webrtcAudioPrivate.onSinksChanged.addListener(clientListener); | 212 chrome.webrtcAudioPrivate.onSinksChanged.addListener(clientListener); |
219 | 213 |
220 port.onDisconnect.addListener(function() { | 214 port.onDisconnect.addListener(function() { |
221 chrome.webrtcAudioPrivate.onSinksChanged.removeListener( | 215 chrome.webrtcAudioPrivate.onSinksChanged.removeListener(clientListener); |
222 clientListener); | |
223 }); | 216 }); |
224 } | 217 } |
225 | 218 |
226 // This is a one-time-use port for calling chooseDesktopMedia. The page | 219 // This is a one-time-use port for calling chooseDesktopMedia. The page |
227 // sends one message, identifying the requested source types, and the | 220 // sends one message, identifying the requested source types, and the |
228 // extension sends a single reply, with the user's selected streamId. A port | 221 // extension sends a single reply, with the user's selected streamId. A port |
229 // is used so that if the page is closed before that message is sent, the | 222 // is used so that if the page is closed before that message is sent, the |
230 // window picker dialog will be closed. | 223 // window picker dialog will be closed. |
231 function onChooseDesktopMediaPort(port) { | 224 function onChooseDesktopMediaPort(port) { |
232 function sendResponse(streamId) { | 225 function sendResponse(streamId) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 if (!tabProcess) { | 273 if (!tabProcess) { |
281 return; | 274 return; |
282 } | 275 } |
283 var pluginProcessCpu = 0, browserProcessCpu = 0, gpuProcessCpu = 0; | 276 var pluginProcessCpu = 0, browserProcessCpu = 0, gpuProcessCpu = 0; |
284 for (var pid in processes) { | 277 for (var pid in processes) { |
285 var process = processes[pid]; | 278 var process = processes[pid]; |
286 if (process.type == 'browser') { | 279 if (process.type == 'browser') { |
287 browserProcessCpu = process.cpu; | 280 browserProcessCpu = process.cpu; |
288 } else if (process.type == 'gpu') { | 281 } else if (process.type == 'gpu') { |
289 gpuProcessCpu = process.cpu; | 282 gpuProcessCpu = process.cpu; |
290 } else if ((process.type == 'plugin' || process.type == 'nacl') && | 283 } else if ( |
291 process.title.toLowerCase().indexOf('hangouts') > 0) { | 284 (process.type == 'plugin' || process.type == 'nacl') && |
| 285 process.title.toLowerCase().indexOf('hangouts') > 0) { |
292 pluginProcessCpu = process.cpu; | 286 pluginProcessCpu = process.cpu; |
293 } | 287 } |
294 } | 288 } |
295 | 289 |
296 port.postMessage({ | 290 port.postMessage({ |
297 'tabCpuUsage': tabProcess.cpu, | 291 'tabCpuUsage': tabProcess.cpu, |
298 'browserCpuUsage': browserProcessCpu, | 292 'browserCpuUsage': browserProcessCpu, |
299 'gpuCpuUsage': gpuProcessCpu, | 293 'gpuCpuUsage': gpuProcessCpu, |
300 'pluginCpuUsage': pluginProcessCpu | 294 'pluginCpuUsage': pluginProcessCpu |
301 }); | 295 }); |
(...skipping 15 matching lines...) Expand all Loading... |
317 onSinksChangedPort(port); | 311 onSinksChangedPort(port); |
318 } else if (port.name == 'chooseDesktopMedia') { | 312 } else if (port.name == 'chooseDesktopMedia') { |
319 onChooseDesktopMediaPort(port); | 313 onChooseDesktopMediaPort(port); |
320 } else if (port.name == 'processCpu') { | 314 } else if (port.name == 'processCpu') { |
321 onProcessCpu(port); | 315 onProcessCpu(port); |
322 } else { | 316 } else { |
323 // Unknown port type. | 317 // Unknown port type. |
324 port.disconnect(); | 318 port.disconnect(); |
325 } | 319 } |
326 }); | 320 }); |
OLD | NEW |