| 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 DCHECK = requireNative('logging').DCHECK; | 5 requireNative('runtime'); |
| 6 var GetAvailability = requireNative('v8_context').GetAvailability; | |
| 7 var GetGlobal = requireNative('sendRequest').GetGlobal; | |
| 8 | 6 |
| 9 // Utility for setting chrome.*.lastError. | 7 function set(message) { |
| 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 | |
| 24 var errorObject = { 'message': message }; | 8 var errorObject = { 'message': message }; |
| 25 if (GetAvailability('extension').is_available) | 9 if (chrome.extension) |
| 26 targetChrome.extension.lastError = errorObject; | 10 chrome.extension.lastError = errorObject; |
| 27 targetChrome.runtime.lastError = errorObject; | 11 chrome.runtime.lastError = errorObject; |
| 28 }; | 12 }; |
| 29 | 13 |
| 30 /** | 14 function clear() { |
| 31 * Clears the last error on |targetChrome|. | 15 if (chrome.extension) |
| 32 */ | 16 delete chrome.extension.lastError; |
| 33 function clear(targetChrome) { | 17 delete chrome.runtime.lastError; |
| 34 DCHECK(targetChrome != undefined); | |
| 35 if (GetAvailability('extension').is_available) | |
| 36 delete targetChrome.extension.lastError; | |
| 37 delete targetChrome.runtime.lastError; | |
| 38 }; | 18 }; |
| 39 | 19 |
| 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 | |
| 56 exports.clear = clear; | 20 exports.clear = clear; |
| 57 exports.set = set; | 21 exports.set = set; |
| 58 exports.run = run; | |
| OLD | NEW |