Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: chrome/browser/plugins/plugin_prefs.cc

Issue 10910168: Separate plugin_metadata from plugin_installer, thread-safe plugin_finder (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fix policy tests Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/plugins/plugin_prefs.h" 5 #include "chrome/browser/plugins/plugin_prefs.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 #include "build/build_config.h" 16 #include "build/build_config.h"
17 #include "chrome/browser/plugins/plugin_installer.h" 17 #include "chrome/browser/plugins/plugin_installer.h"
18 #include "chrome/browser/plugins/plugin_metadata.h"
18 #include "chrome/browser/plugins/plugin_prefs_factory.h" 19 #include "chrome/browser/plugins/plugin_prefs_factory.h"
20 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/prefs/scoped_user_pref_update.h" 21 #include "chrome/browser/prefs/scoped_user_pref_update.h"
20 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_keyed_service.h" 23 #include "chrome/browser/profiles/profile_keyed_service.h"
22 #include "chrome/common/chrome_constants.h" 24 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/chrome_content_client.h" 25 #include "chrome/common/chrome_content_client.h"
24 #include "chrome/common/chrome_notification_types.h" 26 #include "chrome/common/chrome_notification_types.h"
25 #include "chrome/common/chrome_paths.h" 27 #include "chrome/common/chrome_paths.h"
26 #include "chrome/common/chrome_switches.h" 28 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
28 #include "content/public/browser/browser_thread.h" 30 #include "content/public/browser/browser_thread.h"
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 PluginPrefsFactory::GetInstance()->SetTestingFactoryAndUse( 103 PluginPrefsFactory::GetInstance()->SetTestingFactoryAndUse(
102 profile, &PluginPrefsFactory::CreateForTestingProfile).get()); 104 profile, &PluginPrefsFactory::CreateForTestingProfile).get());
103 } 105 }
104 106
105 void PluginPrefs::SetPluginListForTesting( 107 void PluginPrefs::SetPluginListForTesting(
106 webkit::npapi::PluginList* plugin_list) { 108 webkit::npapi::PluginList* plugin_list) {
107 plugin_list_ = plugin_list; 109 plugin_list_ = plugin_list;
108 } 110 }
109 111
110 void PluginPrefs::EnablePluginGroup(bool enabled, const string16& group_name) { 112 void PluginPrefs::EnablePluginGroup(bool enabled, const string16& group_name) {
111 PluginFinder::Get(
112 base::Bind(&PluginPrefs::GetPluginFinderForEnablePluginGroup,
113 this, enabled, group_name));
114 }
115
116 void PluginPrefs::GetPluginFinderForEnablePluginGroup(
117 bool enabled,
118 const string16& group_name,
119 PluginFinder* finder) {
120 PluginService::GetInstance()->GetPlugins( 113 PluginService::GetInstance()->GetPlugins(
121 base::Bind(&PluginPrefs::EnablePluginGroupInternal, 114 base::Bind(&PluginPrefs::EnablePluginGroupInternal,
122 this, enabled, group_name, finder)); 115 this, enabled, group_name));
123 } 116 }
124 117
125 void PluginPrefs::EnablePluginGroupInternal( 118 void PluginPrefs::EnablePluginGroupInternal(
126 bool enabled, 119 bool enabled,
127 const string16& group_name, 120 const string16& group_name,
128 PluginFinder* finder,
129 const std::vector<webkit::WebPluginInfo>& plugins) { 121 const std::vector<webkit::WebPluginInfo>& plugins) {
130 base::AutoLock auto_lock(lock_); 122 base::AutoLock auto_lock(lock_);
123 PluginFinder* finder = PluginFinder::GetInstance();
131 124
132 // Set the desired state for the group. 125 // Set the desired state for the group.
133 plugin_group_state_[group_name] = enabled; 126 plugin_group_state_[group_name] = enabled;
134 127
135 // Update the state for all plug-ins in the group. 128 // Update the state for all plug-ins in the group.
136 for (size_t i = 0; i < plugins.size(); ++i) { 129 for (size_t i = 0; i < plugins.size(); ++i) {
137 PluginInstaller* installer = finder->GetPluginInstaller(plugins[i]); 130 PluginMetadata* plugin = finder->GetPluginMetadata(plugins[i]);
138 if (group_name != installer->name()) 131 if (group_name != plugin->name())
139 continue; 132 continue;
140 plugin_state_.Set(plugins[i].path, enabled); 133 plugin_state_.Set(plugins[i].path, enabled);
141 } 134 }
142 135
143 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 136 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
144 base::Bind(&PluginPrefs::OnUpdatePreferences, this, plugins, finder)); 137 base::Bind(&PluginPrefs::OnUpdatePreferences, this, plugins));
145 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 138 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
146 base::Bind(&PluginPrefs::NotifyPluginStatusChanged, this)); 139 base::Bind(&PluginPrefs::NotifyPluginStatusChanged, this));
147 } 140 }
148 141
149 void PluginPrefs::EnablePluginIfPossibleCallback( 142 void PluginPrefs::EnablePlugin(
150 bool enabled, const FilePath& path, 143 bool enabled, const FilePath& path,
151 const base::Callback<void(bool)>& callback, 144 const base::Callback<void(bool)>& callback) {
152 PluginFinder* finder) { 145 PluginFinder* finder = PluginFinder::GetInstance();
153 webkit::WebPluginInfo plugin; 146 webkit::WebPluginInfo plugin;
154 bool can_enable = true; 147 bool can_enable = true;
155 if (PluginService::GetInstance()->GetPluginInfoByPath(path, &plugin)) { 148 if (PluginService::GetInstance()->GetPluginInfoByPath(path, &plugin)) {
156 PluginInstaller* installer = finder->GetPluginInstaller(plugin); 149 PluginMetadata* plugin_metadata = finder->GetPluginMetadata(plugin);
157 PolicyStatus plugin_status = PolicyStatusForPlugin(plugin.name); 150 PolicyStatus plugin_status = PolicyStatusForPlugin(plugin.name);
158 PolicyStatus group_status = PolicyStatusForPlugin(installer->name()); 151 PolicyStatus group_status = PolicyStatusForPlugin(plugin_metadata->name());
159 if (enabled) { 152 if (enabled) {
160 if (plugin_status == POLICY_DISABLED || group_status == POLICY_DISABLED) 153 if (plugin_status == POLICY_DISABLED || group_status == POLICY_DISABLED)
161 can_enable = false; 154 can_enable = false;
162 } else { 155 } else {
163 if (plugin_status == POLICY_ENABLED || group_status == POLICY_ENABLED) 156 if (plugin_status == POLICY_ENABLED || group_status == POLICY_ENABLED)
164 can_enable = false; 157 can_enable = false;
165 } 158 }
166 } else { 159 } else {
167 NOTREACHED(); 160 NOTREACHED();
168 } 161 }
169 162
170 if (!can_enable) { 163 if (!can_enable) {
171 callback.Run(false); 164 callback.Run(false);
ibraaaa 2012/09/24 19:10:21 Do you mean calling this callback by MessageLoop::
Bernhard Bauer 2012/09/24 19:34:28 Yes, exactly. (Sorry, forgot to submit my commen
ibraaaa 2012/09/24 20:07:13 Done.
172 return; 165 return;
173 } 166 }
174 167
175 PluginService::GetInstance()->GetPlugins( 168 PluginService::GetInstance()->GetPlugins(
176 base::Bind(&PluginPrefs::EnablePluginInternal, this, 169 base::Bind(&PluginPrefs::EnablePluginInternal, this,
177 enabled, path, finder, callback)); 170 enabled, path, finder, callback));
178 } 171 }
179 172
180 void PluginPrefs::EnablePlugin(
181 bool enabled, const FilePath& path,
182 const base::Callback<void(bool)>& callback) {
183 PluginFinder::Get(base::Bind(&PluginPrefs::EnablePluginIfPossibleCallback,
184 this, enabled, path, callback));
185 }
186
187 void PluginPrefs::EnablePluginInternal( 173 void PluginPrefs::EnablePluginInternal(
188 bool enabled, 174 bool enabled,
189 const FilePath& path, 175 const FilePath& path,
190 PluginFinder* plugin_finder, 176 PluginFinder* plugin_finder,
191 const base::Callback<void(bool)>& callback, 177 const base::Callback<void(bool)>& callback,
192 const std::vector<webkit::WebPluginInfo>& plugins) { 178 const std::vector<webkit::WebPluginInfo>& plugins) {
193 { 179 {
194 // Set the desired state for the plug-in. 180 // Set the desired state for the plug-in.
195 base::AutoLock auto_lock(lock_); 181 base::AutoLock auto_lock(lock_);
196 plugin_state_.Set(path, enabled); 182 plugin_state_.Set(path, enabled);
197 } 183 }
198 184
199 string16 group_name; 185 string16 group_name;
200 for (size_t i = 0; i < plugins.size(); ++i) { 186 for (size_t i = 0; i < plugins.size(); ++i) {
201 if (plugins[i].path == path) { 187 if (plugins[i].path == path) {
202 PluginInstaller* installer = 188 PluginMetadata* plugin_metadata =
203 plugin_finder->GetPluginInstaller(plugins[i]); 189 plugin_finder->GetPluginMetadata(plugins[i]);
204 // set the group name for this plug-in. 190 // set the group name for this plug-in.
205 group_name = installer->name(); 191 group_name = plugin_metadata->name();
206 DCHECK_EQ(enabled, IsPluginEnabled(plugins[i])); 192 DCHECK_EQ(enabled, IsPluginEnabled(plugins[i]));
207 break; 193 break;
208 } 194 }
209 } 195 }
210 196
211 bool all_disabled = true; 197 bool all_disabled = true;
212 for (size_t i = 0; i < plugins.size(); ++i) { 198 for (size_t i = 0; i < plugins.size(); ++i) {
213 PluginInstaller* installer = plugin_finder->GetPluginInstaller(plugins[i]); 199 PluginMetadata* plugin_metadata =
214 DCHECK(!installer->name().empty()); 200 plugin_finder->GetPluginMetadata(plugins[i]);
215 if (group_name == installer->name()) { 201 DCHECK(!plugin_metadata->name().empty());
202 if (group_name == plugin_metadata->name()) {
216 all_disabled = all_disabled && !IsPluginEnabled(plugins[i]); 203 all_disabled = all_disabled && !IsPluginEnabled(plugins[i]);
217 } 204 }
218 } 205 }
219 206
220 if (!group_name.empty()) { 207 if (!group_name.empty()) {
221 // Update the state for the corresponding plug-in group. 208 // Update the state for the corresponding plug-in group.
222 base::AutoLock auto_lock(lock_); 209 base::AutoLock auto_lock(lock_);
223 plugin_group_state_[group_name] = !all_disabled; 210 plugin_group_state_[group_name] = !all_disabled;
224 } 211 }
225 212
226 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 213 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
227 base::Bind(&PluginPrefs::OnUpdatePreferences, this, 214 base::Bind(&PluginPrefs::OnUpdatePreferences, this, plugins));
228 plugins, plugin_finder));
229 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 215 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
230 base::Bind(&PluginPrefs::NotifyPluginStatusChanged, this)); 216 base::Bind(&PluginPrefs::NotifyPluginStatusChanged, this));
231 callback.Run(true); 217 callback.Run(true);
232 } 218 }
233 219
234 PluginPrefs::PolicyStatus PluginPrefs::PolicyStatusForPlugin( 220 PluginPrefs::PolicyStatus PluginPrefs::PolicyStatusForPlugin(
235 const string16& name) const { 221 const string16& name) const {
236 base::AutoLock auto_lock(lock_); 222 base::AutoLock auto_lock(lock_);
237 if (IsStringMatchedInSet(name, policy_enabled_plugin_patterns_)) { 223 if (IsStringMatchedInSet(name, policy_enabled_plugin_patterns_)) {
238 return POLICY_ENABLED; 224 return POLICY_ENABLED;
239 } else if (IsStringMatchedInSet(name, policy_disabled_plugin_patterns_) && 225 } else if (IsStringMatchedInSet(name, policy_disabled_plugin_patterns_) &&
240 !IsStringMatchedInSet( 226 !IsStringMatchedInSet(
241 name, policy_disabled_plugin_exception_patterns_)) { 227 name, policy_disabled_plugin_exception_patterns_)) {
242 return POLICY_DISABLED; 228 return POLICY_DISABLED;
243 } else { 229 } else {
244 return NO_POLICY; 230 return NO_POLICY;
245 } 231 }
246 } 232 }
247 233
248 bool PluginPrefs::IsPluginEnabled(const webkit::WebPluginInfo& plugin) const { 234 bool PluginPrefs::IsPluginEnabled(const webkit::WebPluginInfo& plugin) const {
249 scoped_ptr<webkit::npapi::PluginGroup> group( 235 PluginFinder* finder = PluginFinder::GetInstance();
250 GetPluginList()->GetPluginGroup(plugin)); 236 string16 group_name = finder->GetPluginMetadata(plugin)->name();
251 string16 group_name = group->GetGroupName();
252 237
253 // Check if the plug-in or its group is enabled by policy. 238 // Check if the plug-in or its group is enabled by policy.
254 PolicyStatus plugin_status = PolicyStatusForPlugin(plugin.name); 239 PolicyStatus plugin_status = PolicyStatusForPlugin(plugin.name);
255 PolicyStatus group_status = PolicyStatusForPlugin(group_name); 240 PolicyStatus group_status = PolicyStatusForPlugin(group_name);
256 if (plugin_status == POLICY_ENABLED || group_status == POLICY_ENABLED) 241 if (plugin_status == POLICY_ENABLED || group_status == POLICY_ENABLED)
257 return true; 242 return true;
258 243
259 // Check if the plug-in or its group is disabled by policy. 244 // Check if the plug-in or its group is disabled by policy.
260 if (plugin_status == POLICY_DISABLED || group_status == POLICY_DISABLED) 245 if (plugin_status == POLICY_DISABLED || group_status == POLICY_DISABLED)
261 return false; 246 return false;
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 policy_enabled_plugin_patterns_ = enabled_patterns; 569 policy_enabled_plugin_patterns_ = enabled_patterns;
585 } 570 }
586 571
587 webkit::npapi::PluginList* PluginPrefs::GetPluginList() const { 572 webkit::npapi::PluginList* PluginPrefs::GetPluginList() const {
588 if (plugin_list_) 573 if (plugin_list_)
589 return plugin_list_; 574 return plugin_list_;
590 return PluginService::GetInstance()->GetPluginList(); 575 return PluginService::GetInstance()->GetPluginList();
591 } 576 }
592 577
593 void PluginPrefs::GetPreferencesDataOnFileThread() { 578 void PluginPrefs::GetPreferencesDataOnFileThread() {
594 PluginFinder::Get(
595 base::Bind(&PluginPrefs::GetPluginFinderForGetPreferencesDataOnFileThread,
596 this));
597 }
598
599 void PluginPrefs::GetPluginFinderForGetPreferencesDataOnFileThread(
600 PluginFinder* finder) {
601 std::vector<webkit::WebPluginInfo> plugins; 579 std::vector<webkit::WebPluginInfo> plugins;
602 webkit::npapi::PluginList* plugin_list = GetPluginList(); 580 webkit::npapi::PluginList* plugin_list = GetPluginList();
603 plugin_list->GetPluginsNoRefresh(&plugins); 581 plugin_list->GetPluginsNoRefresh(&plugins);
604 582
605 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 583 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
606 base::Bind(&PluginPrefs::OnUpdatePreferences, this, plugins, finder)); 584 base::Bind(&PluginPrefs::OnUpdatePreferences, this, plugins));
607 } 585 }
608 586
609 void PluginPrefs::OnUpdatePreferences( 587 void PluginPrefs::OnUpdatePreferences(
610 const std::vector<webkit::WebPluginInfo>& plugins, 588 const std::vector<webkit::WebPluginInfo>& plugins) {
611 PluginFinder* finder) {
612 if (!prefs_) 589 if (!prefs_)
613 return; 590 return;
614 591
592 PluginFinder* finder = PluginFinder::GetInstance();
615 ListPrefUpdate update(prefs_, prefs::kPluginsPluginsList); 593 ListPrefUpdate update(prefs_, prefs::kPluginsPluginsList);
616 ListValue* plugins_list = update.Get(); 594 ListValue* plugins_list = update.Get();
617 plugins_list->Clear(); 595 plugins_list->Clear();
618 596
619 FilePath internal_dir; 597 FilePath internal_dir;
620 if (PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &internal_dir)) 598 if (PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &internal_dir))
621 prefs_->SetFilePath(prefs::kPluginsLastInternalDirectory, internal_dir); 599 prefs_->SetFilePath(prefs::kPluginsLastInternalDirectory, internal_dir);
622 600
623 base::AutoLock auto_lock(lock_); 601 base::AutoLock auto_lock(lock_);
624 602
625 // Add the plugin files. 603 // Add the plugin files.
626 std::set<string16> group_names; 604 std::set<string16> group_names;
627 for (size_t i = 0; i < plugins.size(); ++i) { 605 for (size_t i = 0; i < plugins.size(); ++i) {
628 DictionaryValue* summary = new DictionaryValue(); 606 DictionaryValue* summary = new DictionaryValue();
629 summary->SetString("path", plugins[i].path.value()); 607 summary->SetString("path", plugins[i].path.value());
630 summary->SetString("name", plugins[i].name); 608 summary->SetString("name", plugins[i].name);
631 summary->SetString("version", plugins[i].version); 609 summary->SetString("version", plugins[i].version);
632 bool enabled = true; 610 bool enabled = true;
633 plugin_state_.Get(plugins[i].path, &enabled); 611 plugin_state_.Get(plugins[i].path, &enabled);
634 summary->SetBoolean("enabled", enabled); 612 summary->SetBoolean("enabled", enabled);
635 plugins_list->Append(summary); 613 plugins_list->Append(summary);
636 614
637 PluginInstaller* installer = finder->GetPluginInstaller(plugins[i]); 615 PluginMetadata* plugin_metadata = finder->GetPluginMetadata(plugins[i]);
638 // Insert into a set of all group names. 616 // Insert into a set of all group names.
639 group_names.insert(installer->name()); 617 group_names.insert(plugin_metadata->name());
640 } 618 }
641 619
642 // Add the plug-in groups. 620 // Add the plug-in groups.
643 for (std::set<string16>::const_iterator it = group_names.begin(); 621 for (std::set<string16>::const_iterator it = group_names.begin();
644 it != group_names.end(); ++it) { 622 it != group_names.end(); ++it) {
645 DictionaryValue* summary = new DictionaryValue(); 623 DictionaryValue* summary = new DictionaryValue();
646 summary->SetString("name", *it); 624 summary->SetString("name", *it);
647 bool enabled = true; 625 bool enabled = true;
648 std::map<string16, bool>::iterator gstate_it = 626 std::map<string16, bool>::iterator gstate_it =
649 plugin_group_state_.find(*it); 627 plugin_group_state_.find(*it);
650 if (gstate_it != plugin_group_state_.end()) 628 if (gstate_it != plugin_group_state_.end())
651 enabled = gstate_it->second; 629 enabled = gstate_it->second;
652 summary->SetBoolean("enabled", enabled); 630 summary->SetBoolean("enabled", enabled);
653 plugins_list->Append(summary); 631 plugins_list->Append(summary);
654 } 632 }
655 } 633 }
656 634
657 void PluginPrefs::NotifyPluginStatusChanged() { 635 void PluginPrefs::NotifyPluginStatusChanged() {
658 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 636 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
659 content::NotificationService::current()->Notify( 637 content::NotificationService::current()->Notify(
660 chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, 638 chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED,
661 content::Source<Profile>(profile_), 639 content::Source<Profile>(profile_),
662 content::NotificationService::NoDetails()); 640 content::NotificationService::NoDetails());
663 } 641 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698