Content settings: remove some plugin-related code/resources when... there are no...
[chromium-blink-merge.git] / components / content_settings / core / browser / content_settings_registry.cc
blobec68eb5d10d4101149ba6d93f76e85db28ecac45
1 // Copyright 2015 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.
5 #include "components/content_settings/core/browser/content_settings_registry.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "components/content_settings/core/browser/content_settings_utils.h"
11 #include "components/content_settings/core/browser/website_settings_registry.h"
12 #include "components/content_settings/core/common/content_settings.h"
14 #if defined(ENABLE_PLUGINS)
15 #include "components/content_settings/core/browser/plugins_field_trial.h"
16 #endif
18 namespace content_settings {
20 namespace {
22 base::LazyInstance<content_settings::ContentSettingsRegistry> g_instance =
23 LAZY_INSTANCE_INITIALIZER;
25 // These functions return a vector of schemes in which various permissions will
26 // be whitelisted.
27 const std::vector<std::string>& NoWhitelistedSchemes() {
28 CR_DEFINE_STATIC_LOCAL(std::vector<std::string>, kNoWhitelistedSchemes, ());
29 return kNoWhitelistedSchemes;
32 const std::vector<std::string>& WhitelistedForWebUI() {
33 CR_DEFINE_STATIC_LOCAL(std::vector<std::string>, kWhitelistedForWebUI, ());
34 if (kWhitelistedForWebUI.size() == 0) {
35 kWhitelistedForWebUI.push_back(kChromeDevToolsScheme);
36 kWhitelistedForWebUI.push_back(kChromeUIScheme);
38 return kWhitelistedForWebUI;
41 const std::vector<std::string>& WhitelistedForWebUIAndExtensions() {
42 CR_DEFINE_STATIC_LOCAL(std::vector<std::string>,
43 kWhitelistedForWebUIAndExtensions, ());
44 if (kWhitelistedForWebUIAndExtensions.size() == 0) {
45 kWhitelistedForWebUIAndExtensions = WhitelistedForWebUI();
46 #if defined(ENABLE_EXTENSIONS)
47 kWhitelistedForWebUIAndExtensions.push_back(kExtensionScheme);
48 #endif
50 return kWhitelistedForWebUIAndExtensions;
53 ContentSetting GetDefaultPluginsContentSetting() {
54 #if defined(ENABLE_PLUGINS)
55 return PluginsFieldTrial::GetDefaultPluginsContentSetting();
56 #else
57 return CONTENT_SETTING_BLOCK;
58 #endif
61 } // namespace
63 // static
64 ContentSettingsRegistry* ContentSettingsRegistry::GetInstance() {
65 return g_instance.Pointer();
68 ContentSettingsRegistry::ContentSettingsRegistry()
69 : ContentSettingsRegistry(WebsiteSettingsRegistry::GetInstance()) {}
71 ContentSettingsRegistry::ContentSettingsRegistry(
72 WebsiteSettingsRegistry* website_settings_registry)
73 // This object depends on WebsiteSettingsRegistry, so get it first so that
74 // they will be destroyed in reverse order.
75 : website_settings_registry_(website_settings_registry) {
76 Init();
79 void ContentSettingsRegistry::ResetForTest() {
80 website_settings_registry_->ResetForTest();
81 content_settings_info_.clear();
82 Init();
85 ContentSettingsRegistry::~ContentSettingsRegistry() {}
87 const ContentSettingsInfo* ContentSettingsRegistry::Get(
88 ContentSettingsType type) const {
89 DCHECK_GE(type, 0);
90 DCHECK_LT(type, static_cast<int>(content_settings_info_.size()));
91 return content_settings_info_[type];
94 void ContentSettingsRegistry::Init() {
95 content_settings_info_.resize(CONTENT_SETTINGS_NUM_TYPES);
97 // TODO(raymes): This registration code should not have to be in a single
98 // location. It should be possible to register a setting from the code
99 // associated with it.
101 // WARNING: The string names of the permissions passed in below are used to
102 // generate preference names and should never be changed!
104 // Content settings (those with allow/block/ask/etc. values).
105 Register(CONTENT_SETTINGS_TYPE_COOKIES, "cookies", CONTENT_SETTING_ALLOW,
106 WebsiteSettingsInfo::SYNCABLE, WhitelistedForWebUI());
107 Register(CONTENT_SETTINGS_TYPE_IMAGES, "images", CONTENT_SETTING_ALLOW,
108 WebsiteSettingsInfo::SYNCABLE, WhitelistedForWebUIAndExtensions());
109 Register(CONTENT_SETTINGS_TYPE_JAVASCRIPT, "javascript",
110 CONTENT_SETTING_ALLOW, WebsiteSettingsInfo::SYNCABLE,
111 WhitelistedForWebUIAndExtensions());
112 Register(CONTENT_SETTINGS_TYPE_PLUGINS, "plugins",
113 GetDefaultPluginsContentSetting(),
114 WebsiteSettingsInfo::SYNCABLE, WhitelistedForWebUI());
115 Register(CONTENT_SETTINGS_TYPE_POPUPS, "popups", CONTENT_SETTING_BLOCK,
116 WebsiteSettingsInfo::SYNCABLE, WhitelistedForWebUIAndExtensions());
117 Register(CONTENT_SETTINGS_TYPE_GEOLOCATION, "geolocation",
118 CONTENT_SETTING_ASK, WebsiteSettingsInfo::UNSYNCABLE,
119 NoWhitelistedSchemes());
120 Register(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, "notifications",
121 CONTENT_SETTING_ASK, WebsiteSettingsInfo::UNSYNCABLE,
122 NoWhitelistedSchemes());
123 Register(CONTENT_SETTINGS_TYPE_FULLSCREEN, "fullscreen", CONTENT_SETTING_ASK,
124 WebsiteSettingsInfo::SYNCABLE, WhitelistedForWebUI());
125 Register(CONTENT_SETTINGS_TYPE_MOUSELOCK, "mouselock", CONTENT_SETTING_ASK,
126 WebsiteSettingsInfo::SYNCABLE, WhitelistedForWebUI());
127 Register(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, "media-stream-mic",
128 CONTENT_SETTING_ASK, WebsiteSettingsInfo::UNSYNCABLE,
129 WhitelistedForWebUI());
130 Register(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, "media-stream-camera",
131 CONTENT_SETTING_ASK, WebsiteSettingsInfo::UNSYNCABLE,
132 WhitelistedForWebUI());
133 Register(CONTENT_SETTINGS_TYPE_PPAPI_BROKER, "ppapi-broker",
134 CONTENT_SETTING_ASK, WebsiteSettingsInfo::UNSYNCABLE,
135 WhitelistedForWebUI());
136 Register(CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, "automatic-downloads",
137 CONTENT_SETTING_ASK, WebsiteSettingsInfo::SYNCABLE,
138 WhitelistedForWebUIAndExtensions());
139 Register(CONTENT_SETTINGS_TYPE_MIDI_SYSEX, "midi-sysex", CONTENT_SETTING_ASK,
140 WebsiteSettingsInfo::SYNCABLE, NoWhitelistedSchemes());
141 Register(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, "push-messaging",
142 CONTENT_SETTING_ASK, WebsiteSettingsInfo::SYNCABLE,
143 NoWhitelistedSchemes());
144 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
145 Register(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER,
146 "protected-media-identifier", CONTENT_SETTING_ASK,
147 WebsiteSettingsInfo::UNSYNCABLE, NoWhitelistedSchemes());
148 #endif
149 Register(CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, "durable-storage",
150 CONTENT_SETTING_ASK, WebsiteSettingsInfo::UNSYNCABLE,
151 NoWhitelistedSchemes());
153 // Content settings that aren't used to store any data. TODO(raymes): use a
154 // different mechanism rather than content settings to represent these.
155 // Since nothing is stored in them, there is no real point in them being a
156 // content setting.
157 Register(CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS, "protocol-handler",
158 CONTENT_SETTING_DEFAULT, WebsiteSettingsInfo::UNSYNCABLE,
159 NoWhitelistedSchemes());
160 Register(CONTENT_SETTINGS_TYPE_MIXEDSCRIPT, "mixed-script",
161 CONTENT_SETTING_DEFAULT, WebsiteSettingsInfo::UNSYNCABLE,
162 NoWhitelistedSchemes());
164 // Deprecated.
165 Register(CONTENT_SETTINGS_TYPE_MEDIASTREAM, "media-stream",
166 CONTENT_SETTING_DEFAULT, WebsiteSettingsInfo::UNSYNCABLE,
167 NoWhitelistedSchemes());
170 void ContentSettingsRegistry::Register(
171 ContentSettingsType type,
172 const std::string& name,
173 ContentSetting initial_default_value,
174 WebsiteSettingsInfo::SyncStatus sync_status,
175 const std::vector<std::string>& whitelisted_schemes) {
176 // Ensure that nothing has been registered yet for the given type.
177 DCHECK(!website_settings_registry_->Get(type));
178 scoped_ptr<base::Value> default_value(
179 new base::FundamentalValue(static_cast<int>(initial_default_value)));
180 const WebsiteSettingsInfo* website_settings_info =
181 website_settings_registry_->Register(type, name, default_value.Pass(),
182 sync_status,
183 WebsiteSettingsInfo::NOT_LOSSY);
184 DCHECK_GE(type, 0);
185 DCHECK_LT(type, static_cast<int>(content_settings_info_.size()));
186 content_settings_info_[type] =
187 new ContentSettingsInfo(website_settings_info, whitelisted_schemes);
190 } // namespace content_settings