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: |
(...skipping 10 matching lines...) Expand all Loading... |
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 var errorObject = { message: message }; | 30 var errorObject = { message: message }; |
31 if (GetAvailability('extension.lastError').is_available) | 31 if (targetChrome && targetChrome.extension) |
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. |
38 privates(targetChrome.runtime).accessedLastError = false; | 38 privates(targetChrome.runtime).accessedLastError = false; |
39 $Object.defineProperty(targetChrome.runtime, 'lastError', { | 39 $Object.defineProperty(targetChrome.runtime, 'lastError', { |
40 configurable: true, | 40 configurable: true, |
41 get: function() { | 41 get: function() { |
(...skipping 15 matching lines...) Expand all Loading... |
57 return privates(targetChrome.runtime).accessedLastError === true; | 57 return privates(targetChrome.runtime).accessedLastError === true; |
58 } | 58 } |
59 | 59 |
60 /** | 60 /** |
61 * Clears the last error on |targetChrome|. | 61 * Clears the last error on |targetChrome|. |
62 */ | 62 */ |
63 function clear(targetChrome) { | 63 function clear(targetChrome) { |
64 if (!targetChrome) | 64 if (!targetChrome) |
65 throw new Error('No target chrome to clear error'); | 65 throw new Error('No target chrome to clear error'); |
66 | 66 |
67 if (GetAvailability('extension.lastError').is_available) | 67 if (targetChrome && targetChrome.extension) |
68 delete targetChrome.extension.lastError; | 68 delete targetChrome.extension.lastError; |
69 | 69 |
70 assertRuntimeIsAvailable(); | 70 assertRuntimeIsAvailable(); |
71 delete targetChrome.runtime.lastError; | 71 delete targetChrome.runtime.lastError; |
72 delete privates(targetChrome.runtime).accessedLastError; | 72 delete privates(targetChrome.runtime).accessedLastError; |
73 }; | 73 }; |
74 | 74 |
75 function assertRuntimeIsAvailable() { | 75 function assertRuntimeIsAvailable() { |
76 // chrome.runtime should always be available, but maybe it's disappeared for | 76 // chrome.runtime should always be available, but maybe it's disappeared for |
77 // some reason? Add debugging for http://crbug.com/258526. | 77 // some reason? Add debugging for http://crbug.com/258526. |
78 var runtimeAvailability = GetAvailability('runtime.lastError'); | 78 var runtimeAvailability = GetAvailability('runtime.lastError'); |
(...skipping 18 matching lines...) Expand all Loading... |
97 $Function.apply(callback, undefined, args); | 97 $Function.apply(callback, undefined, args); |
98 } finally { | 98 } finally { |
99 clear(targetChrome); | 99 clear(targetChrome); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 exports.clear = clear; | 103 exports.clear = clear; |
104 exports.hasAccessed = hasAccessed; | 104 exports.hasAccessed = hasAccessed; |
105 exports.set = set; | 105 exports.set = set; |
106 exports.run = run; | 106 exports.run = run; |
OLD | NEW |