content_settings.js revision 201ade2fbba22bfb27ae029f4d23fca6ded109a0
1// Copyright (c) 2010 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
5cr.define('options', function() {
6
7  var OptionsPage = options.OptionsPage;
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    OptionsPage.call(this, 'content', templateData.contentSettingsPage,
19                     'contentSettingsPage');
20  }
21
22  cr.addSingletonGetter(ContentSettings);
23
24  ContentSettings.prototype = {
25    __proto__: OptionsPage.prototype,
26
27    initializePage: function() {
28      OptionsPage.prototype.initializePage.call(this);
29
30      chrome.send('getContentFilterSettings');
31
32      var exceptionsAreas = this.pageDiv.querySelectorAll('div[contentType]');
33      for (var i = 0; i < exceptionsAreas.length; i++) {
34        options.contentSettings.ExceptionsArea.decorate(exceptionsAreas[i]);
35      }
36
37      cr.ui.decorate('.zippy', options.Zippy);
38      this.pageDiv.addEventListener('measure', function(e) {
39        if (e.target.classList.contains('zippy')) {
40          var lists = e.target.querySelectorAll('list');
41          for (var i = 0; i < lists.length; i++) {
42            if (lists[i].redraw) {
43              lists[i].redraw();
44            }
45          }
46        }
47      });
48
49      // Cookies filter page ---------------------------------------------------
50      $('block-third-party-cookies').onclick = function(event) {
51        chrome.send('setAllowThirdPartyCookies',
52                    [String($('block-third-party-cookies').checked)]);
53      };
54
55      $('show-cookies-button').onclick = function(event) {
56        chrome.send('coreOptionsUserMetricsAction', ['Options_ShowCookies']);
57        OptionsPage.showPageByName('cookiesView');
58      };
59
60      $('plugins-tab').onclick = function(event) {
61        chrome.send('openPluginsTab');
62      };
63
64      if (!templateData.enable_click_to_play)
65        $('click_to_play').style.display = 'none';
66    },
67
68    /**
69     * Handles a hash value in the URL (such as bar in
70     * chrome://options/foo#bar). Overrides the default action of showing an
71     * overlay by instead navigating to a particular subtab.
72     * @param {string} hash The hash value.
73     */
74    handleHash: function(hash) {
75      OptionsPage.showTab($(hash + '-nav-tab'));
76    },
77  };
78
79  /**
80   * Sets the values for all the content settings radios.
81   * @param {Object} dict A mapping from radio groups to the checked value for
82   *     that group.
83   */
84  ContentSettings.setContentFilterSettingsValue = function(dict) {
85    for (var group in dict) {
86      document.querySelector('input[type=radio][name=' + group +
87                             '][value=' + dict[group] + ']').checked = true;
88    }
89  };
90
91  /**
92   * Initializes an exceptions list.
93   * @param {string} type The content type that we are setting exceptions for.
94   * @param {Array} list An array of pairs, where the first element of each pair
95   *     is the filter string, and the second is the setting (allow/block).
96   */
97  ContentSettings.setExceptions = function(type, list) {
98    var exceptionsList =
99        document.querySelector('div[contentType=' + type + ']' +
100                               '[mode=normal] list');
101    exceptionsList.clear();
102    for (var i = 0; i < list.length; i++) {
103      exceptionsList.addException(list[i]);
104    }
105  };
106
107  ContentSettings.setOTRExceptions = function(type, list) {
108    var exceptionsArea =
109        document.querySelector('div[contentType=' + type + '][mode=otr]');
110    exceptionsArea.otrProfileExists = true;
111
112    // Find the containing zippy, set it to show OTR profiles, and remeasure it
113    // to make it smoothly animate to the new size.
114    var zippy = exceptionsArea;
115    while (zippy &&
116           (!zippy.classList || !zippy.classList.contains('zippy'))) {
117      zippy = zippy.parentNode;
118    }
119    if (zippy) {
120      zippy.classList.add('show-otr');
121      zippy.remeasure();
122    }
123
124    var exceptionsList = exceptionsArea.querySelector('list');
125    exceptionsList.clear();
126    for (var i = 0; i < list.length; i++) {
127      exceptionsList.addException(list[i]);
128    }
129
130    // If an OTR table is added while the normal exceptions area is already
131    // showing (because the exceptions area is already expanded), then show
132    // the new OTR table.
133    var parentExceptionsArea =
134        document.querySelector('div[contentType=' + type + '][mode=normal]');
135    if (!parentExceptionsArea.classList.contains('hidden')) {
136      exceptionsArea.querySelector('list').redraw();
137    }
138  };
139
140  /**
141   * Clears and hides the incognito exceptions lists.
142   */
143  ContentSettings.OTRProfileDestroyed = function() {
144    // Find all zippies, set them to hide OTR profiles, and remeasure them
145    // to make them smoothly animate to the new size.
146    var zippies = document.querySelectorAll('.zippy');
147    for (var i = 0; i < zippies.length; i++) {
148      zippies[i].classList.remove('show-otr');
149      zippies[i].remeasure();
150    }
151
152    var exceptionsAreas =
153        document.querySelectorAll('div[contentType][mode=otr]');
154    for (var i = 0; i < exceptionsAreas.length; i++) {
155      exceptionsAreas[i].otrProfileExists = false;
156      exceptionsAreas[i].querySelector('list').clear();
157    }
158  };
159
160  /**
161   * Sets the initial value for the Third Party Cookies checkbox.
162   * @param {boolean=} block True if we are blocking third party cookies.
163   */
164  ContentSettings.setBlockThirdPartyCookies = function(block) {
165    $('block-third-party-cookies').checked = block;
166  };
167
168  /**
169   * The browser's response to a request to check the validity of a given URL
170   * pattern.
171   * @param {string} type The content type.
172   * @param {string} mode The browser mode.
173   * @param {string} pattern The pattern.
174   * @param {bool} valid Whether said pattern is valid in the context of
175   *     a content exception setting.
176   */
177  ContentSettings.patternValidityCheckComplete =
178      function(type, mode, pattern, valid) {
179    var exceptionsList =
180        document.querySelector('div[contentType=' + type + '][mode=' + mode +
181                               '] list');
182    exceptionsList.patternValidityCheckComplete(pattern, valid);
183  };
184
185  // Export
186  return {
187    ContentSettings: ContentSettings
188  };
189
190});
191