- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / examples / api / preferences / enableReferrer / popup.js
1 // Copyright (c) 2012 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 var pref = chrome.privacy.websites.referrersEnabled;
7
8 function $(id) {
9   return document.getElementById(id);
10 }
11
12 /**
13  * Returns whether the |levelOfControl| means that the extension can change the
14  * preference value.
15  *
16  * @param levelOfControl{string}
17  */
18 function settingIsControllable(levelOfControl) {
19   return (levelOfControl == 'controllable_by_this_extension' ||
20           levelOfControl == 'controlled_by_this_extension');
21 }
22
23 /**
24  * Updates the UI to reflect the state of the preference.
25  *
26  * @param settings{object} A settings object, as returned from |get()| or the
27  * |onchange| event.
28  */
29 function updateUI(settings) {
30   var disableUI = !settingIsControllable(settings.levelOfControl);
31   document.getElementById('regularValue').disabled = disableUI;
32   document.getElementById('useSeparateIncognitoSettings').disabled = disableUI;
33   if (settings.hasOwnProperty('incognitoSpecific')) {
34     var hasIncognitoValue = settings.incognitoSpecific;
35     document.getElementById('useSeparateIncognitoSettings').checked =
36         hasIncognitoValue;
37     document.getElementById('incognitoValue').disabled =
38         disableUI || !hasIncognitoValue;
39     document.getElementById('incognitoValue').checked = settings.value;
40   } else {
41     document.getElementById('regularValue').checked = settings.value;
42   }
43 }
44
45 /**
46  * Wrapper for |updateUI| which is used as callback for the |get()| method and
47  * which logs the result.
48  * If there was an error getting the preference, does nothing.
49  *
50  * @param settings{object} A settings object, as returned from |get()|.
51  */
52 function updateUIFromGet(settings) {
53   if (settings) {
54     console.log('pref.get result:' + JSON.stringify(settings));
55     updateUI(settings);
56   }
57 }
58
59 /**
60  * Wrapper for |updateUI| which is used as handler for the |onchange| event
61  * and which logs the result.
62  *
63  * @param settings{object} A settings object, as returned from the |onchange|
64  * event.
65  */
66 function updateUIFromOnChange(settings) {
67   console.log('pref.onChange event:' + JSON.stringify(settings));
68   updateUI(settings);
69 }
70
71 /*
72  * Initializes the UI.
73  */
74 function init() {
75   chrome.extension.isAllowedIncognitoAccess(function(allowed) {
76     if (allowed) {
77       pref.get({'incognito': true}, updateUIFromGet);
78       $('incognito').style.display = 'block';
79       $('incognito-forbidden').style.display = 'none';
80     }
81   });
82   pref.get({}, updateUIFromGet);
83   pref.onChange.addListener(updateUIFromOnChange);
84
85   $('regularValue').addEventListener('click', function () {
86     setPrefValue(this.checked, false);
87   });
88   $('useSeparateIncognitoSettings').addEventListener('click', function () {
89      setUseSeparateIncognitoSettings(this.checked);
90   });
91   $('incognitoValue').addEventListener('click', function () {
92     setPrefValue(this.checked, true);
93   });
94 }
95
96 /**
97  * Called from the UI to change the preference value.
98  *
99  * @param enabled{boolean} The new preference value.
100  * @param incognito{boolean} Whether the value is specific to incognito mode.
101  */
102 function setPrefValue(enabled, incognito) {
103   var scope = incognito ? 'incognito_session_only' : 'regular';
104   pref.set({'value': enabled, 'scope': scope});
105 }
106
107 /**
108  * Called from the UI to change whether to use separate settings for
109  * incognito mode.
110  *
111  * @param value{boolean} whether to use separate settings for
112  * incognito mode.
113  */
114 function setUseSeparateIncognitoSettings(value) {
115   if (!value) {
116     pref.clear({'incognito': true});
117   } else {
118     // Explicitly set the value for incognito mode.
119     pref.get({'incognito': true}, function(settings) {
120       pref.set({'incognito': true, 'value': settings.value});
121     });
122   }
123   document.getElementById('incognitoValue').disabled = !value;
124 }
125
126 // Call `init` to kick things off.
127 document.addEventListener('DOMContentLoaded', init);