Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(675)

Side by Side Diff: chrome/browser/ui/webui/version_handler.cc

Issue 1486403002: Mojo-ifying chrome://version. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 AsWeakPtr(),
Evan Stade 2015/12/04 22:30:20 why this change? AFAIK AsWeakPtr is used when exte
dpapad 2015/12/04 23:28:13 My understanding is that inheriting from base::Sup
Dan Beam 2015/12/04 23:32:55 WeakPtrFactory is safer https://groups.google.com/
dpapad 2015/12/04 23:42:26 Reverted. Thanks for the link.
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 callback.Run(mojo::String::From(*executable_path_data),
85 mojo::String::From(*profile_path_data));
86 }
95 87
96 base::StringValue exec_path(*executable_path_data); 88 void VersionHandler::GetFlashVersion(const GetFlashVersionCallback& callback) {
97 base::StringValue profile_path(*profile_path_data); 89 #if defined(ENABLE_PLUGINS)
98 web_ui()->CallJavascriptFunction(version_ui::kReturnFilePaths, exec_path, 90 // The Flash version information is needed in the response, so make sure
99 profile_path); 91 // the plugins are loaded.
92 content::PluginService::GetInstance()->GetPlugins(
93 base::Bind(&VersionHandler::OnGotPlugins,
94 AsWeakPtr(),
95 callback));
96 #endif
100 } 97 }
101 98
99 void VersionHandler::GetVariations(const GetVariationsCallback& callback) {
100 std::vector<std::string> variations = version_ui::GetVariations();
101 mojo::Array<mojo::String> variations_array =
102 mojo::Array<mojo::String>::From(variations);
103 callback.Run(variations_array.Pass());
104 }
105
106 void VersionHandler::GetOsVersion(const GetOsVersionCallback& callback) {
107 #if defined(OS_CHROMEOS)
108 base::PostTaskAndReplyWithResult(
109 content::BrowserThread::GetBlockingPool(),
110 FROM_HERE,
111 base::Bind(&chromeos::version_loader::GetVersion,
112 chromeos::version_loader::VERSION_FULL),
113 base::Bind(&VersionHandler::OnGotOsVersion,
114 AsWeakPtr(),
115 callback));
116 #endif // defined(OS_CHROMEOS)
117 }
118
119 #if defined(OS_CHROMEOS)
120 void VersionHandler::OnGotOsVersion(
121 const GetOsVersionCallback& callback, const std::string& version) {
122 callback.Run(mojo::String::From(version));
123 }
124 #endif // defined(OS_CHROMEOS)
125
102 #if defined(ENABLE_PLUGINS) 126 #if defined(ENABLE_PLUGINS)
103 void VersionHandler::OnGotPlugins( 127 void VersionHandler::OnGotPlugins(
128 const GetFlashVersionCallback& callback,
104 const std::vector<content::WebPluginInfo>& plugins) { 129 const std::vector<content::WebPluginInfo>& plugins) {
105 // Obtain the version of the first enabled Flash plugin. 130 // Obtain the version of the first enabled Flash plugin.
106 std::vector<content::WebPluginInfo> info_array; 131 std::vector<content::WebPluginInfo> info_array;
107 content::PluginService::GetInstance()->GetPluginInfoArray( 132 content::PluginService::GetInstance()->GetPluginInfoArray(
108 GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL); 133 GURL(), content::kFlashPluginSwfMimeType, false, &info_array, NULL);
109 base::string16 flash_version = 134 base::string16 flash_version =
110 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN); 135 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN);
111 PluginPrefs* plugin_prefs = 136 PluginPrefs* plugin_prefs =
112 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get(); 137 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui_)).get();
113 if (plugin_prefs) { 138 if (plugin_prefs) {
114 for (size_t i = 0; i < info_array.size(); ++i) { 139 for (size_t i = 0; i < info_array.size(); ++i) {
115 if (plugin_prefs->IsPluginEnabled(info_array[i])) { 140 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
116 flash_version = info_array[i].version; 141 flash_version = info_array[i].version;
117 break; 142 break;
118 } 143 }
119 } 144 }
120 } 145 }
121 146
122 base::StringValue arg(flash_version); 147 callback.Run(mojo::String::From(flash_version));
123 web_ui()->CallJavascriptFunction(version_ui::kReturnFlashVersion, arg);
124 } 148 }
125 #endif // defined(ENABLE_PLUGINS) 149 #endif // defined(ENABLE_PLUGINS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698