Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * @fileoverview Utility objects and functions for Google Now extension. | 8 * @fileoverview Utility objects and functions for Google Now extension. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 // TODO(vadimt): Figure out the server name. Use it in the manifest and for | 11 // TODO(vadimt): Figure out the server name. Use it in the manifest and for |
| 12 // NOTIFICATION_CARDS_URL. Meanwhile, to use the feature, you need to manually | 12 // NOTIFICATION_CARDS_URL. Meanwhile, to use the feature, you need to manually |
| 13 // set the server name via local storage. | 13 // set the server name via local storage. |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * Notification server URL. | 16 * Notification server URL. |
| 17 */ | 17 */ |
| 18 var NOTIFICATION_CARDS_URL = localStorage['server_url']; | 18 var NOTIFICATION_CARDS_URL = localStorage['server_url']; |
| 19 | 19 |
| 20 var DEBUG_MODE = localStorage['debug_mode']; | 20 var DEBUG_MODE = localStorage['debug_mode']; |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * Shows a message popup in debug mode. | |
| 24 * @param {string} message Diagnostic message. | |
| 25 */ | |
| 26 function debugAlert(message) { | |
| 27 if (DEBUG_MODE) | |
| 28 alert(message); | |
| 29 } | |
| 30 | |
| 31 /** | |
| 32 * Checks for internal errors. | 23 * Checks for internal errors. |
| 33 * @param {boolean} condition Condition that must be true. | 24 * @param {boolean} condition Condition that must be true. |
| 34 * @param {string} message Diagnostic message for the case when the condition is | 25 * @param {string} message Diagnostic message for the case when the condition is |
| 35 * false. | 26 * false. |
| 36 */ | 27 */ |
| 37 function verify(condition, message) { | 28 function verify(condition, message) { |
| 38 if (!condition) | 29 if (!condition) |
| 39 throw new Error('\nASSERT: ' + message); | 30 throw new Error('\nASSERT: ' + message); |
| 40 } | 31 } |
| 41 | 32 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 /** | 85 /** |
| 95 * True if currently executed code runs in an instrumented callback. | 86 * True if currently executed code runs in an instrumented callback. |
| 96 * @type {boolean} | 87 * @type {boolean} |
| 97 */ | 88 */ |
| 98 var isInInstrumentedCallback = false; | 89 var isInInstrumentedCallback = false; |
| 99 | 90 |
| 100 /** | 91 /** |
| 101 * Checks that we run in an instrumented callback. | 92 * Checks that we run in an instrumented callback. |
| 102 */ | 93 */ |
| 103 function checkInInstrumentedCallback() { | 94 function checkInInstrumentedCallback() { |
| 104 if (!isInInstrumentedCallback) { | 95 if (!isInInstrumentedCallback) |
| 105 // Cannot use verify() since no one will catch the exception. | 96 reportError(new Error('\nNot in instrumented callback')); |
| 106 // This check will detect bugs at the development stage, and is very | |
| 107 // unlikely to be seen by users. | |
| 108 var error = 'Not in instrumented callback: ' + new Error().stack; | |
| 109 console.error(error); | |
| 110 debugAlert(error); | |
| 111 } | |
| 112 } | 97 } |
| 113 | 98 |
| 114 /** | 99 /** |
| 115 * Starts the first queued task. | 100 * Starts the first queued task. |
| 116 */ | 101 */ |
| 117 function startFirst() { | 102 function startFirst() { |
| 118 verify(queue.length >= 1, 'startFirst: queue is empty'); | 103 verify(queue.length >= 1, 'startFirst: queue is empty'); |
| 119 verify(!isInTask, 'startFirst: already in task'); | 104 verify(!isInTask, 'startFirst: already in task'); |
| 120 isInTask = true; | 105 isInTask = true; |
| 121 | 106 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 | 171 |
| 187 if (queue.length >= 1) | 172 if (queue.length >= 1) |
| 188 startFirst(); | 173 startFirst(); |
| 189 } | 174 } |
| 190 | 175 |
| 191 // Limiting 1 error report per background page load. | 176 // Limiting 1 error report per background page load. |
| 192 var errorReported = false; | 177 var errorReported = false; |
| 193 | 178 |
| 194 /** | 179 /** |
| 195 * Sends an error report to the server. | 180 * Sends an error report to the server. |
| 196 * @param {Error} error Error to report. | 181 * @param {Error} error Error to send. |
| 197 */ | 182 */ |
| 198 function sendErrorReport(error) { | 183 function sendErrorReport(error) { |
| 199 var filteredStack = error.stack.replace(/.*\n/, '\n'); | 184 var filteredStack = error.stack.replace(/.*\n/, '\n'); |
| 200 var file; | 185 var file; |
| 201 var line; | 186 var line; |
| 202 var topFrameMatches = filteredStack.match(/\(.*\)/); | 187 var topFrameLineMatch = filteredStack.match(/\n at .*\n/); |
|
skare_
2013/08/07 18:40:38
seems like a good place for a test :D
| |
| 203 // topFrameMatches's example: | 188 var topFrame = topFrameLineMatch && topFrameLineMatch[0]; |
| 204 // (chrome-extension://pmofbkohncoogjjhahejjfbppikbjigm/utility.js:308:19) | 189 if (topFrame) { |
| 205 var crashLocation = topFrameMatches && topFrameMatches[0]; | 190 // Examples of a frame: |
| 206 if (crashLocation) { | 191 // 1. '\n at someFunction (chrome-extension:// |
| 207 var topFrameElements = | 192 // pmofbkohncoogjjhahejjfbppikbjigm/background.js:915:15)\n' |
| 208 crashLocation.substring(1, crashLocation.length - 1).split(':'); | 193 // 2. '\n at chrome-extension://pmofbkohncoogjjhahejjfbppikbjigm/ |
| 209 // topFrameElements for the above example will look like: | 194 // utility.js:269:18\n' |
| 210 // [0] chrome-extension | 195 // 3. '\n at Function.target.(anonymous function) (extensions:: |
| 211 // [1] //pmofbkohncoogjjhahejjfbppikbjigm/utility.js | 196 // SafeBuiltins:19:14)\n' |
| 212 // [2] 308 | 197 // 4. '\n at Event.dispatchToListener (event_bindings:382:22)\n' |
| 213 // [3] 19 | 198 var errorLocation; |
| 199 // Find the the parentheses at the end of the line, if any. | |
| 200 var parenthesesMatch = topFrame.match(/\(.*\)\n/); | |
| 201 if (parenthesesMatch && parenthesesMatch[0]) { | |
| 202 errorLocation = | |
| 203 parenthesesMatch[0].substring(1, parenthesesMatch[0].length - 2); | |
| 204 } else { | |
| 205 errorLocation = topFrame; | |
| 206 } | |
| 207 | |
| 208 var topFrameElements = errorLocation.split(':'); | |
| 209 // topFrameElements is an array that ends like: | |
| 210 // [N-3] //pmofbkohncoogjjhahejjfbppikbjigm/utility.js | |
| 211 // [N-2] 308 | |
| 212 // [N-1] 19 | |
| 214 if (topFrameElements.length >= 3) { | 213 if (topFrameElements.length >= 3) { |
| 215 file = topFrameElements[0] + ':' + topFrameElements[1]; | 214 file = topFrameElements[topFrameElements.length - 3]; |
| 216 line = topFrameElements[2]; | 215 line = topFrameElements[topFrameElements.length - 2]; |
| 217 } | 216 } |
| 218 } | 217 } |
| 218 | |
| 219 var requestParameters = | 219 var requestParameters = |
| 220 'error=' + encodeURIComponent(error.name) + | 220 'error=' + encodeURIComponent(error.name) + |
| 221 '&script=' + encodeURIComponent(file) + | 221 '&script=' + encodeURIComponent(file) + |
| 222 '&line=' + encodeURIComponent(line) + | 222 '&line=' + encodeURIComponent(line) + |
| 223 '&trace=' + encodeURIComponent(filteredStack); | 223 '&trace=' + encodeURIComponent(filteredStack); |
| 224 var request = buildServerRequest('jserror', | 224 var request = buildServerRequest('jserror', |
| 225 'application/x-www-form-urlencoded'); | 225 'application/x-www-form-urlencoded'); |
| 226 request.onloadend = function(event) { | 226 request.onloadend = function(event) { |
| 227 console.log('sendErrorReport status: ' + request.status); | 227 console.log('sendErrorReport status: ' + request.status); |
| 228 }; | 228 }; |
| 229 request.send(requestParameters); | 229 request.send(requestParameters); |
| 230 } | 230 } |
| 231 | 231 |
| 232 /** | 232 /** |
| 233 * Reports an error to the server and the user, as appropriate. | |
| 234 * @param {Error} error Error to report. | |
| 235 */ | |
| 236 function reportError(error) { | |
| 237 var message = 'Critical error:\n' + error.stack; | |
| 238 console.error(message); | |
| 239 if (!errorReported) { | |
| 240 errorReported = true; | |
| 241 chrome.metricsPrivate.getIsCrashReportingEnabled(function(isEnabled) { | |
| 242 if (isEnabled) | |
| 243 sendErrorReport(error); | |
| 244 if (DEBUG_MODE) | |
| 245 alert(message); | |
| 246 }); | |
| 247 } | |
| 248 } | |
| 249 | |
| 250 /** | |
| 233 * Unique ID of the next callback. | 251 * Unique ID of the next callback. |
| 234 * @type {number} | 252 * @type {number} |
| 235 */ | 253 */ |
| 236 var nextCallbackId = 0; | 254 var nextCallbackId = 0; |
| 237 | 255 |
| 238 /** | 256 /** |
| 239 * Adds error processing to an API callback. | 257 * Adds error processing to an API callback. |
| 240 * @param {Function} callback Callback to instrument. | 258 * @param {Function} callback Callback to instrument. |
| 241 * @param {boolean=} opt_isEventListener True if the callback is an event | 259 * @param {boolean=} opt_isEventListener True if the callback is an event |
| 242 * listener. | 260 * listener. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 272 'Instrumented callback is not instrumented upon exit'); | 290 'Instrumented callback is not instrumented upon exit'); |
| 273 isInInstrumentedCallback = false; | 291 isInInstrumentedCallback = false; |
| 274 | 292 |
| 275 if (isTaskCallback) { | 293 if (isTaskCallback) { |
| 276 verify(isInTask, 'wrapCallback: not in task at exit'); | 294 verify(isInTask, 'wrapCallback: not in task at exit'); |
| 277 isInTask = false; | 295 isInTask = false; |
| 278 if (--taskPendingCallbackCount == 0) | 296 if (--taskPendingCallbackCount == 0) |
| 279 finish(); | 297 finish(); |
| 280 } | 298 } |
| 281 } catch (error) { | 299 } catch (error) { |
| 282 var message = 'Uncaught exception:\n' + error.stack; | 300 reportError(error); |
| 283 console.error(message); | |
| 284 if (!errorReported) { | |
| 285 errorReported = true; | |
| 286 chrome.metricsPrivate.getIsCrashReportingEnabled(function(isEnabled) { | |
| 287 if (isEnabled) | |
| 288 sendErrorReport(error); | |
| 289 }); | |
| 290 debugAlert(message); | |
| 291 } | |
| 292 } | 301 } |
| 293 }; | 302 }; |
| 294 } | 303 } |
| 295 | 304 |
| 296 /** | 305 /** |
| 297 * Instruments an API function to add error processing to its user | 306 * Instruments an API function to add error processing to its user |
| 298 * code-provided callback. | 307 * code-provided callback. |
| 299 * @param {Object} namespace Namespace of the API function. | 308 * @param {Object} namespace Namespace of the API function. |
| 300 * @param {string} functionName Name of the API function. | 309 * @param {string} functionName Name of the API function. |
| 301 * @param {number} callbackParameter Index of the callback parameter to this | 310 * @param {number} callbackParameter Index of the callback parameter to this |
| 302 * API function. | 311 * API function. |
| 303 */ | 312 */ |
| 304 function instrumentApiFunction(namespace, functionName, callbackParameter) { | 313 function instrumentApiFunction(namespace, functionName, callbackParameter) { |
| 305 var originalFunction = namespace[functionName]; | 314 var originalFunction = namespace[functionName]; |
| 306 | 315 |
| 307 if (!originalFunction) | 316 if (!originalFunction) |
| 308 debugAlert('Cannot instrument ' + functionName); | 317 reportError(new Error('\nCannot instrument ' + functionName)); |
| 309 | 318 |
| 310 namespace[functionName] = function() { | 319 namespace[functionName] = function() { |
| 311 // This is the wrapper for the API function. Pass the wrapped callback to | 320 // This is the wrapper for the API function. Pass the wrapped callback to |
| 312 // the original function. | 321 // the original function. |
| 313 var callback = arguments[callbackParameter]; | 322 var callback = arguments[callbackParameter]; |
| 314 if (typeof callback != 'function') { | 323 if (typeof callback != 'function') { |
| 315 debugAlert('Argument ' + callbackParameter + ' of ' + functionName + | 324 reportError(new Error('\nArgument ' + callbackParameter + ' of ' + |
| 316 ' is not a function'); | 325 functionName + ' is not a function')); |
| 317 } | 326 } |
| 318 arguments[callbackParameter] = wrapCallback( | 327 arguments[callbackParameter] = wrapCallback( |
| 319 callback, functionName == 'addListener'); | 328 callback, functionName == 'addListener'); |
| 320 return originalFunction.apply(namespace, arguments); | 329 return originalFunction.apply(namespace, arguments); |
| 321 }; | 330 }; |
| 322 } | 331 } |
| 323 | 332 |
| 324 instrumentApiFunction(chrome.alarms, 'get', 1); | 333 instrumentApiFunction(chrome.alarms, 'get', 1); |
| 325 instrumentApiFunction(chrome.alarms.onAlarm, 'addListener', 0); | 334 instrumentApiFunction(chrome.alarms.onAlarm, 'addListener', 0); |
| 326 instrumentApiFunction(chrome.identity, 'getAuthToken', 1); | 335 instrumentApiFunction(chrome.identity, 'getAuthToken', 1); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 528 // Poll for the sign in state every hour. | 537 // Poll for the sign in state every hour. |
| 529 // One hour is just an arbitrary amount of time chosen. | 538 // One hour is just an arbitrary amount of time chosen. |
| 530 chrome.alarms.create(alarmName, {periodInMinutes: 60}); | 539 chrome.alarms.create(alarmName, {periodInMinutes: 60}); |
| 531 | 540 |
| 532 return { | 541 return { |
| 533 addListener: addListener, | 542 addListener: addListener, |
| 534 isSignedIn: isSignedIn, | 543 isSignedIn: isSignedIn, |
| 535 removeToken: removeToken | 544 removeToken: removeToken |
| 536 }; | 545 }; |
| 537 } | 546 } |
| OLD | NEW |