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.experimental.proxy.onProxyError.addListener( 23 this.handleError_.bind(this)); 24 25 // Handle message events from popup. 26 chrome.extension.onRequest.addListener( 27 this.handleOnRequest_.bind(this)); 28}; 29 30/////////////////////////////////////////////////////////////////////////////// 31 32/** 33 * @typedef {{fatal: boolean, error: string, details: string}} 34 */ 35ProxyErrorHandler.ErrorDetails; 36 37/////////////////////////////////////////////////////////////////////////////// 38 39ProxyErrorHandler.prototype = { 40 /** 41 * Details of the most recent error. 42 * @type {?ProxyErrorHandler.ErrorDetails} 43 * @private 44 */ 45 lastError_: null, 46 47 /** 48 * Handle request messages from the popup. 49 * 50 * @param {!{type:string}} request The external request to answer. 51 * @param {!MessageSender} sender Info about the script context that sent 52 * the request. 53 * @param {!function} sendResponse Function to call to send a response. 54 * @private 55 */ 56 handleOnRequest_: function(request, sender, sendResponse) { 57 if (request.type === 'getError') { 58 sendResponse({result: this.getErrorDetails()}); 59 } else if (request.type === 'clearError') { 60 this.clearErrorDetails(); 61 sendResponse({result: true}); 62 } 63 }, 64 65 /** 66 * Handles the error event, storing the error details for later use, and 67 * badges the browser action icon. 68 * 69 * @param {!ProxyErrorHandler.ErrorDetails} details The error details. 70 * @private 71 */ 72 handleError_: function(details) { 73 var RED = [255, 0, 0, 255]; 74 var YELLOW = [255, 205, 0, 255]; 75 76 // Badge the popup icon. 77 var color = details.fatal ? RED : YELLOW; 78 chrome.browserAction.setBadgeBackgroundColor({color: color}); 79 chrome.browserAction.setBadgeText({text: 'X'}); 80 chrome.browserAction.setTitle({ 81 'title': chrome.i18n.getMessage('errorPopupTitle', details.error) 82 }); 83 84 // Store the error for display in the popup. 85 this.lastError_ = JSON.stringify(details); 86 }, 87 88 89 /** 90 * Returns details of the last error handled. 91 * 92 * @return {?ProxyErrorHandler.ErrorDetails} 93 */ 94 getErrorDetails: function() { 95 return this.lastError_; 96 }, 97 98 99 /** 100 * Clears last handled error. 101 */ 102 clearErrorDetails: function() { 103 this.lastError_ = null; 104 } 105} 106