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
7 #include <stddef.h>
8
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/files/file_util.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/strings/string16.h"
14 #include "base/task/post_task.h"
15 #include "base/task/thread_pool.h"
16 #include "base/threading/scoped_blocking_call.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/strings/grit/components_strings.h"
20 #include "components/variations/active_field_trials.h"
21 #include "components/version_ui/version_handler_helper.h"
22 #include "components/version_ui/version_ui_constants.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_ui.h"
26 #include "content/public/browser/web_ui_message_handler.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "url/gurl.h"
29
30 namespace {
31
32 // Retrieves the executable and profile paths on the FILE thread.
GetFilePaths(const base::FilePath & profile_path,base::string16 * exec_path_out,base::string16 * profile_path_out)33 void GetFilePaths(const base::FilePath& profile_path,
34 base::string16* exec_path_out,
35 base::string16* profile_path_out) {
36 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
37 base::BlockingType::MAY_BLOCK);
38
39 base::FilePath executable_path = base::MakeAbsoluteFilePath(
40 base::CommandLine::ForCurrentProcess()->GetProgram());
41 if (!executable_path.empty())
42 *exec_path_out = executable_path.LossyDisplayName();
43 else
44 *exec_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND);
45
46 base::FilePath profile_path_copy(base::MakeAbsoluteFilePath(profile_path));
47 if (!profile_path.empty() && !profile_path_copy.empty())
48 *profile_path_out = profile_path.LossyDisplayName();
49 else
50 *profile_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND);
51 }
52
53 } // namespace
54
VersionHandler()55 VersionHandler::VersionHandler() {}
56
~VersionHandler()57 VersionHandler::~VersionHandler() {
58 }
59
RegisterMessages()60 void VersionHandler::RegisterMessages() {
61 web_ui()->RegisterMessageCallback(
62 version_ui::kRequestVersionInfo,
63 base::BindRepeating(&VersionHandler::HandleRequestVersionInfo,
64 base::Unretained(this)));
65 web_ui()->RegisterMessageCallback(
66 version_ui::kRequestVariationInfo,
67 base::BindRepeating(&VersionHandler::HandleRequestVariationInfo,
68 base::Unretained(this)));
69 web_ui()->RegisterMessageCallback(
70 version_ui::kRequestPathInfo,
71 base::BindRepeating(&VersionHandler::HandleRequestPathInfo,
72 base::Unretained(this)));
73 }
74
HandleRequestVersionInfo(const base::ListValue * args)75 void VersionHandler::HandleRequestVersionInfo(const base::ListValue* args) {
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|.
82 AllowJavascript();
83 }
84
HandleRequestVariationInfo(const base::ListValue * args)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
HandleRequestPathInfo(const base::ListValue * args)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));
110
111 // Grab the executable path on the FILE thread. It is returned in
112 // OnGotFilePaths.
113 base::string16* exec_path_buffer = new base::string16;
114 base::string16* profile_path_buffer = new base::string16;
115 base::ThreadPool::PostTaskAndReply(
116 FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()},
117 base::BindOnce(&GetFilePaths, Profile::FromWebUI(web_ui())->GetPath(),
118 base::Unretained(exec_path_buffer),
119 base::Unretained(profile_path_buffer)),
120 base::BindOnce(&VersionHandler::OnGotFilePaths,
121 weak_ptr_factory_.GetWeakPtr(), callback_id,
122 base::Owned(exec_path_buffer),
123 base::Owned(profile_path_buffer)));
124 }
125
OnGotFilePaths(std::string callback_id,base::string16 * executable_path_data,base::string16 * profile_path_data)126 void VersionHandler::OnGotFilePaths(std::string callback_id,
127 base::string16* executable_path_data,
128 base::string16* profile_path_data) {
129 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
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);
134 }
135