content_settings.js revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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      // Exceptions lists. -----------------------------------------------------
33      function handleExceptionsLinkClickEvent(event) {
34        var exceptionsArea = event.target.parentNode.
35            querySelector('div[contentType][mode=normal]');
36        exceptionsArea.classList.toggle('hidden');
37        exceptionsArea.querySelector('list').redraw();
38
39        var otrExceptionsArea = event.target.parentNode.
40            querySelector('div[contentType][mode=otr]');
41        if (otrExceptionsArea && otrExceptionsArea.otrProfileExists) {
42          otrExceptionsArea.classList.toggle('hidden');
43          otrExceptionsArea.querySelector('list').redraw();
44        }
45
46        return false;
47      }
48      var exceptionsLinks =
49          this.pageDiv.querySelectorAll('.exceptionsLink');
50      for (var i = 0; i < exceptionsLinks.length; i++) {
51        exceptionsLinks[i].onclick = handleExceptionsLinkClickEvent;
52      }
53
54      var exceptionsAreas = this.pageDiv.querySelectorAll('div[contentType]');
55      for (var i = 0; i < exceptionsAreas.length; i++) {
56        options.contentSettings.ExceptionsArea.decorate(exceptionsAreas[i]);
57      }
58
59      // Cookies filter page ---------------------------------------------------
60      $('block-third-party-cookies').onclick = function(event) {
61        chrome.send('setAllowThirdPartyCookies',
62                    [String($('block-third-party-cookies').checked)]);
63      };
64
65      $('show-cookies-button').onclick = function(event) {
66        chrome.send('coreOptionsUserMetricsAction', ['Options_ShowCookies']);
67        OptionsPage.showPageByName('cookiesView');
68      };
69    },
70
71    /**
72     * Handles a hash value in the URL (such as bar in
73     * chrome://options/foo#bar). Overrides the default action of showing an
74     * overlay by instead navigating to a particular subtab.
75     * @param {string} hash The hash value.
76     */
77    handleHash: function(hash) {
78      OptionsPage.showTab($(hash + '-nav-tab'));
79    },
80  };
81
82  /**
83   * Sets the values for all the content settings radios.
84   * @param {Object} dict A mapping from radio groups to the checked value for
85   *     that group.
86   */
87  ContentSettings.setContentFilterSettingsValue = function(dict) {
88    for (var group in dict) {
89      document.querySelector('input[type=radio][name=' + group +
90                             '][value=' + dict[group] + ']').checked = true;
91    }
92  };
93
94  /**
95   * Initializes an exceptions list.
96   * @param {string} type The content type that we are setting exceptions for.
97   * @param {Array} list An array of pairs, where the first element of each pair
98   *     is the filter string, and the second is the setting (allow/block).
99   */
100  ContentSettings.setExceptions = function(type, list) {
101    var exceptionsList =
102        document.querySelector('div[contentType=' + type + ']' +
103                               '[mode=normal] list');
104    exceptionsList.clear();
105    for (var i = 0; i < list.length; i++) {
106      exceptionsList.addException(list[i]);
107    }
108  };
109
110  ContentSettings.setOTRExceptions = function(type, list) {
111    var exceptionsArea =
112        document.querySelector('div[contentType=' + type + '][mode=otr]');
113    exceptionsArea.otrProfileExists = true;
114
115    var exceptionsList = exceptionsArea.querySelector('list');
116    exceptionsList.clear();
117    for (var i = 0; i < list.length; i++) {
118      exceptionsList.addException(list[i]);
119    }
120  };
121
122  /**
123   * Sets the initial value for the Third Party Cookies checkbox.
124   * @param {boolean=} block True if we are blocking third party cookies.
125   */
126  ContentSettings.setBlockThirdPartyCookies = function(block) {
127    $('block-third-party-cookies').checked = block;
128  };
129
130  /**
131   * The browser's response to a request to check the validity of a given URL
132   * pattern.
133   * @param {string} type The content type.
134   * @param {string} mode The browser mode.
135   * @param {string} pattern The pattern.
136   * @param {bool} valid Whether said pattern is valid in the context of
137   *     a content exception setting.
138   */
139  ContentSettings.patternValidityCheckComplete =
140      function(type, mode, pattern, valid) {
141    var exceptionsList =
142        document.querySelector('div[contentType=' + type + '][mode=' + mode +
143                               '] list');
144    exceptionsList.patternValidityCheckComplete(pattern, valid);
145  };
146
147  // Export
148  return {
149    ContentSettings: ContentSettings
150  };
151
152});
153
154