Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: chrome/common/extensions/docs/examples/api/power/background.js

Issue 13493019: Update chrome.power example extension. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add chrome.windows.onCreated listener Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/common/extensions/docs/examples/api/power/manifest.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /** 5 /**
6 * Available levels of power-saving-overriding. 6 * States that the extension can be in.
7 */ 7 */
8 var LevelEnum = { 8 var StateEnum = {
9 DISABLED: '', 9 DISABLED: '',
10 DISPLAY: 'display', 10 DISPLAY: 'display',
11 SYSTEM: 'system' 11 SYSTEM: 'system'
12 }; 12 };
13 13
14 /** 14 /**
15 * Key used for storing the current level in {localStorage}. 15 * Key used for storing the current state in {localStorage}.
16 */ 16 */
17 var LEVEL_KEY = 'level'; 17 var STATE_KEY = 'level';
18
19 /**
20 * Current {LevelEnum}.
21 */
22 var currentLevel = LevelEnum.DISABLED;
23 18
24 /** 19 /**
25 * Should the old {chrome.experimental.power} API be used rather than 20 * Should the old {chrome.experimental.power} API be used rather than
26 * {chrome.power}? 21 * {chrome.power}?
27 */ 22 */
28 var useOldApi = !chrome.power; 23 var useOldApi = !chrome.power;
29 24
30 /** 25 /**
31 * Returns the previously-used level. 26 * Returns the currently-used state.
32 * @return {string} Saved {LevelEnum} from local storage. 27 * @return {string} Saved {StateEnum} from local storage.
33 */ 28 */
34 function getInitialLevel() { 29 function getSavedState() {
35 if (LEVEL_KEY in localStorage) { 30 if (STATE_KEY in localStorage) {
Matt Perry 2013/04/08 21:00:22 We're trying to move away from localStorage. Can y
36 var savedLevel = localStorage[LEVEL_KEY]; 31 var savedState = localStorage[STATE_KEY];
37 for (var key in LevelEnum) { 32 for (var key in StateEnum) {
38 if (savedLevel == LevelEnum[key]) { 33 if (savedState == StateEnum[key]) {
39 return savedLevel; 34 return savedState;
40 } 35 }
41 } 36 }
42 } 37 }
43 return LevelEnum.DISABLED; 38 return StateEnum.DISABLED;
44 } 39 }
45 40
46 /** 41 /**
47 * Switches to a new power-saving-overriding level. 42 * Switches to a new state.
48 * @param {string} newLevel New {LevelEnum} to use. 43 * @param {string} newState New {StateEnum} to use.
49 */ 44 */
50 function setLevel(newLevel) { 45 function setState(newState) {
51 var imagePrefix = 'night'; 46 var imagePrefix = 'night';
52 var title = ''; 47 var title = '';
53 48
54 // The old API doesn't support the "system" level. 49 // The old API doesn't support the "system" level.
55 if (useOldApi && newLevel == LevelEnum.SYSTEM) 50 if (useOldApi && newState == StateEnum.SYSTEM)
56 newLevel = LevelEnum.DISPLAY; 51 newState = StateEnum.DISPLAY;
57 52
58 switch (newLevel) { 53 switch (newState) {
59 case LevelEnum.DISABLED: 54 case StateEnum.DISABLED:
60 (useOldApi ? chrome.experimental.power : chrome.power).releaseKeepAwake(); 55 (useOldApi ? chrome.experimental.power : chrome.power).releaseKeepAwake();
61 imagePrefix = 'night'; 56 imagePrefix = 'night';
62 title = chrome.i18n.getMessage('disabledTitle'); 57 title = chrome.i18n.getMessage('disabledTitle');
63 break; 58 break;
64 case LevelEnum.DISPLAY: 59 case StateEnum.DISPLAY:
65 if (useOldApi) 60 if (useOldApi)
66 chrome.experimental.power.requestKeepAwake(function() {}); 61 chrome.experimental.power.requestKeepAwake(function() {});
67 else 62 else
68 chrome.power.requestKeepAwake('display'); 63 chrome.power.requestKeepAwake('display');
69 imagePrefix = 'day'; 64 imagePrefix = 'day';
70 title = chrome.i18n.getMessage('displayTitle'); 65 title = chrome.i18n.getMessage('displayTitle');
71 break; 66 break;
72 case LevelEnum.SYSTEM: 67 case StateEnum.SYSTEM:
73 chrome.power.requestKeepAwake('system'); 68 chrome.power.requestKeepAwake('system');
74 imagePrefix = 'sunset'; 69 imagePrefix = 'sunset';
75 title = chrome.i18n.getMessage('systemTitle'); 70 title = chrome.i18n.getMessage('systemTitle');
76 break; 71 break;
77 default: 72 default:
78 throw 'Invalid level "' + newLevel + '"'; 73 throw 'Invalid state "' + newState + '"';
79 } 74 }
80 75
81 currentLevel = newLevel; 76 currentState = newState;
82 localStorage[LEVEL_KEY] = currentLevel; 77 localStorage[STATE_KEY] = currentState;
83 78
84 chrome.browserAction.setIcon({ 79 chrome.browserAction.setIcon({
85 path: { 80 path: {
86 '19': 'images/' + imagePrefix + '-19.png', 81 '19': 'images/' + imagePrefix + '-19.png',
87 '38': 'images/' + imagePrefix + '-38.png' 82 '38': 'images/' + imagePrefix + '-38.png'
88 } 83 }
89 }); 84 });
90 chrome.browserAction.setTitle({title: title}); 85 chrome.browserAction.setTitle({title: title});
91 } 86 }
92 87
93 /** 88 chrome.browserAction.onClicked.addListener(function() {
94 * Cycles levels in response to browser action icon clicks. 89 var currentState = getSavedState();
95 */ 90 switch (currentState) {
96 function handleClicked() { 91 case StateEnum.DISABLED:
97 switch (currentLevel) { 92 setState(StateEnum.DISPLAY);
98 case LevelEnum.DISABLED:
99 setLevel(LevelEnum.DISPLAY);
100 break; 93 break;
101 case LevelEnum.DISPLAY: 94 case StateEnum.DISPLAY:
102 setLevel(useOldApi ? LevelEnum.DISABLED : LevelEnum.SYSTEM); 95 setState(useOldApi ? StateEnum.DISABLED : StateEnum.SYSTEM);
103 break; 96 break;
104 case LevelEnum.SYSTEM: 97 case StateEnum.SYSTEM:
105 setLevel(LevelEnum.DISABLED); 98 setState(StateEnum.DISABLED);
106 break; 99 break;
107 default: 100 default:
108 throw 'Invalid level "' + currentLevel + '"'; 101 throw 'Invalid state "' + currentState + '"';
109 } 102 }
110 } 103 });
111 104
112 chrome.browserAction.onClicked.addListener(handleClicked); 105 chrome.runtime.onStartup.addListener(function() {
113 setLevel(getInitialLevel()); 106 setState(getSavedState());
107 });
108
109 // TODO(derat): Remove this once http://crbug.com/222473 is fixed.
110 chrome.windows.onCreated.addListener(function() {
111 setState(getSavedState());
112 });
OLDNEW
« no previous file with comments | « no previous file | chrome/common/extensions/docs/examples/api/power/manifest.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698