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

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: more estade changes 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 namespace {
22
23 // Retrieves the executable and profile paths on the FILE thread.
24 void GetFilePaths(FilePath profile_path,
Evan Stade 2012/09/14 15:11:30 const ref
SteveT 2012/09/14 15:28:37 Unfortunately AbsolutePath below takes a FilePath*
Evan Stade 2012/09/14 16:31:04 ok. This should still be a const ref. Explicitly c
SteveT 2012/09/14 17:03:15 Okay sounds good. Copy made.
25 std::string* exec_path_out,
26 std::string* profile_path_out) {
27 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
28
29 FilePath executable_path = CommandLine::ForCurrentProcess()->GetProgram();
30 if (file_util::AbsolutePath(&executable_path)) {
31 *exec_path_out = UTF16ToASCII(executable_path.LossyDisplayName());
Evan Stade 2012/09/14 15:11:30 you can't just convert to ascii. You'd have to con
SteveT 2012/09/14 15:28:37 Everything in string16 as discussed. Thanks for ca
32 } else {
33 *exec_path_out =
34 l10n_util::GetStringUTF8(IDS_ABOUT_VERSION_PATH_NOTFOUND);
35 }
36
37 if (!profile_path.empty() && file_util::AbsolutePath(&profile_path)) {
38 *profile_path_out = UTF16ToASCII(profile_path.LossyDisplayName());
39 } else {
40 *profile_path_out =
41 l10n_util::GetStringUTF8(IDS_ABOUT_VERSION_PATH_NOTFOUND);
42 }
43 }
44
45 } // namespace
46
47 VersionHandler::VersionHandler()
48 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
49 }
50
51 VersionHandler::~VersionHandler() {
52 }
53
54 void VersionHandler::RegisterMessages() {
55 web_ui()->RegisterMessageCallback(
56 "requestVersionInfo",
57 base::Bind(&VersionHandler::HandleRequestVersionInfo,
58 base::Unretained(this)));
59 }
60
61 void VersionHandler::HandleRequestVersionInfo(const ListValue* args) {
62 // The Flash version information is needed in the response, so make sure
63 // the plugins are loaded.
64 content::PluginService::GetInstance()->GetPlugins(
65 base::Bind(&VersionHandler::OnGotPlugins,
66 weak_ptr_factory_.GetWeakPtr()));
67
68 FilePath profile_path;
69 Profile* profile = Profile::FromWebUI(web_ui());
70 if (profile) {
Evan Stade 2012/09/14 15:11:30 I don't think it's possible for this to fail
SteveT 2012/09/14 15:28:37 The original code checked for this, but it seems l
71 profile_path = profile->GetPath();
72 } else {
73 scoped_ptr<StringValue> profile_path(
Evan Stade 2012/09/14 15:11:30 shadowing is confusing
SteveT 2012/09/14 15:28:37 Shadowing? You mean use of scoped_ptr here? I've
Evan Stade 2012/09/14 16:31:04 I meant that this local profile_path shadowed the
SteveT 2012/09/14 17:03:15 Oh damn, I didn't notice that. It's been resolved
74 Value::CreateStringValue(
75 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND)));
76 web_ui()->CallJavascriptFunction("returnProfilePath", *profile_path);
77 }
78
79 // Grab the executable path on the FILE thread. It is returned in
80 // OnGotFilePaths.
81 std::string* exec_path_buffer = new std::string();
82 std::string* profile_path_buffer = new std::string();
83 content::BrowserThread::PostTaskAndReply(
84 content::BrowserThread::FILE, FROM_HERE,
85 base::Bind(&GetFilePaths, profile_path,
86 base::Unretained(exec_path_buffer),
87 base::Unretained(profile_path_buffer)),
88 base::Bind(&VersionHandler::OnGotFilePaths,
89 weak_ptr_factory_.GetWeakPtr(),
90 base::Owned(exec_path_buffer),
91 base::Owned(profile_path_buffer)));
92
93 // Respond with the variations info immediately.
94 scoped_ptr<ListValue> variations_list(new ListValue());
95 #if !defined(NDEBUG)
96 std::string variation_state;
97 base::FieldTrialList::StatesToString(&variation_state);
98
99 std::vector<std::string> tokens;
100 base::SplitString(variation_state,
101 base::FieldTrialList::kPersistentStringSeparator,
102 &tokens);
103 // Since StatesToString appends a separator at the end, SplitString will
104 // append an extra empty string in the vector. Drop it. There should
105 // always be an even number of tokens left.
106 tokens.pop_back();
107 DCHECK_EQ(0U, tokens.size() % 2);
108
109 // |variations| is used to store the formatted strings that will be passed to
110 // the Javascript through |variations_list|.
111 std::vector<std::string> variations;
112 for (size_t i = 0; i < tokens.size(); i += 2)
113 variations.push_back(tokens[i] + ":" + tokens[i + 1]);
114
115 for (std::vector<std::string>::const_iterator it = variations.begin();
116 it != variations.end(); ++it) {
117 variations_list->Append(Value::CreateStringValue(*it));
118 }
119 #endif
120 // In release mode, this will return an empty list to clear the section.
121 web_ui()->CallJavascriptFunction("returnVariationInfo",
122 *variations_list.release());
123 }
124
125 void VersionHandler::OnGotFilePaths(std::string* executable_path_data,
126 std::string* profile_path_data) {
127 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
128
129 scoped_ptr<StringValue> exec_path(
Evan Stade 2012/09/14 15:11:30 just base::StringValue(*executable_path_data) is s
SteveT 2012/09/14 15:28:37 I believe I did this earlier but had complaints on
Evan Stade 2012/09/14 16:31:04 well it seems to work in some places: http://code.
SteveT 2012/09/14 17:03:15 You're right that it's a simpler approach. Done ev
130 Value::CreateStringValue(*executable_path_data));
131 web_ui()->CallJavascriptFunction("returnExecutablePath", *exec_path);
132 scoped_ptr<StringValue> profile_path(
133 Value::CreateStringValue(*profile_path_data));
134 web_ui()->CallJavascriptFunction("returnProfilePath", *profile_path);
135 }
136
137 void VersionHandler::OnGotPlugins(
138 const std::vector<webkit::WebPluginInfo>& plugins) {
139 #if !defined(OS_ANDROID)
140 // Obtain the version of the first enabled Flash plugin.
141 std::vector<webkit::WebPluginInfo> info_array;
142 content::PluginService::GetInstance()->GetPluginInfoArray(
143 GURL(), "application/x-shockwave-flash", false, &info_array, NULL);
144 string16 flash_version =
145 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN);
146 PluginPrefs* plugin_prefs =
147 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui()));
148 if (plugin_prefs) {
149 for (size_t i = 0; i < info_array.size(); ++i) {
150 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
151 flash_version = info_array[i].version;
152 break;
153 }
154 }
155 }
156
157 scoped_ptr<StringValue> arg(Value::CreateStringValue(flash_version));
158 web_ui()->CallJavascriptFunction("returnFlashVersion", *arg);
159 #endif
160 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698