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 CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_POLICY_PROVIDER_H_
6#define CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_POLICY_PROVIDER_H_
7#pragma once
8
9// A content settings provider that takes its settings out of policies.
10
11#include <vector>
12
13#include "base/basictypes.h"
14#include "base/synchronization/lock.h"
15#include "base/tuple.h"
16#include "chrome/browser/content_settings/content_settings_base_provider.h"
17#include "chrome/browser/content_settings/content_settings_provider.h"
18#include "chrome/browser/prefs/pref_change_registrar.h"
19#include "content/common/notification_observer.h"
20#include "content/common/notification_registrar.h"
21
22class ContentSettingsDetails;
23class DictionaryValue;
24class PrefService;
25class Profile;
26
27namespace content_settings {
28
29class PolicyDefaultProvider : public DefaultProviderInterface,
30                              public NotificationObserver {
31 public:
32  explicit PolicyDefaultProvider(Profile* profile);
33  virtual ~PolicyDefaultProvider();
34
35  // DefaultContentSettingsProvider implementation.
36  virtual ContentSetting ProvideDefaultSetting(
37      ContentSettingsType content_type) const;
38  virtual void UpdateDefaultSetting(ContentSettingsType content_type,
39                                    ContentSetting setting);
40  virtual void ResetToDefaults();
41  virtual bool DefaultSettingIsManaged(ContentSettingsType content_type) const;
42
43  static void RegisterUserPrefs(PrefService* prefs);
44
45  // NotificationObserver implementation.
46  virtual void Observe(NotificationType type,
47                       const NotificationSource& source,
48                       const NotificationDetails& details);
49
50 private:
51  // Informs observers that content settings have changed. Make sure that
52  // |lock_| is not held when calling this, as listeners will usually call one
53  // of the GetSettings functions in response, which would then lead to a
54  // mutex deadlock.
55  void NotifyObservers(const ContentSettingsDetails& details);
56
57  void UnregisterObservers();
58
59  // Reads the policy managed default settings.
60  void ReadManagedDefaultSettings();
61
62  // Reads the policy controlled default settings for a specific content type.
63  void UpdateManagedDefaultSetting(ContentSettingsType content_type);
64
65  // Copies of the pref data, so that we can read it on the IO thread.
66  ContentSettings managed_default_content_settings_;
67
68  Profile* profile_;
69
70  // Whether this settings map is for an OTR session.
71  bool is_off_the_record_;
72
73  // Used around accesses to the managed_default_content_settings_ object to
74  // guarantee thread safety.
75  mutable base::Lock lock_;
76
77  PrefChangeRegistrar pref_change_registrar_;
78  NotificationRegistrar notification_registrar_;
79
80  DISALLOW_COPY_AND_ASSIGN(PolicyDefaultProvider);
81};
82
83// PolicyProvider that provider managed content-settings.
84class PolicyProvider : public BaseProvider,
85                       public NotificationObserver {
86 public:
87  explicit PolicyProvider(Profile* profile);
88  ~PolicyProvider();
89  static void RegisterUserPrefs(PrefService* prefs);
90
91  // BaseProvider Implementation
92  virtual void Init();
93
94  // ProviderInterface Implementation
95  virtual bool ContentSettingsTypeIsManaged(
96      ContentSettingsType content_type);
97
98  virtual void SetContentSetting(
99      const ContentSettingsPattern& requesting_pattern,
100      const ContentSettingsPattern& embedding_pattern,
101      ContentSettingsType content_type,
102      const ResourceIdentifier& resource_identifier,
103      ContentSetting content_setting);
104
105  virtual ContentSetting GetContentSetting(
106      const GURL& requesting_url,
107      const GURL& embedding_url,
108      ContentSettingsType content_type,
109      const ResourceIdentifier& resource_identifier) const;
110
111  virtual void ClearAllContentSettingsRules(
112      ContentSettingsType content_type);
113
114  virtual void ResetToDefaults();
115
116  // NotificationObserver implementation.
117  virtual void Observe(NotificationType type,
118                       const NotificationSource& source,
119                       const NotificationDetails& details);
120 private:
121  typedef Tuple5<
122      ContentSettingsPattern,
123      ContentSettingsPattern,
124      ContentSettingsType,
125      content_settings::ProviderInterface::ResourceIdentifier,
126      ContentSetting> ContentSettingsRule;
127
128  typedef std::vector<ContentSettingsRule> ContentSettingsRules;
129
130  void ReadManagedContentSettings(bool overwrite);
131
132  void GetContentSettingsFromPreferences(PrefService* prefs,
133                                         ContentSettingsRules* rules);
134
135  void ReadManagedContentSettingsTypes(
136      ContentSettingsType content_type);
137
138  void NotifyObservers(const ContentSettingsDetails& details);
139
140  void UnregisterObservers();
141
142  Profile* profile_;
143
144  bool content_type_is_managed_[CONTENT_SETTINGS_NUM_TYPES];
145
146  PrefChangeRegistrar pref_change_registrar_;
147  NotificationRegistrar notification_registrar_;
148
149  DISALLOW_COPY_AND_ASSIGN(PolicyProvider);
150};
151
152}  // namespace content_settings
153
154#endif  // CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_POLICY_PROVIDER_H_
155