diff options
author | raymes <raymes@chromium.org> | 2015-07-28 00:19:59 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-28 07:20:52 +0000 |
commit | 7989563b9c827f01950c8e72d2d44fff1cae8d79 (patch) | |
tree | 8c679c4f5434bf580d8e916bd17e73efa0dca49a /components/content_settings | |
parent | fb5d24159dc83fc2c28e35e008bdaa481406c32a (diff) | |
download | chromium_src-7989563b9c827f01950c8e72d2d44fff1cae8d79.zip chromium_src-7989563b9c827f01950c8e72d2d44fff1cae8d79.tar.gz chromium_src-7989563b9c827f01950c8e72d2d44fff1cae8d79.tar.bz2 |
Add a WebsiteSettingsRegistry and use it to store website settings type names
Right now we have a bunch of arrays lying around the codebase which map
ContentSettingsTypes to various properties of website settings. When a user
adds a new setting, they have to modify each of these maps. We should
consolidate these into a single object which is registered on startup and
contains all properties of website settings.
This CL introduces a WebsiteSettingsRegistry to where website settings are
registered with these properties in WebsiteSettingsInfo objects. Currently
only the ContentSettingsType and string name of the setting are stored.
Two locations in the codebase which previously had separate arrays listing
the string names for each content setting have been changed to use the
WebsiteSettingsRegistry instead.
In followup CLs more properties will be moved into WebsiteSettingsInfo.
BUG=512683
Review URL: https://codereview.chromium.org/1247253003
Cr-Commit-Position: refs/heads/master@{#340651}
Diffstat (limited to 'components/content_settings')
8 files changed, 236 insertions, 35 deletions
diff --git a/components/content_settings/core/browser/BUILD.gn b/components/content_settings/core/browser/BUILD.gn index 173ffd6..6db040a 100644 --- a/components/content_settings/core/browser/BUILD.gn +++ b/components/content_settings/core/browser/BUILD.gn @@ -36,6 +36,10 @@ static_library("browser") { "local_shared_objects_counter.h", "plugins_field_trial.cc", "plugins_field_trial.h", + "website_settings_info.cc", + "website_settings_info.h", + "website_settings_registry.cc", + "website_settings_registry.h", ] deps = [ diff --git a/components/content_settings/core/browser/content_settings_utils.cc b/components/content_settings/core/browser/content_settings_utils.cc index 5c48e9b..6a9a655 100644 --- a/components/content_settings/core/browser/content_settings_utils.cc +++ b/components/content_settings/core/browser/content_settings_utils.cc @@ -16,46 +16,14 @@ #include "components/content_settings/core/browser/content_settings_provider.h" #include "components/content_settings/core/browser/content_settings_rule.h" #include "components/content_settings/core/browser/host_content_settings_map.h" +#include "components/content_settings/core/browser/website_settings_info.h" +#include "components/content_settings/core/browser/website_settings_registry.h" #include "components/content_settings/core/common/content_settings_pattern.h" #include "components/pref_registry/pref_registry_syncable.h" #include "url/gurl.h" namespace { -// The names of the ContentSettingsType values, for use with dictionary prefs. -const char* kTypeNames[] = { - "cookies", - "images", - "javascript", - "plugins", - "popups", - "geolocation", - "notifications", - "auto-select-certificate", - "fullscreen", - "mouselock", - "mixed-script", - "media-stream", - "media-stream-mic", - "media-stream-camera", - "register-protocol-handler", - "ppapi-broker", - "multiple-automatic-downloads", - "midi-sysex", - "push-messaging", - "ssl-cert-decisions", -#if defined(OS_WIN) - "metro-switch-to-desktop", -#elif defined(OS_ANDROID) || defined(OS_CHROMEOS) - "protected-media-identifier", -#endif - "app-banner", - "site-engagement", - "durable-storage" -}; -static_assert(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES, - "kTypeNames should have CONTENT_SETTINGS_NUM_TYPES elements"); - const char kPatternSeparator[] = ","; } // namespace @@ -63,7 +31,7 @@ const char kPatternSeparator[] = ","; namespace content_settings { std::string GetTypeName(ContentSettingsType type) { - return std::string(kTypeNames[type]); + return WebsiteSettingsRegistry::GetInstance()->Get(type)->name(); } std::string ContentSettingToString(ContentSetting setting) { diff --git a/components/content_settings/core/browser/content_settings_utils.h b/components/content_settings/core/browser/content_settings_utils.h index 8db0cbd..b1f712a 100644 --- a/components/content_settings/core/browser/content_settings_utils.h +++ b/components/content_settings/core/browser/content_settings_utils.h @@ -28,6 +28,9 @@ class RuleIterator; typedef std::pair<ContentSettingsPattern, ContentSettingsPattern> PatternPair; +// Deprecated, do not call this! +// Call WebsiteSettingsRegistry::GetInstance()->Get(type)->name() instead. +// TODO(raymes): Inline this function at it's callsites. std::string GetTypeName(ContentSettingsType type); std::string ContentSettingToString(ContentSetting setting); diff --git a/components/content_settings/core/browser/website_settings_info.cc b/components/content_settings/core/browser/website_settings_info.cc new file mode 100644 index 0000000..fa19d9c --- /dev/null +++ b/components/content_settings/core/browser/website_settings_info.cc @@ -0,0 +1,15 @@ +// 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. + +#include "components/content_settings/core/browser/website_settings_info.h" + +namespace content_settings { + +WebsiteSettingsInfo::WebsiteSettingsInfo(ContentSettingsType type, + const std::string& name) + : type_(type), name_(name) {} + +WebsiteSettingsInfo::~WebsiteSettingsInfo() {} + +} // namespace content_settings diff --git a/components/content_settings/core/browser/website_settings_info.h b/components/content_settings/core/browser/website_settings_info.h new file mode 100644 index 0000000..435b9df0 --- /dev/null +++ b/components/content_settings/core/browser/website_settings_info.h @@ -0,0 +1,34 @@ +// 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. + +#ifndef COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_WEBSITE_SETTINGS_INFO_H_ +#define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_WEBSITE_SETTINGS_INFO_H_ + +#include <string> + +#include "base/macros.h" +#include "components/content_settings/core/common/content_settings_types.h" + +namespace content_settings { + +// This class stores the properties related to a website setting. +// TODO(raymes): Move more properties into this class. +class WebsiteSettingsInfo { + public: + WebsiteSettingsInfo(ContentSettingsType type, const std::string& name); + ~WebsiteSettingsInfo(); + + ContentSettingsType type() const { return type_; } + const std::string& name() const { return name_; } + + private: + const ContentSettingsType type_; + const std::string name_; + + DISALLOW_COPY_AND_ASSIGN(WebsiteSettingsInfo); +}; + +} // namespace content_settings + +#endif // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_WEBSITE_SETTINGS_INFO_H_ diff --git a/components/content_settings/core/browser/website_settings_registry.cc b/components/content_settings/core/browser/website_settings_registry.cc new file mode 100644 index 0000000..c59c32e --- /dev/null +++ b/components/content_settings/core/browser/website_settings_registry.cc @@ -0,0 +1,90 @@ +// 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. + +#include "components/content_settings/core/browser/website_settings_registry.h" + +#include "base/logging.h" + +namespace { + +base::LazyInstance<content_settings::WebsiteSettingsRegistry>::Leaky + g_instance = LAZY_INSTANCE_INITIALIZER; + +} // namespace + +namespace content_settings { + +// static +WebsiteSettingsRegistry* WebsiteSettingsRegistry::GetInstance() { + return g_instance.Pointer(); +} + +WebsiteSettingsRegistry::WebsiteSettingsRegistry() { + website_settings_info_.resize(CONTENT_SETTINGS_NUM_TYPES); + + // TODO(raymes): This registration code should not have to be in a single + // location. It should be possible to register a setting from the code + // associated with it. + + Register(CONTENT_SETTINGS_TYPE_COOKIES, "cookies"); + Register(CONTENT_SETTINGS_TYPE_IMAGES, "images"); + Register(CONTENT_SETTINGS_TYPE_JAVASCRIPT, "javascript"); + Register(CONTENT_SETTINGS_TYPE_PLUGINS, "plugins"); + Register(CONTENT_SETTINGS_TYPE_POPUPS, "popups"); + Register(CONTENT_SETTINGS_TYPE_GEOLOCATION, "geolocation"); + Register(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, "notifications"); + Register(CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, + "auto-select-certificate"); + Register(CONTENT_SETTINGS_TYPE_FULLSCREEN, "fullscreen"); + Register(CONTENT_SETTINGS_TYPE_MOUSELOCK, "mouselock"); + Register(CONTENT_SETTINGS_TYPE_MIXEDSCRIPT, "mixed-script"); + Register(CONTENT_SETTINGS_TYPE_MEDIASTREAM, "media-stream"); + Register(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, "media-stream-mic"); + Register(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, "media-stream-camera"); + Register(CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS, + "register-protocol-handler"); + Register(CONTENT_SETTINGS_TYPE_PPAPI_BROKER, "ppapi-broker"); + Register(CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, + "multiple-automatic-downloads"); + Register(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, "midi-sysex"); + Register(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, "push-messaging"); + Register(CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS, "ssl-cert-decisions"); +#if defined(OS_WIN) + Register(CONTENT_SETTINGS_TYPE_METRO_SWITCH_TO_DESKTOP, + "metro-switch-to-desktop"); +#elif defined(OS_ANDROID) || defined(OS_CHROMEOS) + Register(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, + "protected-media-identifier"); +#endif + Register(CONTENT_SETTINGS_TYPE_APP_BANNER, "app-banner"); + Register(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, "site-engagement"); + Register(CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, "durable-storage"); +} + +WebsiteSettingsRegistry::~WebsiteSettingsRegistry() {} + +const WebsiteSettingsInfo* WebsiteSettingsRegistry::Get( + ContentSettingsType type) const { + DCHECK_GE(type, 0); + DCHECK_LT(type, static_cast<int>(website_settings_info_.size())); + return website_settings_info_[type]; +} + +const WebsiteSettingsInfo* WebsiteSettingsRegistry::GetByName( + const std::string& name) const { + for (const auto& info : website_settings_info_) { + if (info && info->name() == name) + return info; + } + return nullptr; +} + +void WebsiteSettingsRegistry::Register(ContentSettingsType type, + const std::string& name) { + DCHECK_GE(type, 0); + DCHECK_LT(type, static_cast<int>(website_settings_info_.size())); + website_settings_info_[type] = new WebsiteSettingsInfo(type, name); +} + +} // namespace content_settings diff --git a/components/content_settings/core/browser/website_settings_registry.h b/components/content_settings/core/browser/website_settings_registry.h new file mode 100644 index 0000000..8d10a3d --- /dev/null +++ b/components/content_settings/core/browser/website_settings_registry.h @@ -0,0 +1,45 @@ +// 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. + +#ifndef COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_WEBSITE_SETTINGS_REGISTRY_H_ +#define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_WEBSITE_SETTINGS_REGISTRY_H_ + +#include <string> + +#include "base/lazy_instance.h" +#include "base/macros.h" +#include "base/memory/scoped_vector.h" +#include "components/content_settings/core/browser/website_settings_info.h" +#include "components/content_settings/core/common/content_settings_types.h" + +namespace content_settings { + +// This class stores WebsiteSettingsInfo objects for each website setting in the +// system and provides access to them. Global instances can be fetched and +// methods called from from any thread because all of its public methods are +// const. +class WebsiteSettingsRegistry { + public: + static WebsiteSettingsRegistry* GetInstance(); + + const WebsiteSettingsInfo* Get(ContentSettingsType type) const; + const WebsiteSettingsInfo* GetByName(const std::string& name) const; + + private: + WebsiteSettingsRegistry(); + ~WebsiteSettingsRegistry(); + + // Register a new WebsiteSettingsInfo. + void Register(ContentSettingsType type, const std::string& name); + + ScopedVector<WebsiteSettingsInfo> website_settings_info_; + + DISALLOW_COPY_AND_ASSIGN(WebsiteSettingsRegistry); + friend class WebsiteSettingsRegistryTest; + friend struct base::DefaultLazyInstanceTraits<WebsiteSettingsRegistry>; +}; + +} // namespace content_settings + +#endif // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_WEBSITE_SETTINGS_REGISTRY_H_ diff --git a/components/content_settings/core/browser/website_settings_registry_unittest.cc b/components/content_settings/core/browser/website_settings_registry_unittest.cc new file mode 100644 index 0000000..5f9da12 --- /dev/null +++ b/components/content_settings/core/browser/website_settings_registry_unittest.cc @@ -0,0 +1,42 @@ +// 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. + +#include "base/logging.h" +#include "components/content_settings/core/browser/website_settings_info.h" +#include "components/content_settings/core/browser/website_settings_registry.h" +#include "components/content_settings/core/common/content_settings_types.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace content_settings { + +class WebsiteSettingsRegistryTest : public testing::Test { + protected: + const WebsiteSettingsRegistry* registry() { return ®istry_; } + + private: + WebsiteSettingsRegistry registry_; +}; + +TEST_F(WebsiteSettingsRegistryTest, Get) { + // CONTENT_SETTINGS_TYPE_COOKIES should be registered. + const WebsiteSettingsInfo* info = + registry()->Get(CONTENT_SETTINGS_TYPE_COOKIES); + ASSERT_TRUE(info); + EXPECT_EQ(CONTENT_SETTINGS_TYPE_COOKIES, info->type()); + EXPECT_EQ("cookies", info->name()); +} + +TEST_F(WebsiteSettingsRegistryTest, GetByName) { + // Random string shouldn't be registered. + EXPECT_FALSE(registry()->GetByName("abc")); + + // "cookies" should be registered. + const WebsiteSettingsInfo* info = registry()->GetByName("cookies"); + ASSERT_TRUE(info); + EXPECT_EQ(CONTENT_SETTINGS_TYPE_COOKIES, info->type()); + EXPECT_EQ("cookies", info->name()); + EXPECT_EQ(registry()->Get(CONTENT_SETTINGS_TYPE_COOKIES), info); +} + +} // namespace content_settings |