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

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: copy FilePath 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(const FilePath& profile_path,
25 string16* exec_path_out,
26 string16* 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 = executable_path.LossyDisplayName();
32 } else {
33 *exec_path_out =
34 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND);
35 }
36
37 FilePath profile_path_copy(profile_path);
38 if (!profile_path.empty() && file_util::AbsolutePath(&profile_path_copy)) {
39 *profile_path_out = profile_path.LossyDisplayName();
40 } else {
41 *profile_path_out =
42 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND);
43 }
44 }
45
46 } // namespace
47
48 VersionHandler::VersionHandler()
49 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
50 }
51
52 VersionHandler::~VersionHandler() {
53 }
54
55 void VersionHandler::RegisterMessages() {
56 web_ui()->RegisterMessageCallback(
57 "requestVersionInfo",
58 base::Bind(&VersionHandler::HandleRequestVersionInfo,
59 base::Unretained(this)));
60 }
61
62 void VersionHandler::HandleRequestVersionInfo(const ListValue* args) {
63 // The Flash version information is needed in the response, so make sure
64 // the plugins are loaded.
65 content::PluginService::GetInstance()->GetPlugins(
66 base::Bind(&VersionHandler::OnGotPlugins,
67 weak_ptr_factory_.GetWeakPtr()));
68
69 FilePath profile_path = Profile::FromWebUI(web_ui())->GetPath();
Evan Stade 2012/09/17 09:24:45 probably not worth storing in a local var
SteveT 2012/09/17 15:45:28 Done.
70
71 // Grab the executable path on the FILE thread. It is returned in
72 // OnGotFilePaths.
73 string16* exec_path_buffer = new string16;
74 string16* profile_path_buffer = new string16;
75 content::BrowserThread::PostTaskAndReply(
76 content::BrowserThread::FILE, FROM_HERE,
77 base::Bind(&GetFilePaths, profile_path,
78 base::Unretained(exec_path_buffer),
79 base::Unretained(profile_path_buffer)),
80 base::Bind(&VersionHandler::OnGotFilePaths,
81 weak_ptr_factory_.GetWeakPtr(),
82 base::Owned(exec_path_buffer),
83 base::Owned(profile_path_buffer)));
84
85 // Respond with the variations info immediately.
86 scoped_ptr<ListValue> variations_list(new ListValue());
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
101 // |variations| is used to store the formatted strings that will be passed to
102 // the Javascript through |variations_list|.
103 std::vector<std::string> variations;
104 for (size_t i = 0; i < tokens.size(); i += 2)
105 variations.push_back(tokens[i] + ":" + tokens[i + 1]);
106
107 for (std::vector<std::string>::const_iterator it = variations.begin();
108 it != variations.end(); ++it) {
109 variations_list->Append(Value::CreateStringValue(*it));
110 }
111 #endif
112 // In release mode, this will return an empty list to clear the section.
113 web_ui()->CallJavascriptFunction("returnVariationInfo",
114 *variations_list.release());
115 }
116
117 void VersionHandler::OnGotFilePaths(string16* executable_path_data,
118 string16* profile_path_data) {
119 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
120
121 StringValue exec_path(*executable_path_data);
122 web_ui()->CallJavascriptFunction("returnExecutablePath", exec_path);
Evan Stade 2012/09/17 09:24:45 and now these two js calls can be combined.
SteveT 2012/09/17 15:45:28 Ah yes. Done.
123 StringValue profile_path(*profile_path_data);
124 web_ui()->CallJavascriptFunction("returnProfilePath", profile_path);
125 }
126
127 void VersionHandler::OnGotPlugins(
128 const std::vector<webkit::WebPluginInfo>& plugins) {
129 #if !defined(OS_ANDROID)
130 // Obtain the version of the first enabled Flash plugin.
131 std::vector<webkit::WebPluginInfo> info_array;
132 content::PluginService::GetInstance()->GetPluginInfoArray(
133 GURL(), "application/x-shockwave-flash", false, &info_array, NULL);
134 string16 flash_version =
135 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN);
136 PluginPrefs* plugin_prefs =
137 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui()));
138 if (plugin_prefs) {
139 for (size_t i = 0; i < info_array.size(); ++i) {
140 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
141 flash_version = info_array[i].version;
142 break;
143 }
144 }
145 }
146
147 StringValue arg(flash_version);
148 web_ui()->CallJavascriptFunction("returnFlashVersion", arg);
149 #endif
150 }
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