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