blob: 51be80b1e5b31b0335a07615e451d16ad0e2d145 [file] [log] [blame]
[email protected]f8c85dd2012-01-12 02:26:431// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0a8b1e22010-07-02 09:31:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]8a2713682011-08-19 10:36:595#include "chrome/browser/plugin_prefs.h"
[email protected]0a8b1e22010-07-02 09:31:116
7#include <string>
[email protected]0a8b1e22010-07-02 09:31:118
[email protected]ff4fa712011-09-28 22:05:139#include "base/bind.h"
[email protected]8a2713682011-08-19 10:36:5910#include "base/command_line.h"
[email protected]f7be2cba2011-09-15 15:33:4511#include "base/lazy_instance.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/scoped_ptr.h"
[email protected]8a2713682011-08-19 10:36:5913#include "base/memory/singleton.h"
[email protected]a446a532010-10-11 17:24:5214#include "base/message_loop.h"
[email protected]0a8b1e22010-07-02 09:31:1115#include "base/path_service.h"
[email protected]f7be2cba2011-09-15 15:33:4516#include "base/string_util.h"
[email protected]cceaf852010-10-07 22:01:1317#include "base/utf_string_conversions.h"
[email protected]0a8b1e22010-07-02 09:31:1118#include "base/values.h"
[email protected]daf66aa2010-08-06 06:24:2819#include "base/version.h"
[email protected]f7be2cba2011-09-15 15:33:4520#include "chrome/browser/browser_process.h"
[email protected]a42f2e22011-10-10 22:28:1721#include "chrome/browser/plugin_prefs_factory.h"
[email protected]805cd372011-04-04 12:39:5622#include "chrome/browser/prefs/scoped_user_pref_update.h"
[email protected]8ecad5e2010-12-02 21:18:3323#include "chrome/browser/profiles/profile.h"
[email protected]8a2713682011-08-19 10:36:5924#include "chrome/browser/profiles/profile_keyed_service.h"
[email protected]f7be2cba2011-09-15 15:33:4525#include "chrome/browser/profiles/profile_manager.h"
[email protected]cebc3dc2011-04-18 17:15:0026#include "chrome/common/chrome_content_client.h"
[email protected]432115822011-07-10 15:52:2727#include "chrome/common/chrome_notification_types.h"
[email protected]ed17c7b2010-07-14 12:03:3328#include "chrome/common/chrome_paths.h"
[email protected]8a2713682011-08-19 10:36:5929#include "chrome/common/chrome_switches.h"
[email protected]0a8b1e22010-07-02 09:31:1130#include "chrome/common/pref_names.h"
[email protected]c38831a12011-10-28 12:44:4931#include "content/public/browser/browser_thread.h"
[email protected]ad50def52011-10-19 23:17:0732#include "content/public/browser/notification_service.h"
[email protected]3a5180ae2011-12-21 02:39:3833#include "content/public/browser/plugin_service.h"
[email protected]f7be2cba2011-09-15 15:33:4534#include "webkit/plugins/npapi/plugin_group.h"
[email protected]191eb3f72010-12-21 06:27:5035#include "webkit/plugins/npapi/plugin_list.h"
[email protected]91d9f3d2011-08-14 05:24:4436#include "webkit/plugins/webplugininfo.h"
[email protected]0a8b1e22010-07-02 09:31:1137
[email protected]631bb742011-11-02 11:29:3938using content::BrowserThread;
[email protected]3a5180ae2011-12-21 02:39:3839using content::PluginService;
[email protected]631bb742011-11-02 11:29:3940
[email protected]8a2713682011-08-19 10:36:5941namespace {
42
[email protected]f7be2cba2011-09-15 15:33:4543// Default state for a plug-in (not state of the default plug-in!).
44// Accessed only on the UI thread.
[email protected]6de0fd1d2011-11-15 13:31:4945base::LazyInstance<std::map<FilePath, bool> > g_default_plugin_state =
46 LAZY_INSTANCE_INITIALIZER;
[email protected]f7be2cba2011-09-15 15:33:4547
[email protected]a42f2e22011-10-10 22:28:1748} // namespace
[email protected]8a2713682011-08-19 10:36:5949
[email protected]41a9a3d2010-10-27 19:25:4350// How long to wait to save the plugin enabled information, which might need to
51// go to disk.
52#define kPluginUpdateDelayMs (60 * 1000)
53
[email protected]8a2713682011-08-19 10:36:5954// static
55PluginPrefs* PluginPrefs::GetForProfile(Profile* profile) {
[email protected]476a27032011-09-13 13:40:4956 PluginPrefsWrapper* wrapper =
[email protected]a42f2e22011-10-10 22:28:1757 PluginPrefsFactory::GetInstance()->GetWrapperForProfile(profile);
[email protected]476a27032011-09-13 13:40:4958 if (!wrapper)
59 return NULL;
60 return wrapper->plugin_prefs();
[email protected]8a2713682011-08-19 10:36:5961}
62
[email protected]f7be2cba2011-09-15 15:33:4563// static
64PluginPrefs* PluginPrefs::GetForTestingProfile(Profile* profile) {
65 ProfileKeyedService* wrapper =
[email protected]a42f2e22011-10-10 22:28:1766 PluginPrefsFactory::GetInstance()->SetTestingFactoryAndUse(
67 profile, &PluginPrefsFactory::CreateWrapperForProfile);
[email protected]f7be2cba2011-09-15 15:33:4568 return static_cast<PluginPrefsWrapper*>(wrapper)->plugin_prefs();
[email protected]ed17c7b2010-07-14 12:03:3369}
70
[email protected]672b2c6f2011-09-27 02:34:2571void PluginPrefs::SetPluginListForTesting(
72 webkit::npapi::PluginList* plugin_list) {
73 plugin_list_ = plugin_list;
74}
75
[email protected]f7be2cba2011-09-15 15:33:4576void PluginPrefs::EnablePluginGroup(bool enabled, const string16& group_name) {
[email protected]183d4b82011-11-11 18:50:2677 PluginService::GetInstance()->GetPluginGroups(
78 base::Bind(&PluginPrefs::EnablePluginGroupInternal,
79 this, enabled, group_name));
80}
[email protected]f7be2cba2011-09-15 15:33:4581
[email protected]183d4b82011-11-11 18:50:2682void PluginPrefs::EnablePluginGroupInternal(
83 bool enabled,
84 const string16& group_name,
85 const std::vector<webkit::npapi::PluginGroup>& groups) {
[email protected]f7be2cba2011-09-15 15:33:4586 base::AutoLock auto_lock(lock_);
87
88 // Set the desired state for the group.
89 plugin_group_state_[group_name] = enabled;
90
91 // Update the state for all plug-ins in the group.
92 for (size_t i = 0; i < groups.size(); ++i) {
93 if (groups[i].GetGroupName() != group_name)
94 continue;
95 const std::vector<webkit::WebPluginInfo>& plugins =
96 groups[i].web_plugin_infos();
97 for (size_t j = 0; j < plugins.size(); ++j)
98 plugin_state_[plugins[j].path] = enabled;
99 break;
100 }
101
102 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]ff4fa712011-09-28 22:05:13103 base::Bind(&PluginPrefs::OnUpdatePreferences, this, groups));
[email protected]f7be2cba2011-09-15 15:33:45104 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]ff4fa712011-09-28 22:05:13105 base::Bind(&PluginPrefs::NotifyPluginStatusChanged, this));
[email protected]0a8b1e22010-07-02 09:31:11106}
107
[email protected]672b2c6f2011-09-27 02:34:25108bool PluginPrefs::EnablePlugin(bool enabled, const FilePath& path) {
109 // Do policy checks first. These don't need to run on the FILE thread.
110 webkit::npapi::PluginList* plugin_list = GetPluginList();
111 webkit::WebPluginInfo plugin;
[email protected]88ca4912011-10-12 14:00:43112 if (PluginService::GetInstance()->GetPluginInfoByPath(path, &plugin)) {
[email protected]672b2c6f2011-09-27 02:34:25113 scoped_ptr<webkit::npapi::PluginGroup> group(
114 plugin_list->GetPluginGroup(plugin));
115 PolicyStatus plugin_status = PolicyStatusForPlugin(plugin.name);
116 PolicyStatus group_status = PolicyStatusForPlugin(group->GetGroupName());
117 if (enabled) {
118 if (plugin_status == POLICY_DISABLED || group_status == POLICY_DISABLED)
119 return false;
120 } else {
121 if (plugin_status == POLICY_ENABLED || group_status == POLICY_ENABLED)
122 return false;
123 }
124 }
125
[email protected]183d4b82011-11-11 18:50:26126 PluginService::GetInstance()->GetPluginGroups(
127 base::Bind(&PluginPrefs::EnablePluginInternal, this, enabled, path));
[email protected]ff4fa712011-09-28 22:05:13128 return true;
129}
[email protected]492d2142010-09-10 13:55:18130
[email protected]183d4b82011-11-11 18:50:26131void PluginPrefs::EnablePluginInternal(
132 bool enabled,
133 const FilePath& path,
134 const std::vector<webkit::npapi::PluginGroup>& groups) {
[email protected]f7be2cba2011-09-15 15:33:45135 {
136 // Set the desired state for the plug-in.
137 base::AutoLock auto_lock(lock_);
138 plugin_state_[path] = enabled;
139 }
140
[email protected]f7be2cba2011-09-15 15:33:45141 bool found_group = false;
142 for (size_t i = 0; i < groups.size(); ++i) {
143 bool all_disabled = true;
144 const std::vector<webkit::WebPluginInfo>& plugins =
145 groups[i].web_plugin_infos();
146 for (size_t j = 0; j < plugins.size(); ++j) {
147 all_disabled = all_disabled && !IsPluginEnabled(plugins[j]);
148 if (plugins[j].path == path) {
149 found_group = true;
150 DCHECK_EQ(enabled, IsPluginEnabled(plugins[j]));
151 }
152 }
153 if (found_group) {
154 // Update the state for the corresponding plug-in group.
155 base::AutoLock auto_lock(lock_);
156 plugin_group_state_[groups[i].GetGroupName()] = !all_disabled;
157 break;
158 }
159 }
160
161 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]ff4fa712011-09-28 22:05:13162 base::Bind(&PluginPrefs::OnUpdatePreferences, this, groups));
[email protected]f7be2cba2011-09-15 15:33:45163 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]ff4fa712011-09-28 22:05:13164 base::Bind(&PluginPrefs::NotifyPluginStatusChanged, this));
[email protected]f7be2cba2011-09-15 15:33:45165}
166
167// static
[email protected]672b2c6f2011-09-27 02:34:25168bool PluginPrefs::EnablePluginGlobally(bool enable, const FilePath& file_path) {
[email protected]f7be2cba2011-09-15 15:33:45169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
170 g_default_plugin_state.Get()[file_path] = enable;
171 std::vector<Profile*> profiles =
172 g_browser_process->profile_manager()->GetLoadedProfiles();
[email protected]672b2c6f2011-09-27 02:34:25173 bool result = true;
[email protected]f7be2cba2011-09-15 15:33:45174 for (std::vector<Profile*>::iterator it = profiles.begin();
175 it != profiles.end(); ++it) {
176 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(*it);
177 DCHECK(plugin_prefs);
[email protected]672b2c6f2011-09-27 02:34:25178 if (!plugin_prefs->EnablePlugin(enable, file_path))
179 result = false;
[email protected]f7be2cba2011-09-15 15:33:45180 }
[email protected]672b2c6f2011-09-27 02:34:25181 return result;
[email protected]f7be2cba2011-09-15 15:33:45182}
183
184PluginPrefs::PolicyStatus PluginPrefs::PolicyStatusForPlugin(
[email protected]f8c85dd2012-01-12 02:26:43185 const string16& name) const {
[email protected]f7be2cba2011-09-15 15:33:45186 base::AutoLock auto_lock(lock_);
187 if (IsStringMatchedInSet(name, policy_enabled_plugin_patterns_)) {
188 return POLICY_ENABLED;
189 } else if (IsStringMatchedInSet(name, policy_disabled_plugin_patterns_) &&
190 !IsStringMatchedInSet(
191 name, policy_disabled_plugin_exception_patterns_)) {
192 return POLICY_DISABLED;
193 } else {
194 return NO_POLICY;
195 }
[email protected]0a8b1e22010-07-02 09:31:11196}
197
[email protected]f8c85dd2012-01-12 02:26:43198bool PluginPrefs::IsPluginEnabled(const webkit::WebPluginInfo& plugin) const {
[email protected]f7be2cba2011-09-15 15:33:45199 scoped_ptr<webkit::npapi::PluginGroup> group(
[email protected]672b2c6f2011-09-27 02:34:25200 GetPluginList()->GetPluginGroup(plugin));
[email protected]f7be2cba2011-09-15 15:33:45201 string16 group_name = group->GetGroupName();
202
203 // Check if the plug-in or its group is enabled by policy.
204 PolicyStatus plugin_status = PolicyStatusForPlugin(plugin.name);
205 PolicyStatus group_status = PolicyStatusForPlugin(group_name);
206 if (plugin_status == POLICY_ENABLED || group_status == POLICY_ENABLED)
207 return true;
208
209 // Check if the plug-in or its group is disabled by policy.
210 if (plugin_status == POLICY_DISABLED || group_status == POLICY_DISABLED)
211 return false;
212
[email protected]8a2713682011-08-19 10:36:59213 // If enabling NaCl, make sure the plugin is also enabled. See bug
214 // http://code.google.com/p/chromium/issues/detail?id=81010 for more
215 // information.
216 // TODO(dspringer): When NaCl is on by default, remove this code.
217 if ((plugin.name ==
218 ASCIIToUTF16(chrome::ChromeContentClient::kNaClPluginName)) &&
219 CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableNaCl)) {
220 return true;
221 }
[email protected]f7be2cba2011-09-15 15:33:45222
223 base::AutoLock auto_lock(lock_);
224 // Check user preferences for the plug-in.
[email protected]f8c85dd2012-01-12 02:26:43225 std::map<FilePath, bool>::const_iterator plugin_it =
[email protected]f7be2cba2011-09-15 15:33:45226 plugin_state_.find(plugin.path);
227 if (plugin_it != plugin_state_.end())
228 return plugin_it->second;
229
230 // Check user preferences for the plug-in group.
[email protected]f8c85dd2012-01-12 02:26:43231 std::map<string16, bool>::const_iterator group_it(
[email protected]f7be2cba2011-09-15 15:33:45232 plugin_group_state_.find(plugin.name));
233 if (group_it != plugin_group_state_.end())
234 return group_it->second;
235
236 // Default to enabled.
237 return true;
[email protected]8a2713682011-08-19 10:36:59238}
239
240void PluginPrefs::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53241 const content::NotificationSource& source,
242 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27243 DCHECK_EQ(chrome::NOTIFICATION_PREF_CHANGED, type);
[email protected]6c2381d2011-10-19 02:52:53244 const std::string* pref_name = content::Details<std::string>(details).ptr();
[email protected]492d2142010-09-10 13:55:18245 if (!pref_name) {
246 NOTREACHED();
247 return;
248 }
[email protected]6c2381d2011-10-19 02:52:53249 DCHECK_EQ(prefs_, content::Source<PrefService>(source).ptr());
[email protected]f7be2cba2011-09-15 15:33:45250 if (*pref_name == prefs::kPluginsDisabledPlugins) {
251 base::AutoLock auto_lock(lock_);
252 ListValueToStringSet(prefs_->GetList(prefs::kPluginsDisabledPlugins),
253 &policy_disabled_plugin_patterns_);
254 } else if (*pref_name == prefs::kPluginsDisabledPluginsExceptions) {
255 base::AutoLock auto_lock(lock_);
256 ListValueToStringSet(
257 prefs_->GetList(prefs::kPluginsDisabledPluginsExceptions),
258 &policy_disabled_plugin_exception_patterns_);
259 } else if (*pref_name == prefs::kPluginsEnabledPlugins) {
260 base::AutoLock auto_lock(lock_);
261 ListValueToStringSet(prefs_->GetList(prefs::kPluginsEnabledPlugins),
262 &policy_enabled_plugin_patterns_);
263 } else {
264 NOTREACHED();
[email protected]492d2142010-09-10 13:55:18265 }
[email protected]a446a532010-10-11 17:24:52266 NotifyPluginStatusChanged();
[email protected]492d2142010-09-10 13:55:18267}
268
[email protected]f7be2cba2011-09-15 15:33:45269/*static*/
270bool PluginPrefs::IsStringMatchedInSet(const string16& name,
271 const std::set<string16>& pattern_set) {
272 std::set<string16>::const_iterator pattern(pattern_set.begin());
273 while (pattern != pattern_set.end()) {
274 if (MatchPattern(name, *pattern))
275 return true;
276 ++pattern;
277 }
278
279 return false;
280}
281
282/* static */
[email protected]8a2713682011-08-19 10:36:59283void PluginPrefs::ListValueToStringSet(const ListValue* src,
284 std::set<string16>* dest) {
[email protected]24ff43d72011-03-01 13:28:58285 DCHECK(src);
286 DCHECK(dest);
287 ListValue::const_iterator end(src->end());
288 for (ListValue::const_iterator current(src->begin());
289 current != end; ++current) {
290 string16 plugin_name;
291 if ((*current)->GetAsString(&plugin_name)) {
292 dest->insert(plugin_name);
293 }
294 }
295}
296
[email protected]e0379d52011-09-02 20:08:47297void PluginPrefs::SetPrefs(PrefService* prefs) {
298 prefs_ = prefs;
[email protected]0a8b1e22010-07-02 09:31:11299 bool update_internal_dir = false;
300 FilePath last_internal_dir =
[email protected]8a2713682011-08-19 10:36:59301 prefs_->GetFilePath(prefs::kPluginsLastInternalDirectory);
[email protected]0a8b1e22010-07-02 09:31:11302 FilePath cur_internal_dir;
303 if (PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &cur_internal_dir) &&
304 cur_internal_dir != last_internal_dir) {
305 update_internal_dir = true;
[email protected]8a2713682011-08-19 10:36:59306 prefs_->SetFilePath(
[email protected]0a8b1e22010-07-02 09:31:11307 prefs::kPluginsLastInternalDirectory, cur_internal_dir);
308 }
309
[email protected]0a8b1e22010-07-02 09:31:11310 bool force_enable_internal_pdf = false;
[email protected]47a87ca2011-02-14 18:54:39311 bool internal_pdf_enabled = false;
[email protected]cebc3dc2011-04-18 17:15:00312 string16 pdf_group_name =
313 ASCIIToUTF16(chrome::ChromeContentClient::kPDFPluginName);
[email protected]0a8b1e22010-07-02 09:31:11314 FilePath pdf_path;
315 PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf_path);
316 FilePath::StringType pdf_path_str = pdf_path.value();
[email protected]8a2713682011-08-19 10:36:59317 if (!prefs_->GetBoolean(prefs::kPluginsEnabledInternalPDF)) {
[email protected]0a8b1e22010-07-02 09:31:11318 // We switched to the internal pdf plugin being on by default, and so we
319 // need to force it to be enabled. We only want to do it this once though,
320 // i.e. we don't want to enable it again if the user disables it afterwards.
[email protected]8a2713682011-08-19 10:36:59321 prefs_->SetBoolean(prefs::kPluginsEnabledInternalPDF, true);
[email protected]0a8b1e22010-07-02 09:31:11322 force_enable_internal_pdf = true;
323 }
324
[email protected]aa6a39332011-08-26 01:22:49325 bool force_enable_nacl = false;
326 string16 nacl_group_name =
327 ASCIIToUTF16(chrome::ChromeContentClient::kNaClPluginName);
328 // Since the NaCl Plugin changed names between Chrome 13 and 14, we need to
329 // check for both because either could be stored as the plugin group name.
330 string16 old_nacl_group_name =
331 ASCIIToUTF16(chrome::ChromeContentClient::kNaClOldPluginName);
332 FilePath nacl_path;
333 PathService::Get(chrome::FILE_NACL_PLUGIN, &nacl_path);
334 FilePath::StringType nacl_path_str = nacl_path.value();
335 if (!prefs_->GetBoolean(prefs::kPluginsEnabledNaCl)) {
336 // We switched to the nacl plugin being on by default, and so we need to
337 // force it to be enabled. We only want to do it this once though, i.e.
338 // we don't want to enable it again if the user disables it afterwards.
339 prefs_->SetBoolean(prefs::kPluginsEnabledNaCl, true);
340 force_enable_nacl = true;
341 }
342
[email protected]805cd372011-04-04 12:39:56343 { // Scoped update of prefs::kPluginsPluginsList.
[email protected]8a2713682011-08-19 10:36:59344 ListPrefUpdate update(prefs_, prefs::kPluginsPluginsList);
[email protected]805cd372011-04-04 12:39:56345 ListValue* saved_plugins_list = update.Get();
[email protected]a027e512011-06-21 03:57:05346 if (saved_plugins_list && !saved_plugins_list->empty()) {
[email protected]805cd372011-04-04 12:39:56347 for (ListValue::const_iterator it = saved_plugins_list->begin();
348 it != saved_plugins_list->end();
349 ++it) {
350 if (!(*it)->IsType(Value::TYPE_DICTIONARY)) {
351 LOG(WARNING) << "Invalid entry in " << prefs::kPluginsPluginsList;
352 continue; // Oops, don't know what to do with this item.
[email protected]0a8b1e22010-07-02 09:31:11353 }
354
[email protected]805cd372011-04-04 12:39:56355 DictionaryValue* plugin = static_cast<DictionaryValue*>(*it);
356 string16 group_name;
[email protected]4c4eb7782011-06-22 07:51:58357 bool enabled;
358 if (!plugin->GetBoolean("enabled", &enabled))
359 enabled = true;
[email protected]805cd372011-04-04 12:39:56360
361 FilePath::StringType path;
362 // The plugin list constains all the plugin files in addition to the
363 // plugin groups.
364 if (plugin->GetString("path", &path)) {
365 // Files have a path attribute, groups don't.
366 FilePath plugin_path(path);
367 if (update_internal_dir &&
368 FilePath::CompareIgnoreCase(plugin_path.DirName().value(),
369 last_internal_dir.value()) == 0) {
370 // If the internal plugin directory has changed and if the plugin
371 // looks internal, update its path in the prefs.
372 plugin_path = cur_internal_dir.Append(plugin_path.BaseName());
373 path = plugin_path.value();
374 plugin->SetString("path", path);
[email protected]0a8b1e22010-07-02 09:31:11375 }
[email protected]47a87ca2011-02-14 18:54:39376
[email protected]805cd372011-04-04 12:39:56377 if (FilePath::CompareIgnoreCase(path, pdf_path_str) == 0) {
378 if (!enabled && force_enable_internal_pdf) {
379 enabled = true;
380 plugin->SetBoolean("enabled", true);
381 }
382
383 internal_pdf_enabled = enabled;
[email protected]aa6a39332011-08-26 01:22:49384 } else if (FilePath::CompareIgnoreCase(path, nacl_path_str) == 0) {
385 if (!enabled && force_enable_nacl) {
386 enabled = true;
387 plugin->SetBoolean("enabled", true);
388 }
[email protected]805cd372011-04-04 12:39:56389 }
390
[email protected]f7be2cba2011-09-15 15:33:45391 plugin_state_[plugin_path] = enabled;
[email protected]805cd372011-04-04 12:39:56392 } else if (!enabled && plugin->GetString("name", &group_name)) {
[email protected]aa6a39332011-08-26 01:22:49393 // Don't disable this group if it's for the pdf or nacl plugins and
394 // we just forced it on.
[email protected]805cd372011-04-04 12:39:56395 if (force_enable_internal_pdf && pdf_group_name == group_name)
396 continue;
[email protected]aa6a39332011-08-26 01:22:49397 if (force_enable_nacl && (nacl_group_name == group_name ||
398 old_nacl_group_name == group_name))
399 continue;
[email protected]805cd372011-04-04 12:39:56400
401 // Otherwise this is a list of groups.
[email protected]f7be2cba2011-09-15 15:33:45402 plugin_group_state_[group_name] = false;
[email protected]0a8b1e22010-07-02 09:31:11403 }
[email protected]0a8b1e22010-07-02 09:31:11404 }
[email protected]a027e512011-06-21 03:57:05405 } else {
406 // If the saved plugin list is empty, then the call to UpdatePreferences()
407 // below failed in an earlier run, possibly because the user closed the
[email protected]aa6a39332011-08-26 01:22:49408 // browser too quickly. Try to force enable the internal PDF and nacl
409 // plugins again.
[email protected]a027e512011-06-21 03:57:05410 force_enable_internal_pdf = true;
[email protected]aa6a39332011-08-26 01:22:49411 force_enable_nacl = true;
[email protected]0a8b1e22010-07-02 09:31:11412 }
[email protected]805cd372011-04-04 12:39:56413 } // Scoped update of prefs::kPluginsPluginsList.
[email protected]0a8b1e22010-07-02 09:31:11414
[email protected]24ff43d72011-03-01 13:28:58415 // Build the set of policy enabled/disabled plugin patterns once and cache it.
[email protected]e541d9f2010-07-15 16:04:49416 // Don't do this in the constructor, there's no profile available there.
[email protected]f7be2cba2011-09-15 15:33:45417 ListValueToStringSet(prefs_->GetList(prefs::kPluginsDisabledPlugins),
418 &policy_disabled_plugin_patterns_);
419 ListValueToStringSet(
420 prefs_->GetList(prefs::kPluginsDisabledPluginsExceptions),
421 &policy_disabled_plugin_exception_patterns_);
422 ListValueToStringSet(prefs_->GetList(prefs::kPluginsEnabledPlugins),
423 &policy_enabled_plugin_patterns_);
[email protected]e541d9f2010-07-15 16:04:49424
[email protected]8a2713682011-08-19 10:36:59425 registrar_.Init(prefs_);
[email protected]732d5af2011-04-28 11:36:30426 registrar_.Add(prefs::kPluginsDisabledPlugins, this);
427 registrar_.Add(prefs::kPluginsDisabledPluginsExceptions, this);
428 registrar_.Add(prefs::kPluginsEnabledPlugins, this);
429
[email protected]47a87ca2011-02-14 18:54:39430 if (force_enable_internal_pdf || internal_pdf_enabled) {
[email protected]a446a532010-10-11 17:24:52431 // See http://crbug.com/50105 for background.
[email protected]f7be2cba2011-09-15 15:33:45432 plugin_group_state_[ASCIIToUTF16(
433 webkit::npapi::PluginGroup::kAdobeReaderGroupName)] = false;
[email protected]47a87ca2011-02-14 18:54:39434 }
[email protected]41a9a3d2010-10-27 19:25:43435
[email protected]aa6a39332011-08-26 01:22:49436 if (force_enable_internal_pdf || force_enable_nacl) {
[email protected]41a9a3d2010-10-27 19:25:43437 // We want to save this, but doing so requires loading the list of plugins,
438 // so do it after a minute as to not impact startup performance. Note that
439 // plugins are loaded after 30s by the metrics service.
[email protected]f7be2cba2011-09-15 15:33:45440 BrowserThread::PostDelayedTask(
441 BrowserThread::FILE,
442 FROM_HERE,
[email protected]ff4fa712011-09-28 22:05:13443 base::Bind(&PluginPrefs::GetPreferencesDataOnFileThread, this),
[email protected]f7be2cba2011-09-15 15:33:45444 kPluginUpdateDelayMs);
[email protected]a446a532010-10-11 17:24:52445 }
[email protected]f7be2cba2011-09-15 15:33:45446
447 NotifyPluginStatusChanged();
[email protected]0a8b1e22010-07-02 09:31:11448}
449
[email protected]8a2713682011-08-19 10:36:59450void PluginPrefs::ShutdownOnUIThread() {
451 prefs_ = NULL;
[email protected]732d5af2011-04-28 11:36:30452 registrar_.RemoveAll();
453}
454
[email protected]f7be2cba2011-09-15 15:33:45455PluginPrefs::PluginPrefs() : plugin_state_(g_default_plugin_state.Get()),
[email protected]45a22e62011-10-12 09:48:02456 profile_(NULL),
[email protected]672b2c6f2011-09-27 02:34:25457 prefs_(NULL),
458 plugin_list_(NULL) {
[email protected]8a2713682011-08-19 10:36:59459}
460
461PluginPrefs::~PluginPrefs() {
462}
463
[email protected]f7be2cba2011-09-15 15:33:45464void PluginPrefs::SetPolicyEnforcedPluginPatterns(
465 const std::set<string16>& disabled_patterns,
466 const std::set<string16>& disabled_exception_patterns,
467 const std::set<string16>& enabled_patterns) {
468 policy_disabled_plugin_patterns_ = disabled_patterns;
469 policy_disabled_plugin_exception_patterns_ = disabled_exception_patterns;
470 policy_enabled_plugin_patterns_ = enabled_patterns;
[email protected]8a2713682011-08-19 10:36:59471}
472
[email protected]f8c85dd2012-01-12 02:26:43473webkit::npapi::PluginList* PluginPrefs::GetPluginList() const {
[email protected]672b2c6f2011-09-27 02:34:25474 if (plugin_list_)
475 return plugin_list_;
[email protected]3a5180ae2011-12-21 02:39:38476 return PluginService::GetInstance()->GetPluginList();
[email protected]672b2c6f2011-09-27 02:34:25477}
478
[email protected]8a2713682011-08-19 10:36:59479void PluginPrefs::GetPreferencesDataOnFileThread() {
[email protected]191eb3f72010-12-21 06:27:50480 std::vector<webkit::npapi::PluginGroup> groups;
[email protected]a446a532010-10-11 17:24:52481
[email protected]672b2c6f2011-09-27 02:34:25482 webkit::npapi::PluginList* plugin_list = GetPluginList();
[email protected]8a2713682011-08-19 10:36:59483 plugin_list->GetPluginGroups(false, &groups);
484
485 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]ff4fa712011-09-28 22:05:13486 base::Bind(&PluginPrefs::OnUpdatePreferences, this, groups));
[email protected]a446a532010-10-11 17:24:52487}
488
[email protected]8a2713682011-08-19 10:36:59489void PluginPrefs::OnUpdatePreferences(
[email protected]35635b2b2012-01-24 21:44:41490 const std::vector<webkit::npapi::PluginGroup>& groups) {
[email protected]8a2713682011-08-19 10:36:59491 if (!prefs_)
492 return;
493
494 ListPrefUpdate update(prefs_, prefs::kPluginsPluginsList);
[email protected]805cd372011-04-04 12:39:56495 ListValue* plugins_list = update.Get();
[email protected]0a8b1e22010-07-02 09:31:11496 plugins_list->Clear();
497
498 FilePath internal_dir;
499 if (PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &internal_dir))
[email protected]8a2713682011-08-19 10:36:59500 prefs_->SetFilePath(prefs::kPluginsLastInternalDirectory, internal_dir);
[email protected]0a8b1e22010-07-02 09:31:11501
[email protected]f7be2cba2011-09-15 15:33:45502 base::AutoLock auto_lock(lock_);
[email protected]3eff9e22011-09-15 09:42:23503
[email protected]f7be2cba2011-09-15 15:33:45504 // Add the plug-in groups.
[email protected]3eff9e22011-09-15 09:42:23505 for (size_t i = 0; i < groups.size(); ++i) {
[email protected]f7be2cba2011-09-15 15:33:45506 // Add the plugin files to the same list.
507 const std::vector<webkit::WebPluginInfo>& plugins =
508 groups[i].web_plugin_infos();
509 for (size_t j = 0; j < plugins.size(); ++j) {
510 DictionaryValue* summary = new DictionaryValue();
511 summary->SetString("path", plugins[j].path.value());
512 summary->SetString("name", plugins[j].name);
513 summary->SetString("version", plugins[j].version);
514 bool enabled = true;
515 std::map<FilePath, bool>::iterator it =
516 plugin_state_.find(plugins[j].path);
517 if (it != plugin_state_.end())
518 enabled = it->second;
519 summary->SetBoolean("enabled", enabled);
[email protected]3eff9e22011-09-15 09:42:23520 plugins_list->Append(summary);
[email protected]f7be2cba2011-09-15 15:33:45521 }
522
523 DictionaryValue* summary = new DictionaryValue();
524 string16 name = groups[i].GetGroupName();
525 summary->SetString("name", name);
526 bool enabled = true;
527 std::map<string16, bool>::iterator it =
528 plugin_group_state_.find(name);
529 if (it != plugin_group_state_.end())
530 enabled = it->second;
531 summary->SetBoolean("enabled", enabled);
532 plugins_list->Append(summary);
[email protected]3eff9e22011-09-15 09:42:23533 }
[email protected]0a8b1e22010-07-02 09:31:11534}
[email protected]ed17c7b2010-07-14 12:03:33535
[email protected]8a2713682011-08-19 10:36:59536void PluginPrefs::NotifyPluginStatusChanged() {
[email protected]f7be2cba2011-09-15 15:33:45537 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]ad50def52011-10-19 23:17:07538 content::NotificationService::current()->Notify(
[email protected]dfba8762011-09-02 12:49:54539 chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED,
[email protected]6c2381d2011-10-19 02:52:53540 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:07541 content::NotificationService::NoDetails());
[email protected]a446a532010-10-11 17:24:52542}