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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/version_handler.cc
===================================================================
--- chrome/browser/ui/webui/version_handler.cc (revision 0)
+++ chrome/browser/ui/webui/version_handler.cc (revision 0)
@@ -0,0 +1,175 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/ui/webui/version_handler.h"
+
+#include "base/command_line.h"
+#include "base/file_util.h"
+#include "base/metrics/field_trial.h"
+#include "base/string_split.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/plugin_prefs.h"
+#include "chrome/browser/profiles/profile.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/plugin_service.h"
+#include "content/public/browser/web_ui.h"
+#include "googleurl/src/gurl.h"
+#include "grit/generated_resources.h"
+#include "ui/base/l10n/l10n_util.h"
+
+namespace {
+
+#if defined(OS_CHROMEOS)
+// ChromeOSAboutVersionHandler is responsible for loading the Chrome OS
+// version.
+// ChromeOSAboutVersionHandler handles deleting itself once the version has
+// been obtained and AboutUIHTMLSource notified.
+class ChromeOSAboutVersionHandler {
+ public:
+ explicit ChromeOSAboutVersionHandler(WebUI* web_ui);
+
+ // Callback from chromeos::VersionLoader giving the version.
+ void OnVersion(chromeos::VersionLoader::Handle handle,
+ const std::string& version);
+
+ private:
+ // The WebUI instance to respond to.
+ WebUI* web_ui_;
+
+ // Handles asynchronously loading the version.
+ chromeos::VersionLoader loader_;
+
+ // Used to request the version.
+ CancelableRequestConsumer consumer_;
+
+ DISALLOW_COPY_AND_ASSIGN(ChromeOSAboutVersionHandler);
+};
+
+ChromeOSAboutVersionHandler::ChromeOSAboutVersionHandler(WebUI* web_ui)
+ : web_ui_(web_ui) {
+ loader_.GetVersion(&consumer_,
+ base::Bind(&ChromeOSAboutVersionHandler::OnVersion,
+ base::Unretained(this)),
+ chromeos::VersionLoader::VERSION_FULL);
+}
+
+void ChromeOSAboutVersionHandler::OnVersion(
+ chromeos::VersionLoader::Handle handle,
+ const std::string& version) {
+ web_ui_->CallJavascriptFunction("returnOsVersion",
+ StringValue(os_version_data));
+
+ // CancelableRequestProvider isn't happy when it's deleted and servicing a
+ // task, so we delay the deletion.
+ MessageLoop::current()->DeleteSoon(FROM_HERE, this);
+}
+#endif
+
+// Retrieves the executable path on the FILE thread.
+void GetExecutablePath(string16* out_string) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
+
+ FilePath executable_path = CommandLine::ForCurrentProcess()->GetProgram();
+ if (file_util::AbsolutePath(&executable_path))
+ *out_string = ASCIIToUTF16(executable_path.value());
+ else
+ *out_string = l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND);
+}
+
+} // namespace
+
+VersionHandler::VersionHandler()
+ : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+}
+
+VersionHandler::~VersionHandler() {
+}
+
+void VersionHandler::RegisterMessages() {
+ web_ui()->RegisterMessageCallback(
+ "requestVersionInfo",
+ base::Bind(&VersionHandler::HandleRequestVersionInfo,
+ base::Unretained(this)));
+}
+
+void VersionHandler::HandleRequestVersionInfo(const ListValue* args) {
+ // The Flash version information is needed in the response, so make sure
+ // the plugins are loaded.
+ content::PluginService::GetInstance()->GetPlugins(
+ base::Bind(&VersionHandler::OnGotPlugins,
+ weak_ptr_factory_.GetWeakPtr()));
+
+ // Grab the executable path on the FILE thread. It is returned in
+ // OnFilePath.
+ content::BrowserThread::PostTaskAndReply(content::BrowserThread::FILE,
+ FROM_HERE, base::Bind(&GetExecutablePath, &executable_path_),
+ base::Bind(&VersionHandler::OnGotExecutablePath,
+ weak_ptr_factory_.GetWeakPtr()));
+
+ // Respond with the variations info immediately.
+ scoped_ptr<ListValue> variations_list(new ListValue());
+#if !defined(NDEBUG)
+ std::string variation_state;
+ base::FieldTrialList::StatesToString(&variation_state);
+
+ std::vector<std::string> tokens;
+ base::SplitString(variation_state,
+ base::FieldTrialList::kPersistentStringSeparator,
+ &tokens);
+ // Since StatesToString appends a separator at the end, SplitString will
+ // append an extra empty string in the vector. Drop it. There should
+ // always be an even number of tokens left.
+ tokens.pop_back();
+ DCHECK_EQ(0U, tokens.size() % 2);
+
+ // |variations| is used to store the formatted strings that will be passed to
+ // the Javascript through |variations_list|.
+ std::vector<std::string> variations;
+ for (size_t i = 0; i < tokens.size(); i += 2)
+ variations.push_back(tokens[i] + ":" + tokens[i + 1]);
+
+ for (std::vector<std::string>::const_iterator it = variations.begin();
+ it != variations.end(); ++it) {
+ variations_list->Append(Value::CreateStringValue(*it));
+ }
+#endif
+ // In release mode, this will return an empty list to clear the section.
+ web_ui()->CallJavascriptFunction("returnVariationInfo",
+ *variations_list.release());
+}
+
+void VersionHandler::OnGotExecutablePath() {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ web_ui()->CallJavascriptFunction("returnExecutablePath",
+ StringValue(executable_path_));
+}
+
+void VersionHandler::OnGotPlugins(
+ const std::vector<webkit::WebPluginInfo>& plugins) {
+#if defined(OS_CHROMEOS)
+ new ChromeOSAboutVersionHandler(web_ui());
+#endif
+
+#if !defined(OS_ANDROID)
+ // Obtain the version of the first enabled Flash plugin.
+ std::vector<webkit::WebPluginInfo> info_array;
+ content::PluginService::GetInstance()->GetPluginInfoArray(
+ GURL(), "application/x-shockwave-flash", false, &info_array, NULL);
+ string16 flash_version =
+ l10n_util::GetStringUTF16(IDS_PLUGINS_DISABLED_PLUGIN);
+ PluginPrefs* plugin_prefs =
+ PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui()));
+ if (plugin_prefs) {
+ for (size_t i = 0; i < info_array.size(); ++i) {
+ if (plugin_prefs->IsPluginEnabled(info_array[i])) {
+ flash_version = info_array[i].version;
+ break;
+ }
+ }
+ }
+ web_ui()->CallJavascriptFunction("returnFlashVersion",
+ StringValue(flash_version));
+#endif
+}
Property changes on: chrome/browser/ui/webui/version_handler.cc
___________________________________________________________________
Added: svn:eol-style
+ LF

Powered by Google App Engine
This is Rietveld 408576698