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_CONTENT_SETTINGS_REGISTRY_H_
6 #define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_REGISTRY_H_
7 
8 #include <map>
9 #include <memory>
10 #include <set>
11 #include <string>
12 #include <vector>
13 
14 #include "base/lazy_instance.h"
15 #include "base/macros.h"
16 #include "components/content_settings/core/browser/content_settings_info.h"
17 #include "components/content_settings/core/browser/content_settings_utils.h"
18 #include "components/content_settings/core/browser/website_settings_info.h"
19 #include "components/content_settings/core/common/content_settings.h"
20 #include "components/content_settings/core/common/content_settings_types.h"
21 
22 namespace content_settings {
23 
24 class WebsiteSettingsRegistry;
25 
26 // This class stores ContentSettingsInfo objects for each content setting in the
27 // system and provides access to them. Global instances can be fetched and
28 // methods called from from any thread because all of its public methods are
29 // const.
30 class ContentSettingsRegistry {
31  public:
32   using Map =
33       std::map<ContentSettingsType, std::unique_ptr<ContentSettingsInfo>>;
34   using const_iterator = MapValueIterator<typename Map::const_iterator,
35                                           const ContentSettingsInfo*>;
36 
37   static ContentSettingsRegistry* GetInstance();
38 
39   // Reset the instance for use inside tests.
40   void ResetForTest();
41 
42   const ContentSettingsInfo* Get(ContentSettingsType type) const;
43 
44   const_iterator begin() const;
45   const_iterator end() const;
46 
47  private:
48   friend class ContentSettingsRegistryTest;
49   friend struct base::LazyInstanceTraitsBase<ContentSettingsRegistry>;
50 
51   ContentSettingsRegistry();
52   ContentSettingsRegistry(WebsiteSettingsRegistry* website_settings_registry);
53   ~ContentSettingsRegistry();
54 
55   void Init();
56 
57   typedef uint32_t Platforms;
58 
59   // Register a new content setting. This maps an origin to an ALLOW/ASK/BLOCK
60   // value (see the ContentSetting enum).
61   void Register(ContentSettingsType type,
62                 const std::string& name,
63                 ContentSetting initial_default_value,
64                 WebsiteSettingsInfo::SyncStatus sync_status,
65                 const std::vector<std::string>& whitelisted_schemes,
66                 const base::flat_set<url::Origin>& force_allowed_origins,
67                 const std::set<ContentSetting>& valid_settings,
68                 WebsiteSettingsInfo::ScopingType scoping_type,
69                 Platforms platforms,
70                 ContentSettingsInfo::IncognitoBehavior incognito_behavior,
71                 ContentSettingsInfo::StorageBehavior storage_behavior,
72                 ContentSettingsInfo::OriginRestriction origin_restriction);
73 
74   Map content_settings_info_;
75   WebsiteSettingsRegistry* website_settings_registry_;
76 
77   DISALLOW_COPY_AND_ASSIGN(ContentSettingsRegistry);
78 };
79 
80 }  // namespace content_settings
81 
82 #endif  // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_REGISTRY_H_
83