Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / ui / webui / plugins_ui.cc
blob46b10da3e20362d6f2e1b5ac0aa790790d497a93
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 #include "chrome/browser/ui/webui/plugins_ui.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/ref_counted_memory.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/singleton.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/path_service.h"
20 #include "base/prefs/pref_member.h"
21 #include "base/prefs/pref_service.h"
22 #include "base/prefs/scoped_user_pref_update.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/values.h"
25 #include "chrome/browser/chrome_notification_types.h"
26 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
27 #include "chrome/browser/plugins/plugin_finder.h"
28 #include "chrome/browser/plugins/plugin_metadata.h"
29 #include "chrome/browser/plugins/plugin_prefs.h"
30 #include "chrome/browser/profiles/profile.h"
31 #include "chrome/browser/ui/browser.h"
32 #include "chrome/browser/ui/browser_window.h"
33 #include "chrome/common/chrome_content_client.h"
34 #include "chrome/common/chrome_paths.h"
35 #include "chrome/common/pref_names.h"
36 #include "chrome/common/url_constants.h"
37 #include "chrome/grit/generated_resources.h"
38 #include "components/content_settings/core/browser/host_content_settings_map.h"
39 #include "components/pref_registry/pref_registry_syncable.h"
40 #include "content/public/browser/notification_source.h"
41 #include "content/public/browser/plugin_service.h"
42 #include "content/public/browser/web_contents.h"
43 #include "content/public/browser/web_ui.h"
44 #include "content/public/browser/web_ui_data_source.h"
45 #include "content/public/browser/web_ui_message_handler.h"
46 #include "content/public/common/content_constants.h"
47 #include "grit/browser_resources.h"
48 #include "grit/theme_resources.h"
49 #include "ui/base/l10n/l10n_util.h"
50 #include "ui/base/resource/resource_bundle.h"
52 #if defined(OS_CHROMEOS)
53 #include "chrome/browser/ui/webui/chromeos/ui_account_tweaks.h"
54 #endif
56 using content::PluginService;
57 using content::WebContents;
58 using content::WebPluginInfo;
59 using content::WebUIMessageHandler;
61 namespace {
63 // Callback function to process result of EnablePlugin method.
64 void AssertPluginEnabled(bool did_enable) {
65 DCHECK(did_enable);
68 content::WebUIDataSource* CreatePluginsUIHTMLSource(Profile* profile) {
69 content::WebUIDataSource* source =
70 content::WebUIDataSource::Create(chrome::kChromeUIPluginsHost);
72 source->AddLocalizedString("pluginsTitle", IDS_PLUGINS_TITLE);
73 source->AddLocalizedString("pluginsDetailsModeLink",
74 IDS_PLUGINS_DETAILS_MODE_LINK);
75 source->AddLocalizedString("pluginsNoneInstalled",
76 IDS_PLUGINS_NONE_INSTALLED);
77 source->AddLocalizedString("pluginDisabled", IDS_PLUGINS_DISABLED_PLUGIN);
78 source->AddLocalizedString("pluginDisabledByPolicy",
79 IDS_PLUGINS_DISABLED_BY_POLICY_PLUGIN);
80 source->AddLocalizedString("pluginEnabledByPolicy",
81 IDS_PLUGINS_ENABLED_BY_POLICY_PLUGIN);
82 source->AddLocalizedString("pluginGroupManagedByPolicy",
83 IDS_PLUGINS_GROUP_MANAGED_BY_POLICY);
84 source->AddLocalizedString("pluginDownload", IDS_PLUGINS_DOWNLOAD);
85 source->AddLocalizedString("pluginName", IDS_PLUGINS_NAME);
86 source->AddLocalizedString("pluginVersion", IDS_PLUGINS_VERSION);
87 source->AddLocalizedString("pluginDescription", IDS_PLUGINS_DESCRIPTION);
88 source->AddLocalizedString("pluginPath", IDS_PLUGINS_PATH);
89 source->AddLocalizedString("pluginType", IDS_PLUGINS_TYPE);
90 source->AddLocalizedString("pluginMimeTypes", IDS_PLUGINS_MIME_TYPES);
91 source->AddLocalizedString("pluginMimeTypesMimeType",
92 IDS_PLUGINS_MIME_TYPES_MIME_TYPE);
93 source->AddLocalizedString("pluginMimeTypesDescription",
94 IDS_PLUGINS_MIME_TYPES_DESCRIPTION);
95 source->AddLocalizedString("pluginMimeTypesFileExtensions",
96 IDS_PLUGINS_MIME_TYPES_FILE_EXTENSIONS);
97 source->AddLocalizedString("disable", IDS_PLUGINS_DISABLE);
98 source->AddLocalizedString("enable", IDS_PLUGINS_ENABLE);
99 source->AddLocalizedString("alwaysAllowed", IDS_PLUGINS_ALWAYS_ALLOWED);
100 source->AddLocalizedString("noPlugins", IDS_PLUGINS_NO_PLUGINS);
102 source->SetJsonPath("strings.js");
103 source->AddResourcePath("plugins.js", IDR_PLUGINS_JS);
104 source->SetDefaultResource(IDR_PLUGINS_HTML);
105 #if defined(OS_CHROMEOS)
106 chromeos::AddAccountUITweaksLocalizedValues(source, profile);
107 #endif
108 return source;
111 base::string16 PluginTypeToString(int type) {
112 // The type is stored as an |int|, but doing the switch on the right
113 // enumeration type gives us better build-time error checking (if someone adds
114 // a new type).
115 switch (static_cast<WebPluginInfo::PluginType>(type)) {
116 case WebPluginInfo::PLUGIN_TYPE_NPAPI:
117 return l10n_util::GetStringUTF16(IDS_PLUGINS_NPAPI);
118 case WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS:
119 return l10n_util::GetStringUTF16(IDS_PLUGINS_PPAPI_IN_PROCESS);
120 case WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS:
121 return l10n_util::GetStringUTF16(IDS_PLUGINS_PPAPI_OUT_OF_PROCESS);
122 case WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN:
123 return l10n_util::GetStringUTF16(IDS_PLUGINS_BROWSER_PLUGIN);
125 NOTREACHED();
126 return base::string16();
129 ////////////////////////////////////////////////////////////////////////////////
131 // PluginsDOMHandler
133 ////////////////////////////////////////////////////////////////////////////////
135 // The handler for Javascript messages for the chrome://plugins/ page.
136 // TODO(viettrungluu): Make plugin list updates notify, and then observe
137 // changes; maybe replumb plugin list through plugin service?
138 // <http://crbug.com/39101>
139 class PluginsDOMHandler : public WebUIMessageHandler,
140 public content::NotificationObserver {
141 public:
142 PluginsDOMHandler();
143 ~PluginsDOMHandler() override {}
145 // WebUIMessageHandler implementation.
146 void RegisterMessages() override;
148 // Callback for the "requestPluginsData" message.
149 void HandleRequestPluginsData(const base::ListValue* args);
151 // Callback for the "enablePlugin" message.
152 void HandleEnablePluginMessage(const base::ListValue* args);
154 // Callback for the "saveShowDetailsToPrefs" message.
155 void HandleSaveShowDetailsToPrefs(const base::ListValue* args);
157 // Calback for the "getShowDetails" message.
158 void HandleGetShowDetails(const base::ListValue* args);
160 // Callback for the "setPluginAlwaysAllowed" message.
161 void HandleSetPluginAlwaysAllowed(const base::ListValue* args);
163 // content::NotificationObserver method overrides
164 void Observe(int type,
165 const content::NotificationSource& source,
166 const content::NotificationDetails& details) override;
168 private:
169 void LoadPlugins();
171 // Called on the UI thread when the plugin information is ready.
172 void PluginsLoaded(const std::vector<WebPluginInfo>& plugins);
174 content::NotificationRegistrar registrar_;
176 // Holds grouped plugins. The key is the group identifier and
177 // the value is the list of plugins belonging to the group.
178 typedef base::hash_map<std::string, std::vector<const WebPluginInfo*> >
179 PluginGroups;
181 // This pref guards the value whether about:plugins is in the details mode or
182 // not.
183 BooleanPrefMember show_details_;
185 base::WeakPtrFactory<PluginsDOMHandler> weak_ptr_factory_;
187 DISALLOW_COPY_AND_ASSIGN(PluginsDOMHandler);
190 PluginsDOMHandler::PluginsDOMHandler()
191 : weak_ptr_factory_(this) {
194 void PluginsDOMHandler::RegisterMessages() {
195 Profile* profile = Profile::FromWebUI(web_ui());
197 PrefService* prefs = profile->GetPrefs();
198 show_details_.Init(prefs::kPluginsShowDetails, prefs);
200 registrar_.Add(this,
201 chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED,
202 content::Source<Profile>(profile));
204 web_ui()->RegisterMessageCallback("requestPluginsData",
205 base::Bind(&PluginsDOMHandler::HandleRequestPluginsData,
206 base::Unretained(this)));
207 web_ui()->RegisterMessageCallback("enablePlugin",
208 base::Bind(&PluginsDOMHandler::HandleEnablePluginMessage,
209 base::Unretained(this)));
210 web_ui()->RegisterMessageCallback("setPluginAlwaysAllowed",
211 base::Bind(&PluginsDOMHandler::HandleSetPluginAlwaysAllowed,
212 base::Unretained(this)));
213 web_ui()->RegisterMessageCallback("saveShowDetailsToPrefs",
214 base::Bind(&PluginsDOMHandler::HandleSaveShowDetailsToPrefs,
215 base::Unretained(this)));
216 web_ui()->RegisterMessageCallback("getShowDetails",
217 base::Bind(&PluginsDOMHandler::HandleGetShowDetails,
218 base::Unretained(this)));
221 void PluginsDOMHandler::HandleRequestPluginsData(const base::ListValue* args) {
222 LoadPlugins();
225 void PluginsDOMHandler::HandleEnablePluginMessage(const base::ListValue* args) {
226 Profile* profile = Profile::FromWebUI(web_ui());
228 // Be robust in accepting badness since plugins display HTML (hence
229 // JavaScript).
230 if (args->GetSize() != 3) {
231 NOTREACHED();
232 return;
235 std::string enable_str;
236 std::string is_group_str;
237 if (!args->GetString(1, &enable_str) || !args->GetString(2, &is_group_str)) {
238 NOTREACHED();
239 return;
241 bool enable = enable_str == "true";
243 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile).get();
244 if (is_group_str == "true") {
245 base::string16 group_name;
246 if (!args->GetString(0, &group_name)) {
247 NOTREACHED();
248 return;
251 plugin_prefs->EnablePluginGroup(enable, group_name);
252 if (enable) {
253 // See http://crbug.com/50105 for background.
254 base::string16 adobereader = base::ASCIIToUTF16(
255 PluginMetadata::kAdobeReaderGroupName);
256 base::string16 internalpdf =
257 base::ASCIIToUTF16(ChromeContentClient::kPDFPluginName);
258 if (group_name == adobereader)
259 plugin_prefs->EnablePluginGroup(false, internalpdf);
260 else if (group_name == internalpdf)
261 plugin_prefs->EnablePluginGroup(false, adobereader);
263 } else {
264 base::FilePath::StringType file_path;
265 if (!args->GetString(0, &file_path)) {
266 NOTREACHED();
267 return;
270 plugin_prefs->EnablePlugin(enable, base::FilePath(file_path),
271 base::Bind(&AssertPluginEnabled));
275 void PluginsDOMHandler::HandleSaveShowDetailsToPrefs(
276 const base::ListValue* args) {
277 std::string details_mode;
278 if (!args->GetString(0, &details_mode)) {
279 NOTREACHED();
280 return;
282 show_details_.SetValue(details_mode == "true");
285 void PluginsDOMHandler::HandleGetShowDetails(const base::ListValue* args) {
286 base::FundamentalValue show_details(show_details_.GetValue());
287 web_ui()->CallJavascriptFunction("loadShowDetailsFromPrefs", show_details);
290 void PluginsDOMHandler::HandleSetPluginAlwaysAllowed(
291 const base::ListValue* args) {
292 // Be robust in the input parameters, but crash in a Debug build.
293 if (args->GetSize() != 2) {
294 NOTREACHED();
295 return;
298 std::string plugin;
299 bool allowed = false;
300 if (!args->GetString(0, &plugin) || !args->GetBoolean(1, &allowed)) {
301 NOTREACHED();
302 return;
304 Profile* profile = Profile::FromWebUI(web_ui());
305 HostContentSettingsMapFactory::GetForProfile(profile)->SetContentSetting(
306 ContentSettingsPattern::Wildcard(),
307 ContentSettingsPattern::Wildcard(),
308 CONTENT_SETTINGS_TYPE_PLUGINS,
309 plugin,
310 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_DEFAULT);
312 // Keep track of the whitelist separately, so that we can distinguish plugins
313 // whitelisted by the user from automatically whitelisted ones.
314 DictionaryPrefUpdate update(profile->GetPrefs(),
315 prefs::kContentSettingsPluginWhitelist);
316 update->SetBoolean(plugin, allowed);
319 void PluginsDOMHandler::Observe(int type,
320 const content::NotificationSource& source,
321 const content::NotificationDetails& details) {
322 DCHECK_EQ(chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, type);
323 LoadPlugins();
326 void PluginsDOMHandler::LoadPlugins() {
327 if (weak_ptr_factory_.HasWeakPtrs())
328 return;
330 PluginService::GetInstance()->GetPlugins(
331 base::Bind(&PluginsDOMHandler::PluginsLoaded,
332 weak_ptr_factory_.GetWeakPtr()));
335 void PluginsDOMHandler::PluginsLoaded(
336 const std::vector<WebPluginInfo>& plugins) {
337 Profile* profile = Profile::FromWebUI(web_ui());
338 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile).get();
340 ContentSettingsPattern wildcard = ContentSettingsPattern::Wildcard();
342 PluginFinder* plugin_finder = PluginFinder::GetInstance();
343 // Group plugins by identifier. This is done to be able to display
344 // the plugins in UI in a grouped fashion.
345 PluginGroups groups;
346 for (size_t i = 0; i < plugins.size(); ++i) {
347 scoped_ptr<PluginMetadata> plugin(
348 plugin_finder->GetPluginMetadata(plugins[i]));
349 groups[plugin->identifier()].push_back(&plugins[i]);
352 // Construct DictionaryValues to return to UI.
353 base::ListValue* plugin_groups_data = new base::ListValue();
354 for (PluginGroups::const_iterator it = groups.begin();
355 it != groups.end(); ++it) {
356 const std::vector<const WebPluginInfo*>& group_plugins = it->second;
357 base::ListValue* plugin_files = new base::ListValue();
358 scoped_ptr<PluginMetadata> plugin_metadata(
359 plugin_finder->GetPluginMetadata(*group_plugins[0]));
360 base::string16 group_name = plugin_metadata->name();
361 std::string group_identifier = plugin_metadata->identifier();
362 bool group_enabled = false;
363 bool all_plugins_enabled_by_policy = true;
364 bool all_plugins_disabled_by_policy = true;
365 bool all_plugins_managed_by_policy = true;
366 const WebPluginInfo* active_plugin = NULL;
367 for (size_t j = 0; j < group_plugins.size(); ++j) {
368 const WebPluginInfo& group_plugin = *group_plugins[j];
370 base::DictionaryValue* plugin_file = new base::DictionaryValue();
371 plugin_file->SetString("name", group_plugin.name);
373 // If this plugin is Pepper Flash, and the plugin path is the same as the
374 // path for the Pepper Flash System plugin, then mark this plugin
375 // description as the system plugin to help the user disambiguate the
376 // two plugins.
377 base::string16 desc = group_plugin.desc;
378 if (group_plugin.is_pepper_plugin() &&
379 group_plugin.name == base::ASCIIToUTF16(content::kFlashPluginName)) {
380 base::FilePath system_flash_path;
381 PathService::Get(chrome::FILE_PEPPER_FLASH_SYSTEM_PLUGIN,
382 &system_flash_path);
383 if (base::FilePath::CompareEqualIgnoreCase(group_plugin.path.value(),
384 system_flash_path.value())) {
385 #if defined(GOOGLE_CHROME_BUILD)
386 // Existing documentation for debugging Flash describe this plugin as
387 // "Debug" so preserve this nomenclature here.
388 desc += base::ASCIIToUTF16(" Debug");
389 #else
390 // On Chromium, we can name it what it really is; the system plugin.
391 desc += base::ASCIIToUTF16(" System");
392 #endif
395 plugin_file->SetString("description", desc);
397 plugin_file->SetString("path", group_plugin.path.value());
398 plugin_file->SetString("version", group_plugin.version);
399 plugin_file->SetString("type", PluginTypeToString(group_plugin.type));
401 base::ListValue* mime_types = new base::ListValue();
402 const std::vector<content::WebPluginMimeType>& plugin_mime_types =
403 group_plugin.mime_types;
404 for (size_t k = 0; k < plugin_mime_types.size(); ++k) {
405 base::DictionaryValue* mime_type = new base::DictionaryValue();
406 mime_type->SetString("mimeType", plugin_mime_types[k].mime_type);
407 mime_type->SetString("description", plugin_mime_types[k].description);
409 base::ListValue* file_extensions = new base::ListValue();
410 const std::vector<std::string>& mime_file_extensions =
411 plugin_mime_types[k].file_extensions;
412 for (size_t l = 0; l < mime_file_extensions.size(); ++l) {
413 file_extensions->Append(
414 new base::StringValue(mime_file_extensions[l]));
416 mime_type->Set("fileExtensions", file_extensions);
418 mime_types->Append(mime_type);
420 plugin_file->Set("mimeTypes", mime_types);
422 bool plugin_enabled = plugin_prefs->IsPluginEnabled(group_plugin);
424 if (!active_plugin || (plugin_enabled && !group_enabled))
425 active_plugin = &group_plugin;
426 group_enabled = plugin_enabled || group_enabled;
428 std::string enabled_mode;
429 PluginPrefs::PolicyStatus plugin_status =
430 plugin_prefs->PolicyStatusForPlugin(group_plugin.name);
431 PluginPrefs::PolicyStatus group_status =
432 plugin_prefs->PolicyStatusForPlugin(group_name);
433 if (plugin_status == PluginPrefs::POLICY_ENABLED ||
434 group_status == PluginPrefs::POLICY_ENABLED) {
435 enabled_mode = "enabledByPolicy";
436 all_plugins_disabled_by_policy = false;
437 } else {
438 all_plugins_enabled_by_policy = false;
439 if (plugin_status == PluginPrefs::POLICY_DISABLED ||
440 group_status == PluginPrefs::POLICY_DISABLED) {
441 enabled_mode = "disabledByPolicy";
442 } else {
443 all_plugins_disabled_by_policy = false;
444 all_plugins_managed_by_policy = false;
445 if (plugin_enabled) {
446 enabled_mode = "enabledByUser";
447 } else {
448 enabled_mode = "disabledByUser";
452 plugin_file->SetString("enabledMode", enabled_mode);
454 plugin_files->Append(plugin_file);
456 base::DictionaryValue* group_data = new base::DictionaryValue();
458 group_data->Set("plugin_files", plugin_files);
459 group_data->SetString("name", group_name);
460 group_data->SetString("id", group_identifier);
461 group_data->SetString("description", active_plugin->desc);
462 group_data->SetString("version", active_plugin->version);
464 #if defined(ENABLE_PLUGIN_INSTALLATION)
465 bool out_of_date = plugin_metadata->GetSecurityStatus(*active_plugin) ==
466 PluginMetadata::SECURITY_STATUS_OUT_OF_DATE;
467 group_data->SetBoolean("critical", out_of_date);
468 group_data->SetString("update_url", plugin_metadata->plugin_url().spec());
469 #endif
471 std::string enabled_mode;
472 if (all_plugins_enabled_by_policy) {
473 enabled_mode = "enabledByPolicy";
474 } else if (all_plugins_disabled_by_policy) {
475 enabled_mode = "disabledByPolicy";
476 } else if (all_plugins_managed_by_policy) {
477 enabled_mode = "managedByPolicy";
478 } else if (group_enabled) {
479 enabled_mode = "enabledByUser";
480 } else {
481 enabled_mode = "disabledByUser";
483 group_data->SetString("enabledMode", enabled_mode);
485 bool always_allowed = false;
486 if (group_enabled) {
487 const base::DictionaryValue* whitelist =
488 profile->GetPrefs()->GetDictionary(
489 prefs::kContentSettingsPluginWhitelist);
490 whitelist->GetBoolean(group_identifier, &always_allowed);
492 group_data->SetBoolean("alwaysAllowed", always_allowed);
494 plugin_groups_data->Append(group_data);
496 base::DictionaryValue results;
497 results.Set("plugins", plugin_groups_data);
498 web_ui()->CallJavascriptFunction("returnPluginsData", results);
501 } // namespace
503 ///////////////////////////////////////////////////////////////////////////////
505 // PluginsUI
507 ///////////////////////////////////////////////////////////////////////////////
509 PluginsUI::PluginsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
510 web_ui->AddMessageHandler(new PluginsDOMHandler());
512 // Set up the chrome://plugins/ source.
513 Profile* profile = Profile::FromWebUI(web_ui);
514 content::WebUIDataSource::Add(profile, CreatePluginsUIHTMLSource(profile));
517 // static
518 base::RefCountedMemory* PluginsUI::GetFaviconResourceBytes(
519 ui::ScaleFactor scale_factor) {
520 return ResourceBundle::GetSharedInstance().
521 LoadDataResourceBytesForScale(IDR_PLUGINS_FAVICON, scale_factor);
524 // static
525 void PluginsUI::RegisterProfilePrefs(
526 user_prefs::PrefRegistrySyncable* registry) {
527 registry->RegisterBooleanPref(prefs::kPluginsShowDetails, false);
528 registry->RegisterDictionaryPref(
529 prefs::kContentSettingsPluginWhitelist,
530 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);