Home | History | Annotate | Download | only in content_settings
      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_PREF_PROVIDER_H_
      6 #define CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PREF_PROVIDER_H_
      7 #pragma once
      8 
      9 // A content settings provider that takes its settings out of the pref service.
     10 
     11 #include <map>
     12 #include <string>
     13 #include <utility>
     14 
     15 #include "base/basictypes.h"
     16 #include "base/synchronization/lock.h"
     17 #include "chrome/browser/content_settings/content_settings_base_provider.h"
     18 #include "chrome/browser/content_settings/content_settings_provider.h"
     19 #include "chrome/browser/prefs/pref_change_registrar.h"
     20 #include "content/common/notification_observer.h"
     21 #include "content/common/notification_registrar.h"
     22 
     23 class ContentSettingsDetails;
     24 class DictionaryValue;
     25 class PrefService;
     26 class Profile;
     27 
     28 namespace content_settings {
     29 
     30 // Content settings provider that provides default content settings based on
     31 // user prefs.
     32 class PrefDefaultProvider : public DefaultProviderInterface,
     33                             public NotificationObserver {
     34  public:
     35   explicit PrefDefaultProvider(Profile* profile);
     36   virtual ~PrefDefaultProvider();
     37 
     38   // DefaultContentSettingsProvider implementation.
     39   virtual ContentSetting ProvideDefaultSetting(
     40       ContentSettingsType content_type) const;
     41   virtual void UpdateDefaultSetting(ContentSettingsType content_type,
     42                                     ContentSetting setting);
     43   virtual void ResetToDefaults();
     44   virtual bool DefaultSettingIsManaged(ContentSettingsType content_type) const;
     45 
     46   static void RegisterUserPrefs(PrefService* prefs);
     47 
     48   // NotificationObserver implementation.
     49   virtual void Observe(NotificationType type,
     50                        const NotificationSource& source,
     51                        const NotificationDetails& details);
     52 
     53  private:
     54   // Informs observers that content settings have changed. Make sure that
     55   // |lock_| is not held when calling this, as listeners will usually call one
     56   // of the GetSettings functions in response, which would then lead to a
     57   // mutex deadlock.
     58   void NotifyObservers(const ContentSettingsDetails& details);
     59 
     60   void UnregisterObservers();
     61 
     62   // Sets the fields of |settings| based on the values in |dictionary|.
     63   void GetSettingsFromDictionary(const DictionaryValue* dictionary,
     64                                  ContentSettings* settings);
     65 
     66   // Forces the default settings to be explicitly set instead of themselves
     67   // being CONTENT_SETTING_DEFAULT.
     68   void ForceDefaultsToBeExplicit();
     69 
     70   // Reads the default settings from the preferences service. If |overwrite| is
     71   // true and the preference is missing, the local copy will be cleared as well.
     72   void ReadDefaultSettings(bool overwrite);
     73 
     74   void MigrateObsoleteNotificationPref(PrefService* prefs);
     75 
     76   // Copies of the pref data, so that we can read it on the IO thread.
     77   ContentSettings default_content_settings_;
     78 
     79   Profile* profile_;
     80 
     81   // Whether this settings map is for an Incognito session.
     82   bool is_incognito_;
     83 
     84   // Used around accesses to the default_content_settings_ object to guarantee
     85   // thread safety.
     86   mutable base::Lock lock_;
     87 
     88   PrefChangeRegistrar pref_change_registrar_;
     89   NotificationRegistrar notification_registrar_;
     90 
     91   // Whether we are currently updating preferences, this is used to ignore
     92   // notifications from the preferences service that we triggered ourself.
     93   bool updating_preferences_;
     94 
     95   bool initializing_;
     96 
     97   DISALLOW_COPY_AND_ASSIGN(PrefDefaultProvider);
     98 };
     99 
    100 // Content settings provider that provides content settings from the user
    101 // preference.
    102 class PrefProvider : public BaseProvider,
    103                      public NotificationObserver {
    104  public:
    105   static void RegisterUserPrefs(PrefService* prefs);
    106 
    107   explicit PrefProvider(Profile* profile);
    108   virtual ~PrefProvider();
    109 
    110   // ContentSettingsProvider implementation.
    111   virtual bool ContentSettingsTypeIsManaged(
    112       ContentSettingsType content_type);
    113 
    114   virtual void SetContentSetting(
    115       const ContentSettingsPattern& requesting_pattern,
    116       const ContentSettingsPattern& embedding_pattern,
    117       ContentSettingsType content_type,
    118       const ResourceIdentifier& resource_identifier,
    119       ContentSetting content_setting);
    120 
    121   virtual void ClearAllContentSettingsRules(
    122       ContentSettingsType content_type);
    123 
    124   virtual void ResetToDefaults();
    125 
    126   // BaseProvider implementations.
    127   virtual void Init();
    128 
    129   // NotificationObserver implementation.
    130   virtual void Observe(NotificationType type,
    131                        const NotificationSource& source,
    132                        const NotificationDetails& details);
    133 
    134  private:
    135   void ReadExceptions(bool overwrite);
    136 
    137   // Various migration methods (old cookie, popup and per-host data gets
    138   // migrated to the new format).
    139   void MigrateObsoletePerhostPref(PrefService* prefs);
    140   void MigrateObsoletePopupsPref(PrefService* prefs);
    141 
    142   void CanonicalizeContentSettingsExceptions(
    143       DictionaryValue* all_settings_dictionary);
    144 
    145   void GetSettingsFromDictionary(
    146       const DictionaryValue* dictionary,
    147       ContentSettings* settings);
    148 
    149   void GetResourceSettingsFromDictionary(
    150       const DictionaryValue* dictionary,
    151       ResourceContentSettings* settings);
    152 
    153   void NotifyObservers(const ContentSettingsDetails& details);
    154 
    155   void UnregisterObservers();
    156 
    157   Profile* profile_;
    158 
    159   PrefChangeRegistrar pref_change_registrar_;
    160   NotificationRegistrar notification_registrar_;
    161 
    162   // Whether we are currently updating preferences, this is used to ignore
    163   // notifications from the preferences service that we triggered ourself.
    164   bool updating_preferences_;
    165 
    166   // Do not fire any Notifications as long as we are in the constructor.
    167   bool initializing_;
    168 
    169   DISALLOW_COPY_AND_ASSIGN(PrefProvider);
    170 };
    171 
    172 }  // namespace content_settings
    173 
    174 #endif  // CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PREF_PROVIDER_H_
    175