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.
6 * States that the extension can be in.
15 * Key used for storing the current state in {localStorage}.
17 var STATE_KEY
= 'state';
20 * Loads the locally-saved state asynchronously.
21 * @param {function} callback Callback invoked with the loaded {StateEnum}.
23 function loadSavedState(callback
) {
24 chrome
.storage
.local
.get(STATE_KEY
, function(items
) {
25 var savedState
= items
[STATE_KEY
];
26 for (var key
in StateEnum
) {
27 if (savedState
== StateEnum
[key
]) {
32 callback(StateEnum
.DISABLED
);
37 * Switches to a new state.
38 * @param {string} newState New {StateEnum} to use.
40 function setState(newState
) {
41 var imagePrefix
= 'night';
45 case StateEnum
.DISABLED
:
46 chrome
.power
.releaseKeepAwake();
47 imagePrefix
= 'night';
48 title
= chrome
.i18n
.getMessage('disabledTitle');
50 case StateEnum
.DISPLAY
:
51 chrome
.power
.requestKeepAwake('display');
53 title
= chrome
.i18n
.getMessage('displayTitle');
55 case StateEnum
.SYSTEM
:
56 chrome
.power
.requestKeepAwake('system');
57 imagePrefix
= 'sunset';
58 title
= chrome
.i18n
.getMessage('systemTitle');
61 throw 'Invalid state "' + newState
+ '"';
65 items
[STATE_KEY
] = newState
;
66 chrome
.storage
.local
.set(items
);
68 chrome
.browserAction
.setIcon({
70 '19': 'images/' + imagePrefix
+ '-19.png',
71 '38': 'images/' + imagePrefix
+ '-38.png'
74 chrome
.browserAction
.setTitle({title
: title
});
77 chrome
.browserAction
.onClicked
.addListener(function() {
78 loadSavedState(function(state
) {
80 case StateEnum
.DISABLED
:
81 setState(StateEnum
.DISPLAY
);
83 case StateEnum
.DISPLAY
:
84 setState(StateEnum
.SYSTEM
);
86 case StateEnum
.SYSTEM
:
87 setState(StateEnum
.DISABLED
);
90 throw 'Invalid state "' + state
+ '"';
95 chrome
.runtime
.onStartup
.addListener(function() {
96 loadSavedState(function(state
) { setState(state
); });
99 chrome
.runtime
.onInstalled
.addListener(function(details
) {
100 loadSavedState(function(state
) { setState(state
); });