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/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/task/post_task.h"
17 #include "base/task/thread_pool.h"
18 #include "base/threading/scoped_blocking_call.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "components/strings/grit/components_strings.h"
22 #include "components/variations/active_field_trials.h"
23 #include "components/version_ui/version_handler_helper.h"
24 #include "components/version_ui/version_ui_constants.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/browser/web_ui.h"
28 #include "content/public/browser/web_ui_message_handler.h"
29 #include "content/public/common/content_constants.h"
30 #include "ppapi/buildflags/buildflags.h"
31 #include "ui/base/l10n/l10n_util.h"
32 #include "url/gurl.h"
33 
34 #if BUILDFLAG(ENABLE_PLUGINS)
35 #include "chrome/browser/plugins/plugin_prefs.h"
36 #include "content/public/browser/plugin_service.h"
37 #endif
38 
39 namespace {
40 
41 // 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)42 void GetFilePaths(const base::FilePath& profile_path,
43                   base::string16* exec_path_out,
44                   base::string16* profile_path_out) {
45   base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
46                                                 base::BlockingType::MAY_BLOCK);
47 
48   base::FilePath executable_path = base::MakeAbsoluteFilePath(
49       base::CommandLine::ForCurrentProcess()->GetProgram());
50   if (!executable_path.empty())
51     *exec_path_out = executable_path.LossyDisplayName();
52   else
53     *exec_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND);
54 
55   base::FilePath profile_path_copy(base::MakeAbsoluteFilePath(profile_path));
56   if (!profile_path.empty() && !profile_path_copy.empty())
57     *profile_path_out = profile_path.LossyDisplayName();
58   else
59     *profile_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND);
60 }
61 
62 }  // namespace
63 
VersionHandler()64 VersionHandler::VersionHandler() {}
65 
~VersionHandler()66 VersionHandler::~VersionHandler() {
67 }
68 
RegisterMessages()69 void VersionHandler::RegisterMessages() {
70   web_ui()->RegisterMessageCallback(
71       version_ui::kRequestVersionInfo,
72       base::BindRepeating(&VersionHandler::HandleRequestVersionInfo,
73                           base::Unretained(this)));
74   web_ui()->RegisterMessageCallback(
75       version_ui::kRequestVariationInfo,
76       base::BindRepeating(&VersionHandler::HandleRequestVariationInfo,
77                           base::Unretained(this)));
78   web_ui()->RegisterMessageCallback(
79       version_ui::kRequestPluginInfo,
80       base::BindRepeating(&VersionHandler::HandleRequestPluginInfo,
81                           base::Unretained(this)));
82   web_ui()->RegisterMessageCallback(
83       version_ui::kRequestPathInfo,
84       base::BindRepeating(&VersionHandler::HandleRequestPathInfo,
85                           base::Unretained(this)));
86 }
87 
HandleRequestVersionInfo(const base::ListValue * args)88 void VersionHandler::HandleRequestVersionInfo(const base::ListValue* args) {
89   // This method is overridden by platform-specific handlers which may still
90   // use |CallJavascriptFunction|. Main version info is returned by promise
91   // using handlers below.
92   // TODO(orinj): To fully eliminate chrome.send usage in JS, derived classes
93   // could be made to work more like this base class, using
94   // |ResolveJavascriptCallback| instead of |CallJavascriptFunction|.
95   AllowJavascript();
96 }
97 
HandleRequestVariationInfo(const base::ListValue * args)98 void VersionHandler::HandleRequestVariationInfo(const base::ListValue* args) {
99   AllowJavascript();
100 
101   std::string callback_id;
102   bool include_variations_cmd;
103   CHECK_EQ(2U, args->GetSize());
104   CHECK(args->GetString(0, &callback_id));
105   CHECK(args->GetBoolean(1, &include_variations_cmd));
106 
107   base::Value response(base::Value::Type::DICTIONARY);
108   response.SetKey(version_ui::kKeyVariationsList,
109                   std::move(*version_ui::GetVariationsList()));
110   if (include_variations_cmd) {
111     response.SetKey(version_ui::kKeyVariationsCmd,
112                     version_ui::GetVariationsCommandLineAsValue());
113   }
114   ResolveJavascriptCallback(base::Value(callback_id), response);
115 }
116 
HandleRequestPluginInfo(const base::ListValue * args)117 void VersionHandler::HandleRequestPluginInfo(const base::ListValue* args) {
118   AllowJavascript();
119 
120   std::string callback_id;
121   CHECK_EQ(1U, args->GetSize());
122   CHECK(args->GetString(0, &callback_id));
123 
124 #if BUILDFLAG(ENABLE_PLUGINS)
125   // The Flash version information is needed in the response, so make sure
126   // the plugins are loaded.
127   content::PluginService::GetInstance()->GetPlugins(
128       base::Bind(&VersionHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr(),
129                  callback_id));
130 #else
131   RejectJavascriptCallback(base::Value(callback_id), base::Value());
132 #endif
133 }
134 
HandleRequestPathInfo(const base::ListValue * args)135 void VersionHandler::HandleRequestPathInfo(const base::ListValue* args) {
136   AllowJavascript();
137 
138   std::string callback_id;
139   CHECK_EQ(1U, args->GetSize());
140   CHECK(args->GetString(0, &callback_id));
141 
142   // Grab the executable path on the FILE thread. It is returned in
143   // OnGotFilePaths.
144   base::string16* exec_path_buffer = new base::string16;
145   base::string16* profile_path_buffer = new base::string16;
146   base::ThreadPool::PostTaskAndReply(
147       FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()},
148       base::BindOnce(&GetFilePaths, Profile::FromWebUI(web_ui())->GetPath(),
149                      base::Unretained(exec_path_buffer),
150                      base::Unretained(profile_path_buffer)),
151       base::BindOnce(&VersionHandler::OnGotFilePaths,
152                      weak_ptr_factory_.GetWeakPtr(), callback_id,
153                      base::Owned(exec_path_buffer),
154                      base::Owned(profile_path_buffer)));
155 }
156 
OnGotFilePaths(std::string callback_id,base::string16 * executable_path_data,base::string16 * profile_path_data)157 void VersionHandler::OnGotFilePaths(std::string callback_id,
158                                     base::string16* executable_path_data,
159                                     base::string16* profile_path_data) {
160   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
161   base::Value response(base::Value::Type::DICTIONARY);
162   response.SetKey(version_ui::kKeyExecPath, base::Value(*executable_path_data));
163   response.SetKey(version_ui::kKeyProfilePath, base::Value(*profile_path_data));
164   ResolveJavascriptCallback(base::Value(callback_id), response);
165 }
166 
167 #if BUILDFLAG(ENABLE_PLUGINS)
OnGotPlugins(std::string callback_id,const std::vector<content::WebPluginInfo> & plugins)168 void VersionHandler::OnGotPlugins(
169     std::string callback_id,
170     const std::vector<content::WebPluginInfo>& plugins) {
171   // Obtain the version of the first enabled Flash plugin.
172   std::vector<content::WebPluginInfo> info_array;
173   content::PluginService::GetInstance()->GetPluginInfoArray(
174       GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL);
175   std::string flash_version_and_path =
176       l10n_util::GetStringUTF8(IDS_PLUGINS_DISABLED_PLUGIN);
177   PluginPrefs* plugin_prefs =
178       PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get();
179   if (plugin_prefs) {
180     for (size_t i = 0; i < info_array.size(); ++i) {
181       if (plugin_prefs->IsPluginEnabled(info_array[i])) {
182         flash_version_and_path = base::StringPrintf(
183             "%s %s", base::UTF16ToUTF8(info_array[i].version).c_str(),
184             base::UTF16ToUTF8(info_array[i].path.LossyDisplayName()).c_str());
185         break;
186       }
187     }
188   }
189   ResolveJavascriptCallback(base::Value(callback_id),
190                             base::Value(flash_version_and_path));
191 }
192 #endif  // BUILDFLAG(ENABLE_PLUGINS)
193