diff options
13 files changed, 255 insertions, 71 deletions
diff --git a/chrome/browser/extensions/api/content_settings/content_settings_helpers.cc b/chrome/browser/extensions/api/content_settings/content_settings_helpers.cc index 07e9717..9606a4d 100644 --- a/chrome/browser/extensions/api/content_settings/content_settings_helpers.cc +++ b/chrome/browser/extensions/api/content_settings/content_settings_helpers.cc @@ -7,6 +7,8 @@ #include "base/basictypes.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" +#include "components/content_settings/core/browser/website_settings_info.h" +#include "components/content_settings/core/browser/website_settings_registry.h" #include "content/public/common/url_constants.h" #include "extensions/common/url_pattern.h" @@ -17,32 +19,6 @@ const char kNoPathWildcardsError[] = const char kNoPathsError[] = "Specific paths are not allowed."; const char kInvalidPatternError[] = "The pattern \"*\" is invalid."; -const char* const kContentSettingsTypeNames[] = { - "cookies", - "images", - "javascript", - "plugins", - "popups", - "location", - "notifications", - "auto-select-certificate", - "fullscreen", - "mouselock", - "mixed-script", - "media-stream", - "media-stream-mic", - "media-stream-camera", - "register-protocol-handler", - "ppapi-broker", - "multiple-automatic-downloads" -}; - -// TODO(msramek): Assert that |kContentSettingsTypeNames| is synced with -// enum |ContentSettingsType|. -static_assert(arraysize(kContentSettingsTypeNames) <= - CONTENT_SETTINGS_NUM_TYPES, - "kContentSettingsTypeNames has an unexpected number of elements"); - const char* const kContentSettingNames[] = { "default", "allow", @@ -129,17 +105,19 @@ ContentSettingsPattern ParseExtensionPattern(const std::string& pattern_str, ContentSettingsType StringToContentSettingsType( const std::string& content_type) { - for (size_t type = 0; type < arraysize(kContentSettingsTypeNames); ++type) { - if (content_type == kContentSettingsTypeNames[type]) - return static_cast<ContentSettingsType>(type); - } + const content_settings::WebsiteSettingsInfo* info = + content_settings::WebsiteSettingsRegistry::GetInstance()->GetByName( + content_type); + if (info) + return info->type(); + return CONTENT_SETTINGS_TYPE_DEFAULT; } -const char* ContentSettingsTypeToString(ContentSettingsType type) { - size_t index = static_cast<size_t>(type); - DCHECK_LT(index, arraysize(kContentSettingsTypeNames)); - return kContentSettingsTypeNames[index]; +std::string ContentSettingsTypeToString(ContentSettingsType type) { + return content_settings::WebsiteSettingsRegistry::GetInstance() + ->Get(type) + ->name(); } bool StringToContentSetting(const std::string& setting_str, diff --git a/chrome/browser/extensions/api/content_settings/content_settings_helpers.h b/chrome/browser/extensions/api/content_settings/content_settings_helpers.h index db82fe4..ddd8dd95 100644 --- a/chrome/browser/extensions/api/content_settings/content_settings_helpers.h +++ b/chrome/browser/extensions/api/content_settings/content_settings_helpers.h @@ -28,7 +28,7 @@ ContentSettingsPattern ParseExtensionPattern(const std::string& pattern_str, ContentSettingsType StringToContentSettingsType( const std::string& content_type); // Returns a string representation of a ContentSettingsType. -const char* ContentSettingsTypeToString(ContentSettingsType type); +std::string ContentSettingsTypeToString(ContentSettingsType type); // Converts a content setting string to the corresponding ContentSetting. // Returns true if |setting_str| specifies a valid content setting, diff --git a/chrome/common/extensions/api/content_settings.json b/chrome/common/extensions/api/content_settings.json index 98a9621..0500c32 100644 --- a/chrome/common/extensions/api/content_settings.json +++ b/chrome/common/extensions/api/content_settings.json @@ -260,7 +260,7 @@ "$ref": "ContentSetting", "description": "Whether to allow Geolocation. One of <br><var>allow</var>: Allow sites to track your physical location,<br><var>block</var>: Don't allow sites to track your physical location,<br><var>ask</var>: Ask before allowing sites to track your physical location. <br>Default is <var>ask</var>.<br>The primary URL is the URL of the document which requested location data. The secondary URL is the URL of the top-level frame (which may or may not differ from the requesting URL).", "value": [ - "location", + "geolocation", {"$ref":"LocationContentSetting"} ] }, diff --git a/components/components_tests.gyp b/components/components_tests.gyp index bbfd4ed..246ff17 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -104,6 +104,7 @@ 'content_settings/core/browser/content_settings_utils_unittest.cc', 'content_settings/core/browser/cookie_settings_unittest.cc', 'content_settings/core/browser/plugins_field_trial_unittest.cc', + 'content_settings/core/browser/website_settings_registry_unittest.cc', 'content_settings/core/common/content_settings_pattern_parser_unittest.cc', 'content_settings/core/common/content_settings_pattern_unittest.cc', ], diff --git a/components/content_settings.gypi b/components/content_settings.gypi index df40838..0242088 100644 --- a/components/content_settings.gypi +++ b/components/content_settings.gypi @@ -55,6 +55,10 @@ 'content_settings/core/browser/local_shared_objects_counter.h', 'content_settings/core/browser/plugins_field_trial.cc', 'content_settings/core/browser/plugins_field_trial.h', + 'content_settings/core/browser/website_settings_info.cc', + 'content_settings/core/browser/website_settings_info.h', + 'content_settings/core/browser/website_settings_registry.cc', + 'content_settings/core/browser/website_settings_registry.h', ], # TODO(jschuh): crbug.com/167187 fix size_t to int truncations. 'msvs_disabled_warnings': [4267, ], 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 |