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 "chrome/browser/profiles/refcounted_profile_keyed_service.h"
33 // This class stores information about whether a plug-in or a plug-in group is
34 // enabled or disabled.
35 // Except where otherwise noted, it can be used on every thread.
36 class PluginPrefs
: public RefcountedProfileKeyedService
{
39 NO_POLICY
= 0, // Neither enabled or disabled by policy.
44 // Returns the instance associated with |profile|, creating it if necessary.
45 static scoped_refptr
<PluginPrefs
> GetForProfile(Profile
* profile
);
47 // Usually the PluginPrefs associated with a TestingProfile is NULL.
48 // This method overrides that for a given TestingProfile, returning the newly
49 // created PluginPrefs object.
50 static scoped_refptr
<PluginPrefs
> GetForTestingProfile(Profile
* profile
);
52 // Sets the plug-in list for tests.
53 void SetPluginListForTesting(webkit::npapi::PluginList
* plugin_list
);
55 // Creates a new instance. This method should only be used for testing.
58 // Associates this instance with |prefs|. This enables or disables
59 // plugin groups as defined by the user's preferences.
60 // This method should only be called on the UI thread.
61 void SetPrefs(PrefService
* prefs
);
63 // Enable or disable a plugin group.
64 void EnablePluginGroup(bool enable
, const string16
& group_name
);
66 // Enables or disables a specific plug-in file, if possible.
67 // If the plug-in state can't be changed (because of a policy for example)
68 // then enabling/disabling the plug-in is ignored and |callback| is run
69 // with 'false' passed to it. Otherwise the plug-in state is changed
70 // and |callback| is run with 'true' passed to it.
71 void EnablePlugin(bool enable
, const base::FilePath
& file_path
,
72 const base::Callback
<void(bool)>& callback
);
74 // Returns whether there is a policy enabling or disabling plug-ins of the
76 PolicyStatus
PolicyStatusForPlugin(const string16
& name
) const;
78 // Returns whether the plugin is enabled or not.
79 bool IsPluginEnabled(const webkit::WebPluginInfo
& plugin
) const;
81 void set_profile(Profile
* profile
) { profile_
= profile
; }
83 // RefCountedProfileKeyedBase method override.
84 virtual void ShutdownOnUIThread() OVERRIDE
;
87 friend class base::RefCountedThreadSafe
<PluginPrefs
>;
88 friend class PluginPrefsTest
;
90 // PluginState stores a mapping from plugin path to enable/disable state. We
91 // don't simply use a std::map, because we would like to keep the state of
92 // some plugins in sync with each other.
98 // Returns whether |plugin| is found. If |plugin| cannot be found,
99 // |*enabled| won't be touched.
100 bool Get(const base::FilePath
& plugin
, bool* enabled
) const;
101 void Set(const base::FilePath
& plugin
, bool enabled
);
104 base::FilePath
ConvertMapKey(const base::FilePath
& plugin
) const;
106 std::map
<base::FilePath
, bool> state_
;
109 virtual ~PluginPrefs();
111 // Called to update one of the policy_xyz patterns below when a
112 // preference changes.
113 void UpdatePatternsAndNotify(std::set
<string16
>* patterns
,
114 const std::string
& pref_name
);
116 // Allows unit tests to directly set enforced plug-in patterns.
117 void SetPolicyEnforcedPluginPatterns(
118 const std::set
<string16
>& disabled_patterns
,
119 const std::set
<string16
>& disabled_exception_patterns
,
120 const std::set
<string16
>& enabled_patterns
);
122 // Returns the plugin list to use, either the singleton or the override.
123 webkit::npapi::PluginList
* GetPluginList() const;
125 // Callback for after the plugin groups have been loaded.
126 void EnablePluginGroupInternal(
128 const string16
& group_name
,
129 const std::vector
<webkit::WebPluginInfo
>& plugins
);
130 void EnablePluginInternal(
132 const base::FilePath
& path
,
133 PluginFinder
* plugin_finder
,
134 const base::Callback
<void(bool)>& callback
,
135 const std::vector
<webkit::WebPluginInfo
>& plugins
);
137 // Called on the file thread to get the data necessary to update the saved
139 void GetPreferencesDataOnFileThread();
141 // Called on the UI thread with the plugin data to save the preferences.
142 void OnUpdatePreferences(const std::vector
<webkit::WebPluginInfo
>& plugins
);
144 // Sends the notification that plugin data has changed.
145 void NotifyPluginStatusChanged();
147 static void ListValueToStringSet(const base::ListValue
* src
,
148 std::set
<string16
>* dest
);
150 // Checks if |name| matches any of the patterns in |pattern_set|.
151 static bool IsStringMatchedInSet(const string16
& name
,
152 const std::set
<string16
>& pattern_set
);
154 // Guards access to the following data structures.
155 mutable base::Lock lock_
;
157 PluginState plugin_state_
;
158 std::map
<string16
, bool> plugin_group_state_
;
160 std::set
<string16
> policy_disabled_plugin_patterns_
;
161 std::set
<string16
> policy_disabled_plugin_exception_patterns_
;
162 std::set
<string16
> policy_enabled_plugin_patterns_
;
164 // Weak pointer, owns us. Only used as a notification source.
167 // Weak pointer, owned by the profile.
170 // PluginList to use for testing. If this is NULL, defaults to the global
172 webkit::npapi::PluginList
* plugin_list_
;
174 PrefChangeRegistrar registrar_
;
176 DISALLOW_COPY_AND_ASSIGN(PluginPrefs
);
179 #endif // CHROME_BROWSER_PLUGINS_PLUGIN_PREFS_H_