Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / content_settings.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 cr.define('options', function() {
6   /** @const */ var Page = cr.ui.pageManager.Page;
7   /** @const */ var PageManager = cr.ui.pageManager.PageManager;
8
9   //////////////////////////////////////////////////////////////////////////////
10   // ContentSettings class:
11
12   /**
13    * Encapsulated handling of content settings page.
14    * @constructor
15    */
16   function ContentSettings() {
17     this.activeNavTab = null;
18     Page.call(this, 'content',
19               loadTimeData.getString('contentSettingsPageTabTitle'),
20               'content-settings-page');
21   }
22
23   cr.addSingletonGetter(ContentSettings);
24
25   ContentSettings.prototype = {
26     __proto__: Page.prototype,
27
28     /** @override */
29     initializePage: function() {
30       Page.prototype.initializePage.call(this);
31
32       var exceptionsButtons =
33           this.pageDiv.querySelectorAll('.exceptions-list-button');
34       for (var i = 0; i < exceptionsButtons.length; i++) {
35         exceptionsButtons[i].onclick = function(event) {
36           var page = ContentSettingsExceptionsArea.getInstance();
37
38           // Add on the proper hash for the content type, and store that in the
39           // history so back/forward and tab restore works.
40           var hash = event.currentTarget.getAttribute('contentType');
41           var url = page.name + '#' + hash;
42           uber.pushState({pageName: page.name}, url);
43
44           // Navigate after the local history has been replaced in order to have
45           // the correct hash loaded.
46           PageManager.showPageByName('contentExceptions', false);
47         };
48       }
49
50       var manageHandlersButton = $('manage-handlers-button');
51       if (manageHandlersButton) {
52         manageHandlersButton.onclick = function(event) {
53           PageManager.showPageByName('handlers');
54         };
55       }
56
57       if (cr.isChromeOS) {
58         // Disable some controls for Guest in Chrome OS.
59         UIAccountTweaks.applyGuestSessionVisibility(document);
60
61         // Disable some controls for Public session in Chrome OS.
62         UIAccountTweaks.applyPublicSessionVisibility(document);
63       }
64
65       // Cookies filter page ---------------------------------------------------
66       $('show-cookies-button').onclick = function(event) {
67         chrome.send('coreOptionsUserMetricsAction', ['Options_ShowCookies']);
68         PageManager.showPageByName('cookies');
69       };
70
71       $('content-settings-overlay-confirm').onclick =
72           PageManager.closeOverlay.bind(PageManager);
73
74       $('media-pepper-flash-default').hidden = true;
75       $('media-pepper-flash-exceptions').hidden = true;
76
77       $('media-select-mic').addEventListener('change',
78           ContentSettings.setDefaultMicrophone_);
79       $('media-select-camera').addEventListener('change',
80           ContentSettings.setDefaultCamera_);
81     },
82   };
83
84   ContentSettings.updateHandlersEnabledRadios = function(enabled) {
85     var selector = '#content-settings-page input[type=radio][value=' +
86         (enabled ? 'allow' : 'block') + '].handler-radio';
87     document.querySelector(selector).checked = true;
88   };
89
90   /**
91    * Sets the values for all the content settings radios.
92    * @param {Object} dict A mapping from radio groups to the checked value for
93    *     that group.
94    */
95   ContentSettings.setContentFilterSettingsValue = function(dict) {
96     for (var group in dict) {
97       var managedBy = dict[group].managedBy;
98       var controlledBy = managedBy == 'policy' || managedBy == 'extension' ?
99           managedBy : null;
100       document.querySelector('input[type=radio][name=' + group + '][value=' +
101                              dict[group].value + ']').checked = true;
102       var radios = document.querySelectorAll('input[type=radio][name=' +
103                                              group + ']');
104       for (var i = 0, len = radios.length; i < len; i++) {
105         radios[i].disabled = (managedBy != 'default');
106         radios[i].controlledBy = controlledBy;
107       }
108       var indicators = document.querySelectorAll(
109           'span.controlled-setting-indicator[content-setting=' + group + ']');
110       if (indicators.length == 0)
111         continue;
112       // Create a synthetic pref change event decorated as
113       // CoreOptionsHandler::CreateValueForPref() does.
114       var event = new Event(group);
115       event.value = {
116         value: dict[group].value,
117         controlledBy: controlledBy,
118       };
119       for (var i = 0; i < indicators.length; i++) {
120         indicators[i].handlePrefChange(event);
121       }
122     }
123   };
124
125   /**
126    * Updates the labels and indicators for the Media settings. Those require
127    * special handling because they are backed by multiple prefs and can change
128    * their scope based on the managed state of the backing prefs.
129    * @param {Object} mediaSettings A dictionary containing the following fields:
130    *     {String} askText The label for the ask radio button.
131    *     {String} blockText The label for the block radio button.
132    *     {Boolean} cameraDisabled Whether to disable the camera dropdown.
133    *     {Boolean} micDisabled Whether to disable the microphone dropdown.
134    *     {Boolean} showBubble Wether to show the managed icon and bubble for the
135    *         media label.
136    *     {String} bubbleText The text to use inside the bubble if it is shown.
137    */
138   ContentSettings.updateMediaUI = function(mediaSettings) {
139     $('media-stream-ask-label').innerHTML =
140         loadTimeData.getString(mediaSettings.askText);
141     $('media-stream-block-label').innerHTML =
142         loadTimeData.getString(mediaSettings.blockText);
143
144     if (mediaSettings.micDisabled)
145       $('media-select-mic').disabled = true;
146     if (mediaSettings.cameraDisabled)
147       $('media-select-camera').disabled = true;
148
149     PageManager.hideBubble();
150     // Create a synthetic pref change event decorated as
151     // CoreOptionsHandler::CreateValueForPref() does.
152     // TODO(arv): It was not clear what event type this should use?
153     var event = new Event('undefined');
154     event.value = {};
155
156     if (mediaSettings.showBubble) {
157       event.value = { controlledBy: 'policy' };
158       $('media-indicator').setAttribute(
159           'textpolicy', loadTimeData.getString(mediaSettings.bubbleText));
160       $('media-indicator').location = cr.ui.ArrowLocation.TOP_START;
161     }
162
163     $('media-indicator').handlePrefChange(event);
164   };
165
166   /**
167    * Initializes an exceptions list.
168    * @param {string} type The content type that we are setting exceptions for.
169    * @param {Array} exceptions An array of pairs, where the first element of
170    *     each pair is the filter string, and the second is the setting
171    *     (allow/block).
172    */
173   ContentSettings.setExceptions = function(type, exceptions) {
174     this.getExceptionsList(type, 'normal').setExceptions(exceptions);
175   };
176
177   ContentSettings.setHandlers = function(handlers) {
178     $('handlers-list').setHandlers(handlers);
179   };
180
181   ContentSettings.setIgnoredHandlers = function(ignoredHandlers) {
182     $('ignored-handlers-list').setHandlers(ignoredHandlers);
183   };
184
185   ContentSettings.setOTRExceptions = function(type, otrExceptions) {
186     var exceptionsList = this.getExceptionsList(type, 'otr');
187     // Settings for Guest hides many sections, so check for null first.
188     if (exceptionsList) {
189       exceptionsList.parentNode.hidden = false;
190       exceptionsList.setExceptions(otrExceptions);
191     }
192   };
193
194   /**
195    * @param {string} type The type of exceptions (e.g. "location") to get.
196    * @param {string} mode The mode of the desired exceptions list (e.g. otr).
197    * @return {?ExceptionsList} The corresponding exceptions list or null.
198    */
199   ContentSettings.getExceptionsList = function(type, mode) {
200     return document.querySelector(
201         'div[contentType=' + type + '] list[mode=' + mode + ']');
202   };
203
204   /**
205    * The browser's response to a request to check the validity of a given URL
206    * pattern.
207    * @param {string} type The content type.
208    * @param {string} mode The browser mode.
209    * @param {string} pattern The pattern.
210    * @param {bool} valid Whether said pattern is valid in the context of
211    *     a content exception setting.
212    */
213   ContentSettings.patternValidityCheckComplete =
214       function(type, mode, pattern, valid) {
215     this.getExceptionsList(type, mode).patternValidityCheckComplete(pattern,
216                                                                     valid);
217   };
218
219   /**
220    * Shows/hides the link to the Pepper Flash camera and microphone default
221    * settings.
222    * Please note that whether the link is actually showed or not is also
223    * affected by the style class pepper-flash-settings.
224    */
225   ContentSettings.showMediaPepperFlashDefaultLink = function(show) {
226     $('media-pepper-flash-default').hidden = !show;
227   };
228
229   /**
230    * Shows/hides the link to the Pepper Flash camera and microphone
231    * site-specific settings.
232    * Please note that whether the link is actually showed or not is also
233    * affected by the style class pepper-flash-settings.
234    */
235   ContentSettings.showMediaPepperFlashExceptionsLink = function(show) {
236     $('media-pepper-flash-exceptions').hidden = !show;
237   };
238
239   /**
240    * Shows/hides the whole Web MIDI settings.
241    * @param {bool} show Wether to show the whole Web MIDI settings.
242    */
243   ContentSettings.showExperimentalWebMIDISettings = function(show) {
244     $('experimental-web-midi-settings').hidden = !show;
245   };
246
247   /**
248    * Updates the microphone/camera devices menu with the given entries.
249    * @param {string} type The device type.
250    * @param {Array} devices List of available devices.
251    * @param {string} defaultdevice The unique id of the current default device.
252    */
253   ContentSettings.updateDevicesMenu = function(type, devices, defaultdevice) {
254     var deviceSelect = '';
255     if (type == 'mic') {
256       deviceSelect = $('media-select-mic');
257     } else if (type == 'camera') {
258       deviceSelect = $('media-select-camera');
259     } else {
260       console.error('Unknown device type for <device select> UI element: ' +
261                     type);
262       return;
263     }
264
265     deviceSelect.textContent = '';
266
267     var deviceCount = devices.length;
268     var defaultIndex = -1;
269     for (var i = 0; i < deviceCount; i++) {
270       var device = devices[i];
271       var option = new Option(device.name, device.id);
272       if (option.value == defaultdevice)
273         defaultIndex = i;
274       deviceSelect.appendChild(option);
275     }
276     if (defaultIndex >= 0)
277       deviceSelect.selectedIndex = defaultIndex;
278   };
279
280   /**
281    * Enables/disables the protected content exceptions button.
282    * @param {bool} enable Whether to enable the button.
283    */
284   ContentSettings.enableProtectedContentExceptions = function(enable) {
285     var exceptionsButton = $('protected-content-exceptions');
286     if (exceptionsButton)
287       exceptionsButton.disabled = !enable;
288   };
289
290   /**
291    * Set the default microphone device based on the popup selection.
292    * @private
293    */
294   ContentSettings.setDefaultMicrophone_ = function() {
295     var deviceSelect = $('media-select-mic');
296     chrome.send('setDefaultCaptureDevice', ['mic', deviceSelect.value]);
297   };
298
299   /**
300    * Set the default camera device based on the popup selection.
301    * @private
302    */
303   ContentSettings.setDefaultCamera_ = function() {
304     var deviceSelect = $('media-select-camera');
305     chrome.send('setDefaultCaptureDevice', ['camera', deviceSelect.value]);
306   };
307
308   // Export
309   return {
310     ContentSettings: ContentSettings
311   };
312
313 });