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

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: Win fixes 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,212 @@
+// 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"
+
+#if defined(OS_CHROMEOS)
+#include "chrome/browser/chromeos/version_loader.h"
+#endif
+
+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 {
Evan Stade 2012/09/14 09:18:02 I don't think this should be its own class; it sho
SteveT 2012/09/14 14:48:15 Done.
+ public:
+ explicit ChromeOSAboutVersionHandler(content::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.
+ content::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(content::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) {
+ scoped_ptr<StringValue> arg(Value::CreateStringValue(version));
+ LOG(INFO) << "Returning OS VERSION: " << version;
+ web_ui_->CallJavascriptFunction("returnOsVersion", *arg);
+
+ // 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 GetFilePaths(FilePath profile_path,
+ std::string* exec_path_out,
Evan Stade 2012/09/14 09:18:02 instead of passing in member variables as outparam
SteveT 2012/09/14 14:48:15 Kept this as a standalone helper method and ensure
+ std::string* profile_path_out) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
+
+ FilePath executable_path = CommandLine::ForCurrentProcess()->GetProgram();
+ if (file_util::AbsolutePath(&executable_path))
+#if defined(OS_WIN)
+ *exec_path_out = WideToUTF8(executable_path.value());
Evan Stade 2012/09/14 09:18:02 it would be easier to just do LossyDisplayName() (
SteveT 2012/09/14 14:48:15 Done.
+#else
+ *exec_path_out = executable_path.value();
+#endif
+ else
+ *exec_path_out = l10n_util::GetStringUTF8(IDS_ABOUT_VERSION_PATH_NOTFOUND);
+
+ if (!profile_path.empty() && file_util::AbsolutePath(&profile_path)) {
+#if defined(OS_WIN)
+ *profile_path_out = WideToUTF8(profile_path.value());
+#else
+ *profile_path_out = profile_path.value();
+#endif
+ } else {
+ *profile_path_out =
+ l10n_util::GetStringUTF8(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) {
+#if defined(OS_CHROMEOS)
+ new ChromeOSAboutVersionHandler(web_ui());
Evan Stade 2012/09/14 09:18:02 what is going on here?
Evan Stade 2012/09/14 09:19:27 ignore this
+#endif
+
+ // 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()));
+
+ FilePath profile_path;
+ Profile* profile = Profile::FromWebUI(web_ui());
+ if (profile) {
+ profile_path = profile->GetPath();
+ } else {
+ scoped_ptr<StringValue> profile_path(
+ Value::CreateStringValue(
+ l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_PATH_NOTFOUND)));
+ web_ui()->CallJavascriptFunction("returnProfilePath", *profile_path);
+ }
+
+ // Grab the executable path on the FILE thread. It is returned in
+ // OnFilePath.
+ content::BrowserThread::PostTaskAndReply(content::BrowserThread::FILE,
+ FROM_HERE, base::Bind(&GetFilePaths, profile_path, &executable_path_,
+ &profile_path_),
+ base::Bind(&VersionHandler::OnGotFilePaths,
+ weak_ptr_factory_.GetWeakPtr()));
+
+ // Respond with the variations info immediately.
+ scoped_ptr<ListValue> variations_list(new ListValue());
+#if !defined(NDEBUG)
Evan Stade 2012/09/14 09:18:02 it seems like this should be disabled for official
SteveT 2012/09/14 14:48:15 I was the author :) We decided that we want to sh
+ 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::OnGotFilePaths() {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ scoped_ptr<StringValue> exec_path(Value::CreateStringValue(executable_path_));
+ scoped_ptr<StringValue> profile_path(Value::CreateStringValue(profile_path_));
+ web_ui()->CallJavascriptFunction("returnExecutablePath", *exec_path);
+ web_ui()->CallJavascriptFunction("returnProfilePath", *profile_path);
Evan Stade 2012/09/14 09:18:02 any particular reason these are two separate js ca
SteveT 2012/09/14 14:48:15 In the case where the profile was not yet availabl
+}
+
+void VersionHandler::OnGotPlugins(
+ const std::vector<webkit::WebPluginInfo>& plugins) {
+#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;
+ }
+ }
+ }
+
+ scoped_ptr<StringValue> arg(Value::CreateStringValue(flash_version));
+ web_ui()->CallJavascriptFunction("returnFlashVersion", *arg);
+#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