[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 | |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 9 | #include "base/command_line.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 11 | #include "base/metrics/field_trial.h" |
[email protected] | 539f6b3 | 2014-08-12 02:50:00 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
kerrnel | 74ec606 | 2017-01-04 20:20:50 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
[email protected] | 774cc3c | 2013-06-07 20:26:45 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 15 | #include "base/task/post_task.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/plugins/plugin_prefs.h" |
| 18 | #include "chrome/browser/profiles/profile.h" |
thestig | 884a160 | 2014-08-27 01:29:39 | [diff] [blame] | 19 | #include "chrome/grit/generated_resources.h" |
thestig | 4a2e88e | 2016-08-27 23:23:51 | [diff] [blame] | 20 | #include "components/strings/grit/components_strings.h" |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 21 | #include "components/variations/active_field_trials.h" |
droger | 09a79c4 | 2015-10-27 12:02:32 | [diff] [blame] | 22 | #include "components/version_ui/version_handler_helper.h" |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 23 | #include "components/version_ui/version_ui_constants.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 24 | #include "content/public/browser/browser_thread.h" |
| 25 | #include "content/public/browser/plugin_service.h" |
Gayane Petrosyan | 5efebf1 | 2018-02-05 17:25:48 | [diff] [blame] | 26 | #include "content/public/browser/web_contents.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 27 | #include "content/public/browser/web_ui.h" |
[email protected] | 7327029 | 2013-08-09 03:48:07 | [diff] [blame] | 28 | #include "content/public/common/content_constants.h" |
Scott Violet | 02e38b9 | 2018-03-27 23:42:14 | [diff] [blame] | 29 | #include "ppapi/buildflags/buildflags.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 30 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 31 | #include "url/gurl.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 32 | |
| 33 | namespace { |
| 34 | |
| 35 | // Retrieves the executable and profile paths on the FILE thread. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 36 | void GetFilePaths(const base::FilePath& profile_path, |
[email protected] | e9273e19 | 2013-12-11 17:51:49 | [diff] [blame] | 37 | base::string16* exec_path_out, |
| 38 | base::string16* profile_path_out) { |
Etienne Pierre-doray | 7d835e1 | 2018-08-29 19:00:09 | [diff] [blame^] | 39 | base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 40 | |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 41 | base::FilePath executable_path = base::MakeAbsoluteFilePath( |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 42 | base::CommandLine::ForCurrentProcess()->GetProgram()); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 43 | if (!executable_path.empty()) |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 44 | *exec_path_out = executable_path.LossyDisplayName(); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 45 | else |
| 46 | *exec_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 47 | |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 48 | base::FilePath profile_path_copy(base::MakeAbsoluteFilePath(profile_path)); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 49 | if (!profile_path.empty() && !profile_path_copy.empty()) |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 50 | *profile_path_out = profile_path.LossyDisplayName(); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 51 | else |
| 52 | *profile_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | } // namespace |
| 56 | |
Gayane Petrosyan | 5efebf1 | 2018-02-05 17:25:48 | [diff] [blame] | 57 | VersionHandler::VersionHandler() : weak_ptr_factory_(this) {} |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 58 | |
| 59 | VersionHandler::~VersionHandler() { |
| 60 | } |
| 61 | |
| 62 | void VersionHandler::RegisterMessages() { |
| 63 | web_ui()->RegisterMessageCallback( |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 64 | version_ui::kRequestVersionInfo, |
Avi Drissman | 5e5875b | 2018-03-24 01:39:47 | [diff] [blame] | 65 | base::BindRepeating(&VersionHandler::HandleRequestVersionInfo, |
| 66 | base::Unretained(this))); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | c7c161a | 2013-12-23 21:16:21 | [diff] [blame] | 69 | void VersionHandler::HandleRequestVersionInfo(const base::ListValue* args) { |
Gayane Petrosyan | 5efebf1 | 2018-02-05 17:25:48 | [diff] [blame] | 70 | AllowJavascript(); |
brettw | 4b46108 | 2016-11-19 18:55:16 | [diff] [blame] | 71 | #if BUILDFLAG(ENABLE_PLUGINS) |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 72 | // The Flash version information is needed in the response, so make sure |
| 73 | // the plugins are loaded. |
| 74 | content::PluginService::GetInstance()->GetPlugins( |
| 75 | base::Bind(&VersionHandler::OnGotPlugins, |
| 76 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 77 | #endif |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 78 | |
| 79 | // Grab the executable path on the FILE thread. It is returned in |
| 80 | // OnGotFilePaths. |
[email protected] | e9273e19 | 2013-12-11 17:51:49 | [diff] [blame] | 81 | base::string16* exec_path_buffer = new base::string16; |
| 82 | base::string16* profile_path_buffer = new base::string16; |
Xiyuan Xia | 4c99a3b | 2017-06-23 22:58:07 | [diff] [blame] | 83 | base::PostTaskWithTraitsAndReply( |
| 84 | FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()}, |
tzik | 22036cc | 2017-04-21 04:08:18 | [diff] [blame] | 85 | base::BindOnce(&GetFilePaths, Profile::FromWebUI(web_ui())->GetPath(), |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 86 | base::Unretained(exec_path_buffer), |
| 87 | base::Unretained(profile_path_buffer)), |
tzik | 22036cc | 2017-04-21 04:08:18 | [diff] [blame] | 88 | base::BindOnce( |
| 89 | &VersionHandler::OnGotFilePaths, weak_ptr_factory_.GetWeakPtr(), |
| 90 | base::Owned(exec_path_buffer), base::Owned(profile_path_buffer))); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 91 | |
| 92 | // Respond with the variations info immediately. |
Gayane Petrosyan | 5efebf1 | 2018-02-05 17:25:48 | [diff] [blame] | 93 | CallJavascriptFunction(version_ui::kReturnVariationInfo, |
| 94 | *version_ui::GetVariationsList()); |
| 95 | GURL current_url = web_ui()->GetWebContents()->GetVisibleURL(); |
| 96 | if (current_url.query().find(version_ui::kVariationsShowCmdQuery) != |
| 97 | std::string::npos) { |
| 98 | CallJavascriptFunction(version_ui::kReturnVariationCmd, |
| 99 | version_ui::GetVariationsCommandLineAsValue()); |
| 100 | } |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 103 | void VersionHandler::OnGotFilePaths(base::string16* executable_path_data, |
[email protected] | e9273e19 | 2013-12-11 17:51:49 | [diff] [blame] | 104 | base::string16* profile_path_data) { |
[email protected] | a4a46be3 | 2014-04-02 06:41:18 | [diff] [blame] | 105 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 106 | |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 107 | base::Value exec_path(*executable_path_data); |
| 108 | base::Value profile_path(*profile_path_data); |
Gayane Petrosyan | 5efebf1 | 2018-02-05 17:25:48 | [diff] [blame] | 109 | CallJavascriptFunction(version_ui::kReturnFilePaths, exec_path, profile_path); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 110 | } |
| 111 | |
brettw | 4b46108 | 2016-11-19 18:55:16 | [diff] [blame] | 112 | #if BUILDFLAG(ENABLE_PLUGINS) |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 113 | void VersionHandler::OnGotPlugins( |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 114 | const std::vector<content::WebPluginInfo>& plugins) { |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 115 | // Obtain the version of the first enabled Flash plugin. |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 116 | std::vector<content::WebPluginInfo> info_array; |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 117 | content::PluginService::GetInstance()->GetPluginInfoArray( |
[email protected] | 7327029 | 2013-08-09 03:48:07 | [diff] [blame] | 118 | GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL); |
kerrnel | 74ec606 | 2017-01-04 20:20:50 | [diff] [blame] | 119 | std::string flash_version_and_path = |
| 120 | l10n_util::GetStringUTF8(IDS_PLUGINS_DISABLED_PLUGIN); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 121 | PluginPrefs* plugin_prefs = |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 122 | PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get(); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 123 | if (plugin_prefs) { |
| 124 | for (size_t i = 0; i < info_array.size(); ++i) { |
| 125 | if (plugin_prefs->IsPluginEnabled(info_array[i])) { |
kerrnel | 74ec606 | 2017-01-04 20:20:50 | [diff] [blame] | 126 | flash_version_and_path = base::StringPrintf( |
| 127 | "%s %s", base::UTF16ToUTF8(info_array[i].version).c_str(), |
| 128 | base::UTF16ToUTF8(info_array[i].path.LossyDisplayName()).c_str()); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 134 | base::Value arg(flash_version_and_path); |
kerrnel | 74ec606 | 2017-01-04 20:20:50 | [diff] [blame] | 135 | |
Gayane Petrosyan | 5efebf1 | 2018-02-05 17:25:48 | [diff] [blame] | 136 | CallJavascriptFunction(version_ui::kReturnFlashVersion, arg); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 137 | } |
brettw | 4b46108 | 2016-11-19 18:55:16 | [diff] [blame] | 138 | #endif // BUILDFLAG(ENABLE_PLUGINS) |