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

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: Addressed estade comments 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 #if defined(OS_CHROMEOS)
24 // ChromeOSAboutVersionHandler is responsible for loading the Chrome OS
25 // version.
26 // ChromeOSAboutVersionHandler handles deleting itself once the version has
27 // been obtained and AboutUIHTMLSource notified.
28 class ChromeOSAboutVersionHandler {
29 public:
30 explicit ChromeOSAboutVersionHandler(WebUI* web_ui);
31
32 // Callback from chromeos::VersionLoader giving the version.
33 void OnVersion(chromeos::VersionLoader::Handle handle,
34 const std::string& version);
35
36 private:
37 // The WebUI instance to respond to.
38 WebUI* web_ui_;
39
40 // Handles asynchronously loading the version.
41 chromeos::VersionLoader loader_;
42
43 // Used to request the version.
44 CancelableRequestConsumer consumer_;
45
46 DISALLOW_COPY_AND_ASSIGN(ChromeOSAboutVersionHandler);
47 };
48
49 ChromeOSAboutVersionHandler::ChromeOSAboutVersionHandler(WebUI* web_ui)
50 : web_ui_(web_ui) {
51 loader_.GetVersion(&consumer_,
52 base::Bind(&ChromeOSAboutVersionHandler::OnVersion,
53 base::Unretained(this)),
54 chromeos::VersionLoader::VERSION_FULL);
55 }
56
57 void ChromeOSAboutVersionHandler::OnVersion(
58 chromeos::VersionLoader::Handle handle,
59 const std::string& version) {
60 web_ui_->CallJavascriptFunction("returnOsVersion",
61 StringValue(os_version_data));
62
63 // CancelableRequestProvider isn't happy when it's deleted and servicing a
64 // task, so we delay the deletion.
65 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
66 }
67 #endif
68
69 // Retrieves the executable path on the FILE thread.
70 void GetExecutablePath(string16* out_string) {
71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
72
73 FilePath executable_path = CommandLine::ForCurrentProcess()->GetProgram();
74 if (file_util::AbsolutePath(&executable_path))
75 *out_string = ASCIIToUTF16(executable_path.value());
76 else
77 *out_string = l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND);
78 }
79
80 } // namespace
81
82 VersionHandler::VersionHandler()
83 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
84 }
85
86 VersionHandler::~VersionHandler() {
87 }
88
89 void VersionHandler::RegisterMessages() {
90 web_ui()->RegisterMessageCallback(
91 "requestVersionInfo",
92 base::Bind(&VersionHandler::HandleRequestVersionInfo,
93 base::Unretained(this)));
94 }
95
96 void VersionHandler::HandleRequestVersionInfo(const ListValue* args) {
97 // The Flash version information is needed in the response, so make sure
98 // the plugins are loaded.
99 content::PluginService::GetInstance()->GetPlugins(
100 base::Bind(&VersionHandler::OnGotPlugins,
101 weak_ptr_factory_.GetWeakPtr()));
102
103 // Grab the executable path on the FILE thread. It is returned in
104 // OnFilePath.
105 content::BrowserThread::PostTaskAndReply(content::BrowserThread::FILE,
106 FROM_HERE, base::Bind(&GetExecutablePath, &executable_path_),
107 base::Bind(&VersionHandler::OnGotExecutablePath,
108 weak_ptr_factory_.GetWeakPtr()));
109
110 // Respond with the variations info immediately.
111 scoped_ptr<ListValue> variations_list(new ListValue());
112 #if !defined(NDEBUG)
113 std::string variation_state;
114 base::FieldTrialList::StatesToString(&variation_state);
115
116 std::vector<std::string> tokens;
117 base::SplitString(variation_state,
118 base::FieldTrialList::kPersistentStringSeparator,
119 &tokens);
120 // Since StatesToString appends a separator at the end, SplitString will
121 // append an extra empty string in the vector. Drop it. There should
122 // always be an even number of tokens left.
123 tokens.pop_back();
124 DCHECK_EQ(0U, tokens.size() % 2);
125
126 // |variations| is used to store the formatted strings that will be passed to
127 // the Javascript through |variations_list|.
128 std::vector<std::string> variations;
129 for (size_t i = 0; i < tokens.size(); i += 2)
130 variations.push_back(tokens[i] + ":" + tokens[i + 1]);
131
132 for (std::vector<std::string>::const_iterator it = variations.begin();
133 it != variations.end(); ++it) {
134 variations_list->Append(Value::CreateStringValue(*it));
135 }
136 #endif
137 // In release mode, this will return an empty list to clear the section.
138 web_ui()->CallJavascriptFunction("returnVariationInfo",
139 *variations_list.release());
140 }
141
142 void VersionHandler::OnGotExecutablePath() {
143 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
144
145 web_ui()->CallJavascriptFunction("returnExecutablePath",
146 StringValue(executable_path_));
147 }
148
149 void VersionHandler::OnGotPlugins(
150 const std::vector<webkit::WebPluginInfo>& plugins) {
151 #if defined(OS_CHROMEOS)
152 new ChromeOSAboutVersionHandler(web_ui());
153 #endif
154
155 #if !defined(OS_ANDROID)
156 // Obtain the version of the first enabled Flash plugin.
157 std::vector<webkit::WebPluginInfo> info_array;
158 content::PluginService::GetInstance()->GetPluginInfoArray(
159 GURL(), "application/x-shockwave-flash", false, &info_array, NULL);
160 string16 flash_version =
161 l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN);
162 PluginPrefs* plugin_prefs =
163 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui()));
164 if (plugin_prefs) {
165 for (size_t i = 0; i < info_array.size(); ++i) {
166 if (plugin_prefs->IsPluginEnabled(info_array[i])) {
167 flash_version = info_array[i].version;
168 break;
169 }
170 }
171 }
172 web_ui()->CallJavascriptFunction("returnFlashVersion",
173 StringValue(flash_version));
174 #endif
175 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698