1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_PLUGINS_PLUGIN_PREFS_H_
6 #define CHROME_BROWSER_PLUGINS_PLUGIN_PREFS_H_
12 #include "base/basictypes.h"
13 #include "base/files/file_path.h"
14 #include "base/prefs/pref_change_registrar.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/synchronization/lock.h"
17 #include "chrome/browser/plugins/plugin_finder.h"
18 #include "components/browser_context_keyed_service/refcounted_browser_context_keyed_service.h"
30 // This class stores information about whether a plug-in or a plug-in group is
31 // enabled or disabled.
32 // Except where otherwise noted, it can be used on every thread.
33 class PluginPrefs
: public RefcountedBrowserContextKeyedService
{
36 NO_POLICY
= 0, // Neither enabled or disabled by policy.
41 // Returns the instance associated with |profile|, creating it if necessary.
42 static scoped_refptr
<PluginPrefs
> GetForProfile(Profile
* profile
);
44 // Usually the PluginPrefs associated with a TestingProfile is NULL.
45 // This method overrides that for a given TestingProfile, returning the newly
46 // created PluginPrefs object.
47 static scoped_refptr
<PluginPrefs
> GetForTestingProfile(Profile
* profile
);
49 // Creates a new instance. This method should only be used for testing.
52 // Associates this instance with |prefs|. This enables or disables
53 // plugin groups as defined by the user's preferences.
54 // This method should only be called on the UI thread.
55 void SetPrefs(PrefService
* prefs
);
57 // Enable or disable a plugin group.
58 void EnablePluginGroup(bool enable
, const string16
& group_name
);
60 // Enables or disables a specific plug-in file, if possible.
61 // If the plug-in state can't be changed (because of a policy for example)
62 // then enabling/disabling the plug-in is ignored and |callback| is run
63 // with 'false' passed to it. Otherwise the plug-in state is changed
64 // and |callback| is run with 'true' passed to it.
65 void EnablePlugin(bool enable
, const base::FilePath
& file_path
,
66 const base::Callback
<void(bool)>& callback
);
68 // Returns whether there is a policy enabling or disabling plug-ins of the
70 PolicyStatus
PolicyStatusForPlugin(const string16
& name
) const;
72 // Returns whether the plugin is enabled or not.
73 bool IsPluginEnabled(const content::WebPluginInfo
& plugin
) const;
75 void set_profile(Profile
* profile
) { profile_
= profile
; }
77 // RefCountedProfileKeyedBase method override.
78 virtual void ShutdownOnUIThread() OVERRIDE
;
81 friend class base::RefCountedThreadSafe
<PluginPrefs
>;
82 friend class PluginPrefsTest
;
84 // PluginState stores a mapping from plugin path to enable/disable state. We
85 // don't simply use a std::map, because we would like to keep the state of
86 // some plugins in sync with each other.
92 // Returns whether |plugin| is found. If |plugin| cannot be found,
93 // |*enabled| won't be touched.
94 bool Get(const base::FilePath
& plugin
, bool* enabled
) const;
95 void Set(const base::FilePath
& plugin
, bool enabled
);
98 base::FilePath
ConvertMapKey(const base::FilePath
& plugin
) const;
100 std::map
<base::FilePath
, bool> state_
;
103 virtual ~PluginPrefs();
105 // Called to update one of the policy_xyz patterns below when a
106 // preference changes.
107 void UpdatePatternsAndNotify(std::set
<string16
>* patterns
,
108 const std::string
& pref_name
);
110 // Allows unit tests to directly set enforced plug-in patterns.
111 void SetPolicyEnforcedPluginPatterns(
112 const std::set
<string16
>& disabled_patterns
,
113 const std::set
<string16
>& disabled_exception_patterns
,
114 const std::set
<string16
>& enabled_patterns
);
116 // Callback for after the plugin groups have been loaded.
117 void EnablePluginGroupInternal(
119 const string16
& group_name
,
120 const std::vector
<content::WebPluginInfo
>& plugins
);
121 void EnablePluginInternal(
123 const base::FilePath
& path
,
124 PluginFinder
* plugin_finder
,
125 const base::Callback
<void(bool)>& callback
,
126 const std::vector
<content::WebPluginInfo
>& plugins
);
128 // Called on the UI thread with the plugin data to save the preferences.
129 void OnUpdatePreferences(const std::vector
<content::WebPluginInfo
>& plugins
);
131 // Sends the notification that plugin data has changed.
132 void NotifyPluginStatusChanged();
134 static void ListValueToStringSet(const base::ListValue
* src
,
135 std::set
<string16
>* dest
);
137 // Checks if |name| matches any of the patterns in |pattern_set|.
138 static bool IsStringMatchedInSet(const string16
& name
,
139 const std::set
<string16
>& pattern_set
);
141 // Guards access to the following data structures.
142 mutable base::Lock lock_
;
144 PluginState plugin_state_
;
145 std::map
<string16
, bool> plugin_group_state_
;
147 std::set
<string16
> policy_disabled_plugin_patterns_
;
148 std::set
<string16
> policy_disabled_plugin_exception_patterns_
;
149 std::set
<string16
> policy_enabled_plugin_patterns_
;
151 // Weak pointer, owns us. Only used as a notification source.
154 // Weak pointer, owned by the profile.
157 PrefChangeRegistrar registrar_
;
159 DISALLOW_COPY_AND_ASSIGN(PluginPrefs
);
162 #endif // CHROME_BROWSER_PLUGINS_PLUGIN_PREFS_H_