[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/ui/webui/version_handler.h" |
| 6 | |
avi | 0f23343 | 2015-12-25 02:23:38 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 10 | #include "base/command_line.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 11 | #include "base/files/file_util.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 12 | #include "base/metrics/field_trial.h" |
dpapad | 4caf63c | 2020-10-07 19:52:55 | [diff] [blame^] | 13 | #include "base/strings/string16.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 14 | #include "base/task/post_task.h" |
Gabriel Charette | 05503913 | 2020-02-26 23:02:06 | [diff] [blame] | 15 | #include "base/task/thread_pool.h" |
Etienne Pierre-doray | 7d835e1 | 2018-08-29 19:00:09 | [diff] [blame] | 16 | #include "base/threading/scoped_blocking_call.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile.h" |
thestig | 884a160 | 2014-08-27 01:29:39 | [diff] [blame] | 18 | #include "chrome/grit/generated_resources.h" |
thestig | 4a2e88e | 2016-08-27 23:23:51 | [diff] [blame] | 19 | #include "components/strings/grit/components_strings.h" |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 20 | #include "components/variations/active_field_trials.h" |
droger | 09a79c4 | 2015-10-27 12:02:32 | [diff] [blame] | 21 | #include "components/version_ui/version_handler_helper.h" |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 22 | #include "components/version_ui/version_ui_constants.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 23 | #include "content/public/browser/browser_thread.h" |
Gayane Petrosyan | 5efebf1 | 2018-02-05 17:25:48 | [diff] [blame] | 24 | #include "content/public/browser/web_contents.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 25 | #include "content/public/browser/web_ui.h" |
Orin Jaworski | e5a1aeda | 2019-07-30 17:23:54 | [diff] [blame] | 26 | #include "content/public/browser/web_ui_message_handler.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 27 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 28 | #include "url/gurl.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 29 | |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | // Retrieves the executable and profile paths on the FILE thread. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 33 | void GetFilePaths(const base::FilePath& profile_path, |
[email protected] | e9273e19 | 2013-12-11 17:51:49 | [diff] [blame] | 34 | base::string16* exec_path_out, |
| 35 | base::string16* profile_path_out) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 36 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 37 | base::BlockingType::MAY_BLOCK); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 38 | |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 39 | base::FilePath executable_path = base::MakeAbsoluteFilePath( |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 40 | base::CommandLine::ForCurrentProcess()->GetProgram()); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 41 | if (!executable_path.empty()) |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 42 | *exec_path_out = executable_path.LossyDisplayName(); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 43 | else |
| 44 | *exec_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 45 | |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 46 | base::FilePath profile_path_copy(base::MakeAbsoluteFilePath(profile_path)); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 47 | if (!profile_path.empty() && !profile_path_copy.empty()) |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 48 | *profile_path_out = profile_path.LossyDisplayName(); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 49 | else |
| 50 | *profile_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 55 | VersionHandler::VersionHandler() {} |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 56 | |
| 57 | VersionHandler::~VersionHandler() { |
| 58 | } |
| 59 | |
| 60 | void VersionHandler::RegisterMessages() { |
| 61 | web_ui()->RegisterMessageCallback( |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 62 | version_ui::kRequestVersionInfo, |
Avi Drissman | 5e5875b | 2018-03-24 01:39:47 | [diff] [blame] | 63 | base::BindRepeating(&VersionHandler::HandleRequestVersionInfo, |
| 64 | base::Unretained(this))); |
Orin Jaworski | e5a1aeda | 2019-07-30 17:23:54 | [diff] [blame] | 65 | web_ui()->RegisterMessageCallback( |
| 66 | version_ui::kRequestVariationInfo, |
| 67 | base::BindRepeating(&VersionHandler::HandleRequestVariationInfo, |
| 68 | base::Unretained(this))); |
| 69 | web_ui()->RegisterMessageCallback( |
Orin Jaworski | e5a1aeda | 2019-07-30 17:23:54 | [diff] [blame] | 70 | version_ui::kRequestPathInfo, |
| 71 | base::BindRepeating(&VersionHandler::HandleRequestPathInfo, |
| 72 | base::Unretained(this))); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | c7c161a | 2013-12-23 21:16:21 | [diff] [blame] | 75 | void VersionHandler::HandleRequestVersionInfo(const base::ListValue* args) { |
Orin Jaworski | e5a1aeda | 2019-07-30 17:23:54 | [diff] [blame] | 76 | // 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 Petrosyan | 5efebf1 | 2018-02-05 17:25:48 | [diff] [blame] | 82 | AllowJavascript(); |
Orin Jaworski | e5a1aeda | 2019-07-30 17:23:54 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void 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 Jaworski | e5a1aeda | 2019-07-30 17:23:54 | [diff] [blame] | 104 | void 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] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 110 | |
| 111 | // Grab the executable path on the FILE thread. It is returned in |
| 112 | // OnGotFilePaths. |
[email protected] | e9273e19 | 2013-12-11 17:51:49 | [diff] [blame] | 113 | base::string16* exec_path_buffer = new base::string16; |
| 114 | base::string16* profile_path_buffer = new base::string16; |
Gabriel Charette | 05503913 | 2020-02-26 23:02:06 | [diff] [blame] | 115 | base::ThreadPool::PostTaskAndReply( |
| 116 | FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()}, |
tzik | 22036cc | 2017-04-21 04:08:18 | [diff] [blame] | 117 | base::BindOnce(&GetFilePaths, Profile::FromWebUI(web_ui())->GetPath(), |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 118 | base::Unretained(exec_path_buffer), |
| 119 | base::Unretained(profile_path_buffer)), |
Orin Jaworski | e5a1aeda | 2019-07-30 17:23:54 | [diff] [blame] | 120 | 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] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 124 | } |
| 125 | |
Orin Jaworski | e5a1aeda | 2019-07-30 17:23:54 | [diff] [blame] | 126 | void VersionHandler::OnGotFilePaths(std::string callback_id, |
| 127 | base::string16* executable_path_data, |
[email protected] | e9273e19 | 2013-12-11 17:51:49 | [diff] [blame] | 128 | base::string16* profile_path_data) { |
[email protected] | a4a46be3 | 2014-04-02 06:41:18 | [diff] [blame] | 129 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Orin Jaworski | e5a1aeda | 2019-07-30 17:23:54 | [diff] [blame] | 130 | 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] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 134 | } |