[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 | |
| 7 | #include "base/command_line.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 9 | #include "base/metrics/field_trial.h" |
[email protected] | 539f6b3 | 2014-08-12 02:50:00 | [diff] [blame] | 10 | #include "base/strings/string_util.h" |
[email protected] | 774cc3c | 2013-06-07 20:26:45 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 12 | #include "chrome/browser/plugins/plugin_prefs.h" |
| 13 | #include "chrome/browser/profiles/profile.h" |
thestig | 884a160 | 2014-08-27 01:29:39 | [diff] [blame] | 14 | #include "chrome/grit/generated_resources.h" |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 15 | #include "components/variations/active_field_trials.h" |
droger | 09a79c4 | 2015-10-27 12:02:32 | [diff] [blame^] | 16 | #include "components/version_ui/version_handler_helper.h" |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 17 | #include "components/version_ui/version_ui_constants.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 18 | #include "content/public/browser/browser_thread.h" |
| 19 | #include "content/public/browser/plugin_service.h" |
| 20 | #include "content/public/browser/web_ui.h" |
[email protected] | 7327029 | 2013-08-09 03:48:07 | [diff] [blame] | 21 | #include "content/public/common/content_constants.h" |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 22 | #include "grit/components_strings.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 23 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 24 | #include "url/gurl.h" |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
| 28 | // Retrieves the executable and profile paths on the FILE thread. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 29 | void GetFilePaths(const base::FilePath& profile_path, |
[email protected] | e9273e19 | 2013-12-11 17:51:49 | [diff] [blame] | 30 | base::string16* exec_path_out, |
| 31 | base::string16* profile_path_out) { |
[email protected] | a4a46be3 | 2014-04-02 06:41:18 | [diff] [blame] | 32 | DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 33 | |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 34 | base::FilePath executable_path = base::MakeAbsoluteFilePath( |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 35 | base::CommandLine::ForCurrentProcess()->GetProgram()); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 36 | if (!executable_path.empty()) |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 37 | *exec_path_out = executable_path.LossyDisplayName(); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 38 | else |
| 39 | *exec_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND); |
[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 profile_path_copy(base::MakeAbsoluteFilePath(profile_path)); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 42 | if (!profile_path.empty() && !profile_path_copy.empty()) |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 43 | *profile_path_out = profile_path.LossyDisplayName(); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 44 | else |
| 45 | *profile_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | VersionHandler::VersionHandler() |
[email protected] | d4b2d23 | 2013-04-30 21:14:23 | [diff] [blame] | 51 | : weak_ptr_factory_(this) { |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | VersionHandler::~VersionHandler() { |
| 55 | } |
| 56 | |
| 57 | void VersionHandler::RegisterMessages() { |
| 58 | web_ui()->RegisterMessageCallback( |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 59 | version_ui::kRequestVersionInfo, |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 60 | base::Bind(&VersionHandler::HandleRequestVersionInfo, |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 61 | base::Unretained(this))); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | c7c161a | 2013-12-23 21:16:21 | [diff] [blame] | 64 | void VersionHandler::HandleRequestVersionInfo(const base::ListValue* args) { |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 65 | #if defined(ENABLE_PLUGINS) |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 66 | // The Flash version information is needed in the response, so make sure |
| 67 | // the plugins are loaded. |
| 68 | content::PluginService::GetInstance()->GetPlugins( |
| 69 | base::Bind(&VersionHandler::OnGotPlugins, |
| 70 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 71 | #endif |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 72 | |
| 73 | // Grab the executable path on the FILE thread. It is returned in |
| 74 | // OnGotFilePaths. |
[email protected] | e9273e19 | 2013-12-11 17:51:49 | [diff] [blame] | 75 | base::string16* exec_path_buffer = new base::string16; |
| 76 | base::string16* profile_path_buffer = new base::string16; |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 77 | content::BrowserThread::PostTaskAndReply( |
| 78 | content::BrowserThread::FILE, FROM_HERE, |
| 79 | base::Bind(&GetFilePaths, Profile::FromWebUI(web_ui())->GetPath(), |
| 80 | base::Unretained(exec_path_buffer), |
| 81 | base::Unretained(profile_path_buffer)), |
| 82 | base::Bind(&VersionHandler::OnGotFilePaths, |
| 83 | weak_ptr_factory_.GetWeakPtr(), |
| 84 | base::Owned(exec_path_buffer), |
| 85 | base::Owned(profile_path_buffer))); |
| 86 | |
| 87 | // Respond with the variations info immediately. |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 88 | web_ui()->CallJavascriptFunction(version_ui::kReturnVariationInfo, |
droger | 09a79c4 | 2015-10-27 12:02:32 | [diff] [blame^] | 89 | *version_ui::GetVariationsList()); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 92 | void VersionHandler::OnGotFilePaths(base::string16* executable_path_data, |
[email protected] | e9273e19 | 2013-12-11 17:51:49 | [diff] [blame] | 93 | base::string16* profile_path_data) { |
[email protected] | a4a46be3 | 2014-04-02 06:41:18 | [diff] [blame] | 94 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 95 | |
[email protected] | c7c161a | 2013-12-23 21:16:21 | [diff] [blame] | 96 | base::StringValue exec_path(*executable_path_data); |
| 97 | base::StringValue profile_path(*profile_path_data); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 98 | web_ui()->CallJavascriptFunction(version_ui::kReturnFilePaths, exec_path, |
| 99 | profile_path); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 102 | #if defined(ENABLE_PLUGINS) |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 103 | void VersionHandler::OnGotPlugins( |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 104 | const std::vector<content::WebPluginInfo>& plugins) { |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 105 | // Obtain the version of the first enabled Flash plugin. |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 106 | std::vector<content::WebPluginInfo> info_array; |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 107 | content::PluginService::GetInstance()->GetPluginInfoArray( |
[email protected] | 7327029 | 2013-08-09 03:48:07 | [diff] [blame] | 108 | GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL); |
[email protected] | e9273e19 | 2013-12-11 17:51:49 | [diff] [blame] | 109 | base::string16 flash_version = |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 110 | l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN); |
| 111 | PluginPrefs* plugin_prefs = |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 112 | PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get(); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 113 | if (plugin_prefs) { |
| 114 | for (size_t i = 0; i < info_array.size(); ++i) { |
| 115 | if (plugin_prefs->IsPluginEnabled(info_array[i])) { |
| 116 | flash_version = info_array[i].version; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
[email protected] | c7c161a | 2013-12-23 21:16:21 | [diff] [blame] | 122 | base::StringValue arg(flash_version); |
droger | fd959bad | 2015-10-16 09:07:01 | [diff] [blame] | 123 | web_ui()->CallJavascriptFunction(version_ui::kReturnFlashVersion, arg); |
[email protected] | 0eeeba6 | 2012-09-26 00:18:12 | [diff] [blame] | 124 | } |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 125 | #endif // defined(ENABLE_PLUGINS) |