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

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

Issue 10916182: Refactor the about:version code out of about_ui. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Refactoring complete 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_ui.h"
6
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/string_split.h"
12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/plugin_prefs.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
16 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
17 #include "chrome/common/chrome_version_info.h"
18 #include "chrome/common/jstemplate_builder.h"
19 #include "chrome/common/url_constants.h"
20 #include "content/public/browser/plugin_service.h"
21 #include "content/public/browser/web_ui.h"
22 #include "content/public/browser/web_ui_message_handler.h"
23 #include "content/public/common/content_client.h"
24 #include "grit/browser_resources.h"
25 #include "grit/chromium_strings.h"
26 #include "grit/generated_resources.h"
27 #include "grit/google_chrome_strings.h"
28 #include "ui/base/l10n/l10n_util.h"
29 #include "v8/include/v8.h"
30 #include "webkit/plugins/webplugininfo.h"
31 #include "webkit/user_agent/user_agent_util.h"
32
33 #if defined(ENABLE_THEMES)
34 #include "chrome/browser/ui/webui/theme_source.h"
35 #endif
36
37 #if defined(OS_CHROMEOS)
38 #include "chrome/browser/chromeos/version_loader.h"
39 #endif
40
41 namespace {
42
43 ChromeWebUIDataSource* CreateVersionUIDataSource(Profile* profile) {
44 ChromeWebUIDataSource* html_source =
45 new ChromeWebUIDataSource(chrome::kChromeUIVersionHost);
46
47 // Localized and data strings.
48 html_source->AddLocalizedString("title", IDS_ABOUT_VERSION_TITLE);
49 chrome::VersionInfo version_info;
Evan Stade 2012/09/13 08:05:50 switch lines 49 and 50
SteveT 2012/09/13 14:58:01 Done.
50 html_source->AddLocalizedString("name", IDS_PRODUCT_NAME);
51 html_source->AddString("version", ASCIIToUTF16(version_info.Version()));
52 // Bug 79458: Need to evaluate the use of getting the version string on
Evan Stade 2012/09/13 08:05:50 nit: Prefer URL format: http://crbug.com/79458
SteveT 2012/09/13 14:58:01 Done.
53 // this thread.
54 base::ThreadRestrictions::ScopedAllowIO allow_io;
55 html_source->AddString("version_modifier",
56 ASCIIToUTF16(chrome::VersionInfo::GetVersionStringModifier()));
57 html_source->AddLocalizedString("os_name", IDS_ABOUT_VERSION_OS);
58 html_source->AddLocalizedString("platform", IDS_PLATFORM_LABEL);
59 html_source->AddString("os_type", ASCIIToUTF16(version_info.OSType()));
60 html_source->AddString("os_version", string16());
61 html_source->AddString("webkit_version",
62 ASCIIToUTF16(webkit_glue::GetWebKitVersion()));
63 html_source->AddString("js_engine", ASCIIToUTF16("V8"));
64 html_source->AddString("js_version", ASCIIToUTF16(v8::V8::GetVersion()));
65
66 #if !defined(OS_ANDROID)
67 html_source->AddString("flash_plugin", ASCIIToUTF16("Flash"));
68 // Note that the Flash version is retrieve asynchronously and returned in
69 // VersionDOMHandler::OnGotPlugins. The initial text is a loading message.
70 html_source->AddLocalizedString("flash_version",
71 IDS_ABOUT_VERSION_LOADING_MESSAGE);
72 #endif
73 html_source->AddLocalizedString("company", IDS_ABOUT_VERSION_COMPANY_NAME);
74 html_source->AddLocalizedString("copyright", IDS_ABOUT_VERSION_COPYRIGHT);
75 html_source->AddString("cl", ASCIIToUTF16(version_info.LastChange()));
76 html_source->AddLocalizedString("official",
77 version_info.IsOfficialBuild() ?
78 IDS_ABOUT_VERSION_OFFICIAL
79 : IDS_ABOUT_VERSION_UNOFFICIAL);
80 html_source->AddLocalizedString("user_agent_name",
81 IDS_ABOUT_VERSION_USER_AGENT);
82 html_source->AddString("useragent",
83 ASCIIToUTF16(content::GetUserAgent(GURL())));
84 html_source->AddLocalizedString("command_line_name",
85 IDS_ABOUT_VERSION_COMMAND_LINE);
86
87 #if defined(OS_WIN)
88 html_source->AddString("command_line",
89 WideToUTF16(CommandLine::ForCurrentProcess()->GetCommandLineString()));
90 #elif defined(OS_POSIX)
91 std::string command_line = "";
92 typedef std::vector<std::string> ArgvList;
93 const ArgvList& argv = CommandLine::ForCurrentProcess()->argv();
94 for (ArgvList::const_iterator iter = argv.begin(); iter != argv.end(); iter++)
95 command_line += " " + *iter;
96 // TODO(viettrungluu): |command_line| could really have any encoding, whereas
97 // below we assumes it's UTF-8.
Evan Stade 2012/09/13 08:05:50 you've changed this code and now this comment is n
SteveT 2012/09/13 14:58:01 Added AddString as you suggested and used that ins
98 html_source->AddString("command_line", ASCIIToUTF16(command_line));
99 #endif
100
101 // Allow IO temporarily based on allow_io (defined above)
102 // since the following operation will complete quickly
Evan Stade 2012/09/13 08:05:50 1) do not execute file operations on the UI thread
SteveT 2012/09/13 14:58:01 Resolved this whole thing by having DomHandler dis
103 html_source->AddLocalizedString("executable_path_name",
104 IDS_ABOUT_VERSION_EXECUTABLE_PATH);
105 FilePath executable_path = CommandLine::ForCurrentProcess()->GetProgram();
106 if (file_util::AbsolutePath(&executable_path)) {
107 html_source->AddString("executable_path",
108 ASCIIToUTF16(executable_path.value()));
109 } else {
110 html_source->AddLocalizedString("executable_path",
111 IDS_ABOUT_VERSION_PATH_NOTFOUND);
112 }
113 html_source->AddLocalizedString("profile_path_name",
114 IDS_ABOUT_VERSION_PROFILE_PATH);
115 if (profile) {
116 FilePath profile_path = profile->GetPath();
117 if (file_util::AbsolutePath(&profile_path))
118 html_source->AddString("profile_path",
119 ASCIIToUTF16(profile_path.value()));
120 else
121 html_source->AddLocalizedString("profile_path",
122 IDS_ABOUT_VERSION_PATH_NOTFOUND);
123 } else {
124 html_source->AddLocalizedString("profile_path",
125 IDS_ABOUT_VERSION_PATH_NOTFOUND);
126 }
127
128 #if !defined(NDEBUG)
129 html_source->AddLocalizedString("variations_name",
130 IDS_ABOUT_VERSION_VARIATIONS);
131 #endif
132 html_source->set_use_json_js_format_v2();
133 html_source->set_json_path("strings.js");
134 html_source->add_resource_path("version.js", IDR_ABOUT_VERSION_JS);
135 html_source->set_default_resource(IDR_ABOUT_VERSION_HTML);
136 return html_source;
137 }
138
139 #if defined(OS_CHROMEOS)
140 // ChromeOSAboutVersionHandler is responsible for loading the Chrome OS
141 // version.
142 // ChromeOSAboutVersionHandler handles deleting itself once the version has
143 // been obtained and AboutUIHTMLSource notified.
144 class ChromeOSAboutVersionHandler {
145 public:
146 explicit ChromeOSAboutVersionHandler(WebUI* web_ui);
147
148 // Callback from chromeos::VersionLoader giving the version.
149 void OnVersion(chromeos::VersionLoader::Handle handle,
150 const std::string& version);
151
152 private:
153 // The WebUI instance to respond to.
154 WebUI* web_ui_;
155
156 // Handles asynchronously loading the version.
157 chromeos::VersionLoader loader_;
158
159 // Used to request the version.
160 CancelableRequestConsumer consumer_;
161
162 DISALLOW_COPY_AND_ASSIGN(ChromeOSAboutVersionHandler);
163 };
164
165 ChromeOSAboutVersionHandler::ChromeOSAboutVersionHandler(WebUI* web_ui)
166 : web_ui_(web_ui) {
167 loader_.GetVersion(&consumer_,
168 base::Bind(&ChromeOSAboutVersionHandler::OnVersion,
169 base::Unretained(this)),
170 chromeos::VersionLoader::VERSION_FULL);
171 }
172
173 void ChromeOSAboutVersionHandler::OnVersion(
174 chromeos::VersionLoader::Handle handle,
175 const std::string& version) {
176 web_ui_->CallJavascriptFunction("returnOsVersion",
177 StringValue(os_version_data));
178
179 // CancelableRequestProvider isn't happy when it's deleted and servicing a
180 // task, so we delay the deletion.
181 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
182 }
183 #endif
184
185 // Handler class for Version page operations.
186 class VersionDOMHandler : public content::WebUIMessageHandler {
Evan Stade 2012/09/13 08:05:50 Should just be VersionHandler, and should be in it
SteveT 2012/09/13 14:58:01 Done.
187 public:
188 VersionDOMHandler();
189 virtual ~VersionDOMHandler();
190
191 // content::WebUIMessageHandler implementation.
192 virtual void RegisterMessages() OVERRIDE;
193
194 // Callback for the "requestVersionInfo" message. This asynchronously requests
195 // the flash version and eventually returns it to the front end along with the
196 // list of variations using OnGotPlugins.
197 void HandleRequestVersionInfo(const ListValue* args);
198
199 private:
200 // Callback for GetPlugins which responds to the page with the Flash version.
201 // This also initiates the OS Version load on ChromeOS.
202 void OnGotPlugins(const std::vector<webkit::WebPluginInfo>& plugins);
203
204 // Factory for the creating refs in callbacks.
205 base::WeakPtrFactory<VersionDOMHandler> weak_ptr_factory_;
206
207 DISALLOW_COPY_AND_ASSIGN(VersionDOMHandler);
208 };
209
210 VersionDOMHandler::VersionDOMHandler()
211 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
212 }
213
214 VersionDOMHandler::~VersionDOMHandler() {
215 }
216
217 void VersionDOMHandler::RegisterMessages() {
218 web_ui()->RegisterMessageCallback(
219 "requestVersionInfo",
220 base::Bind(&VersionDOMHandler::HandleRequestVersionInfo,
221 base::Unretained(this)));
222 }
223
224 void VersionDOMHandler::HandleRequestVersionInfo(const ListValue* args) {
225 // The Flash version information is needed in the response, so make sure
226 // the plugins are loaded.
227 content::PluginService::GetInstance()->GetPlugins(
228 base::Bind(&VersionDOMHandler::OnGotPlugins,
229 weak_ptr_factory_.GetWeakPtr()));
230
231 // Respond with the variations info immediately.
232 scoped_ptr<ListValue> variations_list(new ListValue());
233 #if !defined(NDEBUG)
234 std::string variation_state;
235 base::FieldTrialList::StatesToString(&variation_state);
236
237 std::vector<std::string> tokens;
238 base::SplitString(variation_state,
239 base::FieldTrialList::kPersistentStringSeparator,
240 &tokens);
241 // Since StatesToString appends a separator at the end, SplitString will
242 // append an extra empty string in the vector. Drop it. There should
243 // always be an even number of tokens left.
244 tokens.pop_back();
245 DCHECK_EQ(0U, tokens.size() % 2);
246
247 // |variations| is used to store the formatted strings that will be passed to
248 // the Javascript through |variations_list|.
249 std::vector<std::string> variations;
250 for (size_t i = 0; i < tokens.size(); i += 2)
251 variations.push_back(tokens[i] + ":" + tokens[i + 1]);
252
253 for (std::vector<std::string>::const_iterator it = variations.begin();
254 it != variations.end(); ++it) {
255 variations_list->Append(Value::CreateStringValue(*it));
256 }
257 #endif
258 // In release mode, this will return an empty list to clear the section.
259 web_ui()->CallJavascriptFunction("returnVariationInfo",
260 *variations_list.release());
261 }
262
263 void VersionDOMHandler::OnGotPlugins(
264 const std::vector<webkit::WebPluginInfo>& plugins) {
265 #if defined(OS_CHROMEOS)
266 new ChromeOSAboutVersionHandler(web_ui());
267 #endif
268
269 #if !defined(OS_ANDROID)
270 // Obtain the version of the first enabled Flash plugin.
271 std::vector<webkit::WebPluginInfo> info_array;
272 content::PluginService::GetInstance()->GetPluginInfoArray(
273 GURL(), "application/x-shockwave-flash", false, &info_array, NULL);
274 string16 flash_version =
275 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN);
276 PluginPrefs* plugin_prefs =
277 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui()));
278 if (plugin_prefs) {
279 for (size_t i = 0; i < info_array.size(); ++i) {
280 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
281 flash_version = info_array[i].version;
282 break;
283 }
284 }
285 }
286 web_ui()->CallJavascriptFunction("returnFlashVersion",
287 StringValue(flash_version));
288 #endif
289 }
290
291 } // namespace
292
293 VersionUI::VersionUI(content::WebUI* web_ui)
294 : content::WebUIController(web_ui) {
295 Profile* profile = Profile::FromWebUI(web_ui);
296
297 web_ui->AddMessageHandler(new VersionDOMHandler());
298
299 #if defined(ENABLE_THEMES)
300 // Set up the chrome://theme/ source.
301 ThemeSource* theme = new ThemeSource(profile);
302 ChromeURLDataManager::AddDataSource(profile, theme);
303 #endif
304
305 ChromeURLDataManager::AddDataSource(profile,
306 CreateVersionUIDataSource(profile));
307 }
308
309 VersionUI::~VersionUI() {
310 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698