summaryrefslogtreecommitdiffstats
path: root/components/content_settings/core/browser/content_settings_pref_provider.h
blob: ec235a9407d9fe4950dcc4fcdc783cedcedbdb9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_PROVIDER_H_
#define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_PROVIDER_H_

// A content settings provider that takes its settings out of the pref service.

#include <vector>

#include "base/basictypes.h"
#include "base/memory/scoped_vector.h"
#include "base/prefs/pref_change_registrar.h"
#include "components/content_settings/core/browser/content_settings_observable_provider.h"
#include "components/content_settings/core/browser/content_settings_utils.h"

class PrefService;

namespace base {
class Clock;
class DictionaryValue;
}

namespace user_prefs {
class PrefRegistrySyncable;
}

namespace content_settings {

class ContentSettingsPref;

// Content settings provider that provides content settings from the user
// preference.
class PrefProvider : public ObservableProvider {
 public:
  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

  PrefProvider(PrefService* prefs, bool incognito);
  ~PrefProvider() override;

  // ProviderInterface implementations.
  RuleIterator* GetRuleIterator(ContentSettingsType content_type,
                                const ResourceIdentifier& resource_identifier,
                                bool incognito) const override;

  bool SetWebsiteSetting(const ContentSettingsPattern& primary_pattern,
                         const ContentSettingsPattern& secondary_pattern,
                         ContentSettingsType content_type,
                         const ResourceIdentifier& resource_identifier,
                         base::Value* value) override;

  void ClearAllContentSettingsRules(ContentSettingsType content_type) override;

  void ShutdownOnUIThread() override;

  // Records the last time the given pattern has used a certain content setting.
  void UpdateLastUsage(const ContentSettingsPattern& primary_pattern,
                       const ContentSettingsPattern& secondary_pattern,
                       ContentSettingsType content_type);

  base::Time GetLastUsage(const ContentSettingsPattern& primary_pattern,
                          const ContentSettingsPattern& secondary_pattern,
                          ContentSettingsType content_type);

  void Notify(const ContentSettingsPattern& primary_pattern,
              const ContentSettingsPattern& secondary_pattern,
              ContentSettingsType content_type,
              const std::string& resource_identifier);

  // Gains ownership of |clock|.
  void SetClockForTesting(scoped_ptr<base::Clock> clock);

 private:
  friend class DeadlockCheckerThread;  // For testing.

  // Migrate the old media setting into new mic/camera content settings.
  void MigrateObsoleteMediaContentSetting();

  // Migrate the settings from the old aggregate dictionary into the new format.
  void MigrateAllExceptions();

  // Writes the contents of the old aggregate dictionary preferences into
  // separate dictionaries for content types. If |syncable_only| is true,
  // only syncable content types will be written.
  void WriteSettingsToNewPreferences(bool syncable_only);

  // Weak; owned by the Profile and reset in ShutdownOnUIThread.
  PrefService* prefs_;

  // Can be set for testing.
  scoped_ptr<base::Clock> clock_;

  bool is_incognito_;

  PrefChangeRegistrar pref_change_registrar_;

  ScopedVector<ContentSettingsPref> content_settings_prefs_;

  DISALLOW_COPY_AND_ASSIGN(PrefProvider);

  bool TestAllLocks() const;

  // All functionality regarding reading and writing of preferences has been
  // moved to |ContentSettingsPref|, which manages one content type per
  // instance. However, for backward compatibility, we need to be able to write
  // to the old and deprecated aggregate dictionary preference which maintains
  // all content types. Therefore, |ContentSettingsPrefProvider| must still
  // retain some of the functionality of |ContentSettingsPref|. The following
  // attributes and methods serve this purpose.
  // TODO(msramek): Remove this migration code after two stable releases.
  struct ContentSettingsPrefEntry {
    ContentSettingsPrefEntry(const ContentSettingsPattern primary_pattern,
                             const ContentSettingsPattern secondary_pattern,
                             const ResourceIdentifier resource_identifier,
                             base::Value* value);
    ContentSettingsPrefEntry(const ContentSettingsPrefEntry& entry);
    ContentSettingsPrefEntry& operator=(const ContentSettingsPrefEntry& entry);
    ~ContentSettingsPrefEntry();

    ContentSettingsPattern primary_pattern;
    ContentSettingsPattern secondary_pattern;
    ResourceIdentifier resource_identifier;
    scoped_ptr<base::Value> value;
  };

  // Stores exceptions read from the old preference before writing them
  // to the new one.
  ScopedVector<ContentSettingsPrefEntry>
      pref_entry_map_[CONTENT_SETTINGS_NUM_TYPES];

  // Clears |pref_entry_map_|.
  void ClearPrefEntryMap();

  // Guards access to |pref_entry_map_|.
  mutable base::Lock old_lock_;

  // Indicates whether the old preferences are updated.
  bool updating_old_preferences_;

  // Called when the old preference changes.
  void OnOldContentSettingsPatternPairsChanged();

  // Reads the old preference and writes it to |pref_entry_map_|.
  void ReadContentSettingsFromOldPref();

  base::ThreadChecker thread_checker_;
};

}  // namespace content_settings

#endif  // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_PREF_PROVIDER_H_