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

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

Issue 10916182: Refactor the about:version code out of about_ui. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: rebase + lei nit Created 8 years, 2 months 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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"
8 #include "base/file_util.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/string_split.h"
11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/plugins/plugin_prefs.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/metrics/variations/variations_util.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/plugin_service.h"
17 #include "content/public/browser/web_ui.h"
18 #include "googleurl/src/gurl.h"
19 #include "grit/generated_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
21
22 namespace {
23
24 // Retrieves the executable and profile paths on the FILE thread.
25 void GetFilePaths(const FilePath& profile_path,
26 string16* exec_path_out,
27 string16* profile_path_out) {
28 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
29
30 FilePath executable_path = CommandLine::ForCurrentProcess()->GetProgram();
31 if (file_util::AbsolutePath(&executable_path)) {
32 *exec_path_out = executable_path.LossyDisplayName();
33 } else {
34 *exec_path_out =
35 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND);
36 }
37
38 FilePath profile_path_copy(profile_path);
39 if (!profile_path.empty() && file_util::AbsolutePath(&profile_path_copy)) {
40 *profile_path_out = profile_path.LossyDisplayName();
41 } else {
42 *profile_path_out =
43 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND);
44 }
45 }
46
47 } // namespace
48
49 VersionHandler::VersionHandler()
50 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
51 }
52
53 VersionHandler::~VersionHandler() {
54 }
55
56 void VersionHandler::RegisterMessages() {
57 web_ui()->RegisterMessageCallback(
58 "requestVersionInfo",
59 base::Bind(&VersionHandler::HandleRequestVersionInfo,
60 base::Unretained(this)));
61 }
62
63 void VersionHandler::HandleRequestVersionInfo(const ListValue* args) {
64 // The Flash version information is needed in the response, so make sure
65 // the plugins are loaded.
66 content::PluginService::GetInstance()->GetPlugins(
67 base::Bind(&VersionHandler::OnGotPlugins,
68 weak_ptr_factory_.GetWeakPtr()));
69
70 // Grab the executable path on the FILE thread. It is returned in
71 // OnGotFilePaths.
72 string16* exec_path_buffer = new string16;
73 string16* profile_path_buffer = new string16;
74 content::BrowserThread::PostTaskAndReply(
75 content::BrowserThread::FILE, FROM_HERE,
76 base::Bind(&GetFilePaths, Profile::FromWebUI(web_ui())->GetPath(),
77 base::Unretained(exec_path_buffer),
78 base::Unretained(profile_path_buffer)),
79 base::Bind(&VersionHandler::OnGotFilePaths,
80 weak_ptr_factory_.GetWeakPtr(),
81 base::Owned(exec_path_buffer),
82 base::Owned(profile_path_buffer)));
83
84 // Respond with the variations info immediately.
85 scoped_ptr<ListValue> variations_list(new ListValue());
86 std::vector<std::string> variations;
87 #if !defined(NDEBUG)
88 std::string variation_state;
89 base::FieldTrialList::StatesToString(&variation_state);
90
91 std::vector<std::string> tokens;
92 base::SplitString(variation_state,
93 base::FieldTrialList::kPersistentStringSeparator,
94 &tokens);
95 // Since StatesToString appends a separator at the end, SplitString will
96 // append an extra empty string in the vector. Drop it. There should
97 // always be an even number of tokens left.
98 tokens.pop_back();
99 DCHECK_EQ(0U, tokens.size() % 2);
100 for (size_t i = 0; i < tokens.size(); i += 2)
101 variations.push_back(tokens[i] + ":" + tokens[i + 1]);
102 #else
103 // In release mode, display the hashes only.
104 std::vector<string16> selected_groups;
105 chrome_variations::GetFieldTrialSelectedGroupIdsAsStrings(&selected_groups);
106 for (size_t i = 0; i < selected_groups.size(); ++i)
107 variations.push_back(UTF16ToASCII(selected_groups[i]));
108 #endif
109
110 for (std::vector<std::string>::const_iterator it = variations.begin();
111 it != variations.end(); ++it) {
112 variations_list->Append(Value::CreateStringValue(*it));
113 }
114
115 // In release mode, this will return an empty list to clear the section.
116 web_ui()->CallJavascriptFunction("returnVariationInfo",
117 *variations_list.release());
118 }
119
120 void VersionHandler::OnGotFilePaths(string16* executable_path_data,
121 string16* profile_path_data) {
122 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
123
124 StringValue exec_path(*executable_path_data);
125 StringValue profile_path(*profile_path_data);
126 web_ui()->CallJavascriptFunction("returnFilePaths", exec_path, profile_path);
127 }
128
129 void VersionHandler::OnGotPlugins(
130 const std::vector<webkit::WebPluginInfo>& plugins) {
131 #if !defined(OS_ANDROID)
132 // Obtain the version of the first enabled Flash plugin.
133 std::vector<webkit::WebPluginInfo> info_array;
134 content::PluginService::GetInstance()->GetPluginInfoArray(
135 GURL(), "application/x-shockwave-flash", false, &info_array, NULL);
136 string16 flash_version =
137 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN);
138 PluginPrefs* plugin_prefs =
139 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui()));
140 if (plugin_prefs) {
141 for (size_t i = 0; i < info_array.size(); ++i) {
142 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
143 flash_version = info_array[i].version;
144 break;
145 }
146 }
147 }
148
149 StringValue arg(flash_version);
150 web_ui()->CallJavascriptFunction("returnFlashVersion", arg);
151 #endif
152 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/version_handler.h ('k') | chrome/browser/ui/webui/version_handler_chromeos.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698