Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 var GetAvailability = requireNative('v8_context').GetAvailability; | 5 var GetAvailability = requireNative('v8_context').GetAvailability; |
| 6 var GetGlobal = requireNative('sendRequest').GetGlobal; | 6 var GetGlobal = requireNative('sendRequest').GetGlobal; |
| 7 | 7 |
| 8 // Utility for setting chrome.*.lastError. | 8 // Utility for setting chrome.*.lastError. |
| 9 // | 9 // |
| 10 // A utility here is useful for two reasons: | 10 // A utility here is useful for two reasons: |
| 11 // 1. For backwards compatibility we need to set chrome.extension.lastError, | 11 // 1. For backwards compatibility we need to set chrome.extension.lastError, |
| 12 // but not all contexts actually have access to the extension namespace. | 12 // but not all contexts actually have access to the extension namespace. |
| 13 // 2. When calling across contexts, the global object that gets lastError set | 13 // 2. When calling across contexts, the global object that gets lastError set |
| 14 // needs to be that of the caller. We force callers to explicitly specify | 14 // needs to be that of the caller. We force callers to explicitly specify |
| 15 // the chrome object to try to prevent bugs here. | 15 // the chrome object to try to prevent bugs here. |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Sets the last error for |name| on |targetChrome| to |message| with an | 18 * Sets the last error for |name| on |targetChrome| to |message| with an |
| 19 * optional |stack|. | 19 * optional |stack|. |
| 20 */ | 20 */ |
| 21 function set(name, message, stack, targetChrome) { | 21 function set(name, message, stack, targetChrome) { |
| 22 var errorMessage = name + ': ' + message; | 22 if (!targetChrome) { |
| 23 if (stack != null && stack != '') | 23 var errorMessage = name + ': ' + message; |
| 24 errorMessage += '\n' + stack; | 24 if (stack != null && stack != '') |
| 25 | 25 errorMessage += '\n' + stack; |
| 26 if (!targetChrome) | |
| 27 throw new Error('No chrome object to set error: ' + errorMessage); | 26 throw new Error('No chrome object to set error: ' + errorMessage); |
| 27 } | |
| 28 clear(targetChrome); // in case somebody has set a sneaky getter/setter | 28 clear(targetChrome); // in case somebody has set a sneaky getter/setter |
| 29 | 29 |
| 30 var errorObject = { message: message }; | 30 var errorObject = { message: message }; |
| 31 if (GetAvailability('extension.lastError').is_available) | 31 if (GetAvailability('extension.lastError').is_available) |
| 32 targetChrome.extension.lastError = errorObject; | 32 targetChrome.extension.lastError = errorObject; |
| 33 | 33 |
| 34 assertRuntimeIsAvailable(); | 34 assertRuntimeIsAvailable(); |
| 35 | 35 |
| 36 // We check to see if developers access runtime.lastError in order to decide | 36 // We check to see if developers access runtime.lastError in order to decide |
| 37 // whether or not to log it in the (error) console. | 37 // whether or not to log it in the (error) console. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 * | 106 * |
| 107 * The target chrome object is the global object's of the callback, so this | 107 * The target chrome object is the global object's of the callback, so this |
| 108 * method won't work if the real callback has been wrapped (etc). | 108 * method won't work if the real callback has been wrapped (etc). |
| 109 */ | 109 */ |
| 110 function run(name, message, stack, callback, args) { | 110 function run(name, message, stack, callback, args) { |
| 111 var targetChrome = GetGlobal(callback).chrome; | 111 var targetChrome = GetGlobal(callback).chrome; |
| 112 set(name, message, stack, targetChrome); | 112 set(name, message, stack, targetChrome); |
| 113 try { | 113 try { |
| 114 $Function.apply(callback, undefined, args); | 114 $Function.apply(callback, undefined, args); |
| 115 } finally { | 115 } finally { |
| 116 reportIfUnchecked(name, targetChrome, stack); | |
| 116 clear(targetChrome); | 117 clear(targetChrome); |
| 117 } | 118 } |
| 118 } | 119 } |
| 119 | 120 |
| 121 /** | |
| 122 * Checks whether chrome.runtime.lastError has been accessed if set. | |
| 123 * If it was set but not accessed, the error is reported to the console. | |
| 124 * | |
| 125 * @param {string=} name - name of API. | |
| 126 * @param {Object} targetChrome - the Chrome object to check. | |
| 127 * @param {string=} stack - Stack trace of the call up to the error. | |
| 128 */ | |
| 129 function reportIfUnchecked(name, targetChrome, stack) { | |
| 130 var error = hasAccessed(targetChrome) && targetChrome.runtime.lastError; | |
|
not at google - send to devlin
2015/01/22 17:59:59
targetChrome.runtime.lastError --> hasError(target
robwu
2015/01/22 18:07:22
|error| is used below as |error.message| to get th
not at google - send to devlin
2015/01/22 18:12:53
Alright. Well use that as a reason to not overload
robwu
2015/01/22 18:19:33
Done.
| |
| 131 if (!error) | |
| 132 return; | |
| 133 console.error("Unchecked runtime.lastError while running " + | |
| 134 (name || "unknown") + ": " + error.message + (stack ? "\n" + stack : "")); | |
| 135 } | |
| 136 | |
| 120 exports.clear = clear; | 137 exports.clear = clear; |
| 121 exports.hasAccessed = hasAccessed; | 138 exports.hasAccessed = hasAccessed; |
| 122 exports.hasError = hasError; | 139 exports.hasError = hasError; |
| 123 exports.set = set; | 140 exports.set = set; |
| 124 exports.run = run; | 141 exports.run = run; |
| 142 exports.reportIfUnchecked = reportIfUnchecked; | |
| OLD | NEW |