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

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: Win fixes Created 8 years, 3 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/plugin_prefs.h"
13 #include "chrome/browser/profiles/profile.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 "googleurl/src/gurl.h"
18 #include "grit/generated_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/version_loader.h"
23 #endif
24
25 namespace {
26
27 #if defined(OS_CHROMEOS)
28 // ChromeOSAboutVersionHandler is responsible for loading the Chrome OS
29 // version.
30 // ChromeOSAboutVersionHandler handles deleting itself once the version has
31 // been obtained and AboutUIHTMLSource notified.
32 class ChromeOSAboutVersionHandler {
Evan Stade 2012/09/14 09:18:02 I don't think this should be its own class; it sho
SteveT 2012/09/14 14:48:15 Done.
33 public:
34 explicit ChromeOSAboutVersionHandler(content::WebUI* web_ui);
35
36 // Callback from chromeos::VersionLoader giving the version.
37 void OnVersion(chromeos::VersionLoader::Handle handle,
38 const std::string& version);
39
40 private:
41 // The WebUI instance to respond to.
42 content::WebUI* web_ui_;
43
44 // Handles asynchronously loading the version.
45 chromeos::VersionLoader loader_;
46
47 // Used to request the version.
48 CancelableRequestConsumer consumer_;
49
50 DISALLOW_COPY_AND_ASSIGN(ChromeOSAboutVersionHandler);
51 };
52
53 ChromeOSAboutVersionHandler::ChromeOSAboutVersionHandler(content::WebUI* web_ui)
54 : web_ui_(web_ui) {
55 loader_.GetVersion(&consumer_,
56 base::Bind(&ChromeOSAboutVersionHandler::OnVersion,
57 base::Unretained(this)),
58 chromeos::VersionLoader::VERSION_FULL);
59 }
60
61 void ChromeOSAboutVersionHandler::OnVersion(
62 chromeos::VersionLoader::Handle handle,
63 const std::string& version) {
64 scoped_ptr<StringValue> arg(Value::CreateStringValue(version));
65 LOG(INFO) << "Returning OS VERSION: " << version;
66 web_ui_->CallJavascriptFunction("returnOsVersion", *arg);
67
68 // CancelableRequestProvider isn't happy when it's deleted and servicing a
69 // task, so we delay the deletion.
70 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
71 }
72 #endif
73
74 // Retrieves the executable path on the FILE thread.
75 void GetFilePaths(FilePath profile_path,
76 std::string* exec_path_out,
Evan Stade 2012/09/14 09:18:02 instead of passing in member variables as outparam
SteveT 2012/09/14 14:48:15 Kept this as a standalone helper method and ensure
77 std::string* profile_path_out) {
78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
79
80 FilePath executable_path = CommandLine::ForCurrentProcess()->GetProgram();
81 if (file_util::AbsolutePath(&executable_path))
82 #if defined(OS_WIN)
83 *exec_path_out = WideToUTF8(executable_path.value());
Evan Stade 2012/09/14 09:18:02 it would be easier to just do LossyDisplayName() (
SteveT 2012/09/14 14:48:15 Done.
84 #else
85 *exec_path_out = executable_path.value();
86 #endif
87 else
88 *exec_path_out = l10n_util::GetStringUTF8(IDS_ABOUT_VERSION_PATH_NOTFOUND);
89
90 if (!profile_path.empty() && file_util::AbsolutePath(&profile_path)) {
91 #if defined(OS_WIN)
92 *profile_path_out = WideToUTF8(profile_path.value());
93 #else
94 *profile_path_out = profile_path.value();
95 #endif
96 } else {
97 *profile_path_out =
98 l10n_util::GetStringUTF8(IDS_ABOUT_VERSION_PATH_NOTFOUND);
99 }
100 }
101
102 } // namespace
103
104 VersionHandler::VersionHandler()
105 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
106 }
107
108 VersionHandler::~VersionHandler() {
109 }
110
111 void VersionHandler::RegisterMessages() {
112 web_ui()->RegisterMessageCallback(
113 "requestVersionInfo",
114 base::Bind(&VersionHandler::HandleRequestVersionInfo,
115 base::Unretained(this)));
116 }
117
118 void VersionHandler::HandleRequestVersionInfo(const ListValue* args) {
119 #if defined(OS_CHROMEOS)
120 new ChromeOSAboutVersionHandler(web_ui());
Evan Stade 2012/09/14 09:18:02 what is going on here?
Evan Stade 2012/09/14 09:19:27 ignore this
121 #endif
122
123 // The Flash version information is needed in the response, so make sure
124 // the plugins are loaded.
125 content::PluginService::GetInstance()->GetPlugins(
126 base::Bind(&VersionHandler::OnGotPlugins,
127 weak_ptr_factory_.GetWeakPtr()));
128
129 FilePath profile_path;
130 Profile* profile = Profile::FromWebUI(web_ui());
131 if (profile) {
132 profile_path = profile->GetPath();
133 } else {
134 scoped_ptr<StringValue> profile_path(
135 Value::CreateStringValue(
136 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND)));
137 web_ui()->CallJavascriptFunction("returnProfilePath", *profile_path);
138 }
139
140 // Grab the executable path on the FILE thread. It is returned in
141 // OnFilePath.
142 content::BrowserThread::PostTaskAndReply(content::BrowserThread::FILE,
143 FROM_HERE, base::Bind(&GetFilePaths, profile_path, &executable_path_,
144 &profile_path_),
145 base::Bind(&VersionHandler::OnGotFilePaths,
146 weak_ptr_factory_.GetWeakPtr()));
147
148 // Respond with the variations info immediately.
149 scoped_ptr<ListValue> variations_list(new ListValue());
150 #if !defined(NDEBUG)
Evan Stade 2012/09/14 09:18:02 it seems like this should be disabled for official
SteveT 2012/09/14 14:48:15 I was the author :) We decided that we want to sh
151 std::string variation_state;
152 base::FieldTrialList::StatesToString(&variation_state);
153
154 std::vector<std::string> tokens;
155 base::SplitString(variation_state,
156 base::FieldTrialList::kPersistentStringSeparator,
157 &tokens);
158 // Since StatesToString appends a separator at the end, SplitString will
159 // append an extra empty string in the vector. Drop it. There should
160 // always be an even number of tokens left.
161 tokens.pop_back();
162 DCHECK_EQ(0U, tokens.size() % 2);
163
164 // |variations| is used to store the formatted strings that will be passed to
165 // the Javascript through |variations_list|.
166 std::vector<std::string> variations;
167 for (size_t i = 0; i < tokens.size(); i += 2)
168 variations.push_back(tokens[i] + ":" + tokens[i + 1]);
169
170 for (std::vector<std::string>::const_iterator it = variations.begin();
171 it != variations.end(); ++it) {
172 variations_list->Append(Value::CreateStringValue(*it));
173 }
174 #endif
175 // In release mode, this will return an empty list to clear the section.
176 web_ui()->CallJavascriptFunction("returnVariationInfo",
177 *variations_list.release());
178 }
179
180 void VersionHandler::OnGotFilePaths() {
181 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
182
183 scoped_ptr<StringValue> exec_path(Value::CreateStringValue(executable_path_));
184 scoped_ptr<StringValue> profile_path(Value::CreateStringValue(profile_path_));
185 web_ui()->CallJavascriptFunction("returnExecutablePath", *exec_path);
186 web_ui()->CallJavascriptFunction("returnProfilePath", *profile_path);
Evan Stade 2012/09/14 09:18:02 any particular reason these are two separate js ca
SteveT 2012/09/14 14:48:15 In the case where the profile was not yet availabl
187 }
188
189 void VersionHandler::OnGotPlugins(
190 const std::vector<webkit::WebPluginInfo>& plugins) {
191 #if !defined(OS_ANDROID)
192 // Obtain the version of the first enabled Flash plugin.
193 std::vector<webkit::WebPluginInfo> info_array;
194 content::PluginService::GetInstance()->GetPluginInfoArray(
195 GURL(), "application/x-shockwave-flash", false, &info_array, NULL);
196 string16 flash_version =
197 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN);
198 PluginPrefs* plugin_prefs =
199 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui()));
200 if (plugin_prefs) {
201 for (size_t i = 0; i < info_array.size(); ++i) {
202 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
203 flash_version = info_array[i].version;
204 break;
205 }
206 }
207 }
208
209 scoped_ptr<StringValue> arg(Value::CreateStringValue(flash_version));
210 web_ui()->CallJavascriptFunction("returnFlashVersion", *arg);
211 #endif
212 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698