Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / website_settings.js
index c096d07..0964cf5 100644 (file)
@@ -2,17 +2,33 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-cr.define('options', function() {
+cr.define('options.ContentSettings', function() {
   /** @const */ var Page = cr.ui.pageManager.Page;
   /** @const */ var PageManager = cr.ui.pageManager.PageManager;
   /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel;
 
+  // Lookup table to generate the i18n strings.
+  /** @const */ var permissionsLookup = {
+    'geolocation': 'location',
+    'notifications': 'notifications',
+    'media-stream': 'mediaStream',
+    'cookies': 'cookies',
+    'multiple-automatic-downloads': 'multipleAutomaticDownloads',
+    'images': 'images',
+    'plugins': 'plugins',
+    'popups': 'popups',
+    'javascript': 'javascript',
+    'battery': 'battery',
+    'storage': 'storage'
+  };
+
   /////////////////////////////////////////////////////////////////////////////
   // WebsiteSettingsManager class:
 
   /**
    * Encapsulated handling of the website settings page.
    * @constructor
+   * @extends {cr.ui.pageManager.Page}
    */
   function WebsiteSettingsManager() {
     Page.call(this, 'websiteSettings',
@@ -26,11 +42,18 @@ cr.define('options', function() {
     __proto__: Page.prototype,
 
     /**
-     * The saved origins list.
-     * @type {OriginList}
+     * The saved allowed origins list.
+     * @type {options.OriginList}
      * @private
      */
-    originList_: null,
+    allowedList_: null,
+
+    /**
+     * The saved blocked origins list.
+     * @type {options.OriginList}
+     * @private
+     */
+    blockedList_: null,
 
     /** @override */
     initializePage: function() {
@@ -39,13 +62,13 @@ cr.define('options', function() {
       $('website-settings-overlay-confirm').onclick =
           PageManager.closeOverlay.bind(PageManager);
 
-      $('resourceType').onchange = function() {
-        var target = event.target;
-        assert(target.tagName == 'SELECT');
-        if (target.value == 'storage')
-          chrome.send('updateLocalStorage');
-        else
-          chrome.send('updateOrigins', [target.value]);
+      $('global-setting').onchange = function(event) {
+        chrome.send('setDefaultContentSetting', [this.value]);
+      };
+
+      $('global-setting-toggle').onchange = function(event) {
+        var value = event.target.checked;
+        chrome.send('setGlobalEnabled', [value]);
       };
 
       var searchBox = $('website-settings-search-box');
@@ -58,7 +81,20 @@ cr.define('options', function() {
       };
 
       this.createOriginsList_();
-      chrome.send('updateOrigins', ['geolocation']);
+      this.updatePage_('geolocation');
+    },
+
+    /**
+     * Called after the page has been shown. Show the content settings or
+     * resource auditing for the location's hash.
+     */
+    didShowPage: function() {
+      var hash = this.hash;
+      if (hash)
+        hash = hash.slice(1);
+      else
+        hash = 'geolocation';
+      this.updatePage_(hash);
     },
 
     /**
@@ -66,19 +102,26 @@ cr.define('options', function() {
      * @private
      */
     createOriginsList_: function() {
-      this.originList_ = this.pageDiv.querySelector('.origin-list');
-      options.OriginList.decorate(this.originList_);
-      this.originList_.autoExpands = true;
+      var allowedList = $('allowed-origin-list');
+      options.OriginList.decorate(allowedList);
+      this.allowedList_ = assertInstanceof(allowedList, options.OriginList);
+      this.allowedList_.autoExpands = true;
+
+      var blockedList = $('blocked-origin-list');
+      options.OriginList.decorate(blockedList);
+      this.blockedList_ = assertInstanceof(blockedList, options.OriginList);
+      this.blockedList_.autoExpands = true;
     },
 
     /**
-     * Populate the origin list with all of the origins with a given permission
+     * Populate an origin list with all of the origins with a given permission
      * or that are using a given resource.
+     * @param {options.OriginList} originList A list to populate.
      * @param {!Object} originDict A dictionary of origins to their usage, which
            will be used to sort the origins.
      * @private
      */
-    populateOrigins_: function(originDict) {
+    populateOriginsHelper_: function(originList, originDict) {
       var origins = Object.keys(originDict).map(function(origin) {
         // |usage| means the time of last usage in seconds since epoch
         // (Jan 1, 1970) for permissions and means the amount of local storage
@@ -86,13 +129,43 @@ cr.define('options', function() {
         return {
           origin: origin,
           usage: originDict[origin].usage,
-          usageString: originDict[origin].usageString
+          usageString: originDict[origin].usageString,
+          readableName: originDict[origin].readableName,
         };
       });
       origins.sort(function(first, second) {
         return second.usage - first.usage;
       });
-      this.originList_.dataModel = new ArrayDataModel(origins);
+      originList.dataModel = new ArrayDataModel(origins);
+    },
+
+
+    /**
+     * Populate the origin lists with all of the origins with a given permission
+     * or that are using a given resource, potentially split by if allowed or
+     * denied. If no blocked dictionary is provided, only the allowed list is
+     * shown.
+     * @param {!Object} allowedDict A dictionary of origins to their usage,
+           which will be used to sort the origins in the main/allowed list.
+     * @param {!Object} blockedDict An optional dictionary of origins to their
+           usage, which will be used to sort the origins in the blocked list.
+     * @param {bool} isGloballyEnabled If the content setting is turned on.
+     * @private
+     */
+    populateOrigins: function(allowedDict, blockedDict, isGloballyEnabled) {
+      this.populateOriginsHelper_(this.allowedList_, allowedDict);
+      if (blockedDict) {
+        this.populateOriginsHelper_(this.blockedList_, blockedDict);
+        this.blockedList_.hidden = false;
+        $('blocked-origin-list-title').hidden = false;
+        this.allowedList_.classList.remove('nonsplit-origin-list');
+      } else {
+        this.blockedList_.hidden = true;
+        $('blocked-origin-list-title').hidden = true;
+        $('allowed-origin-list-title').hidden = true;
+        this.allowedList_.classList.add('nonsplit-origin-list');
+      }
+      $('global-setting-toggle').checked = isGloballyEnabled;
     },
 
     /**
@@ -110,17 +183,84 @@ cr.define('options', function() {
      * @param {!Event} e The event object.
      * @private
      */
-    handleSearchQueryChange_: function() {
+    handleSearchQueryChange_: function(e) {
       if (this.queryDelayTimerId_)
         window.clearTimeout(this.queryDelayTimerId_);
 
       this.queryDelayTimerId_ = window.setTimeout(this.searchOrigins.bind(this),
                                                   160);
     },
+
+    /**
+     * Sets the default content setting dropdown on the page to the current
+     * default.
+     * @param {!Object} dict A dictionary with the management and value of the
+     *     default setting for the last selected content setting..
+     */
+    updateDefault: function(dict) {
+      // TODO(dhnishi): Remove duplicate default handling in the Content
+      //     Settings page and here.
+      var managedBy = dict.managedBy;
+      var controlledBy = managedBy == 'policy' || managedBy == 'extension' ?
+                managedBy : null;
+      $('global-setting').disabled = (managedBy != 'default');
+
+      var options = $('global-setting').options;
+      for (var i = 0; i < options.length; i++) {
+        if (options[i].value == dict.value) {
+          options.selectedIndex = i;
+        }
+      }
+    },
+
+    /**
+     * Updates the page with the given content setting or resource name's
+     * information.
+     * @param {string} typeName The name of the content setting or resource.
+     */
+    updatePage_: function(typeName) {
+      if (typeName == 'storage')
+        chrome.send('updateLocalStorage');
+      else if (typeName == 'battery')
+        chrome.send('updateBatteryUsage');
+      else
+        chrome.send('updateOrigins', [typeName]);
+
+      var options = $('global-setting').options;
+      options.length = 0;
+      var permissionString = permissionsLookup[typeName];
+      var permissions = ['Allow', 'Ask', 'Block'];
+      for (var i = 0; i < permissions.length; i++) {
+        var valueId = permissionString + permissions[i];
+        if (loadTimeData.valueExists(valueId)) {
+          options.add(new Option(loadTimeData.getString(valueId),
+              permissions[i].toLowerCase()));
+        }
+      }
+      if (options.length == 0) {
+        $('website-settings-global-controls').hidden = true;
+      } else {
+        $('website-settings-global-controls').hidden = false;
+        chrome.send('updateDefaultSetting');
+      }
+
+      $('website-settings-title').textContent =
+          loadTimeData.getString(permissionString + 'TabLabel');
+    }
+  };
+
+  WebsiteSettingsManager.populateOrigins = function(allowedDict, blockedDict,
+      isGloballyEnabled) {
+    WebsiteSettingsManager.getInstance().populateOrigins(allowedDict,
+        blockedDict, isGloballyEnabled);
+  };
+
+  WebsiteSettingsManager.updateDefault = function(dict) {
+    WebsiteSettingsManager.getInstance().updateDefault(dict);
   };
 
-  WebsiteSettingsManager.populateOrigins = function(originDict) {
-    WebsiteSettingsManager.getInstance().populateOrigins_(originDict);
+  WebsiteSettingsManager.showWebsiteSettings = function(hash) {
+    PageManager.showPageByName('websiteSettings', true, {hash: '#' + hash});
   };
 
   // Export