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.
5 cr.define('options', function() {
6 /** @const */ var Page = cr.ui.pageManager.Page;
7 /** @const */ var PageManager = cr.ui.pageManager.PageManager;
9 //////////////////////////////////////////////////////////////////////////////
10 // ContentSettings class:
13 * Encapsulated handling of content settings page.
16 function ContentSettings() {
17 this.activeNavTab = null;
18 Page.call(this, 'content',
19 loadTimeData.getString('contentSettingsPageTabTitle'),
20 'content-settings-page');
23 cr.addSingletonGetter(ContentSettings);
25 ContentSettings.prototype = {
26 __proto__: Page.prototype,
29 initializePage: function() {
30 Page.prototype.initializePage.call(this);
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();
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);
44 // Navigate after the local history has been replaced in order to have
45 // the correct hash loaded.
46 PageManager.showPageByName('contentExceptions', false);
50 var manageHandlersButton = $('manage-handlers-button');
51 if (manageHandlersButton) {
52 manageHandlersButton.onclick = function(event) {
53 PageManager.showPageByName('handlers');
58 // Disable some controls for Guest in Chrome OS.
59 UIAccountTweaks.applyGuestSessionVisibility(document);
61 // Disable some controls for Public session in Chrome OS.
62 UIAccountTweaks.applyPublicSessionVisibility(document);
65 // Cookies filter page ---------------------------------------------------
66 $('show-cookies-button').onclick = function(event) {
67 chrome.send('coreOptionsUserMetricsAction', ['Options_ShowCookies']);
68 PageManager.showPageByName('cookies');
71 $('content-settings-overlay-confirm').onclick =
72 PageManager.closeOverlay.bind(PageManager);
74 $('media-pepper-flash-default').hidden = true;
75 $('media-pepper-flash-exceptions').hidden = true;
77 $('media-select-mic').addEventListener('change',
78 ContentSettings.setDefaultMicrophone_);
79 $('media-select-camera').addEventListener('change',
80 ContentSettings.setDefaultCamera_);
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;
91 * Sets the values for all the content settings radios.
92 * @param {Object} dict A mapping from radio groups to the checked value for
95 ContentSettings.setContentFilterSettingsValue = function(dict) {
96 for (var group in dict) {
97 var managedBy = dict[group].managedBy;
98 var controlledBy = managedBy == 'policy' || managedBy == 'extension' ?
100 document.querySelector('input[type=radio][name=' + group + '][value=' +
101 dict[group].value + ']').checked = true;
102 var radios = document.querySelectorAll('input[type=radio][name=' +
104 for (var i = 0, len = radios.length; i < len; i++) {
105 radios[i].disabled = (managedBy != 'default');
106 radios[i].controlledBy = controlledBy;
108 var indicators = document.querySelectorAll(
109 'span.controlled-setting-indicator[content-setting=' + group + ']');
110 if (indicators.length == 0)
112 // Create a synthetic pref change event decorated as
113 // CoreOptionsHandler::CreateValueForPref() does.
114 var event = new Event(group);
116 value: dict[group].value,
117 controlledBy: controlledBy,
119 for (var i = 0; i < indicators.length; i++) {
120 indicators[i].handlePrefChange(event);
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
136 * {String} bubbleText The text to use inside the bubble if it is shown.
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);
144 if (mediaSettings.micDisabled)
145 $('media-select-mic').disabled = true;
146 if (mediaSettings.cameraDisabled)
147 $('media-select-camera').disabled = true;
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');
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;
163 $('media-indicator').handlePrefChange(event);
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
173 ContentSettings.setExceptions = function(type, exceptions) {
174 this.getExceptionsList(type, 'normal').setExceptions(exceptions);
177 ContentSettings.setHandlers = function(handlers) {
178 $('handlers-list').setHandlers(handlers);
181 ContentSettings.setIgnoredHandlers = function(ignoredHandlers) {
182 $('ignored-handlers-list').setHandlers(ignoredHandlers);
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);
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.
199 ContentSettings.getExceptionsList = function(type, mode) {
200 return document.querySelector(
201 'div[contentType=' + type + '] list[mode=' + mode + ']');
205 * The browser's response to a request to check the validity of a given URL
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.
213 ContentSettings.patternValidityCheckComplete =
214 function(type, mode, pattern, valid) {
215 this.getExceptionsList(type, mode).patternValidityCheckComplete(pattern,
220 * Shows/hides the link to the Pepper Flash camera and microphone default
222 * Please note that whether the link is actually showed or not is also
223 * affected by the style class pepper-flash-settings.
225 ContentSettings.showMediaPepperFlashDefaultLink = function(show) {
226 $('media-pepper-flash-default').hidden = !show;
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.
235 ContentSettings.showMediaPepperFlashExceptionsLink = function(show) {
236 $('media-pepper-flash-exceptions').hidden = !show;
240 * Shows/hides the whole Web MIDI settings.
241 * @param {bool} show Wether to show the whole Web MIDI settings.
243 ContentSettings.showExperimentalWebMIDISettings = function(show) {
244 $('experimental-web-midi-settings').hidden = !show;
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.
253 ContentSettings.updateDevicesMenu = function(type, devices, defaultdevice) {
254 var deviceSelect = '';
256 deviceSelect = $('media-select-mic');
257 } else if (type == 'camera') {
258 deviceSelect = $('media-select-camera');
260 console.error('Unknown device type for <device select> UI element: ' +
265 deviceSelect.textContent = '';
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)
274 deviceSelect.appendChild(option);
276 if (defaultIndex >= 0)
277 deviceSelect.selectedIndex = defaultIndex;
281 * Enables/disables the protected content exceptions button.
282 * @param {bool} enable Whether to enable the button.
284 ContentSettings.enableProtectedContentExceptions = function(enable) {
285 var exceptionsButton = $('protected-content-exceptions');
286 if (exceptionsButton)
287 exceptionsButton.disabled = !enable;
291 * Set the default microphone device based on the popup selection.
294 ContentSettings.setDefaultMicrophone_ = function() {
295 var deviceSelect = $('media-select-mic');
296 chrome.send('setDefaultCaptureDevice', ['mic', deviceSelect.value]);
300 * Set the default camera device based on the popup selection.
303 ContentSettings.setDefaultCamera_ = function() {
304 var deviceSelect = $('media-select-camera');
305 chrome.send('setDefaultCaptureDevice', ['camera', deviceSelect.value]);
310 ContentSettings: ContentSettings