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.
4
5/*
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.
8 *
9 * @param {function(!Error)} errCallback The callback to invoke for error
10 *     reporting.
11 */
12function checkForExtensionError(errCallback) {
13  if (typeof(chrome.extension.lastError) != 'undefined') {
14    var error = new Error(chrome.extension.lastError.message);
15    errCallback(error);
16    throw error;
17  }
18}
19
20/**
21 * Captures a screenshot of the visible tab.
22 *
23 * @param {function(string)} callback The callback to invoke with the base64
24 *     encoded PNG.
25 * @param {function(!Error)} errCallback The callback to invoke for error
26 *     reporting.
27 */
28function 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
34      errCallback(error);
35      return;
36    }
37    checkForExtensionError(errCallback);
38    var base64 = ';base64,';
39    callback(dataUrl.substr(dataUrl.indexOf(base64) + base64.length))
40  });
41}
42
43/**
44 * Gets info about the current window.
45 *
46 * @param {function(*)} callback The callback to invoke with the window info.
47 * @param {function(!Error)} errCallback The callback to invoke for error
48 *     reporting.
49 */
50function getWindowInfo(callback, errCallback) {
51  chrome.windows.getCurrent({populate: true}, function(window) {
52    checkForExtensionError(errCallback);
53    callback(window);
54  });
55}
56
57/**
58 * Updates the properties of the current window.
59 *
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
63 *     reporting.
64 */
65function 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);
70      callback();
71    });
72  });
73}
74
75/**
76 * Launches an app with the specified id.
77 *
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
81 *     reporting.
82 */
83function launchApp(id, callback, errCallback) {
84  chrome.management.launchApp(id, function() {
85    checkForExtensionError(errCallback);
86    callback();
87  });
88}
89