content_settings.js revision 513209b27ff55e2841eac0e4120199c23acce758
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
65    /**
66     * Handles a hash value in the URL (such as bar in
67     * chrome://options/foo#bar). Overrides the default action of showing an
68     * overlay by instead navigating to a particular subtab.
69     * @param {string} hash The hash value.
70     */
71    handleHash: function(hash) {
72      OptionsPage.showTab($(hash + '-nav-tab'));
73    },
74  };
75
76  /**
77   * Sets the values for all the content settings radios.
78   * @param {Object} dict A mapping from radio groups to the checked value for
79   *     that group.
80   */
81  ContentSettings.setContentFilterSettingsValue = function(dict) {
82    for (var group in dict) {
83      document.querySelector('input[type=radio][name=' + group +
84                             '][value=' + dict[group] + ']').checked = true;
85    }
86  };
87
88  /**
89   * Initializes an exceptions list.
90   * @param {string} type The content type that we are setting exceptions for.
91   * @param {Array} list An array of pairs, where the first element of each pair
92   *     is the filter string, and the second is the setting (allow/block).
93   */
94  ContentSettings.setExceptions = function(type, list) {
95    var exceptionsList =
96        document.querySelector('div[contentType=' + type + ']' +
97                               '[mode=normal] list');
98    exceptionsList.clear();
99    for (var i = 0; i < list.length; i++) {
100      exceptionsList.addException(list[i]);
101    }
102  };
103
104  ContentSettings.setOTRExceptions = function(type, list) {
105    var exceptionsArea =
106        document.querySelector('div[contentType=' + type + '][mode=otr]');
107    exceptionsArea.otrProfileExists = true;
108
109    // Find the containing zippy, set it to show OTR profiles, and remeasure it
110    // to make it smoothly animate to the new size.
111    var zippy = exceptionsArea;
112    while (zippy &&
113           (!zippy.classList || !zippy.classList.contains('zippy'))) {
114      zippy = zippy.parentNode;
115    }
116    if (zippy) {
117      zippy.classList.add('show-otr');
118      zippy.remeasure();
119    }
120
121    var exceptionsList = exceptionsArea.querySelector('list');
122    exceptionsList.clear();
123    for (var i = 0; i < list.length; i++) {
124      exceptionsList.addException(list[i]);
125    }
126
127    // If an OTR table is added while the normal exceptions area is already
128    // showing (because the exceptions area is already expanded), then show
129    // the new OTR table.
130    var parentExceptionsArea =
131        document.querySelector('div[contentType=' + type + '][mode=normal]');
132    if (!parentExceptionsArea.classList.contains('hidden')) {
133      exceptionsArea.querySelector('list').redraw();
134    }
135  };
136
137  /**
138   * Clears and hides the incognito exceptions lists.
139   */
140  ContentSettings.OTRProfileDestroyed = function() {
141    // Find all zippies, set them to hide OTR profiles, and remeasure them
142    // to make them smoothly animate to the new size.
143    var zippies = document.querySelectorAll('.zippy');
144    for (var i = 0; i < zippies.length; i++) {
145      zippies[i].classList.remove('show-otr');
146      zippies[i].remeasure();
147    }
148
149    var exceptionsAreas =
150        document.querySelectorAll('div[contentType][mode=otr]');
151    for (var i = 0; i < exceptionsAreas.length; i++) {
152      exceptionsAreas[i].otrProfileExists = false;
153      exceptionsAreas[i].querySelector('list').clear();
154    }
155  };
156
157  /**
158   * Sets the initial value for the Third Party Cookies checkbox.
159   * @param {boolean=} block True if we are blocking third party cookies.
160   */
161  ContentSettings.setBlockThirdPartyCookies = function(block) {
162    $('block-third-party-cookies').checked = block;
163  };
164
165  /**
166   * The browser's response to a request to check the validity of a given URL
167   * pattern.
168   * @param {string} type The content type.
169   * @param {string} mode The browser mode.
170   * @param {string} pattern The pattern.
171   * @param {bool} valid Whether said pattern is valid in the context of
172   *     a content exception setting.
173   */
174  ContentSettings.patternValidityCheckComplete =
175      function(type, mode, pattern, valid) {
176    var exceptionsList =
177        document.querySelector('div[contentType=' + type + '][mode=' + mode +
178                               '] list');
179    exceptionsList.patternValidityCheckComplete(pattern, valid);
180  };
181
182  // Export
183  return {
184    ContentSettings: ContentSettings
185  };
186
187});
188