Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/version_handler.h" | 5 #include "chrome/browser/ui/webui/version_handler.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/plugins/plugin_prefs.h" | 12 #include "chrome/browser/plugins/plugin_prefs.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
| 15 #include "components/variations/active_field_trials.h" | 15 #include "components/variations/active_field_trials.h" |
| 16 #include "components/version_ui/version_handler_helper.h" | 16 #include "components/version_ui/version_handler_helper.h" |
| 17 #include "components/version_ui/version_ui_constants.h" | |
| 18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/plugin_service.h" | 18 #include "content/public/browser/plugin_service.h" |
| 20 #include "content/public/browser/web_ui.h" | 19 #include "content/public/browser/web_ui.h" |
| 21 #include "content/public/common/content_constants.h" | 20 #include "content/public/common/content_constants.h" |
| 22 #include "grit/components_strings.h" | 21 #include "grit/components_strings.h" |
| 22 #include "mojo/common/common_type_converters.h" | |
| 23 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 25 | 25 |
| 26 #if defined(OS_CHROMEOS) | |
| 27 #include "chromeos/system/version_loader.h" | |
| 28 #endif | |
| 29 | |
| 26 namespace { | 30 namespace { |
| 27 | 31 |
| 28 // Retrieves the executable and profile paths on the FILE thread. | 32 // Retrieves the executable and profile paths on the FILE thread. |
| 29 void GetFilePaths(const base::FilePath& profile_path, | 33 void RetrieveFilePaths(const base::FilePath& profile_path, |
| 30 base::string16* exec_path_out, | 34 base::string16* exec_path_out, |
| 31 base::string16* profile_path_out) { | 35 base::string16* profile_path_out) { |
| 32 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | 36 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
| 33 | 37 |
| 34 base::FilePath executable_path = base::MakeAbsoluteFilePath( | 38 base::FilePath executable_path = base::MakeAbsoluteFilePath( |
| 35 base::CommandLine::ForCurrentProcess()->GetProgram()); | 39 base::CommandLine::ForCurrentProcess()->GetProgram()); |
| 36 if (!executable_path.empty()) | 40 if (!executable_path.empty()) |
| 37 *exec_path_out = executable_path.LossyDisplayName(); | 41 *exec_path_out = executable_path.LossyDisplayName(); |
| 38 else | 42 else |
| 39 *exec_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND); | 43 *exec_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND); |
| 40 | 44 |
| 41 base::FilePath profile_path_copy(base::MakeAbsoluteFilePath(profile_path)); | 45 base::FilePath profile_path_copy(base::MakeAbsoluteFilePath(profile_path)); |
| 42 if (!profile_path.empty() && !profile_path_copy.empty()) | 46 if (!profile_path.empty() && !profile_path_copy.empty()) |
| 43 *profile_path_out = profile_path.LossyDisplayName(); | 47 *profile_path_out = profile_path.LossyDisplayName(); |
| 44 else | 48 else |
| 45 *profile_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND); | 49 *profile_path_out = l10n_util::GetStringUTF16(IDS_VERSION_UI_PATH_NOTFOUND); |
| 46 } | 50 } |
| 47 | 51 |
| 48 } // namespace | 52 } // namespace |
| 49 | 53 |
| 50 VersionHandler::VersionHandler() | 54 VersionHandler::VersionHandler( |
| 51 : weak_ptr_factory_(this) { | 55 content::WebUI* web_ui, |
| 56 mojo::InterfaceRequest<VersionHandlerMojo> request) | |
| 57 : web_ui_(web_ui), binding_(this, request.Pass()) { | |
| 52 } | 58 } |
| 53 | 59 |
| 54 VersionHandler::~VersionHandler() { | 60 VersionHandler::~VersionHandler() { |
| 55 } | 61 } |
| 56 | 62 |
| 57 void VersionHandler::RegisterMessages() { | 63 void VersionHandler::GetFilePaths(const GetFilePathsCallback& callback) { |
| 58 web_ui()->RegisterMessageCallback( | |
| 59 version_ui::kRequestVersionInfo, | |
| 60 base::Bind(&VersionHandler::HandleRequestVersionInfo, | |
| 61 base::Unretained(this))); | |
| 62 } | |
| 63 | |
| 64 void VersionHandler::HandleRequestVersionInfo(const base::ListValue* args) { | |
| 65 #if defined(ENABLE_PLUGINS) | |
| 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())); | |
| 71 #endif | |
| 72 | |
| 73 // Grab the executable path on the FILE thread. It is returned in | 64 // Grab the executable path on the FILE thread. It is returned in |
| 74 // OnGotFilePaths. | 65 // OnGotFilePaths. |
| 75 base::string16* exec_path_buffer = new base::string16; | 66 base::string16* exec_path_buffer = new base::string16; |
| 76 base::string16* profile_path_buffer = new base::string16; | 67 base::string16* profile_path_buffer = new base::string16; |
| 77 content::BrowserThread::PostTaskAndReply( | 68 content::BrowserThread::PostTaskAndReply( |
| 78 content::BrowserThread::FILE, FROM_HERE, | 69 content::BrowserThread::FILE, FROM_HERE, |
| 79 base::Bind(&GetFilePaths, Profile::FromWebUI(web_ui())->GetPath(), | 70 base::Bind(&RetrieveFilePaths, Profile::FromWebUI(web_ui_)->GetPath(), |
| 80 base::Unretained(exec_path_buffer), | 71 base::Unretained(exec_path_buffer), |
| 81 base::Unretained(profile_path_buffer)), | 72 base::Unretained(profile_path_buffer)), |
| 82 base::Bind(&VersionHandler::OnGotFilePaths, | 73 base::Bind(&VersionHandler::OnGotFilePaths, |
| 83 weak_ptr_factory_.GetWeakPtr(), | 74 Unretained(this), |
| 84 base::Owned(exec_path_buffer), | 75 base::Owned(exec_path_buffer), |
| 85 base::Owned(profile_path_buffer))); | 76 base::Owned(profile_path_buffer), |
| 86 | 77 callback)); |
| 87 // Respond with the variations info immediately. | |
| 88 web_ui()->CallJavascriptFunction(version_ui::kReturnVariationInfo, | |
| 89 *version_ui::GetVariationsList()); | |
| 90 } | 78 } |
| 91 | 79 |
| 92 void VersionHandler::OnGotFilePaths(base::string16* executable_path_data, | 80 void VersionHandler::OnGotFilePaths(base::string16* executable_path_data, |
| 93 base::string16* profile_path_data) { | 81 base::string16* profile_path_data, |
| 82 const GetFilePathsCallback& callback) { | |
| 94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 84 mojo::Array<mojo::String> file_paths(0); | |
|
sky
2015/12/04 16:35:52
AFAICT the results for the file path are always tw
dpapad
2015/12/04 18:13:15
Done. Passing two parameters to the callback sound
| |
| 85 file_paths.push_back(mojo::String::From(*executable_path_data)); | |
| 86 file_paths.push_back(mojo::String::From(*profile_path_data)); | |
| 87 callback.Run(file_paths.Pass()); | |
| 88 } | |
| 95 | 89 |
| 96 base::StringValue exec_path(*executable_path_data); | 90 void VersionHandler::GetFlashVersion(const GetFlashVersionCallback& callback) { |
| 97 base::StringValue profile_path(*profile_path_data); | 91 #if defined(ENABLE_PLUGINS) |
| 98 web_ui()->CallJavascriptFunction(version_ui::kReturnFilePaths, exec_path, | 92 // The Flash version information is needed in the response, so make sure |
| 99 profile_path); | 93 // the plugins are loaded. |
| 94 content::PluginService::GetInstance()->GetPlugins( | |
| 95 base::Bind(&VersionHandler::OnGotPlugins, | |
| 96 Unretained(this), | |
|
sky
2015/12/04 16:35:52
Why is it safe to use unretained here and other pl
dpapad
2015/12/04 18:13:15
Done. That was a misunderstanding on my part of Un
| |
| 97 callback)); | |
| 98 #endif | |
| 100 } | 99 } |
| 101 | 100 |
| 101 void VersionHandler::GetVariations(const GetVariationsCallback& callback) { | |
| 102 scoped_ptr<std::vector<std::string>> variations = version_ui::GetVariations(); | |
| 103 mojo::Array<mojo::String> variations_array = | |
| 104 mojo::Array<mojo::String>::From(*variations.get()); | |
| 105 callback.Run(variations_array.Pass()); | |
| 106 } | |
| 107 | |
| 108 void VersionHandler::GetOsVersion(const GetOsVersionCallback& callback) { | |
| 109 #if defined(OS_CHROMEOS) | |
| 110 base::PostTaskAndReplyWithResult( | |
| 111 content::BrowserThread::GetBlockingPool(), | |
| 112 FROM_HERE, | |
| 113 base::Bind(&chromeos::version_loader::GetVersion, | |
| 114 chromeos::version_loader::VERSION_FULL), | |
| 115 base::Bind(&VersionHandler::OnGotOsVersion, | |
| 116 Unretained(this), | |
| 117 callback)); | |
| 118 #endif // defined(OS_CHROMEOS) | |
| 119 } | |
| 120 | |
| 121 #if defined(OS_CHROMEOS) | |
| 122 void VersionHandler::OnGotOsVersion( | |
| 123 const GetOsVersionCallback& callback, const std::string& version) { | |
| 124 callback.Run(mojo::String::From(version)); | |
| 125 } | |
| 126 #endif // defined(OS_CHROMEOS) | |
| 127 | |
| 102 #if defined(ENABLE_PLUGINS) | 128 #if defined(ENABLE_PLUGINS) |
| 103 void VersionHandler::OnGotPlugins( | 129 void VersionHandler::OnGotPlugins( |
| 130 const GetFlashVersionCallback& callback, | |
| 104 const std::vector<content::WebPluginInfo>& plugins) { | 131 const std::vector<content::WebPluginInfo>& plugins) { |
| 105 // Obtain the version of the first enabled Flash plugin. | 132 // Obtain the version of the first enabled Flash plugin. |
| 106 std::vector<content::WebPluginInfo> info_array; | 133 std::vector<content::WebPluginInfo> info_array; |
| 107 content::PluginService::GetInstance()->GetPluginInfoArray( | 134 content::PluginService::GetInstance()->GetPluginInfoArray( |
| 108 GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL); | 135 GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL); |
| 109 base::string16 flash_version = | 136 base::string16 flash_version = |
| 110 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN); | 137 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN); |
| 111 PluginPrefs* plugin_prefs = | 138 PluginPrefs* plugin_prefs = |
| 112 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get(); | 139 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui_)).get(); |
| 113 if (plugin_prefs) { | 140 if (plugin_prefs) { |
| 114 for (size_t i = 0; i < info_array.size(); ++i) { | 141 for (size_t i = 0; i < info_array.size(); ++i) { |
| 115 if (plugin_prefs->IsPluginEnabled(info_array[i])) { | 142 if (plugin_prefs->IsPluginEnabled(info_array[i])) { |
| 116 flash_version = info_array[i].version; | 143 flash_version = info_array[i].version; |
| 117 break; | 144 break; |
| 118 } | 145 } |
| 119 } | 146 } |
| 120 } | 147 } |
| 121 | 148 |
| 122 base::StringValue arg(flash_version); | 149 callback.Run(mojo::String::From(flash_version)); |
| 123 web_ui()->CallJavascriptFunction(version_ui::kReturnFlashVersion, arg); | |
| 124 } | 150 } |
| 125 #endif // defined(ENABLE_PLUGINS) | 151 #endif // defined(ENABLE_PLUGINS) |
| OLD | NEW |