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 requireNative('runtime'); | 5 var DCHECK = requireNative('logging').DCHECK; |
| 6 var GetAvailability = requireNative('v8_context').GetAvailability; |
| 7 var GetGlobal = requireNative('sendRequest').GetGlobal; |
6 | 8 |
7 function set(message) { | 9 // Utility for setting chrome.*.lastError. |
| 10 // |
| 11 // A utility here is useful for two reasons: |
| 12 // 1. For backwards compatibility we need to set chrome.extension.lastError, |
| 13 // but not all contexts actually have access to the extension namespace. |
| 14 // 2. When calling across contexts, the global object that gets lastError set |
| 15 // needs to be that of the caller. We force callers to explicitly specify |
| 16 // the chrome object to try to prevent bugs here. |
| 17 |
| 18 /** |
| 19 * Sets the last error on |targetChrome| to |message|. |
| 20 */ |
| 21 function set(message, targetChrome) { |
| 22 DCHECK(targetChrome != undefined); |
| 23 clear(targetChrome); // in case somebody has set a sneaky getter/setter |
8 var errorObject = { 'message': message }; | 24 var errorObject = { 'message': message }; |
9 if (chrome.extension) | 25 if (GetAvailability('extension').is_available) |
10 chrome.extension.lastError = errorObject; | 26 targetChrome.extension.lastError = errorObject; |
11 chrome.runtime.lastError = errorObject; | 27 targetChrome.runtime.lastError = errorObject; |
12 }; | 28 }; |
13 | 29 |
14 function clear() { | 30 /** |
15 if (chrome.extension) | 31 * Clears the last error on |targetChrome|. |
16 delete chrome.extension.lastError; | 32 */ |
17 delete chrome.runtime.lastError; | 33 function clear(targetChrome) { |
| 34 DCHECK(targetChrome != undefined); |
| 35 if (GetAvailability('extension').is_available) |
| 36 delete targetChrome.extension.lastError; |
| 37 delete targetChrome.runtime.lastError; |
18 }; | 38 }; |
19 | 39 |
| 40 /** |
| 41 * Runs |callback(args)| with last error set to |message|. |
| 42 * |
| 43 * The target chrome object is the global object's of the callback, so this |
| 44 * method won't work if the real callback has been wrapped (etc). |
| 45 */ |
| 46 function run(message, callback, args) { |
| 47 var targetChrome = GetGlobal(callback).chrome; |
| 48 set(message, targetChrome); |
| 49 try { |
| 50 callback.apply(undefined, args); |
| 51 } finally { |
| 52 clear(targetChrome); |
| 53 } |
| 54 } |
| 55 |
20 exports.clear = clear; | 56 exports.clear = clear; |
21 exports.set = set; | 57 exports.set = set; |
| 58 exports.run = run; |
OLD | NEW |