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 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 var errorMessage = name + ': ' + message; |
23 if (stack != null && stack != '') | 23 if (stack != null && stack != '') |
24 errorMessage += '\n' + stack; | 24 errorMessage += '\n' + stack; |
25 | 25 |
26 if (!targetChrome) | 26 if (!targetChrome) |
27 throw new Error('No chrome object to set error: ' + errorMessage); | 27 throw new Error('No chrome object to set error: ' + errorMessage); |
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 console.error(errorMessage); | |
31 | |
32 var errorObject = { message: message }; | 30 var errorObject = { message: message }; |
33 if (GetAvailability('extension.lastError').is_available) | 31 if (GetAvailability('extension.lastError').is_available) |
34 targetChrome.extension.lastError = errorObject; | 32 targetChrome.extension.lastError = errorObject; |
35 | 33 |
36 assertRuntimeIsAvailable(); | 34 assertRuntimeIsAvailable(); |
37 targetChrome.runtime.lastError = errorObject; | 35 targetChrome.runtime.lastError = errorObject; |
38 }; | 36 }; |
39 | 37 |
40 /** | 38 /** |
41 * Clears the last error on |targetChrome|. | 39 * Clears the last error on |targetChrome|. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 try { | 73 try { |
76 $Function.apply(callback, undefined, args); | 74 $Function.apply(callback, undefined, args); |
77 } finally { | 75 } finally { |
78 clear(targetChrome); | 76 clear(targetChrome); |
79 } | 77 } |
80 } | 78 } |
81 | 79 |
82 exports.clear = clear; | 80 exports.clear = clear; |
83 exports.set = set; | 81 exports.set = set; |
84 exports.run = run; | 82 exports.run = run; |
OLD | NEW |