Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / plugins / plugin_prefs.h
blobd95db2d19ede600236fcbef7e0ae308184abe690
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_
8 #include <map>
9 #include <set>
10 #include <vector>
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/keyed_service/core/refcounted_keyed_service.h"
20 class Profile;
22 namespace base {
23 class ListValue;
26 namespace content {
27 struct WebPluginInfo;
30 // This class stores information about whether a plugin or a plugin group is
31 // enabled or disabled.
32 // Except where otherwise noted, it can be used on every thread.
33 class PluginPrefs : public RefcountedKeyedService {
34 public:
35 enum PolicyStatus {
36 NO_POLICY = 0, // Neither enabled or disabled by policy.
37 POLICY_ENABLED,
38 POLICY_DISABLED,
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.
50 PluginPrefs();
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 base::string16& group_name);
60 // Enables or disables a specific plugin file, if possible.
61 // If the plugin state can't be changed (because of a policy for example)
62 // then enabling/disabling the plugin is ignored and |callback| is run
63 // with 'false' passed to it. Otherwise the plugin 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 plugins of the
69 // given name.
70 PolicyStatus PolicyStatusForPlugin(const base::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 void ShutdownOnUIThread() override;
80 private:
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.
87 class PluginState {
88 public:
89 PluginState();
90 ~PluginState();
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);
97 private:
98 base::FilePath ConvertMapKey(const base::FilePath& plugin) const;
100 std::map<base::FilePath, bool> state_;
103 ~PluginPrefs() override;
105 // Called to update one of the policy_xyz patterns below when a
106 // preference changes.
107 void UpdatePatternsAndNotify(std::set<base::string16>* patterns,
108 const std::string& pref_name);
110 // Allows unit tests to directly set enforced plugin patterns.
111 void SetPolicyEnforcedPluginPatterns(
112 const std::set<base::string16>& disabled_patterns,
113 const std::set<base::string16>& disabled_exception_patterns,
114 const std::set<base::string16>& enabled_patterns);
116 // Callback for after the plugin groups have been loaded.
117 void EnablePluginGroupInternal(
118 bool enabled,
119 const base::string16& group_name,
120 const std::vector<content::WebPluginInfo>& plugins);
121 void EnablePluginInternal(
122 bool enabled,
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<base::string16>* dest);
137 // Checks if |name| matches any of the patterns in |pattern_set|.
138 static bool IsStringMatchedInSet(const base::string16& name,
139 const std::set<base::string16>& pattern_set);
141 // Guards access to the following data structures.
142 mutable base::Lock lock_;
144 PluginState plugin_state_;
145 std::map<base::string16, bool> plugin_group_state_;
147 std::set<base::string16> policy_disabled_plugin_patterns_;
148 std::set<base::string16> policy_disabled_plugin_exception_patterns_;
149 std::set<base::string16> policy_enabled_plugin_patterns_;
151 // Weak pointer, owns us. Only used as a notification source.
152 Profile* profile_;
154 // Weak pointer, owned by the profile.
155 PrefService* prefs_;
157 PrefChangeRegistrar registrar_;
159 DISALLOW_COPY_AND_ASSIGN(PluginPrefs);
162 #endif // CHROME_BROWSER_PLUGINS_PLUGIN_PREFS_H_