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.
5 #include "chrome/browser/ui/webui/version_handler.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/plugins/plugin_prefs.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/metrics/variations/variations_util.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/plugin_service.h"
16 #include "content/public/browser/web_ui.h"
17 #include "content/public/common/content_constants.h"
18 #include "grit/generated_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
24 // Retrieves the executable and profile paths on the FILE thread.
25 void GetFilePaths(const base::FilePath
& profile_path
,
26 base::string16
* exec_path_out
,
27 base::string16
* profile_path_out
) {
28 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
30 base::FilePath executable_path
= base::MakeAbsoluteFilePath(
31 CommandLine::ForCurrentProcess()->GetProgram());
32 if (!executable_path
.empty()) {
33 *exec_path_out
= executable_path
.LossyDisplayName();
36 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND
);
39 base::FilePath
profile_path_copy(base::MakeAbsoluteFilePath(profile_path
));
40 if (!profile_path
.empty() && !profile_path_copy
.empty()) {
41 *profile_path_out
= profile_path
.LossyDisplayName();
44 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND
);
50 VersionHandler::VersionHandler()
51 : weak_ptr_factory_(this) {
54 VersionHandler::~VersionHandler() {
57 void VersionHandler::RegisterMessages() {
58 web_ui()->RegisterMessageCallback(
60 base::Bind(&VersionHandler::HandleRequestVersionInfo
,
61 base::Unretained(this)));
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()));
73 // Grab the executable path on the FILE thread. It is returned in
75 base::string16
* exec_path_buffer
= new base::string16
;
76 base::string16
* profile_path_buffer
= new base::string16
;
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
)));
87 // Respond with the variations info immediately.
88 std::vector
<std::string
> variations
;
90 base::FieldTrial::ActiveGroups active_groups
;
91 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups
);
93 const unsigned char kNonBreakingHyphenUTF8
[] = { 0xE2, 0x80, 0x91, '\0' };
94 const std::string
kNonBreakingHyphenUTF8String(
95 reinterpret_cast<const char*>(kNonBreakingHyphenUTF8
));
96 for (size_t i
= 0; i
< active_groups
.size(); ++i
) {
97 std::string line
= active_groups
[i
].trial_name
+ ":" +
98 active_groups
[i
].group_name
;
99 base::ReplaceChars(line
, "-", kNonBreakingHyphenUTF8String
, &line
);
100 variations
.push_back(line
);
103 // In release mode, display the hashes only.
104 chrome_variations::GetFieldTrialActiveGroupIdsAsStrings(&variations
);
107 base::ListValue variations_list
;
108 for (std::vector
<std::string
>::const_iterator it
= variations
.begin();
109 it
!= variations
.end(); ++it
) {
110 variations_list
.Append(base::Value::CreateStringValue(*it
));
113 // In release mode, this will return an empty list to clear the section.
114 web_ui()->CallJavascriptFunction("returnVariationInfo", variations_list
);
117 void VersionHandler::OnGotFilePaths(base::string16
* executable_path_data
,
118 base::string16
* profile_path_data
) {
119 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
121 base::StringValue
exec_path(*executable_path_data
);
122 base::StringValue
profile_path(*profile_path_data
);
123 web_ui()->CallJavascriptFunction("returnFilePaths", exec_path
, profile_path
);
126 #if defined(ENABLE_PLUGINS)
127 void VersionHandler::OnGotPlugins(
128 const std::vector
<content::WebPluginInfo
>& plugins
) {
129 // Obtain the version of the first enabled Flash plugin.
130 std::vector
<content::WebPluginInfo
> info_array
;
131 content::PluginService::GetInstance()->GetPluginInfoArray(
132 GURL(), content::kFlashPluginSwfMimeType
, false, &info_array
, NULL
);
133 base::string16 flash_version
=
134 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN
);
135 PluginPrefs
* plugin_prefs
=
136 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get();
138 for (size_t i
= 0; i
< info_array
.size(); ++i
) {
139 if (plugin_prefs
->IsPluginEnabled(info_array
[i
])) {
140 flash_version
= info_array
[i
].version
;
146 base::StringValue
arg(flash_version
);
147 web_ui()->CallJavascriptFunction("returnFlashVersion", arg
);
149 #endif // defined(ENABLE_PLUGINS)