blob: adef9f837ac8e3658003c595886dd5e7e06d84f0 [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
7#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:048#include "base/files/file_util.h"
[email protected]0eeeba62012-09-26 00:18:129#include "base/metrics/field_trial.h"
[email protected]539f6b32014-08-12 02:50:0010#include "base/strings/string_util.h"
[email protected]774cc3c2013-06-07 20:26:4511#include "base/strings/utf_string_conversions.h"
[email protected]0eeeba62012-09-26 00:18:1212#include "chrome/browser/plugins/plugin_prefs.h"
13#include "chrome/browser/profiles/profile.h"
thestig884a1602014-08-27 01:29:3914#include "chrome/grit/generated_resources.h"
[email protected]b3610d42014-05-19 18:07:2315#include "components/variations/active_field_trials.h"
droger09a79c42015-10-27 12:02:3216#include "components/version_ui/version_handler_helper.h"
drogerfd959bad2015-10-16 09:07:0117#include "components/version_ui/version_ui_constants.h"
[email protected]0eeeba62012-09-26 00:18:1218#include "content/public/browser/browser_thread.h"
19#include "content/public/browser/plugin_service.h"
20#include "content/public/browser/web_ui.h"
[email protected]73270292013-08-09 03:48:0721#include "content/public/common/content_constants.h"
drogerfd959bad2015-10-16 09:07:0122#include "grit/components_strings.h"
[email protected]0eeeba62012-09-26 00:18:1223#include "ui/base/l10n/l10n_util.h"
[email protected]a6483d22013-07-03 22:11:0024#include "url/gurl.h"
[email protected]0eeeba62012-09-26 00:18:1225
26namespace {
27
28// Retrieves the executable and profile paths on the FILE thread.
[email protected]650b2d52013-02-10 03:41:4529void GetFilePaths(const base::FilePath& profile_path,
[email protected]e9273e192013-12-11 17:51:4930 base::string16* exec_path_out,
31 base::string16* profile_path_out) {
[email protected]a4a46be32014-04-02 06:41:1832 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
[email protected]0eeeba62012-09-26 00:18:1233
[email protected]15476932013-04-12 05:17:1534 base::FilePath executable_path = base::MakeAbsoluteFilePath(
avi556c05022014-12-22 23:31:4335 base::CommandLine::ForCurrentProcess()->GetProgram());
drogerfd959bad2015-10-16 09:07:0136 if (!executable_path.empty())
[email protected]0eeeba62012-09-26 00:18:1237 *exec_path_out = executable_path.LossyDisplayName();
drogerfd959bad2015-10-16 09:07:0138 else
39 *exec_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND);
[email protected]0eeeba62012-09-26 00:18:1240
[email protected]15476932013-04-12 05:17:1541 base::FilePath profile_path_copy(base::MakeAbsoluteFilePath(profile_path));
drogerfd959bad2015-10-16 09:07:0142 if (!profile_path.empty() && !profile_path_copy.empty())
[email protected]0eeeba62012-09-26 00:18:1243 *profile_path_out = profile_path.LossyDisplayName();
drogerfd959bad2015-10-16 09:07:0144 else
45 *profile_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND);
[email protected]0eeeba62012-09-26 00:18:1246}
47
48} // namespace
49
50VersionHandler::VersionHandler()
[email protected]d4b2d232013-04-30 21:14:2351 : weak_ptr_factory_(this) {
[email protected]0eeeba62012-09-26 00:18:1252}
53
54VersionHandler::~VersionHandler() {
55}
56
57void VersionHandler::RegisterMessages() {
58 web_ui()->RegisterMessageCallback(
drogerfd959bad2015-10-16 09:07:0159 version_ui::kRequestVersionInfo,
[email protected]0eeeba62012-09-26 00:18:1260 base::Bind(&VersionHandler::HandleRequestVersionInfo,
drogerfd959bad2015-10-16 09:07:0161 base::Unretained(this)));
[email protected]0eeeba62012-09-26 00:18:1262}
63
[email protected]c7c161a2013-12-23 21:16:2164void VersionHandler::HandleRequestVersionInfo(const base::ListValue* args) {
[email protected]ebd71962012-12-20 02:56:5565#if defined(ENABLE_PLUGINS)
[email protected]0eeeba62012-09-26 00:18:1266 // The Flash version information is needed in the response, so make sure
67 // the plugins are loaded.
68 content::PluginService::GetInstance()->GetPlugins(
69 base::Bind(&VersionHandler::OnGotPlugins,
70 weak_ptr_factory_.GetWeakPtr()));
[email protected]ebd71962012-12-20 02:56:5571#endif
[email protected]0eeeba62012-09-26 00:18:1272
73 // Grab the executable path on the FILE thread. It is returned in
74 // OnGotFilePaths.
[email protected]e9273e192013-12-11 17:51:4975 base::string16* exec_path_buffer = new base::string16;
76 base::string16* profile_path_buffer = new base::string16;
[email protected]0eeeba62012-09-26 00:18:1277 content::BrowserThread::PostTaskAndReply(
78 content::BrowserThread::FILE, FROM_HERE,
79 base::Bind(&GetFilePaths, Profile::FromWebUI(web_ui())->GetPath(),
80 base::Unretained(exec_path_buffer),
81 base::Unretained(profile_path_buffer)),
82 base::Bind(&VersionHandler::OnGotFilePaths,
83 weak_ptr_factory_.GetWeakPtr(),
84 base::Owned(exec_path_buffer),
85 base::Owned(profile_path_buffer)));
86
87 // Respond with the variations info immediately.
drogerfd959bad2015-10-16 09:07:0188 web_ui()->CallJavascriptFunction(version_ui::kReturnVariationInfo,
droger09a79c42015-10-27 12:02:3289 *version_ui::GetVariationsList());
[email protected]0eeeba62012-09-26 00:18:1290}
91
[email protected]b959d7d42013-12-13 17:26:3792void VersionHandler::OnGotFilePaths(base::string16* executable_path_data,
[email protected]e9273e192013-12-11 17:51:4993 base::string16* profile_path_data) {
[email protected]a4a46be32014-04-02 06:41:1894 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
[email protected]0eeeba62012-09-26 00:18:1295
[email protected]c7c161a2013-12-23 21:16:2196 base::StringValue exec_path(*executable_path_data);
97 base::StringValue profile_path(*profile_path_data);
drogerfd959bad2015-10-16 09:07:0198 web_ui()->CallJavascriptFunction(version_ui::kReturnFilePaths, exec_path,
99 profile_path);
[email protected]0eeeba62012-09-26 00:18:12100}
101
[email protected]ebd71962012-12-20 02:56:55102#if defined(ENABLE_PLUGINS)
[email protected]0eeeba62012-09-26 00:18:12103void VersionHandler::OnGotPlugins(
[email protected]d7bd3e52013-07-21 04:29:20104 const std::vector<content::WebPluginInfo>& plugins) {
[email protected]0eeeba62012-09-26 00:18:12105 // Obtain the version of the first enabled Flash plugin.
[email protected]d7bd3e52013-07-21 04:29:20106 std::vector<content::WebPluginInfo> info_array;
[email protected]0eeeba62012-09-26 00:18:12107 content::PluginService::GetInstance()->GetPluginInfoArray(
[email protected]73270292013-08-09 03:48:07108 GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL);
[email protected]e9273e192013-12-11 17:51:49109 base::string16 flash_version =
[email protected]0eeeba62012-09-26 00:18:12110 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN);
111 PluginPrefs* plugin_prefs =
[email protected]cadac622013-06-11 16:46:36112 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get();
[email protected]0eeeba62012-09-26 00:18:12113 if (plugin_prefs) {
114 for (size_t i = 0; i < info_array.size(); ++i) {
115 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
116 flash_version = info_array[i].version;
117 break;
118 }
119 }
120 }
121
[email protected]c7c161a2013-12-23 21:16:21122 base::StringValue arg(flash_version);
drogerfd959bad2015-10-16 09:07:01123 web_ui()->CallJavascriptFunction(version_ui::kReturnFlashVersion, arg);
[email protected]0eeeba62012-09-26 00:18:12124}
[email protected]ebd71962012-12-20 02:56:55125#endif // defined(ENABLE_PLUGINS)