Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / resources / options / content_settings.js
blobb9aed82ec306899bb4237c77b079b5deb7132bc1
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.exportPath('options');
7 /**
8 * @typedef {{appId: string,
9 * appName: (string|undefined),
10 * embeddingOrigin: (string|undefined),
11 * origin: string,
12 * setting: string,
13 * source: string,
14 * video: (string|undefined)}}
16 options.Exception;
18 cr.define('options', function() {
19 /** @const */ var Page = cr.ui.pageManager.Page;
20 /** @const */ var PageManager = cr.ui.pageManager.PageManager;
22 // Lookup table to generate the i18n strings.
23 /** @const */ var permissionsLookup = {
24 'location': 'location',
25 'notifications': 'notifications',
26 'media-stream': 'mediaStream',
27 'cookies': 'cookies',
28 'multiple-automatic-downloads': 'multipleAutomaticDownloads',
29 'images': 'images',
30 'plugins': 'plugins',
31 'popups': 'popups',
32 'javascript': 'javascript'
35 //////////////////////////////////////////////////////////////////////////////
36 // ContentSettings class:
38 /**
39 * Encapsulated handling of content settings page.
40 * @constructor
41 * @extends {cr.ui.pageManager.Page}
43 function ContentSettings() {
44 this.activeNavTab = null;
45 this.pluginsAllowOptionDisabled = false;
46 Page.call(this, 'content',
47 loadTimeData.getString('contentSettingsPageTabTitle'),
48 'content-settings-page');
51 cr.addSingletonGetter(ContentSettings);
53 ContentSettings.prototype = {
54 __proto__: Page.prototype,
56 /** @override */
57 initializePage: function() {
58 Page.prototype.initializePage.call(this);
60 var exceptionsButtons =
61 this.pageDiv.querySelectorAll('.exceptions-list-button');
62 for (var i = 0; i < exceptionsButtons.length; i++) {
63 exceptionsButtons[i].onclick = function(event) {
64 var hash = event.currentTarget.getAttribute('contentType');
65 PageManager.showPageByName('contentExceptions', true,
66 {hash: '#' + hash});
70 var experimentalExceptionsButtons =
71 this.pageDiv.querySelectorAll('.website-settings-permission-button');
72 for (var i = 0; i < experimentalExceptionsButtons.length; i++) {
73 experimentalExceptionsButtons[i].onclick = function(event) {
74 var hash = event.currentTarget.getAttribute('contentType');
75 WebsiteSettingsManager.showWebsiteSettings(hash);
79 var manageHandlersButton = $('manage-handlers-button');
80 if (manageHandlersButton) {
81 manageHandlersButton.onclick = function(event) {
82 PageManager.showPageByName('handlers');
86 if (cr.isChromeOS) {
87 // Disable some controls for Guest in Chrome OS.
88 UIAccountTweaks.applyGuestSessionVisibility(document);
90 // Disable some controls for Public session in Chrome OS.
91 UIAccountTweaks.applyPublicSessionVisibility(document);
94 // Cookies filter page ---------------------------------------------------
95 $('show-cookies-button').onclick = function(event) {
96 chrome.send('coreOptionsUserMetricsAction', ['Options_ShowCookies']);
97 PageManager.showPageByName('cookies');
100 $('content-settings-overlay-confirm').onclick =
101 PageManager.closeOverlay.bind(PageManager);
103 $('media-pepper-flash-default').hidden = true;
104 $('media-pepper-flash-exceptions').hidden = true;
106 $('media-select-mic').addEventListener('change',
107 ContentSettings.setDefaultMicrophone_);
108 $('media-select-camera').addEventListener('change',
109 ContentSettings.setDefaultCamera_);
111 if (loadTimeData.getBoolean('websiteSettingsManagerEnabled')) {
112 var oldUI =
113 this.pageDiv.querySelectorAll('.replace-with-website-settings');
114 for (var i = 0; i < oldUI.length; i++) {
115 oldUI[i].hidden = true;
118 var newUI =
119 this.pageDiv.querySelectorAll('.experimental-website-settings');
120 for (var i = 0; i < newUI.length; i++) {
121 newUI[i].hidden = false;
127 ContentSettings.updateHandlersEnabledRadios = function(enabled) {
128 var selector = '#content-settings-page input[type=radio][value=' +
129 (enabled ? 'allow' : 'block') + '].handler-radio';
130 document.querySelector(selector).checked = true;
134 * Sets the values for all the content settings radios and labels.
135 * @param {Object<string, {managedBy: string, value: string}>} dict A mapping
136 * from radio groups to the checked value for that group.
138 ContentSettings.setContentFilterSettingsValue = function(dict) {
139 for (var group in dict) {
140 var settingLabel = $(group + '-default-string');
141 if (settingLabel) {
142 var value = dict[group].value;
143 var valueId =
144 permissionsLookup[group] + value[0].toUpperCase() + value.slice(1);
145 settingLabel.textContent = loadTimeData.getString(valueId);
148 var managedBy = dict[group].managedBy;
149 var controlledBy = managedBy == 'policy' || managedBy == 'extension' ?
150 managedBy : null;
151 document.querySelector('input[type=radio][name=' + group + '][value=' +
152 dict[group].value + ']').checked = true;
153 var radios = document.querySelectorAll('input[type=radio][name=' +
154 group + ']');
155 for (var i = 0, len = radios.length; i < len; i++) {
156 radios[i].disabled = (managedBy != 'default');
157 radios[i].controlledBy = controlledBy;
159 var indicators = document.querySelectorAll(
160 'span.controlled-setting-indicator[content-setting=' + group + ']');
161 if (indicators.length == 0)
162 continue;
163 // Create a synthetic pref change event decorated as
164 // CoreOptionsHandler::CreateValueForPref() does.
165 var event = new Event(group);
166 event.value = {
167 value: dict[group].value,
168 controlledBy: controlledBy,
170 for (var i = 0; i < indicators.length; i++) {
171 indicators[i].handlePrefChange(event);
175 if (this.pluginsAllowOptionDisabled) {
176 $('plugins-allow-radio').disabled = true;
181 * Updates the labels and indicators for the Media settings. Those require
182 * special handling because they are backed by multiple prefs and can change
183 * their scope based on the managed state of the backing prefs.
184 * @param {{askText: string, blockText: string, cameraDisabled: boolean,
185 * micDisabled: boolean, showBubble: boolean, bubbleText: string}}
186 * mediaSettings A dictionary containing the following fields:
187 * askText The label for the ask radio button.
188 * blockText The label for the block radio button.
189 * cameraDisabled Whether to disable the camera dropdown.
190 * micDisabled Whether to disable the microphone dropdown.
191 * showBubble Wether to show the managed icon and bubble for the media
192 * label.
193 * bubbleText The text to use inside the bubble if it is shown.
195 ContentSettings.updateMediaUI = function(mediaSettings) {
196 $('media-stream-ask-label').innerHTML =
197 loadTimeData.getString(mediaSettings.askText);
198 $('media-stream-block-label').innerHTML =
199 loadTimeData.getString(mediaSettings.blockText);
201 if (mediaSettings.micDisabled)
202 $('media-select-mic').disabled = true;
203 if (mediaSettings.cameraDisabled)
204 $('media-select-camera').disabled = true;
206 PageManager.hideBubble();
207 // Create a synthetic pref change event decorated as
208 // CoreOptionsHandler::CreateValueForPref() does.
209 // TODO(arv): It was not clear what event type this should use?
210 var event = new Event('undefined');
211 event.value = {};
213 if (mediaSettings.showBubble) {
214 event.value = { controlledBy: 'policy' };
215 $('media-indicator').setAttribute(
216 'textpolicy', loadTimeData.getString(mediaSettings.bubbleText));
217 $('media-indicator').location = cr.ui.ArrowLocation.TOP_START;
220 $('media-indicator').handlePrefChange(event);
224 * Initializes an exceptions list.
225 * @param {string} type The content type that we are setting exceptions for.
226 * @param {Array<options.Exception>} exceptions An array of pairs, where the
227 * first element of each pair is the filter string, and the second is the
228 * setting (allow/block).
230 ContentSettings.setExceptions = function(type, exceptions) {
231 this.getExceptionsList(type, 'normal').setExceptions(exceptions);
234 ContentSettings.setHandlers = function(handlers) {
235 $('handlers-list').setHandlers(handlers);
238 ContentSettings.setIgnoredHandlers = function(ignoredHandlers) {
239 $('ignored-handlers-list').setHandlers(ignoredHandlers);
242 ContentSettings.setOTRExceptions = function(type, otrExceptions) {
243 var exceptionsList = this.getExceptionsList(type, 'otr');
244 // Settings for Guest hides many sections, so check for null first.
245 if (exceptionsList) {
246 exceptionsList.parentNode.hidden = false;
247 exceptionsList.setExceptions(otrExceptions);
252 * @param {string} type The type of exceptions (e.g. "location") to get.
253 * @param {string} mode The mode of the desired exceptions list (e.g. otr).
254 * @return {?options.contentSettings.ExceptionsList} The corresponding
255 * exceptions list or null.
257 ContentSettings.getExceptionsList = function(type, mode) {
258 var exceptionsList = document.querySelector(
259 'div[contentType=' + type + '] list[mode=' + mode + ']');
260 return !exceptionsList ? null :
261 assertInstanceof(exceptionsList,
262 options.contentSettings.ExceptionsList);
266 * The browser's response to a request to check the validity of a given URL
267 * pattern.
268 * @param {string} type The content type.
269 * @param {string} mode The browser mode.
270 * @param {string} pattern The pattern.
271 * @param {boolean} valid Whether said pattern is valid in the context of
272 * a content exception setting.
274 ContentSettings.patternValidityCheckComplete =
275 function(type, mode, pattern, valid) {
276 this.getExceptionsList(type, mode).patternValidityCheckComplete(pattern,
277 valid);
281 * Shows/hides the link to the Pepper Flash camera and microphone default
282 * settings.
283 * Please note that whether the link is actually showed or not is also
284 * affected by the style class pepper-flash-settings.
286 ContentSettings.showMediaPepperFlashDefaultLink = function(show) {
287 $('media-pepper-flash-default').hidden = !show;
291 * Shows/hides the link to the Pepper Flash camera and microphone
292 * site-specific settings.
293 * Please note that whether the link is actually showed or not is also
294 * affected by the style class pepper-flash-settings.
296 ContentSettings.showMediaPepperFlashExceptionsLink = function(show) {
297 $('media-pepper-flash-exceptions').hidden = !show;
301 * Disable the 'allow' option in the Plugins section.
303 ContentSettings.disablePluginsAllowOption = function() {
304 this.pluginsAllowOptionDisabled = true;
305 $('plugins-allow-radio').disabled = true;
309 * Updates the microphone/camera devices menu with the given entries.
310 * @param {string} type The device type.
311 * @param {Array} devices List of available devices.
312 * @param {string} defaultdevice The unique id of the current default device.
314 ContentSettings.updateDevicesMenu = function(type, devices, defaultdevice) {
315 var deviceSelect = '';
316 if (type == 'mic') {
317 deviceSelect = $('media-select-mic');
318 } else if (type == 'camera') {
319 deviceSelect = $('media-select-camera');
320 } else {
321 console.error('Unknown device type for <device select> UI element: ' +
322 type);
323 return;
326 deviceSelect.textContent = '';
328 var deviceCount = devices.length;
329 var defaultIndex = -1;
330 for (var i = 0; i < deviceCount; i++) {
331 var device = devices[i];
332 var option = new Option(device.name, device.id);
333 if (option.value == defaultdevice)
334 defaultIndex = i;
335 deviceSelect.appendChild(option);
337 if (defaultIndex >= 0)
338 deviceSelect.selectedIndex = defaultIndex;
342 * Enables/disables the protected content exceptions button.
343 * @param {boolean} enable Whether to enable the button.
345 ContentSettings.enableProtectedContentExceptions = function(enable) {
346 var exceptionsButton = $('protected-content-exceptions');
347 if (exceptionsButton)
348 exceptionsButton.disabled = !enable;
352 * Set the default microphone device based on the popup selection.
353 * @private
355 ContentSettings.setDefaultMicrophone_ = function() {
356 var deviceSelect = $('media-select-mic');
357 chrome.send('setDefaultCaptureDevice', ['mic', deviceSelect.value]);
361 * Set the default camera device based on the popup selection.
362 * @private
364 ContentSettings.setDefaultCamera_ = function() {
365 var deviceSelect = $('media-select-camera');
366 chrome.send('setDefaultCaptureDevice', ['camera', deviceSelect.value]);
369 // Export
370 return {
371 ContentSettings: ContentSettings