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

Unified Diff: chrome/browser/ui/webui/version_ui.cc

Issue 10916182: Refactor the about:version code out of about_ui. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Refactoring complete 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_ui.cc
===================================================================
--- chrome/browser/ui/webui/version_ui.cc (revision 0)
+++ chrome/browser/ui/webui/version_ui.cc (revision 0)
@@ -0,0 +1,310 @@
+// 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_ui.h"
+
+#include "base/command_line.h"
+#include "base/file_util.h"
+#include "base/memory/weak_ptr.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 "chrome/browser/ui/webui/chrome_url_data_manager.h"
+#include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
+#include "chrome/common/chrome_version_info.h"
+#include "chrome/common/jstemplate_builder.h"
+#include "chrome/common/url_constants.h"
+#include "content/public/browser/plugin_service.h"
+#include "content/public/browser/web_ui.h"
+#include "content/public/browser/web_ui_message_handler.h"
+#include "content/public/common/content_client.h"
+#include "grit/browser_resources.h"
+#include "grit/chromium_strings.h"
+#include "grit/generated_resources.h"
+#include "grit/google_chrome_strings.h"
+#include "ui/base/l10n/l10n_util.h"
+#include "v8/include/v8.h"
+#include "webkit/plugins/webplugininfo.h"
+#include "webkit/user_agent/user_agent_util.h"
+
+#if defined(ENABLE_THEMES)
+#include "chrome/browser/ui/webui/theme_source.h"
+#endif
+
+#if defined(OS_CHROMEOS)
+#include "chrome/browser/chromeos/version_loader.h"
+#endif
+
+namespace {
+
+ChromeWebUIDataSource* CreateVersionUIDataSource(Profile* profile) {
+ ChromeWebUIDataSource* html_source =
+ new ChromeWebUIDataSource(chrome::kChromeUIVersionHost);
+
+ // Localized and data strings.
+ html_source->AddLocalizedString("title", IDS_ABOUT_VERSION_TITLE);
+ chrome::VersionInfo version_info;
Evan Stade 2012/09/13 08:05:50 switch lines 49 and 50
SteveT 2012/09/13 14:58:01 Done.
+ html_source->AddLocalizedString("name", IDS_PRODUCT_NAME);
+ html_source->AddString("version", ASCIIToUTF16(version_info.Version()));
+ // Bug 79458: Need to evaluate the use of getting the version string on
Evan Stade 2012/09/13 08:05:50 nit: Prefer URL format: http://crbug.com/79458
SteveT 2012/09/13 14:58:01 Done.
+ // this thread.
+ base::ThreadRestrictions::ScopedAllowIO allow_io;
+ html_source->AddString("version_modifier",
+ ASCIIToUTF16(chrome::VersionInfo::GetVersionStringModifier()));
+ html_source->AddLocalizedString("os_name", IDS_ABOUT_VERSION_OS);
+ html_source->AddLocalizedString("platform", IDS_PLATFORM_LABEL);
+ html_source->AddString("os_type", ASCIIToUTF16(version_info.OSType()));
+ html_source->AddString("os_version", string16());
+ html_source->AddString("webkit_version",
+ ASCIIToUTF16(webkit_glue::GetWebKitVersion()));
+ html_source->AddString("js_engine", ASCIIToUTF16("V8"));
+ html_source->AddString("js_version", ASCIIToUTF16(v8::V8::GetVersion()));
+
+#if !defined(OS_ANDROID)
+ html_source->AddString("flash_plugin", ASCIIToUTF16("Flash"));
+ // Note that the Flash version is retrieve asynchronously and returned in
+ // VersionDOMHandler::OnGotPlugins. The initial text is a loading message.
+ html_source->AddLocalizedString("flash_version",
+ IDS_ABOUT_VERSION_LOADING_MESSAGE);
+#endif
+ html_source->AddLocalizedString("company", IDS_ABOUT_VERSION_COMPANY_NAME);
+ html_source->AddLocalizedString("copyright", IDS_ABOUT_VERSION_COPYRIGHT);
+ html_source->AddString("cl", ASCIIToUTF16(version_info.LastChange()));
+ html_source->AddLocalizedString("official",
+ version_info.IsOfficialBuild() ?
+ IDS_ABOUT_VERSION_OFFICIAL
+ : IDS_ABOUT_VERSION_UNOFFICIAL);
+ html_source->AddLocalizedString("user_agent_name",
+ IDS_ABOUT_VERSION_USER_AGENT);
+ html_source->AddString("useragent",
+ ASCIIToUTF16(content::GetUserAgent(GURL())));
+ html_source->AddLocalizedString("command_line_name",
+ IDS_ABOUT_VERSION_COMMAND_LINE);
+
+#if defined(OS_WIN)
+ html_source->AddString("command_line",
+ WideToUTF16(CommandLine::ForCurrentProcess()->GetCommandLineString()));
+#elif defined(OS_POSIX)
+ std::string command_line = "";
+ typedef std::vector<std::string> ArgvList;
+ const ArgvList& argv = CommandLine::ForCurrentProcess()->argv();
+ for (ArgvList::const_iterator iter = argv.begin(); iter != argv.end(); iter++)
+ command_line += " " + *iter;
+ // TODO(viettrungluu): |command_line| could really have any encoding, whereas
+ // below we assumes it's UTF-8.
Evan Stade 2012/09/13 08:05:50 you've changed this code and now this comment is n
SteveT 2012/09/13 14:58:01 Added AddString as you suggested and used that ins
+ html_source->AddString("command_line", ASCIIToUTF16(command_line));
+#endif
+
+ // Allow IO temporarily based on allow_io (defined above)
+ // since the following operation will complete quickly
Evan Stade 2012/09/13 08:05:50 1) do not execute file operations on the UI thread
SteveT 2012/09/13 14:58:01 Resolved this whole thing by having DomHandler dis
+ html_source->AddLocalizedString("executable_path_name",
+ IDS_ABOUT_VERSION_EXECUTABLE_PATH);
+ FilePath executable_path = CommandLine::ForCurrentProcess()->GetProgram();
+ if (file_util::AbsolutePath(&executable_path)) {
+ html_source->AddString("executable_path",
+ ASCIIToUTF16(executable_path.value()));
+ } else {
+ html_source->AddLocalizedString("executable_path",
+ IDS_ABOUT_VERSION_PATH_NOTFOUND);
+ }
+ html_source->AddLocalizedString("profile_path_name",
+ IDS_ABOUT_VERSION_PROFILE_PATH);
+ if (profile) {
+ FilePath profile_path = profile->GetPath();
+ if (file_util::AbsolutePath(&profile_path))
+ html_source->AddString("profile_path",
+ ASCIIToUTF16(profile_path.value()));
+ else
+ html_source->AddLocalizedString("profile_path",
+ IDS_ABOUT_VERSION_PATH_NOTFOUND);
+ } else {
+ html_source->AddLocalizedString("profile_path",
+ IDS_ABOUT_VERSION_PATH_NOTFOUND);
+ }
+
+#if !defined(NDEBUG)
+ html_source->AddLocalizedString("variations_name",
+ IDS_ABOUT_VERSION_VARIATIONS);
+#endif
+ html_source->set_use_json_js_format_v2();
+ html_source->set_json_path("strings.js");
+ html_source->add_resource_path("version.js", IDR_ABOUT_VERSION_JS);
+ html_source->set_default_resource(IDR_ABOUT_VERSION_HTML);
+ return html_source;
+}
+
+#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
+
+// Handler class for Version page operations.
+class VersionDOMHandler : public content::WebUIMessageHandler {
Evan Stade 2012/09/13 08:05:50 Should just be VersionHandler, and should be in it
SteveT 2012/09/13 14:58:01 Done.
+ public:
+ VersionDOMHandler();
+ virtual ~VersionDOMHandler();
+
+ // content::WebUIMessageHandler implementation.
+ virtual void RegisterMessages() OVERRIDE;
+
+ // Callback for the "requestVersionInfo" message. This asynchronously requests
+ // the flash version and eventually returns it to the front end along with the
+ // list of variations using OnGotPlugins.
+ void HandleRequestVersionInfo(const ListValue* args);
+
+ private:
+ // Callback for GetPlugins which responds to the page with the Flash version.
+ // This also initiates the OS Version load on ChromeOS.
+ void OnGotPlugins(const std::vector<webkit::WebPluginInfo>& plugins);
+
+ // Factory for the creating refs in callbacks.
+ base::WeakPtrFactory<VersionDOMHandler> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(VersionDOMHandler);
+};
+
+VersionDOMHandler::VersionDOMHandler()
+ : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+}
+
+VersionDOMHandler::~VersionDOMHandler() {
+}
+
+void VersionDOMHandler::RegisterMessages() {
+ web_ui()->RegisterMessageCallback(
+ "requestVersionInfo",
+ base::Bind(&VersionDOMHandler::HandleRequestVersionInfo,
+ base::Unretained(this)));
+}
+
+void VersionDOMHandler::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(&VersionDOMHandler::OnGotPlugins,
+ 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 VersionDOMHandler::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
+}
+
+} // namespace
+
+VersionUI::VersionUI(content::WebUI* web_ui)
+ : content::WebUIController(web_ui) {
+ Profile* profile = Profile::FromWebUI(web_ui);
+
+ web_ui->AddMessageHandler(new VersionDOMHandler());
+
+#if defined(ENABLE_THEMES)
+ // Set up the chrome://theme/ source.
+ ThemeSource* theme = new ThemeSource(profile);
+ ChromeURLDataManager::AddDataSource(profile, theme);
+#endif
+
+ ChromeURLDataManager::AddDataSource(profile,
+ CreateVersionUIDataSource(profile));
+}
+
+VersionUI::~VersionUI() {
+}
Property changes on: chrome/browser/ui/webui/version_ui.cc
___________________________________________________________________
Added: svn:eol-style
+ LF

Powered by Google App Engine
This is Rietveld 408576698