1 // Copyright (c) 2011 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_POLICY_PROVIDER_H_
6 #define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_POLICY_PROVIDER_H_
7 
8 // A content settings provider that takes its settings out of policies.
9 
10 #include <vector>
11 
12 #include "base/macros.h"
13 #include "base/synchronization/lock.h"
14 #include "components/content_settings/core/browser/content_settings_observable_provider.h"
15 #include "components/content_settings/core/browser/content_settings_origin_identifier_value_map.h"
16 #include "components/prefs/pref_change_registrar.h"
17 
18 class PrefService;
19 
20 namespace user_prefs {
21 class PrefRegistrySyncable;
22 }
23 
24 namespace content_settings {
25 
26 // PolicyProvider that provides managed content-settings.
27 class PolicyProvider : public ObservableProvider {
28  public:
29   explicit PolicyProvider(PrefService* prefs);
30   ~PolicyProvider() override;
31   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
32 
33   // ProviderInterface implementations.
34   std::unique_ptr<RuleIterator> GetRuleIterator(
35       ContentSettingsType content_type,
36       const ResourceIdentifier& resource_identifier,
37       bool incognito) const override;
38 
39   bool SetWebsiteSetting(const ContentSettingsPattern& primary_pattern,
40                          const ContentSettingsPattern& secondary_pattern,
41                          ContentSettingsType content_type,
42                          const ResourceIdentifier& resource_identifier,
43                          std::unique_ptr<base::Value>&& value) override;
44 
45   void ClearAllContentSettingsRules(ContentSettingsType content_type) override;
46 
47   void ShutdownOnUIThread() override;
48 
49  private:
50   struct PrefsForManagedDefaultMapEntry;
51 
52   static const PrefsForManagedDefaultMapEntry kPrefsForManagedDefault[];
53 
54   // Reads the policy managed default settings.
55   void ReadManagedDefaultSettings();
56 
57   // Callback for preference changes.
58   void OnPreferenceChanged(const std::string& pref_name);
59 
60   // Reads the policy controlled default settings for a specific content type.
61   void UpdateManagedDefaultSetting(const PrefsForManagedDefaultMapEntry& entry);
62 
63   void ReadManagedContentSettings(bool overwrite);
64 
65   void GetContentSettingsFromPreferences(OriginIdentifierValueMap* rules);
66 
67   void GetAutoSelectCertificateSettingsFromPreferences(
68       OriginIdentifierValueMap* value_map);
69 
70   void ReadManagedContentSettingsTypes(ContentSettingsType content_type);
71 
72   OriginIdentifierValueMap value_map_;
73 
74   PrefService* prefs_;
75 
76   PrefChangeRegistrar pref_change_registrar_;
77 
78   // Used around accesses to the |value_map_| object to guarantee
79   // thread safety.
80   mutable base::Lock lock_;
81 
82   DISALLOW_COPY_AND_ASSIGN(PolicyProvider);
83 };
84 
85 }  // namespace content_settings
86 
87 #endif  // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_POLICY_PROVIDER_H_
88