- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / examples / api / power / background.js
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.
4
5 /**
6  * States that the extension can be in.
7  */
8 var StateEnum = {
9   DISABLED: 'disabled',
10   DISPLAY: 'display',
11   SYSTEM: 'system'
12 };
13
14 /**
15  * Key used for storing the current state in {localStorage}.
16  */
17 var STATE_KEY = 'state';
18
19 /**
20  * Loads the locally-saved state asynchronously.
21  * @param {function} callback Callback invoked with the loaded {StateEnum}.
22  */
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]) {
28         callback(savedState);
29         return;
30       }
31     }
32     callback(StateEnum.DISABLED);
33   });
34 }
35
36 /**
37  * Switches to a new state.
38  * @param {string} newState New {StateEnum} to use.
39  */
40 function setState(newState) {
41   var imagePrefix = 'night';
42   var title = '';
43
44   switch (newState) {
45     case StateEnum.DISABLED:
46       chrome.power.releaseKeepAwake();
47       imagePrefix = 'night';
48       title = chrome.i18n.getMessage('disabledTitle');
49       break;
50     case StateEnum.DISPLAY:
51       chrome.power.requestKeepAwake('display');
52       imagePrefix = 'day';
53       title = chrome.i18n.getMessage('displayTitle');
54       break;
55     case StateEnum.SYSTEM:
56       chrome.power.requestKeepAwake('system');
57       imagePrefix = 'sunset';
58       title = chrome.i18n.getMessage('systemTitle');
59       break;
60     default:
61       throw 'Invalid state "' + newState + '"';
62   }
63
64   var items = {};
65   items[STATE_KEY] = newState;
66   chrome.storage.local.set(items);
67
68   chrome.browserAction.setIcon({
69     path: {
70       '19': 'images/' + imagePrefix + '-19.png',
71       '38': 'images/' + imagePrefix + '-38.png'
72     }
73   });
74   chrome.browserAction.setTitle({title: title});
75 }
76
77 chrome.browserAction.onClicked.addListener(function() {
78   loadSavedState(function(state) {
79     switch (state) {
80       case StateEnum.DISABLED:
81         setState(StateEnum.DISPLAY);
82         break;
83       case StateEnum.DISPLAY:
84         setState(StateEnum.SYSTEM);
85         break;
86       case StateEnum.SYSTEM:
87         setState(StateEnum.DISABLED);
88         break;
89       default:
90         throw 'Invalid state "' + state + '"';
91     }
92   });
93 });
94
95 chrome.runtime.onStartup.addListener(function() {
96   loadSavedState(function(state) { setState(state); });
97 });
98
99 // TODO(derat): Remove this once http://crbug.com/222473 is fixed.
100 chrome.windows.onCreated.addListener(function() {
101   loadSavedState(function(state) { setState(state); });
102 });