1// Copyright (c) 2011 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.contentSettingsPageTabTitle,
19                     'content-settings-page');
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 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          page.showList(
38              event.target.getAttribute('contentType'));
39          OptionsPage.navigateToPage('contentExceptions');
40          // Add on the proper hash for the content type, and store that in the
41          // history so back/forward and tab restore works.
42          var hash = event.target.getAttribute('contentType');
43          window.history.replaceState({pageName: page.name}, page.title,
44                                      '/' + page.name + "#" + hash);
45        };
46      }
47
48      // Cookies filter page ---------------------------------------------------
49      $('block-third-party-cookies').onclick = function(event) {
50        chrome.send('setAllowThirdPartyCookies',
51                    [String($('block-third-party-cookies').checked)]);
52      };
53
54      $('show-cookies-button').onclick = function(event) {
55        chrome.send('coreOptionsUserMetricsAction', ['Options_ShowCookies']);
56        OptionsPage.navigateToPage('cookies');
57      };
58
59      if (!templateData.enable_click_to_play)
60        $('click_to_play').style.display = 'none';
61    },
62  };
63
64  /**
65   * Sets the values for all the content settings radios.
66   * @param {Object} dict A mapping from radio groups to the checked value for
67   *     that group.
68   */
69  ContentSettings.setContentFilterSettingsValue = function(dict) {
70    for (var group in dict) {
71      document.querySelector('input[type=radio][name=' + group + '][value=' +
72                             dict[group]['value'] + ']').checked = true;
73      var radios = document.querySelectorAll('input[type=radio][name=' +
74                                             group + ']');
75      for (var i = 0, len = radios.length; i < len; i++) {
76        radios[i].disabled = dict[group]['managed'];
77        radios[i].managed = dict[group]['managed'];
78      }
79    }
80    OptionsPage.updateManagedBannerVisibility();
81  };
82
83  /**
84   * Initializes an exceptions list.
85   * @param {string} type The content type that we are setting exceptions for.
86   * @param {Array} list An array of pairs, where the first element of each pair
87   *     is the filter string, and the second is the setting (allow/block).
88   */
89  ContentSettings.setExceptions = function(type, list) {
90    var exceptionsList =
91        document.querySelector('div[contentType=' + type + ']' +
92                               ' list[mode=normal]');
93
94    exceptionsList.setExceptions(list);
95  };
96
97  ContentSettings.setOTRExceptions = function(type, list) {
98    var exceptionsList =
99        document.querySelector('div[contentType=' + type + ']' +
100                               ' list[mode=otr]');
101
102    exceptionsList.parentNode.classList.remove('hidden');
103    exceptionsList.setExceptions(list);
104  };
105
106  /**
107   * Sets the initial value for the Third Party Cookies checkbox.
108   * @param {boolean=} block True if we are blocking third party cookies.
109   */
110  ContentSettings.setBlockThirdPartyCookies = function(block) {
111    $('block-third-party-cookies').checked = block;
112  };
113
114  /**
115   * The browser's response to a request to check the validity of a given URL
116   * pattern.
117   * @param {string} type The content type.
118   * @param {string} mode The browser mode.
119   * @param {string} pattern The pattern.
120   * @param {bool} valid Whether said pattern is valid in the context of
121   *     a content exception setting.
122   */
123  ContentSettings.patternValidityCheckComplete =
124      function(type, mode, pattern, valid) {
125    var exceptionsList =
126        document.querySelector('div[contentType=' + type + '] ' +
127                               'list[mode=' + mode + ']');
128    exceptionsList.patternValidityCheckComplete(pattern, valid);
129  };
130
131  // Export
132  return {
133    ContentSettings: ContentSettings
134  };
135
136});
137