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.
6 var pref = chrome.privacy.websites.referrersEnabled;
9 return document.getElementById(id);
13 * Returns whether the |levelOfControl| means that the extension can change the
16 * @param levelOfControl{string}
18 function settingIsControllable(levelOfControl) {
19 return (levelOfControl == 'controllable_by_this_extension' ||
20 levelOfControl == 'controlled_by_this_extension');
24 * Updates the UI to reflect the state of the preference.
26 * @param settings{object} A settings object, as returned from |get()| or the
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 =
37 document.getElementById('incognitoValue').disabled =
38 disableUI || !hasIncognitoValue;
39 document.getElementById('incognitoValue').checked = settings.value;
41 document.getElementById('regularValue').checked = settings.value;
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.
50 * @param settings{object} A settings object, as returned from |get()|.
52 function updateUIFromGet(settings) {
54 console.log('pref.get result:' + JSON.stringify(settings));
60 * Wrapper for |updateUI| which is used as handler for the |onchange| event
61 * and which logs the result.
63 * @param settings{object} A settings object, as returned from the |onchange|
66 function updateUIFromOnChange(settings) {
67 console.log('pref.onChange event:' + JSON.stringify(settings));
75 chrome.extension.isAllowedIncognitoAccess(function(allowed) {
77 pref.get({'incognito': true}, updateUIFromGet);
78 $('incognito').style.display = 'block';
79 $('incognito-forbidden').style.display = 'none';
82 pref.get({}, updateUIFromGet);
83 pref.onChange.addListener(updateUIFromOnChange);
85 $('regularValue').addEventListener('click', function () {
86 setPrefValue(this.checked, false);
88 $('useSeparateIncognitoSettings').addEventListener('click', function () {
89 setUseSeparateIncognitoSettings(this.checked);
91 $('incognitoValue').addEventListener('click', function () {
92 setPrefValue(this.checked, true);
97 * Called from the UI to change the preference value.
99 * @param enabled{boolean} The new preference value.
100 * @param incognito{boolean} Whether the value is specific to incognito mode.
102 function setPrefValue(enabled, incognito) {
103 var scope = incognito ? 'incognito_session_only' : 'regular';
104 pref.set({'value': enabled, 'scope': scope});
108 * Called from the UI to change whether to use separate settings for
111 * @param value{boolean} whether to use separate settings for
114 function setUseSeparateIncognitoSettings(value) {
116 pref.clear({'incognito': true});
118 // Explicitly set the value for incognito mode.
119 pref.get({'incognito': true}, function(settings) {
120 pref.set({'incognito': true, 'value': settings.value});
123 document.getElementById('incognitoValue').disabled = !value;
126 // Call `init` to kick things off.
127 document.addEventListener('DOMContentLoaded', init);