1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Checks for an extension error that occurred during the asynchronous call.
7 * If an error occurs, will invoke the error callback and throw an exception.
9 * @param {function(!Error)} errCallback The callback to invoke for error
12 function checkForExtensionError(errCallback
) {
13 if (typeof(chrome
.extension
.lastError
) != 'undefined') {
14 var error
= new Error(chrome
.extension
.lastError
.message
);
21 * Captures a screenshot of the visible tab.
23 * @param {function(string)} callback The callback to invoke with the base64
25 * @param {function(!Error)} errCallback The callback to invoke for error
28 function captureScreenshot(callback
, errCallback
) {
29 chrome
.tabs
.captureVisibleTab({format
:'png'}, function(dataUrl
) {
30 if (chrome
.extension
.lastError
&&
31 chrome
.extension
.lastError
.message
.indexOf('permission') != -1) {
32 var error
= new Error(chrome
.extension
.lastError
.message
);
33 error
.code
= 103; // kForbidden
37 checkForExtensionError(errCallback
);
38 var base64
= ';base64,';
39 callback(dataUrl
.substr(dataUrl
.indexOf(base64
) + base64
.length
))
44 * Gets info about the current window.
46 * @param {function(*)} callback The callback to invoke with the window info.
47 * @param {function(!Error)} errCallback The callback to invoke for error
50 function getWindowInfo(callback
, errCallback
) {
51 chrome
.windows
.getCurrent({populate
: true}, function(window
) {
52 checkForExtensionError(errCallback
);
58 * Updates the properties of the current window.
60 * @param {Object} updateInfo Update info to pass to chrome.windows.update.
61 * @param {function()} callback Invoked when the updating is complete.
62 * @param {function(!Error)} errCallback The callback to invoke for error
65 function updateWindow(updateInfo
, callback
, errCallback
) {
66 chrome
.windows
.getCurrent({}, function(window
) {
67 checkForExtensionError(errCallback
);
68 chrome
.windows
.update(window
.id
, updateInfo
, function(window
) {
69 checkForExtensionError(errCallback
);
76 * Launches an app with the specified id.
78 * @param {string} id The ID of the app to launch.
79 * @param {function()} callback Invoked when the launch event is complete.
80 * @param {function(!Error)} errCallback The callback to invoke for error
83 function launchApp(id
, callback
, errCallback
) {
84 chrome
.management
.launchApp(id
, function() {
85 checkForExtensionError(errCallback
);