| 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, |
| 124 // throw them away. | 121 // throw them away. |
| 125 if (chrome.extension.lastError !== undefined) { | 122 if (chrome.extension.lastError !== undefined) { |
| 126 appendLastErrorMessage(errors); | 123 appendLastErrorMessage(errors); |
| 127 errorMessage = errors.join('; '); | 124 errorMessage = errors.join('; '); |
| 128 } | 125 } |
| 129 doSendResponse(uploadValue, errorMessage); | 126 doSendResponse(uploadValue, errorMessage); |
| 127 }); |
| 128 }); |
| 130 }); | 129 }); |
| 131 }); | |
| 132 }); | |
| 133 }); | 130 }); |
| 134 return true; | 131 return true; |
| 135 } else if (method == 'logging.store') { | 132 } else if (method == 'logging.store') { |
| 136 var logId = message['logId']; | 133 var logId = message['logId']; |
| 137 chrome.webrtcLoggingPrivate.store( | 134 chrome.webrtcLoggingPrivate.store( |
| 138 requestInfo, origin, logId, doSendResponse); | 135 requestInfo, origin, logId, doSendResponse); |
| 139 return true; | 136 return true; |
| 140 } else if (method == 'logging.discard') { | 137 } else if (method == 'logging.discard') { |
| 141 chrome.webrtcLoggingPrivate.discard( | 138 chrome.webrtcLoggingPrivate.discard(requestInfo, origin, doSendResponse); |
| 142 requestInfo, origin, doSendResponse); | 139 return true; |
| 143 return true; | 140 } else if (method == 'getSinks') { |
| 144 } else if (method == 'getSinks') { | 141 chrome.webrtcAudioPrivate.getSinks(doSendResponse); |
| 145 chrome.webrtcAudioPrivate.getSinks(doSendResponse); | 142 return true; |
| 146 return true; | 143 } else if (method == 'getActiveSink') { |
| 147 } else if (method == 'getActiveSink') { | 144 chrome.webrtcAudioPrivate.getActiveSink(requestInfo, doSendResponse); |
| 148 chrome.webrtcAudioPrivate.getActiveSink( | 145 return true; |
| 149 requestInfo, doSendResponse); | 146 } else if (method == 'setActiveSink') { |
| 150 return true; | 147 var sinkId = message['sinkId']; |
| 151 } else if (method == 'setActiveSink') { | 148 chrome.webrtcAudioPrivate.setActiveSink( |
| 152 var sinkId = message['sinkId']; | 149 requestInfo, sinkId, doSendResponse); |
| 153 chrome.webrtcAudioPrivate.setActiveSink( | 150 return true; |
| 154 requestInfo, sinkId, doSendResponse); | 151 } else if (method == 'getAssociatedSink') { |
| 155 return true; | 152 var sourceId = message['sourceId']; |
| 156 } else if (method == 'getAssociatedSink') { | 153 chrome.webrtcAudioPrivate.getAssociatedSink( |
| 157 var sourceId = message['sourceId']; | 154 origin, sourceId, doSendResponse); |
| 158 chrome.webrtcAudioPrivate.getAssociatedSink( | 155 return true; |
| 159 origin, sourceId, doSendResponse); | 156 } else if (method == 'isExtensionEnabled') { |
| 160 return true; | 157 // This method is necessary because there may be more than one |
| 161 } else if (method == 'isExtensionEnabled') { | 158 // version of this extension, under different extension IDs. By |
| 162 // This method is necessary because there may be more than one | 159 // first calling this method on the extension ID, the client can |
| 163 // version of this extension, under different extension IDs. By | 160 // check if it's loaded; if it's not, the extension system will |
| 164 // first calling this method on the extension ID, the client can | 161 // call the callback with no arguments and set |
| 165 // check if it's loaded; if it's not, the extension system will | 162 // chrome.runtime.lastError. |
| 166 // call the callback with no arguments and set | 163 doSendResponse(); |
| 167 // chrome.runtime.lastError. | 164 return false; |
| 168 doSendResponse(); | 165 } else if (method == 'getNaclArchitecture') { |
| 169 return false; | 166 chrome.runtime.getPlatformInfo(function(obj) { |
| 170 } else if (method == 'getNaclArchitecture') { | 167 doSendResponse(obj.nacl_arch); |
| 171 chrome.runtime.getPlatformInfo(function(obj) { | 168 }); |
| 172 doSendResponse(obj.nacl_arch); | 169 return true; |
| 173 }); | 170 } else if (method == 'logging.startRtpDump') { |
| 174 return true; | 171 var incoming = message['incoming'] || false; |
| 175 } else if (method == 'logging.startRtpDump') { | 172 var outgoing = message['outgoing'] || false; |
| 176 var incoming = message['incoming'] || false; | 173 chrome.webrtcLoggingPrivate.startRtpDump( |
| 177 var outgoing = message['outgoing'] || false; | 174 requestInfo, origin, incoming, outgoing, doSendResponse); |
| 178 chrome.webrtcLoggingPrivate.startRtpDump( | 175 return true; |
| 179 requestInfo, origin, incoming, outgoing, doSendResponse); | 176 } else if (method == 'logging.stopRtpDump') { |
| 180 return true; | 177 var incoming = message['incoming'] || false; |
| 181 } else if (method == 'logging.stopRtpDump') { | 178 var outgoing = message['outgoing'] || false; |
| 182 var incoming = message['incoming'] || false; | 179 chrome.webrtcLoggingPrivate.stopRtpDump( |
| 183 var outgoing = message['outgoing'] || false; | 180 requestInfo, origin, incoming, outgoing, doSendResponse); |
| 184 chrome.webrtcLoggingPrivate.stopRtpDump( | 181 return true; |
| 185 requestInfo, origin, incoming, outgoing, doSendResponse); | 182 } else if (method == 'logging.startAudioDebugRecordings') { |
| 186 return true; | 183 var seconds = message['seconds'] || 0; |
| 187 } else if (method == 'logging.startAudioDebugRecordings') { | 184 chrome.webrtcLoggingPrivate.startAudioDebugRecordings( |
| 188 var seconds = message['seconds'] || 0; | 185 requestInfo, origin, seconds, doSendResponse); |
| 189 chrome.webrtcLoggingPrivate.startAudioDebugRecordings( | 186 return true; |
| 190 requestInfo, origin, seconds, doSendResponse); | 187 } else if (method == 'logging.stopAudioDebugRecordings') { |
| 191 return true; | 188 chrome.webrtcLoggingPrivate.stopAudioDebugRecordings( |
| 192 } else if (method == 'logging.stopAudioDebugRecordings') { | 189 requestInfo, origin, doSendResponse); |
| 193 chrome.webrtcLoggingPrivate.stopAudioDebugRecordings( | 190 return true; |
| 194 requestInfo, origin, doSendResponse); | 191 } else if (method == 'logging.startWebRtcEventLogging') { |
| 195 return true; | 192 var seconds = message['seconds'] || 0; |
| 196 } else if (method == 'logging.startWebRtcEventLogging') { | 193 chrome.webrtcLoggingPrivate.startWebRtcEventLogging( |
| 197 var seconds = message['seconds'] || 0; | 194 requestInfo, origin, seconds, doSendResponse); |
| 198 chrome.webrtcLoggingPrivate.startWebRtcEventLogging( | 195 return true; |
| 199 requestInfo, origin, seconds, doSendResponse); | 196 } else if (method == 'logging.stopWebRtcEventLogging') { |
| 200 return true; | 197 chrome.webrtcLoggingPrivate.stopWebRtcEventLogging( |
| 201 } else if (method == 'logging.stopWebRtcEventLogging') { | 198 requestInfo, origin, doSendResponse); |
| 202 chrome.webrtcLoggingPrivate.stopWebRtcEventLogging( | 199 return true; |
| 203 requestInfo, origin, doSendResponse); | 200 } |
| 204 return true; | 201 |
| 205 } | 202 throw new Error('Unknown method: ' + method); |
| 206 | 203 } catch (e) { |
| 207 throw new Error('Unknown method: ' + method); | 204 doSendResponse(null, e.name + ': ' + e.message); |
| 208 } catch (e) { | 205 } |
| 209 doSendResponse(null, e.name + ': ' + e.message); | 206 }); |
| 210 } | |
| 211 } | |
| 212 ); | |
| 213 | 207 |
| 214 // If Hangouts connects with a port named 'onSinksChangedListener', we | 208 // If Hangouts connects with a port named 'onSinksChangedListener', we |
| 215 // will register a listener and send it a message {'eventName': | 209 // will register a listener and send it a message {'eventName': |
| 216 // 'onSinksChanged'} whenever the event fires. | 210 // 'onSinksChanged'} whenever the event fires. |
| 217 function onSinksChangedPort(port) { | 211 function onSinksChangedPort(port) { |
| 218 function clientListener() { | 212 function clientListener() { |
| 219 port.postMessage({'eventName': 'onSinksChanged'}); | 213 port.postMessage({'eventName': 'onSinksChanged'}); |
| 220 } | 214 } |
| 221 chrome.webrtcAudioPrivate.onSinksChanged.addListener(clientListener); | 215 chrome.webrtcAudioPrivate.onSinksChanged.addListener(clientListener); |
| 222 | 216 |
| 223 port.onDisconnect.addListener(function() { | 217 port.onDisconnect.addListener(function() { |
| 224 chrome.webrtcAudioPrivate.onSinksChanged.removeListener( | 218 chrome.webrtcAudioPrivate.onSinksChanged.removeListener(clientListener); |
| 225 clientListener); | |
| 226 }); | 219 }); |
| 227 } | 220 } |
| 228 | 221 |
| 229 // This is a one-time-use port for calling chooseDesktopMedia. The page | 222 // This is a one-time-use port for calling chooseDesktopMedia. The page |
| 230 // sends one message, identifying the requested source types, and the | 223 // sends one message, identifying the requested source types, and the |
| 231 // extension sends a single reply, with the user's selected streamId. A port | 224 // extension sends a single reply, with the user's selected streamId. A port |
| 232 // is used so that if the page is closed before that message is sent, the | 225 // is used so that if the page is closed before that message is sent, the |
| 233 // window picker dialog will be closed. | 226 // window picker dialog will be closed. |
| 234 function onChooseDesktopMediaPort(port) { | 227 function onChooseDesktopMediaPort(port) { |
| 235 function sendResponse(streamId) { | 228 function sendResponse(streamId) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 if (!tabProcess) { | 276 if (!tabProcess) { |
| 284 return; | 277 return; |
| 285 } | 278 } |
| 286 var pluginProcessCpu = 0, browserProcessCpu = 0, gpuProcessCpu = 0; | 279 var pluginProcessCpu = 0, browserProcessCpu = 0, gpuProcessCpu = 0; |
| 287 for (var pid in processes) { | 280 for (var pid in processes) { |
| 288 var process = processes[pid]; | 281 var process = processes[pid]; |
| 289 if (process.type == 'browser') { | 282 if (process.type == 'browser') { |
| 290 browserProcessCpu = process.cpu; | 283 browserProcessCpu = process.cpu; |
| 291 } else if (process.type == 'gpu') { | 284 } else if (process.type == 'gpu') { |
| 292 gpuProcessCpu = process.cpu; | 285 gpuProcessCpu = process.cpu; |
| 293 } else if ((process.type == 'plugin' || process.type == 'nacl') && | 286 } else if ( |
| 294 process.title.toLowerCase().indexOf('hangouts') > 0) { | 287 (process.type == 'plugin' || process.type == 'nacl') && |
| 288 process.title.toLowerCase().indexOf('hangouts') > 0) { |
| 295 pluginProcessCpu = process.cpu; | 289 pluginProcessCpu = process.cpu; |
| 296 } | 290 } |
| 297 } | 291 } |
| 298 | 292 |
| 299 port.postMessage({ | 293 port.postMessage({ |
| 300 'tabCpuUsage': tabProcess.cpu, | 294 'tabCpuUsage': tabProcess.cpu, |
| 301 'browserCpuUsage': browserProcessCpu, | 295 'browserCpuUsage': browserProcessCpu, |
| 302 'gpuCpuUsage': gpuProcessCpu, | 296 'gpuCpuUsage': gpuProcessCpu, |
| 303 'pluginCpuUsage': pluginProcessCpu | 297 'pluginCpuUsage': pluginProcessCpu |
| 304 }); | 298 }); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 320 onSinksChangedPort(port); | 314 onSinksChangedPort(port); |
| 321 } else if (port.name == 'chooseDesktopMedia') { | 315 } else if (port.name == 'chooseDesktopMedia') { |
| 322 onChooseDesktopMediaPort(port); | 316 onChooseDesktopMediaPort(port); |
| 323 } else if (port.name == 'processCpu') { | 317 } else if (port.name == 'processCpu') { |
| 324 onProcessCpu(port); | 318 onProcessCpu(port); |
| 325 } else { | 319 } else { |
| 326 // Unknown port type. | 320 // Unknown port type. |
| 327 port.disconnect(); | 321 port.disconnect(); |
| 328 } | 322 } |
| 329 }); | 323 }); |
| OLD | NEW |