Chromium Code Reviews| Index: chrome/browser/resources/settings/site_settings/site_settings_behavior.js |
| diff --git a/chrome/browser/resources/settings/site_settings/site_settings_behavior.js b/chrome/browser/resources/settings/site_settings/site_settings_behavior.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ecfbdb1e3a3bf1581f0126d5cb32c7a1255483ac |
| --- /dev/null |
| +++ b/chrome/browser/resources/settings/site_settings/site_settings_behavior.js |
| @@ -0,0 +1,212 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * @fileoverview Behavior common to Site Settings classes. |
| + */ |
| + |
| +/** @polymerBehavior */ |
| +var SiteSettingsBehavior = { |
| + /** |
| + * Gets the pref at the given prefPath. Throws if the pref is not found. |
| + * @param {string} prefPath |
| + * @return {!chrome.settingsPrivate.PrefObject} |
| + * @private |
| + */ |
| + getPref_: function(prefPath) { |
|
michaelpg
2015/10/22 16:11:34
extract into separate behavior for this and settin
Finnur
2015/10/26 14:38:05
It is a bit unfortunate that behaviors can't inher
michaelpg
2015/10/26 22:15:22
You could override getPref_. Each behavior overrid
Finnur
2015/10/27 11:30:48
I suspect you are suggestion a solution to a diffe
|
| + var pref = /** @type {!chrome.settingsPrivate.PrefObject} */( |
| + this.get(prefPath, this.prefs)); |
| + assert(typeof pref != 'undefined', 'Pref is missing: ' + prefPath); |
| + return pref; |
| + }, |
| + |
| + /** |
| + * Sets the value of the pref at the given prefPath. Throws if the pref is not |
| + * found. |
| + * @param {string} prefPath |
| + * @param {*} value |
| + * @private |
| + */ |
| + setPrefValue_: function(prefPath, value) { |
|
michaelpg
2015/10/22 16:11:34
same
Finnur
2015/10/26 14:38:05
Done.
|
| + this.set('prefs.' + prefPath + '.value', value); |
|
michaelpg
2015/10/22 16:11:34
contradictory to the comment, this doesn't throw i
Finnur
2015/10/26 14:38:05
Done.
|
| + }, |
| + |
| + /** |
| + * Returns whether the category default is set to enabled or not. |
| + * @param {number} category The category to check. |
| + * @return {boolean} True if the category default is set to enabled. |
| + * @private |
| + */ |
| + isPrefEnabled_: function(category) { |
| + var pref = this.getPref_(this.computeTogglePrefName_(this.category)); |
| + |
| + // FullScreen is Allow vs. Ask. |
| + if (category == settings.ContentSettingsTypes.FULLSCREEN) |
| + return pref.value != settings.DefaultValues.ALLOW; |
| + |
| + return pref.value != settings.DefaultValues.BLOCK; |
| + }, |
| + |
| + /** |
| + * A utility function to compute the icon to use for the category. |
| + * @param {number} category The category to show the icon for. |
| + * @return {string} The id of the icon for the given category. |
| + * @private |
| + */ |
| + computeIcon_: function(category) { |
| + // Wonder if any of these enum values are directly accessible from .js? |
|
michaelpg
2015/10/22 16:11:34
make a TODO or remove comment (i'm not sure what t
Finnur
2015/10/26 14:38:05
Removed (the todo is kind of covered already in co
|
| + switch (category) { |
| + case settings.ContentSettingsTypes.COOKIES: |
| + return ''; // Haven't found a good cookies icon under iron-icons. |
| + case settings.ContentSettingsTypes.JAVASCRIPT: |
| + return 'icons:input'; |
| + case settings.ContentSettingsTypes.FULLSCREEN: |
| + return 'icons:fullscreen'; |
| + case settings.ContentSettingsTypes.POPUPS: |
| + return 'icons:open-in-new'; |
| + case settings.ContentSettingsTypes.GEOLOCATION: |
| + return 'communication:location-on'; |
| + case settings.ContentSettingsTypes.NOTIFICATION: |
| + return 'social:notifications'; |
| + case settings.ContentSettingsTypes.CAMERA: |
| + return 'av:videocam'; |
| + case settings.ContentSettingsTypes.MIC: |
| + return 'av:mic'; |
| + default: |
| + assertNotReached(); |
| + return ''; |
| + } |
| + }, |
| + |
| + /** |
| + * A utility function to compute the title of the category. |
| + * @param {number} category The category to show the title for. |
| + * @return {string} The title for the given category. |
| + * @private |
| + */ |
| + computeTitle_: function(category) { |
| + switch (category) { |
| + case settings.ContentSettingsTypes.COOKIES: |
| + return loadTimeData.getString('siteSettingsCookies'); |
| + case settings.ContentSettingsTypes.JAVASCRIPT: |
| + return loadTimeData.getString('siteSettingsJavascript'); |
| + case settings.ContentSettingsTypes.FULLSCREEN: |
| + return loadTimeData.getString('siteSettingsFullscreen'); |
| + case settings.ContentSettingsTypes.POPUPS: |
| + return loadTimeData.getString('siteSettingsPopups'); |
| + case settings.ContentSettingsTypes.GEOLOCATION: |
| + return loadTimeData.getString('siteSettingsLocation'); |
| + case settings.ContentSettingsTypes.NOTIFICATION: |
| + return loadTimeData.getString('siteSettingsNotifications'); |
| + case settings.ContentSettingsTypes.CAMERA: |
| + return loadTimeData.getString('siteSettingsCamera'); |
| + case settings.ContentSettingsTypes.MIC: |
| + return loadTimeData.getString('siteSettingsMic'); |
| + default: |
| + assertNotReached(); |
| + return ''; |
| + } |
| + }, |
| + |
| + /** |
| + * A utility function to compute the name of the pref for the category. |
| + * @param {number} category The category to find the pref name for. |
| + * @return {string} The pref name for the given category. |
| + * @private |
| + */ |
| + computeTogglePrefName_: function(category) { |
| + return 'profile.default_content_setting_values.' + |
| + this.computeCategorySuffix_(category); |
| + }, |
| + |
| + /** |
| + * A utility function to compute the name of the pref for the exceptions |
| + * for a given category. |
| + * @param {number} category The category to find the pref name for. |
| + * @return {string} The pref name for the given category exceptions. |
| + * @private |
| + */ |
| + computeExceptionsPrefName_: function(category) { |
| + return 'profile.content_settings.exceptions.' + |
| + this.computeCategorySuffix_(category); |
| + }, |
| + |
| + /** |
| + * A utility function to convert the category enum into its text |
| + * representation, for use with prefs. |
| + * @param {number} category The category to find the pref name for. |
| + * @return {string} The pref name (suffix) for the given category. |
| + * @private |
| + */ |
| + computeCategorySuffix_: function(category) { |
| + switch (category) { |
| + case settings.ContentSettingsTypes.COOKIES: |
| + return 'cookies'; |
| + case settings.ContentSettingsTypes.JAVASCRIPT: |
| + return 'javascript'; |
| + case settings.ContentSettingsTypes.FULLSCREEN: |
| + return 'fullscreen'; |
| + case settings.ContentSettingsTypes.POPUPS: |
| + return 'popups'; |
| + case settings.ContentSettingsTypes.GEOLOCATION: |
| + return 'geolocation'; |
| + case settings.ContentSettingsTypes.NOTIFICATION: |
| + return 'notifications'; |
| + case settings.ContentSettingsTypes.CAMERA: |
| + return 'media_stream_camera'; |
| + case settings.ContentSettingsTypes.MIC: |
| + return 'media_stream_mic'; |
| + default: |
| + assertNotReached(); |
| + return ''; |
| + } |
| + }, |
| + |
| + /** |
| + * A utility function to compute the description for the category. |
| + * @param {number} category The category to show the description for. |
| + * @param {boolean} categoryEnabled The state of the global toggle. |
| + * @return {string} The category description. |
| + * @private |
| + */ |
| + computeDesc_: function(category, categoryEnabled) { |
| + switch (category) { |
| + case settings.ContentSettingsTypes.JAVASCRIPT: |
| + // "Allowed (recommended)" vs "Blocked". |
| + return categoryEnabled ? |
| + loadTimeData.getString('siteSettingsAllowedRecommended') : |
| + loadTimeData.getString('siteSettingsBlocked'); |
| + case settings.ContentSettingsTypes.POPUPS: |
| + // "Allowed" vs "Blocked (recommended)". |
| + return categoryEnabled ? |
| + loadTimeData.getString('siteSettingsAllowed') : |
| + loadTimeData.getString('siteSettingsBlockedRecommended'); |
| + case settings.ContentSettingsTypes.NOTIFICATION: |
| + // "Ask before sending (recommended)" vs "Blocked". |
| + return categoryEnabled ? |
| + loadTimeData.getString('siteSettingsAskBeforeSending') : |
| + loadTimeData.getString('siteSettingsBlocked'); |
| + case settings.ContentSettingsTypes.GEOLOCATION: |
| + case settings.ContentSettingsTypes.CAMERA: |
| + case settings.ContentSettingsTypes.MIC: |
| + // "Ask before accessing (recommended)" vs "Blocked". |
| + return categoryEnabled ? |
| + loadTimeData.getString('siteSettingsAskBeforeAccessing') : |
| + loadTimeData.getString('siteSettingsBlocked'); |
| + case settings.ContentSettingsTypes.FULLSCREEN: |
| + // "Allowed" vs. "Ask first (recommended)". |
| + return categoryEnabled ? |
| + loadTimeData.getString('siteSettingsAllowed') : |
| + loadTimeData.getString('siteSettingsAskFirstRecommended'); |
| + case settings.ContentSettingsTypes.COOKIES: |
| + // "Allow sites to save and read cookie data" vs "Blocked". |
| + return categoryEnabled ? |
| + loadTimeData.getString('siteSettingsCookiesAllowed') : |
| + loadTimeData.getString('siteSettingsBlocked'); |
| + default: |
| + assertNotReached(); |
| + return ''; |
| + } |
| + }, |
| +}; |