1// Copyright 2014 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.WebsiteSettings', function() {
6  /** @const */ var Page = cr.ui.pageManager.Page;
7
8  /////////////////////////////////////////////////////////////////////////////
9  // WebsiteSettingsEditor class:
10
11  /**
12   * Encapsulated handling of the website settings editor page.
13   * @constructor
14   * @extends {cr.ui.pageManager.Page}
15   */
16  function WebsiteSettingsEditor() {
17    Page.call(this, 'websiteEdit',
18                     loadTimeData.getString('websitesOptionsPageTabTitle'),
19                     'website-settings-edit-page');
20    this.permissions = ['geolocation', 'notifications', 'media-stream',
21                        'cookies', 'multiple-automatic-downloads', 'images',
22                        'plugins', 'popups', 'javascript'];
23    this.permissionsLookup = {
24      'geolocation': 'Location',
25      'notifications': 'Notifications',
26      'media-stream': 'MediaStream',
27      'cookies': 'Cookies',
28      'multiple-automatic-downloads': 'Downloads',
29      'images': 'Images',
30      'plugins': 'Plugins',
31      'popups': 'Popups',
32      'javascript': 'Javascript'
33    };
34  }
35
36  cr.addSingletonGetter(WebsiteSettingsEditor);
37
38  WebsiteSettingsEditor.prototype = {
39    __proto__: Page.prototype,
40
41
42    /** @override */
43    initializePage: function() {
44      Page.prototype.initializePage.call(this);
45
46      $('website-settings-storage-delete-button').onclick = function(event) {
47        chrome.send('deleteLocalStorage');
48      };
49
50      $('website-settings-battery-stop-button').onclick = function(event) {
51        chrome.send('stopOrigin');
52      };
53
54      $('websiteSettingsEditorCancelButton').onclick =
55          PageManager.closeOverlay.bind(PageManager);
56
57      $('websiteSettingsEditorDoneButton').onclick = function(event) {
58        WebsiteSettingsEditor.getInstance().updatePermissions();
59        PageManager.closeOverlay.bind(PageManager)();
60      };
61
62      var permissionList =
63          this.pageDiv.querySelector('.origin-permission-list');
64      for (var key in this.permissions) {
65        permissionList.appendChild(
66            this.makePermissionOption_(this.permissions[key]));
67      }
68    },
69
70    /**
71     * Populates the page with the proper information for a given URL.
72     * @param {string} url The URL of the page.
73     * @private
74     */
75    populatePage: function(url) {
76        this.url = url;
77
78        var titleEl = $('website-title');
79        titleEl.textContent = url;
80        titleEl.style.backgroundImage = getFaviconImageSet(url);
81
82        chrome.send('getOriginInfo', [url]);
83    },
84
85    /**
86     * Populates and displays the page with given origin information.
87     * @param {string} localStorage A string describing the local storage use.
88     * @param {string} batteryUsage A string describing the battery use.
89     * @param {Object} permissions A dictionary of permissions to their
90     *     available and current settings, and if it is editable.
91     * @param {boolean} showPage If the page should raised.
92     * @private
93     */
94    populateOrigin_: function(localStorage, batteryUsage, permissions,
95        showPage) {
96      $('local-storage-title').textContent = localStorage;
97      $('battery-title').textContent = batteryUsage;
98      for (var key in permissions) {
99        var selector = $(key + '-select-option');
100
101        var options = permissions[key].options;
102        selector.options.length = 0;
103        for (var option in options) {
104          selector.options[selector.options.length] =
105              new Option(loadTimeData.getString(options[option] + 'Exception'),
106                  options[option]);
107        }
108
109        selector.value = permissions[key].setting;
110        selector.originalValue = permissions[key].setting;
111        selector.disabled = !permissions[key].editable;
112      }
113      if (showPage)
114        PageManager.showPageByName('websiteEdit', false);
115    },
116
117    updatePermissions: function() {
118      for (var key in this.permissions) {
119        var selection = $(this.permissions[key] + '-select-option');
120        if (selection.value != selection.originalValue) {
121          chrome.send('setOriginPermission',
122              [this.permissions[key], selection.value]);
123        }
124      }
125    },
126
127    /**
128     * Populates the origin permission list with the different usable
129     * permissions.
130     * @param {string} permissionName A string with the permission name.
131     * @return {Element} The element with the usable permission setting.
132     */
133    makePermissionOption_: function(permissionName) {
134      var permissionOption = cr.doc.createElement('div');
135      permissionOption.className = 'permission-option';
136
137      var permissionNameSpan = cr.doc.createElement('span');
138      permissionNameSpan.className = 'permission-name';
139      permissionNameSpan.textContent = loadTimeData.getString('websites' +
140          this.permissionsLookup[permissionName] + 'Description');
141      permissionOption.appendChild(permissionNameSpan);
142
143      var permissionSelector = cr.doc.createElement('select');
144      permissionSelector.setAttribute('id', permissionName + '-select-option');
145      permissionSelector.className = 'weaktrl permission-selection-option';
146      permissionOption.appendChild(permissionSelector);
147      return permissionOption;
148    },
149  };
150
151  WebsiteSettingsEditor.populateOrigin = function(localStorage, batteryUsage,
152      permissions, showPage) {
153    WebsiteSettingsEditor.getInstance().populateOrigin_(localStorage,
154                                                        batteryUsage,
155                                                        permissions,
156                                                        showPage);
157  };
158
159  WebsiteSettingsEditor.showEditPage = function(url) {
160    WebsiteSettingsEditor.getInstance().populatePage(url);
161  };
162
163  // Export
164  return {
165    WebsiteSettingsEditor: WebsiteSettingsEditor
166  };
167
168});
169