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. 4 5 #ifndef CHROME_BROWSER_PLUGINS_PLUGIN_PREFS_H_ 6 #define CHROME_BROWSER_PLUGINS_PLUGIN_PREFS_H_ 7 8 #include <map> 9 #include <set> 10 #include <vector> 11 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/content/refcounted_browser_context_keyed_service.h" 19 20 class Profile; 21 22 namespace base { 23 class ListValue; 24 } 25 26 namespace content { 27 struct WebPluginInfo; 28 } 29 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 { 34 public: 35 enum PolicyStatus { 36 NO_POLICY = 0, // Neither enabled or disabled by policy. 37 POLICY_ENABLED, 38 POLICY_DISABLED, 39 }; 40 41 // Returns the instance associated with |profile|, creating it if necessary. 42 static scoped_refptr<PluginPrefs> GetForProfile(Profile* profile); 43 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); 48 49 // Creates a new instance. This method should only be used for testing. 50 PluginPrefs(); 51 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); 56 57 // Enable or disable a plugin group. 58 void EnablePluginGroup(bool enable, const base::string16& group_name); 59 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); 67 68 // Returns whether there is a policy enabling or disabling plug-ins of the 69 // given name. 70 PolicyStatus PolicyStatusForPlugin(const base::string16& name) const; 71 72 // Returns whether the plugin is enabled or not. 73 bool IsPluginEnabled(const content::WebPluginInfo& plugin) const; 74 set_profile(Profile * profile)75 void set_profile(Profile* profile) { profile_ = profile; } 76 77 // RefCountedProfileKeyedBase method override. 78 virtual void ShutdownOnUIThread() OVERRIDE; 79 80 private: 81 friend class base::RefCountedThreadSafe<PluginPrefs>; 82 friend class PluginPrefsTest; 83 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(); 91 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); 96 97 private: 98 base::FilePath ConvertMapKey(const base::FilePath& plugin) const; 99 100 std::map<base::FilePath, bool> state_; 101 }; 102 103 virtual ~PluginPrefs(); 104 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); 109 110 // Allows unit tests to directly set enforced plug-in 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); 115 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); 127 128 // Called on the UI thread with the plugin data to save the preferences. 129 void OnUpdatePreferences(const std::vector<content::WebPluginInfo>& plugins); 130 131 // Sends the notification that plugin data has changed. 132 void NotifyPluginStatusChanged(); 133 134 static void ListValueToStringSet(const base::ListValue* src, 135 std::set<base::string16>* dest); 136 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); 140 141 // Guards access to the following data structures. 142 mutable base::Lock lock_; 143 144 PluginState plugin_state_; 145 std::map<base::string16, bool> plugin_group_state_; 146 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_; 150 151 // Weak pointer, owns us. Only used as a notification source. 152 Profile* profile_; 153 154 // Weak pointer, owned by the profile. 155 PrefService* prefs_; 156 157 PrefChangeRegistrar registrar_; 158 159 DISALLOW_COPY_AND_ASSIGN(PluginPrefs); 160 }; 161 162 #endif // CHROME_BROWSER_PLUGINS_PLUGIN_PREFS_H_ 163