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; | 6 var GetAvailability = requireNative('v8_context').GetAvailability; |
| 7 var GetGlobal = requireNative('sendRequest').GetGlobal; |
7 | 8 |
8 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); |
9 var errorObject = { 'message': message }; | 23 var errorObject = { 'message': message }; |
10 if (GetAvailability('extension').is_available) | 24 if (GetAvailability('extension').is_available) |
11 chrome.extension.lastError = errorObject; | 25 targetChrome.extension.lastError = errorObject; |
12 chrome.runtime.lastError = errorObject; | 26 targetChrome.runtime.lastError = errorObject; |
13 }; | 27 }; |
14 | 28 |
15 function clear() { | 29 /** |
| 30 * Clears the last error on |targetChrome|. |
| 31 */ |
| 32 function clear(targetChrome) { |
| 33 DCHECK(targetChrome != undefined); |
16 if (GetAvailability('extension').is_available) | 34 if (GetAvailability('extension').is_available) |
17 delete chrome.extension.lastError; | 35 delete targetChrome.extension.lastError; |
18 delete chrome.runtime.lastError; | 36 delete targetChrome.runtime.lastError; |
19 }; | 37 }; |
20 | 38 |
| 39 /** |
| 40 * Runs |callback| with last error set to |message|. |
| 41 * |
| 42 * The target chrome object is the global object's of the callback, so this |
| 43 * method won't work if the real callback has been wrapped (etc). |
| 44 */ |
| 45 function run(message, callback) { |
| 46 var targetChrome = GetGlobal(callback).chrome; |
| 47 set(message, targetChrome); |
| 48 try { |
| 49 callback(); |
| 50 } finally { |
| 51 clear(targetChrome); |
| 52 } |
| 53 } |
| 54 |
21 exports.clear = clear; | 55 exports.clear = clear; |
22 exports.set = set; | 56 exports.set = set; |
| 57 exports.run = run; |
OLD | NEW |