| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * @fileoverview Utility objects and functions for Google Now extension. | 8 * @fileoverview Utility objects and functions for Google Now extension. |
| 9 * Most important entities here: | 9 * Most important entities here: |
| 10 * (1) 'wrapper' is a module used to add error handling and other services to | 10 * (1) 'wrapper' is a module used to add error handling and other services to |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 * when a task completes. | 22 * when a task completes. |
| 23 */ | 23 */ |
| 24 | 24 |
| 25 // TODO(vadimt): Use server name in the manifest. | 25 // TODO(vadimt): Use server name in the manifest. |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Notification server URL. | 28 * Notification server URL. |
| 29 */ | 29 */ |
| 30 var NOTIFICATION_CARDS_URL = 'https://www.googleapis.com/chromenow/v1'; | 30 var NOTIFICATION_CARDS_URL = 'https://www.googleapis.com/chromenow/v1'; |
| 31 | 31 |
| 32 var DEBUG_MODE = localStorage['debug_mode']; | 32 /** |
| 33 * Returns true if debug mode is enabled. |
| 34 * localStorage returns items as strings, which means if we store a boolean, |
| 35 * it returns a string. Use this function to compare against true. |
| 36 * @return {boolean} Whether debug mode is enabled. |
| 37 */ |
| 38 function isInDebugMode() { |
| 39 return localStorage.debug_mode === 'true'; |
| 40 } |
| 33 | 41 |
| 34 /** | 42 /** |
| 35 * Initializes for debug or release modes of operation. | 43 * Initializes for debug or release modes of operation. |
| 36 */ | 44 */ |
| 37 function initializeDebug() { | 45 function initializeDebug() { |
| 38 if (DEBUG_MODE) { | 46 if (isInDebugMode()) { |
| 39 NOTIFICATION_CARDS_URL = | 47 NOTIFICATION_CARDS_URL = |
| 40 localStorage['server_url'] || NOTIFICATION_CARDS_URL; | 48 localStorage['server_url'] || NOTIFICATION_CARDS_URL; |
| 41 } | 49 } |
| 42 } | 50 } |
| 43 | 51 |
| 44 initializeDebug(); | 52 initializeDebug(); |
| 45 | 53 |
| 46 /** | 54 /** |
| 55 * Conditionally allow console.log output based off of the debug mode. |
| 56 */ |
| 57 console.log = function() { |
| 58 var originalConsoleLog = console.log; |
| 59 return function() { |
| 60 if (isInDebugMode()) { |
| 61 originalConsoleLog.apply(console, arguments); |
| 62 } |
| 63 }; |
| 64 }(); |
| 65 |
| 66 /** |
| 47 * Location Card Storage. | 67 * Location Card Storage. |
| 48 */ | 68 */ |
| 49 if (localStorage['locationCardsShown'] === undefined) | 69 if (localStorage['locationCardsShown'] === undefined) |
| 50 localStorage['locationCardsShown'] = 0; | 70 localStorage['locationCardsShown'] = 0; |
| 51 | 71 |
| 52 /** | 72 /** |
| 53 * Builds an error object with a message that may be sent to the server. | 73 * Builds an error object with a message that may be sent to the server. |
| 54 * @param {string} message Error message. This message may be sent to the | 74 * @param {string} message Error message. This message may be sent to the |
| 55 * server. | 75 * server. |
| 56 * @return {Error} Error object. | 76 * @return {Error} Error object. |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 * @param {Error} error Error to report. | 184 * @param {Error} error Error to report. |
| 165 */ | 185 */ |
| 166 function reportError(error) { | 186 function reportError(error) { |
| 167 var message = 'Critical error:\n' + error.stack; | 187 var message = 'Critical error:\n' + error.stack; |
| 168 console.error(message); | 188 console.error(message); |
| 169 if (!errorReported) { | 189 if (!errorReported) { |
| 170 errorReported = true; | 190 errorReported = true; |
| 171 chrome.metricsPrivate.getIsCrashReportingEnabled(function(isEnabled) { | 191 chrome.metricsPrivate.getIsCrashReportingEnabled(function(isEnabled) { |
| 172 if (isEnabled) | 192 if (isEnabled) |
| 173 sendErrorReport(error); | 193 sendErrorReport(error); |
| 174 if (DEBUG_MODE) | 194 if (isInDebugMode()) |
| 175 alert(message); | 195 alert(message); |
| 176 }); | 196 }); |
| 177 } | 197 } |
| 178 } | 198 } |
| 179 | 199 |
| 180 // Partial mirror of chrome.* for all instrumented functions. | 200 // Partial mirror of chrome.* for all instrumented functions. |
| 181 var instrumented = {}; | 201 var instrumented = {}; |
| 182 | 202 |
| 183 /** | 203 /** |
| 184 * Wrapper plugin. These plugins extend instrumentation added by | 204 * Wrapper plugin. These plugins extend instrumentation added by |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 // One hour is just an arbitrary amount of time chosen. | 785 // One hour is just an arbitrary amount of time chosen. |
| 766 chrome.alarms.create(alarmName, {periodInMinutes: 60}); | 786 chrome.alarms.create(alarmName, {periodInMinutes: 60}); |
| 767 | 787 |
| 768 return { | 788 return { |
| 769 addListener: addListener, | 789 addListener: addListener, |
| 770 getAuthToken: getAuthToken, | 790 getAuthToken: getAuthToken, |
| 771 isSignedIn: isSignedIn, | 791 isSignedIn: isSignedIn, |
| 772 removeToken: removeToken | 792 removeToken: removeToken |
| 773 }; | 793 }; |
| 774 } | 794 } |
| OLD | NEW |