blob: 374a98da430bcef91a880590c4ed3cf431785605 [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
Sebastien Marchandf1349f52019-01-25 03:16:419#include "base/bind.h"
[email protected]0eeeba62012-09-26 00:18:1210#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:0411#include "base/files/file_util.h"
[email protected]0eeeba62012-09-26 00:18:1212#include "base/metrics/field_trial.h"
dpapad4caf63c2020-10-07 19:52:5513#include "base/strings/string16.h"
Gabriel Charette44db1422018-08-06 11:19:3314#include "base/task/post_task.h"
Gabriel Charette055039132020-02-26 23:02:0615#include "base/task/thread_pool.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/profiles/profile.h"
thestig884a1602014-08-27 01:29:3918#include "chrome/grit/generated_resources.h"
thestig4a2e88e2016-08-27 23:23:5119#include "components/strings/grit/components_strings.h"
[email protected]b3610d42014-05-19 18:07:2320#include "components/variations/active_field_trials.h"
droger09a79c42015-10-27 12:02:3221#include "components/version_ui/version_handler_helper.h"
drogerfd959bad2015-10-16 09:07:0122#include "components/version_ui/version_ui_constants.h"
[email protected]0eeeba62012-09-26 00:18:1223#include "content/public/browser/browser_thread.h"
Gayane Petrosyan5efebf12018-02-05 17:25:4824#include "content/public/browser/web_contents.h"
[email protected]0eeeba62012-09-26 00:18:1225#include "content/public/browser/web_ui.h"
Orin Jaworskie5a1aeda2019-07-30 17:23:5426#include "content/public/browser/web_ui_message_handler.h"
[email protected]0eeeba62012-09-26 00:18:1227#include "ui/base/l10n/l10n_util.h"
[email protected]a6483d22013-07-03 22:11:0028#include "url/gurl.h"
[email protected]0eeeba62012-09-26 00:18:1229
[email protected]0eeeba62012-09-26 00:18:1230namespace {
31
32// Retrieves the executable and profile paths on the FILE thread.
[email protected]650b2d52013-02-10 03:41:4533void GetFilePaths(const base::FilePath& profile_path,
[email protected]e9273e192013-12-11 17:51:4934 base::string16* exec_path_out,
35 base::string16* profile_path_out) {
Etienne Bergeron436d42212019-02-26 17:15:1236 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
37 base::BlockingType::MAY_BLOCK);
[email protected]0eeeba62012-09-26 00:18:1238
[email protected]15476932013-04-12 05:17:1539 base::FilePath executable_path = base::MakeAbsoluteFilePath(
avi556c05022014-12-22 23:31:4340 base::CommandLine::ForCurrentProcess()->GetProgram());
drogerfd959bad2015-10-16 09:07:0141 if (!executable_path.empty())
[email protected]0eeeba62012-09-26 00:18:1242 *exec_path_out = executable_path.LossyDisplayName();
drogerfd959bad2015-10-16 09:07:0143 else
44 *exec_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND);
[email protected]0eeeba62012-09-26 00:18:1245
[email protected]15476932013-04-12 05:17:1546 base::FilePath profile_path_copy(base::MakeAbsoluteFilePath(profile_path));
drogerfd959bad2015-10-16 09:07:0147 if (!profile_path.empty() && !profile_path_copy.empty())
[email protected]0eeeba62012-09-26 00:18:1248 *profile_path_out = profile_path.LossyDisplayName();
drogerfd959bad2015-10-16 09:07:0149 else
50 *profile_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND);
[email protected]0eeeba62012-09-26 00:18:1251}
52
53} // namespace
54
Jeremy Roman495db682019-07-12 16:03:2455VersionHandler::VersionHandler() {}
[email protected]0eeeba62012-09-26 00:18:1256
57VersionHandler::~VersionHandler() {
58}
59
60void VersionHandler::RegisterMessages() {
61 web_ui()->RegisterMessageCallback(
drogerfd959bad2015-10-16 09:07:0162 version_ui::kRequestVersionInfo,
Avi Drissman5e5875b2018-03-24 01:39:4763 base::BindRepeating(&VersionHandler::HandleRequestVersionInfo,
64 base::Unretained(this)));
Orin Jaworskie5a1aeda2019-07-30 17:23:5465 web_ui()->RegisterMessageCallback(
66 version_ui::kRequestVariationInfo,
67 base::BindRepeating(&VersionHandler::HandleRequestVariationInfo,
68 base::Unretained(this)));
69 web_ui()->RegisterMessageCallback(
Orin Jaworskie5a1aeda2019-07-30 17:23:5470 version_ui::kRequestPathInfo,
71 base::BindRepeating(&VersionHandler::HandleRequestPathInfo,
72 base::Unretained(this)));
[email protected]0eeeba62012-09-26 00:18:1273}
74
[email protected]c7c161a2013-12-23 21:16:2175void VersionHandler::HandleRequestVersionInfo(const base::ListValue* args) {
Orin Jaworskie5a1aeda2019-07-30 17:23:5476 // This method is overridden by platform-specific handlers which may still
77 // use |CallJavascriptFunction|. Main version info is returned by promise
78 // using handlers below.
79 // TODO(orinj): To fully eliminate chrome.send usage in JS, derived classes
80 // could be made to work more like this base class, using
81 // |ResolveJavascriptCallback| instead of |CallJavascriptFunction|.
Gayane Petrosyan5efebf12018-02-05 17:25:4882 AllowJavascript();
Orin Jaworskie5a1aeda2019-07-30 17:23:5483}
84
85void VersionHandler::HandleRequestVariationInfo(const base::ListValue* args) {
86 AllowJavascript();
87
88 std::string callback_id;
89 bool include_variations_cmd;
90 CHECK_EQ(2U, args->GetSize());
91 CHECK(args->GetString(0, &callback_id));
92 CHECK(args->GetBoolean(1, &include_variations_cmd));
93
94 base::Value response(base::Value::Type::DICTIONARY);
95 response.SetKey(version_ui::kKeyVariationsList,
96 std::move(*version_ui::GetVariationsList()));
97 if (include_variations_cmd) {
98 response.SetKey(version_ui::kKeyVariationsCmd,
99 version_ui::GetVariationsCommandLineAsValue());
100 }
101 ResolveJavascriptCallback(base::Value(callback_id), response);
102}
103
Orin Jaworskie5a1aeda2019-07-30 17:23:54104void VersionHandler::HandleRequestPathInfo(const base::ListValue* args) {
105 AllowJavascript();
106
107 std::string callback_id;
108 CHECK_EQ(1U, args->GetSize());
109 CHECK(args->GetString(0, &callback_id));
[email protected]0eeeba62012-09-26 00:18:12110
111 // Grab the executable path on the FILE thread. It is returned in
112 // OnGotFilePaths.
[email protected]e9273e192013-12-11 17:51:49113 base::string16* exec_path_buffer = new base::string16;
114 base::string16* profile_path_buffer = new base::string16;
Gabriel Charette055039132020-02-26 23:02:06115 base::ThreadPool::PostTaskAndReply(
116 FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()},
tzik22036cc2017-04-21 04:08:18117 base::BindOnce(&GetFilePaths, Profile::FromWebUI(web_ui())->GetPath(),
[email protected]0eeeba62012-09-26 00:18:12118 base::Unretained(exec_path_buffer),
119 base::Unretained(profile_path_buffer)),
Orin Jaworskie5a1aeda2019-07-30 17:23:54120 base::BindOnce(&VersionHandler::OnGotFilePaths,
121 weak_ptr_factory_.GetWeakPtr(), callback_id,
122 base::Owned(exec_path_buffer),
123 base::Owned(profile_path_buffer)));
[email protected]0eeeba62012-09-26 00:18:12124}
125
Orin Jaworskie5a1aeda2019-07-30 17:23:54126void VersionHandler::OnGotFilePaths(std::string callback_id,
127 base::string16* executable_path_data,
[email protected]e9273e192013-12-11 17:51:49128 base::string16* profile_path_data) {
[email protected]a4a46be32014-04-02 06:41:18129 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Orin Jaworskie5a1aeda2019-07-30 17:23:54130 base::Value response(base::Value::Type::DICTIONARY);
131 response.SetKey(version_ui::kKeyExecPath, base::Value(*executable_path_data));
132 response.SetKey(version_ui::kKeyProfilePath, base::Value(*profile_path_data));
133 ResolveJavascriptCallback(base::Value(callback_id), response);
[email protected]0eeeba62012-09-26 00:18:12134}