1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var GetAvailability = requireNative('v8_context').GetAvailability;
var GetGlobal = requireNative('sendRequest').GetGlobal;
// Utility for setting chrome.*.lastError.
//
// A utility here is useful for two reasons:
// 1. For backwards compatibility we need to set chrome.extension.lastError,
// but not all contexts actually have access to the extension namespace.
// 2. When calling across contexts, the global object that gets lastError set
// needs to be that of the caller. We force callers to explicitly specify
// the chrome object to try to prevent bugs here.
/**
* Sets the last error for |name| on |targetChrome| to |message| with an
* optional |stack|.
*/
function set(name, message, stack, targetChrome) {
var errorMessage = name + ': ' + message;
if (stack != null && stack != '')
errorMessage += '\n' + stack;
if (!targetChrome)
throw new Error('No chrome object to set error: ' + errorMessage);
clear(targetChrome); // in case somebody has set a sneaky getter/setter
var errorObject = { message: message };
if (GetAvailability('extension.lastError').is_available)
targetChrome.extension.lastError = errorObject;
assertRuntimeIsAvailable();
targetChrome.runtime.lastError = errorObject;
};
/**
* Clears the last error on |targetChrome|.
*/
function clear(targetChrome) {
if (!targetChrome)
throw new Error('No target chrome to clear error');
if (GetAvailability('extension.lastError').is_available)
delete targetChrome.extension.lastError;
assertRuntimeIsAvailable();
delete targetChrome.runtime.lastError;
};
function assertRuntimeIsAvailable() {
// chrome.runtime should always be available, but maybe it's disappeared for
// some reason? Add debugging for http://crbug.com/258526.
var runtimeAvailability = GetAvailability('runtime.lastError');
if (!runtimeAvailability.is_available) {
throw new Error('runtime.lastError is not available: ' +
runtimeAvailability.message);
}
if (!chrome.runtime)
throw new Error('runtime namespace is null or undefined');
}
/**
* Runs |callback(args)| with last error args as in set().
*
* The target chrome object is the global object's of the callback, so this
* method won't work if the real callback has been wrapped (etc).
*/
function run(name, message, stack, callback, args) {
var targetChrome = GetGlobal(callback).chrome;
set(name, message, stack, targetChrome);
try {
$Function.apply(callback, undefined, args);
} finally {
clear(targetChrome);
}
}
exports.clear = clear;
exports.set = set;
exports.run = run;
|