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 #ifndef COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_WEBSITE_SETTINGS_REGISTRY_H_
6 #define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_WEBSITE_SETTINGS_REGISTRY_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 
12 #include "base/lazy_instance.h"
13 #include "base/macros.h"
14 #include "base/values.h"
15 #include "components/content_settings/core/browser/content_settings_utils.h"
16 #include "components/content_settings/core/browser/website_settings_info.h"
17 #include "components/content_settings/core/common/content_settings.h"
18 #include "components/content_settings/core/common/content_settings_types.h"
19 
20 namespace content_settings {
21 
22 // This class stores WebsiteSettingsInfo objects for each website setting in the
23 // system and provides access to them. Global instances can be fetched and
24 // methods called from from any thread because all of its public methods are
25 // const.
26 class WebsiteSettingsRegistry {
27  public:
28   typedef uint32_t Platforms;
29   // TODO(lshang): Remove this enum when content settings can be registered from
30   // within the component in which they are used. When this is possible then
31   // ifdefs can be contained within each component.
32   enum Platform : Platforms {
33     PLATFORM_WINDOWS = 1 << 0,
34     PLATFORM_LINUX = 1 << 1,
35     PLATFORM_CHROMEOS = 1 << 2,
36     PLATFORM_MAC = 1 << 3,
37     PLATFORM_ANDROID = 1 << 4,
38     PLATFORM_IOS = 1 << 5,
39     PLATFORM_FUCHSIA = 1 << 6,
40 
41     // Settings only applied to win, mac, linux, chromeos, and fuchsia.
42     DESKTOP = PLATFORM_WINDOWS | PLATFORM_LINUX | PLATFORM_CHROMEOS |
43               PLATFORM_MAC | PLATFORM_FUCHSIA,
44 
45     // Settings applied to all platforms, including win, mac, linux, chromeos,
46     // android, ios, and fuchsia.
47     ALL_PLATFORMS =
48         DESKTOP | PLATFORM_ANDROID | PLATFORM_IOS | PLATFORM_FUCHSIA,
49   };
50 
51   using Map =
52       std::map<ContentSettingsType, std::unique_ptr<WebsiteSettingsInfo>>;
53   using const_iterator = MapValueIterator<typename Map::const_iterator,
54                                           const WebsiteSettingsInfo*>;
55 
56   static WebsiteSettingsRegistry* GetInstance();
57 
58   // Reset the instance for use inside tests.
59   void ResetForTest();
60 
61   const WebsiteSettingsInfo* Get(ContentSettingsType type) const;
62   const WebsiteSettingsInfo* GetByName(const std::string& name) const;
63 
64   // Register a new website setting. This maps an origin to an arbitrary
65   // base::Value. Returns a pointer to the registered WebsiteSettingsInfo which
66   // is owned by the registry.
67   // A nullptr will be returned if registration fails (for example if
68   // |platforms| doesn't match the current platform).
69   const WebsiteSettingsInfo* Register(
70       ContentSettingsType type,
71       const std::string& name,
72       std::unique_ptr<base::Value> initial_default_value,
73       WebsiteSettingsInfo::SyncStatus sync_status,
74       WebsiteSettingsInfo::LossyStatus lossy_status,
75       WebsiteSettingsInfo::ScopingType scoping_type,
76       Platforms platforms,
77       WebsiteSettingsInfo::IncognitoBehavior incognito_behavior);
78 
79   const_iterator begin() const;
80   const_iterator end() const;
81 
82  private:
83   friend class ContentSettingsRegistryTest;
84   friend class WebsiteSettingsRegistryTest;
85   friend struct base::LazyInstanceTraitsBase<WebsiteSettingsRegistry>;
86 
87   WebsiteSettingsRegistry();
88   ~WebsiteSettingsRegistry();
89 
90   void Init();
91 
92   Map website_settings_info_;
93 
94   DISALLOW_COPY_AND_ASSIGN(WebsiteSettingsRegistry);
95 };
96 
97 }  // namespace content_settings
98 
99 #endif  // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_WEBSITE_SETTINGS_REGISTRY_H_
100