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_DEFAULT_PROVIDER_H_
6#define CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_DEFAULT_PROVIDER_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/memory/linked_ptr.h"
14#include "base/prefs/pref_change_registrar.h"
15#include "base/synchronization/lock.h"
16#include "chrome/browser/content_settings/content_settings_observable_provider.h"
17
18class PrefService;
19
20namespace user_prefs {
21class PrefRegistrySyncable;
22}
23
24namespace content_settings {
25
26// Provider that provides default content settings based on
27// user prefs. If no default values are set by the user we use the hard coded
28// default values.
29class DefaultProvider : public ObservableProvider {
30 public:
31  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
32
33  DefaultProvider(PrefService* prefs,
34                  bool incognito);
35  virtual ~DefaultProvider();
36
37  // ProviderInterface implementations.
38  virtual RuleIterator* GetRuleIterator(
39      ContentSettingsType content_type,
40      const ResourceIdentifier& resource_identifier,
41      bool incognito) const OVERRIDE;
42
43  virtual bool SetWebsiteSetting(
44      const ContentSettingsPattern& primary_pattern,
45      const ContentSettingsPattern& secondary_pattern,
46      ContentSettingsType content_type,
47      const ResourceIdentifier& resource_identifier,
48      Value* value) OVERRIDE;
49
50  virtual void ClearAllContentSettingsRules(
51      ContentSettingsType content_type) OVERRIDE;
52
53  virtual void ShutdownOnUIThread() OVERRIDE;
54
55 private:
56  // Sets the fields of |settings| based on the values in |dictionary|.
57  void GetSettingsFromDictionary(const base::DictionaryValue* dictionary);
58
59  // Forces the default settings to be explicitly set instead of themselves
60  // being CONTENT_SETTING_DEFAULT.
61  void ForceDefaultsToBeExplicit();
62
63  // Reads the default settings from the preferences service. If |overwrite| is
64  // true and the preference is missing, the local copy will be cleared as well.
65  void ReadDefaultSettings(bool overwrite);
66
67  // Called on prefs change.
68  void OnPreferenceChanged(const std::string& pref_name);
69
70  typedef linked_ptr<base::Value> ValuePtr;
71  typedef std::map<ContentSettingsType, ValuePtr> ValueMap;
72  // Copies of the pref data, so that we can read it on the IO thread.
73  ValueMap default_settings_;
74
75  PrefService* prefs_;
76
77  // Whether this settings map is for an Incognito session.
78  bool is_incognito_;
79
80  // Used around accesses to the |default_content_settings_| object to guarantee
81  // thread safety.
82  mutable base::Lock lock_;
83
84  PrefChangeRegistrar pref_change_registrar_;
85
86  // Whether we are currently updating preferences, this is used to ignore
87  // notifications from the preferences service that we triggered ourself.
88  bool updating_preferences_;
89
90  DISALLOW_COPY_AND_ASSIGN(DefaultProvider);
91};
92
93}  // namespace content_settings
94
95#endif  // CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_DEFAULT_PROVIDER_H_
96