blob: 919e04bc47d9d18c930f284e8af31ef5dabe694d [file] [log] [blame]
[email protected]0eeeba62012-09-26 00:18:121// 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#include "chrome/browser/ui/webui/version_handler.h"
6
avi0f233432015-12-25 02:23:387#include <stddef.h>
8
[email protected]0eeeba62012-09-26 00:18:129#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:0410#include "base/files/file_util.h"
[email protected]0eeeba62012-09-26 00:18:1211#include "base/metrics/field_trial.h"
[email protected]539f6b32014-08-12 02:50:0012#include "base/strings/string_util.h"
kerrnel74ec6062017-01-04 20:20:5013#include "base/strings/stringprintf.h"
[email protected]774cc3c2013-06-07 20:26:4514#include "base/strings/utf_string_conversions.h"
Gabriel Charette44db1422018-08-06 11:19:3315#include "base/task/post_task.h"
Etienne Pierre-doray7d835e12018-08-29 19:00:0916#include "base/threading/scoped_blocking_call.h"
[email protected]0eeeba62012-09-26 00:18:1217#include "chrome/browser/plugins/plugin_prefs.h"
18#include "chrome/browser/profiles/profile.h"
thestig884a1602014-08-27 01:29:3919#include "chrome/grit/generated_resources.h"
thestig4a2e88e2016-08-27 23:23:5120#include "components/strings/grit/components_strings.h"
[email protected]b3610d42014-05-19 18:07:2321#include "components/variations/active_field_trials.h"
droger09a79c42015-10-27 12:02:3222#include "components/version_ui/version_handler_helper.h"
drogerfd959bad2015-10-16 09:07:0123#include "components/version_ui/version_ui_constants.h"
[email protected]0eeeba62012-09-26 00:18:1224#include "content/public/browser/browser_thread.h"
25#include "content/public/browser/plugin_service.h"
Gayane Petrosyan5efebf12018-02-05 17:25:4826#include "content/public/browser/web_contents.h"
[email protected]0eeeba62012-09-26 00:18:1227#include "content/public/browser/web_ui.h"
[email protected]73270292013-08-09 03:48:0728#include "content/public/common/content_constants.h"
Scott Violet02e38b92018-03-27 23:42:1429#include "ppapi/buildflags/buildflags.h"
[email protected]0eeeba62012-09-26 00:18:1230#include "ui/base/l10n/l10n_util.h"
[email protected]a6483d22013-07-03 22:11:0031#include "url/gurl.h"
[email protected]0eeeba62012-09-26 00:18:1232
33namespace {
34
35// Retrieves the executable and profile paths on the FILE thread.
[email protected]650b2d52013-02-10 03:41:4536void GetFilePaths(const base::FilePath& profile_path,
[email protected]e9273e192013-12-11 17:51:4937 base::string16* exec_path_out,
38 base::string16* profile_path_out) {
Etienne Pierre-doray7d835e12018-08-29 19:00:0939 base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK);
[email protected]0eeeba62012-09-26 00:18:1240
[email protected]15476932013-04-12 05:17:1541 base::FilePath executable_path = base::MakeAbsoluteFilePath(
avi556c05022014-12-22 23:31:4342 base::CommandLine::ForCurrentProcess()->GetProgram());
drogerfd959bad2015-10-16 09:07:0143 if (!executable_path.empty())
[email protected]0eeeba62012-09-26 00:18:1244 *exec_path_out = executable_path.LossyDisplayName();
drogerfd959bad2015-10-16 09:07:0145 else
46 *exec_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND);
[email protected]0eeeba62012-09-26 00:18:1247
[email protected]15476932013-04-12 05:17:1548 base::FilePath profile_path_copy(base::MakeAbsoluteFilePath(profile_path));
drogerfd959bad2015-10-16 09:07:0149 if (!profile_path.empty() && !profile_path_copy.empty())
[email protected]0eeeba62012-09-26 00:18:1250 *profile_path_out = profile_path.LossyDisplayName();
drogerfd959bad2015-10-16 09:07:0151 else
52 *profile_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND);
[email protected]0eeeba62012-09-26 00:18:1253}
54
55} // namespace
56
Gayane Petrosyan5efebf12018-02-05 17:25:4857VersionHandler::VersionHandler() : weak_ptr_factory_(this) {}
[email protected]0eeeba62012-09-26 00:18:1258
59VersionHandler::~VersionHandler() {
60}
61
62void VersionHandler::RegisterMessages() {
63 web_ui()->RegisterMessageCallback(
drogerfd959bad2015-10-16 09:07:0164 version_ui::kRequestVersionInfo,
Avi Drissman5e5875b2018-03-24 01:39:4765 base::BindRepeating(&VersionHandler::HandleRequestVersionInfo,
66 base::Unretained(this)));
[email protected]0eeeba62012-09-26 00:18:1267}
68
[email protected]c7c161a2013-12-23 21:16:2169void VersionHandler::HandleRequestVersionInfo(const base::ListValue* args) {
Gayane Petrosyan5efebf12018-02-05 17:25:4870 AllowJavascript();
brettw4b461082016-11-19 18:55:1671#if BUILDFLAG(ENABLE_PLUGINS)
[email protected]0eeeba62012-09-26 00:18:1272 // The Flash version information is needed in the response, so make sure
73 // the plugins are loaded.
74 content::PluginService::GetInstance()->GetPlugins(
75 base::Bind(&VersionHandler::OnGotPlugins,
76 weak_ptr_factory_.GetWeakPtr()));
[email protected]ebd71962012-12-20 02:56:5577#endif
[email protected]0eeeba62012-09-26 00:18:1278
79 // Grab the executable path on the FILE thread. It is returned in
80 // OnGotFilePaths.
[email protected]e9273e192013-12-11 17:51:4981 base::string16* exec_path_buffer = new base::string16;
82 base::string16* profile_path_buffer = new base::string16;
Xiyuan Xia4c99a3b2017-06-23 22:58:0783 base::PostTaskWithTraitsAndReply(
84 FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()},
tzik22036cc2017-04-21 04:08:1885 base::BindOnce(&GetFilePaths, Profile::FromWebUI(web_ui())->GetPath(),
[email protected]0eeeba62012-09-26 00:18:1286 base::Unretained(exec_path_buffer),
87 base::Unretained(profile_path_buffer)),
tzik22036cc2017-04-21 04:08:1888 base::BindOnce(
89 &VersionHandler::OnGotFilePaths, weak_ptr_factory_.GetWeakPtr(),
90 base::Owned(exec_path_buffer), base::Owned(profile_path_buffer)));
[email protected]0eeeba62012-09-26 00:18:1291
92 // Respond with the variations info immediately.
Gayane Petrosyan5efebf12018-02-05 17:25:4893 CallJavascriptFunction(version_ui::kReturnVariationInfo,
94 *version_ui::GetVariationsList());
95 GURL current_url = web_ui()->GetWebContents()->GetVisibleURL();
96 if (current_url.query().find(version_ui::kVariationsShowCmdQuery) !=
97 std::string::npos) {
98 CallJavascriptFunction(version_ui::kReturnVariationCmd,
99 version_ui::GetVariationsCommandLineAsValue());
100 }
[email protected]0eeeba62012-09-26 00:18:12101}
102
[email protected]b959d7d42013-12-13 17:26:37103void VersionHandler::OnGotFilePaths(base::string16* executable_path_data,
[email protected]e9273e192013-12-11 17:51:49104 base::string16* profile_path_data) {
[email protected]a4a46be32014-04-02 06:41:18105 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
[email protected]0eeeba62012-09-26 00:18:12106
jdoerrie122c4da2017-03-06 11:12:04107 base::Value exec_path(*executable_path_data);
108 base::Value profile_path(*profile_path_data);
Gayane Petrosyan5efebf12018-02-05 17:25:48109 CallJavascriptFunction(version_ui::kReturnFilePaths, exec_path, profile_path);
[email protected]0eeeba62012-09-26 00:18:12110}
111
brettw4b461082016-11-19 18:55:16112#if BUILDFLAG(ENABLE_PLUGINS)
[email protected]0eeeba62012-09-26 00:18:12113void VersionHandler::OnGotPlugins(
[email protected]d7bd3e52013-07-21 04:29:20114 const std::vector<content::WebPluginInfo>& plugins) {
[email protected]0eeeba62012-09-26 00:18:12115 // Obtain the version of the first enabled Flash plugin.
[email protected]d7bd3e52013-07-21 04:29:20116 std::vector<content::WebPluginInfo> info_array;
[email protected]0eeeba62012-09-26 00:18:12117 content::PluginService::GetInstance()->GetPluginInfoArray(
[email protected]73270292013-08-09 03:48:07118 GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL);
kerrnel74ec6062017-01-04 20:20:50119 std::string flash_version_and_path =
120 l10n_util::GetStringUTF8(IDS_PLUGINS_DISABLED_PLUGIN);
[email protected]0eeeba62012-09-26 00:18:12121 PluginPrefs* plugin_prefs =
[email protected]cadac622013-06-11 16:46:36122 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get();
[email protected]0eeeba62012-09-26 00:18:12123 if (plugin_prefs) {
124 for (size_t i = 0; i < info_array.size(); ++i) {
125 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
kerrnel74ec6062017-01-04 20:20:50126 flash_version_and_path = base::StringPrintf(
127 "%s %s", base::UTF16ToUTF8(info_array[i].version).c_str(),
128 base::UTF16ToUTF8(info_array[i].path.LossyDisplayName()).c_str());
[email protected]0eeeba62012-09-26 00:18:12129 break;
130 }
131 }
132 }
133
jdoerrie122c4da2017-03-06 11:12:04134 base::Value arg(flash_version_and_path);
kerrnel74ec6062017-01-04 20:20:50135
Gayane Petrosyan5efebf12018-02-05 17:25:48136 CallJavascriptFunction(version_ui::kReturnFlashVersion, arg);
[email protected]0eeeba62012-09-26 00:18:12137}
brettw4b461082016-11-19 18:55:16138#endif // BUILDFLAG(ENABLE_PLUGINS)