• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2011 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 * @fileoverview This file implements the ProxyErrorHandler class, which will
7 * flag proxy errors in a visual way for the extension's user.
8 *
9 * @author Mike West <mkwst@google.com>
10 */
11
12
13/**
14 * The proxy error handling object. Binds to the 'onProxyError' event, and
15 * changes the extensions badge to reflect the error state (yellow for
16 * non-fatal errors, red for fatal).
17 *
18 * @constructor
19 */
20function ProxyErrorHandler() {
21  // Handle proxy error events.
22  chrome.proxy.onProxyError.addListener(this.handleError_.bind(this));
23
24  // Handle message events from popup.
25  chrome.extension.onRequest.addListener(this.handleOnRequest_.bind(this));
26};
27
28///////////////////////////////////////////////////////////////////////////////
29
30/**
31 * @typedef {{fatal: boolean, error: string, details: string}}
32 */
33ProxyErrorHandler.ErrorDetails;
34
35///////////////////////////////////////////////////////////////////////////////
36
37ProxyErrorHandler.prototype = {
38  /**
39   * Details of the most recent error.
40   * @type {?ProxyErrorHandler.ErrorDetails}
41   * @private
42   */
43  lastError_: null,
44
45   /**
46    * Handle request messages from the popup.
47    *
48    * @param {!{type:string}} request The external request to answer.
49    * @param {!MessageSender} sender Info about the script context that sent
50    *     the request.
51    * @param {!function} sendResponse Function to call to send a response.
52    * @private
53    */
54  handleOnRequest_: function(request, sender, sendResponse) {
55    if (request.type === 'getError') {
56      sendResponse({result: this.getErrorDetails()});
57    } else if (request.type === 'clearError') {
58      this.clearErrorDetails();
59      sendResponse({result: true});
60    }
61  },
62
63  /**
64   * Handles the error event, storing the error details for later use, and
65   * badges the browser action icon.
66   *
67   * @param {!ProxyErrorHandler.ErrorDetails} details The error details.
68   * @private
69   */
70  handleError_: function(details) {
71    var RED = [255, 0, 0, 255];
72    var YELLOW = [255, 205, 0, 255];
73
74    // Badge the popup icon.
75    var color = details.fatal ? RED : YELLOW;
76    chrome.browserAction.setBadgeBackgroundColor({color: color});
77    chrome.browserAction.setBadgeText({text: 'X'});
78    chrome.browserAction.setTitle({
79      title: chrome.i18n.getMessage('errorPopupTitle', details.error)
80    });
81
82    // Store the error for display in the popup.
83    this.lastError_ = JSON.stringify(details);
84  },
85
86
87  /**
88   * Returns details of the last error handled.
89   *
90   * @return {?ProxyErrorHandler.ErrorDetails}
91   */
92  getErrorDetails: function() {
93    return this.lastError_;
94  },
95
96
97  /**
98   * Clears last handled error.
99   */
100  clearErrorDetails: function() {
101    chrome.browserAction.setBadgeText({text: ''});
102    this.lastError_ = null;
103  }
104}
105