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.
4 
5 #include "components/content_settings/core/browser/website_settings_registry.h"
6 
7 #include <utility>
8 
9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h"
11 #include "build/build_config.h"
12 #include "components/content_settings/core/common/content_settings.h"
13 
14 namespace {
15 
16 base::LazyInstance<content_settings::WebsiteSettingsRegistry>::DestructorAtExit
17     g_website_settings_registry_instance = LAZY_INSTANCE_INITIALIZER;
18 
19 }  // namespace
20 
21 namespace content_settings {
22 
23 // static
GetInstance()24 WebsiteSettingsRegistry* WebsiteSettingsRegistry::GetInstance() {
25   return g_website_settings_registry_instance.Pointer();
26 }
27 
WebsiteSettingsRegistry()28 WebsiteSettingsRegistry::WebsiteSettingsRegistry() {
29   Init();
30 }
31 
~WebsiteSettingsRegistry()32 WebsiteSettingsRegistry::~WebsiteSettingsRegistry() {}
33 
ResetForTest()34 void WebsiteSettingsRegistry::ResetForTest() {
35   website_settings_info_.clear();
36   Init();
37 }
38 
Get(ContentSettingsType type) const39 const WebsiteSettingsInfo* WebsiteSettingsRegistry::Get(
40     ContentSettingsType type) const {
41   const auto& it = website_settings_info_.find(type);
42   if (it != website_settings_info_.end())
43     return it->second.get();
44   return nullptr;
45 }
46 
GetByName(const std::string & name) const47 const WebsiteSettingsInfo* WebsiteSettingsRegistry::GetByName(
48     const std::string& name) const {
49   for (const auto& entry : website_settings_info_) {
50     if (entry.second->name() == name)
51       return entry.second.get();
52   }
53   return nullptr;
54 }
55 
Register(ContentSettingsType type,const std::string & name,std::unique_ptr<base::Value> initial_default_value,WebsiteSettingsInfo::SyncStatus sync_status,WebsiteSettingsInfo::LossyStatus lossy_status,WebsiteSettingsInfo::ScopingType scoping_type,Platforms platform,WebsiteSettingsInfo::IncognitoBehavior incognito_behavior)56 const WebsiteSettingsInfo* WebsiteSettingsRegistry::Register(
57     ContentSettingsType type,
58     const std::string& name,
59     std::unique_ptr<base::Value> initial_default_value,
60     WebsiteSettingsInfo::SyncStatus sync_status,
61     WebsiteSettingsInfo::LossyStatus lossy_status,
62     WebsiteSettingsInfo::ScopingType scoping_type,
63     Platforms platform,
64     WebsiteSettingsInfo::IncognitoBehavior incognito_behavior) {
65 
66 #if defined(OS_WIN)
67   if (!(platform & PLATFORM_WINDOWS))
68     return nullptr;
69 #elif (defined(OS_LINUX) || defined(OS_BSD)) && !defined(OS_CHROMEOS)
70   if (!(platform & PLATFORM_LINUX))
71     return nullptr;
72 #elif defined(OS_MACOSX) && !defined(OS_IOS)
73   if (!(platform & PLATFORM_MAC))
74     return nullptr;
75 #elif defined(OS_CHROMEOS)
76   if (!(platform & PLATFORM_CHROMEOS))
77     return nullptr;
78 #elif defined(OS_ANDROID)
79   if (!(platform & PLATFORM_ANDROID))
80     return nullptr;
81   // Don't sync settings to mobile platforms. The UI is different to desktop and
82   // doesn't allow the settings to be managed in the same way. See
83   // crbug.com/642184.
84   sync_status = WebsiteSettingsInfo::UNSYNCABLE;
85 #elif defined(OS_IOS)
86   if (!(platform & PLATFORM_IOS))
87     return nullptr;
88   // Don't sync settings to mobile platforms. The UI is different to desktop and
89   // doesn't allow the settings to be managed in the same way. See
90   // crbug.com/642184.
91   sync_status = WebsiteSettingsInfo::UNSYNCABLE;
92 #elif defined(OS_FUCHSIA)
93   if (!(platform & PLATFORM_FUCHSIA))
94     return nullptr;
95   sync_status = WebsiteSettingsInfo::UNSYNCABLE;
96 #else
97 #error "Unsupported platform"
98 #endif
99 
100   WebsiteSettingsInfo* info = new WebsiteSettingsInfo(
101       type, name, std::move(initial_default_value), sync_status, lossy_status,
102       scoping_type, incognito_behavior);
103   website_settings_info_[info->type()] = base::WrapUnique(info);
104   return info;
105 }
106 
begin() const107 WebsiteSettingsRegistry::const_iterator WebsiteSettingsRegistry::begin() const {
108   return const_iterator(website_settings_info_.begin());
109 }
110 
end() const111 WebsiteSettingsRegistry::const_iterator WebsiteSettingsRegistry::end() const {
112   return const_iterator(website_settings_info_.end());
113 }
114 
Init()115 void WebsiteSettingsRegistry::Init() {
116   // TODO(raymes): This registration code should not have to be in a single
117   // location. It should be possible to register a setting from the code
118   // associated with it.
119 
120   // WARNING: The string names of the permissions passed in below are used to
121   // generate preference names and should never be changed!
122 
123   // Website settings.
124   Register(ContentSettingsType::AUTO_SELECT_CERTIFICATE,
125            "auto-select-certificate", nullptr, WebsiteSettingsInfo::UNSYNCABLE,
126            WebsiteSettingsInfo::NOT_LOSSY,
127            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
128            ALL_PLATFORMS, WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
129   Register(
130       ContentSettingsType::SSL_CERT_DECISIONS, "ssl-cert-decisions", nullptr,
131       WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::NOT_LOSSY,
132       WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
133       DESKTOP | PLATFORM_ANDROID, WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
134   Register(ContentSettingsType::APP_BANNER, "app-banner", nullptr,
135            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::LOSSY,
136            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
137            DESKTOP | PLATFORM_ANDROID,
138            WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
139   Register(ContentSettingsType::SITE_ENGAGEMENT, "site-engagement", nullptr,
140            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::LOSSY,
141            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
142            DESKTOP | PLATFORM_ANDROID,
143            WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
144   Register(ContentSettingsType::USB_CHOOSER_DATA, "usb-chooser-data", nullptr,
145            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::NOT_LOSSY,
146            WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE,
147            DESKTOP | PLATFORM_ANDROID,
148            WebsiteSettingsInfo::DONT_INHERIT_IN_INCOGNITO);
149   Register(ContentSettingsType::IMPORTANT_SITE_INFO, "important-site-info",
150            nullptr, WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::LOSSY,
151            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
152            DESKTOP | PLATFORM_ANDROID,
153            WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
154   Register(ContentSettingsType::PERMISSION_AUTOBLOCKER_DATA,
155            "permission-autoblocking-data", nullptr,
156            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::NOT_LOSSY,
157            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
158            DESKTOP | PLATFORM_ANDROID,
159            WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
160   Register(ContentSettingsType::PASSWORD_PROTECTION, "password-protection",
161            nullptr, WebsiteSettingsInfo::UNSYNCABLE,
162            WebsiteSettingsInfo::NOT_LOSSY,
163            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
164            ALL_PLATFORMS, WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
165   // Set when an origin is activated for subresource filtering and the
166   // associated UI is shown to the user. Cleared when a site is de-activated or
167   // the first URL matching the origin is removed from history.
168   Register(ContentSettingsType::ADS_DATA, "subresource-filter-data", nullptr,
169            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::NOT_LOSSY,
170            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
171            DESKTOP | PLATFORM_ANDROID,
172            WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
173   Register(ContentSettingsType::MEDIA_ENGAGEMENT, "media-engagement", nullptr,
174            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::LOSSY,
175            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
176            DESKTOP | PLATFORM_ANDROID,
177            WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
178   Register(ContentSettingsType::CLIENT_HINTS, "client-hints", nullptr,
179            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::LOSSY,
180            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
181            DESKTOP | PLATFORM_ANDROID,
182            WebsiteSettingsInfo::DONT_INHERIT_IN_INCOGNITO);
183   // To counteract the reduced usability of the Flash permission when it becomes
184   // ephemeral, we sync the bit indicating that the Flash permission should be
185   // displayed in the page info.
186   Register(ContentSettingsType::PLUGINS_DATA, "flash-data", nullptr,
187            WebsiteSettingsInfo::SYNCABLE, WebsiteSettingsInfo::NOT_LOSSY,
188            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
189            DESKTOP, WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
190   // Set to keep track of dismissals without user's interaction for intent
191   // picker UI.
192   Register(ContentSettingsType::INTENT_PICKER_DISPLAY,
193            "intent-picker-auto-display", nullptr,
194            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::NOT_LOSSY,
195            WebsiteSettingsInfo::SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE,
196            DESKTOP, WebsiteSettingsInfo::DONT_INHERIT_IN_INCOGNITO);
197   Register(ContentSettingsType::SERIAL_CHOOSER_DATA, "serial-chooser-data",
198            nullptr, WebsiteSettingsInfo::UNSYNCABLE,
199            WebsiteSettingsInfo::NOT_LOSSY,
200            WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE,
201            DESKTOP, WebsiteSettingsInfo::DONT_INHERIT_IN_INCOGNITO);
202   Register(ContentSettingsType::HID_CHOOSER_DATA, "hid-chooser-data", nullptr,
203            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::NOT_LOSSY,
204            WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE,
205            DESKTOP, WebsiteSettingsInfo::DONT_INHERIT_IN_INCOGNITO);
206   Register(ContentSettingsType::INSTALLED_WEB_APP_METADATA,
207            "installed-web-app-metadata", nullptr,
208            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::LOSSY,
209            WebsiteSettingsInfo::SINGLE_ORIGIN_ONLY_SCOPE, DESKTOP,
210            WebsiteSettingsInfo::DONT_INHERIT_IN_INCOGNITO);
211   Register(ContentSettingsType::BLUETOOTH_CHOOSER_DATA,
212            "bluetooth-chooser-data",
213            /*initial_default_value=*/nullptr, WebsiteSettingsInfo::UNSYNCABLE,
214            WebsiteSettingsInfo::NOT_LOSSY,
215            WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE,
216            DESKTOP | PLATFORM_ANDROID,
217            WebsiteSettingsInfo::DONT_INHERIT_IN_INCOGNITO);
218   Register(ContentSettingsType::SAFE_BROWSING_URL_CHECK_DATA,
219            "safe-browsing-url-check-data", nullptr,
220            WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::NOT_LOSSY,
221            WebsiteSettingsInfo::SINGLE_ORIGIN_ONLY_SCOPE, ALL_PLATFORMS,
222            WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
223 }
224 
225 }  // namespace content_settings
226