OLD | NEW |
(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/nacl_ui.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/command_line.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/path_service.h" |
| 15 #include "base/string16.h" |
| 16 #include "base/string_number_conversions.h" |
| 17 #include "base/utf_string_conversions.h" |
| 18 #include "base/values.h" |
| 19 #include "chrome/browser/plugin_prefs.h" |
| 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 22 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 23 #include "chrome/common/chrome_paths.h" |
| 24 #include "chrome/common/chrome_switches.h" |
| 25 #include "chrome/common/chrome_version_info.h" |
| 26 #include "chrome/common/url_constants.h" |
| 27 #include "content/public/browser/plugin_service.h" |
| 28 #include "content/public/browser/user_metrics.h" |
| 29 #include "content/public/browser/web_ui.h" |
| 30 #include "content/public/browser/web_ui_message_handler.h" |
| 31 #include "grit/browser_resources.h" |
| 32 #include "grit/chromium_strings.h" |
| 33 #include "grit/generated_resources.h" |
| 34 #include "grit/theme_resources.h" |
| 35 #include "ui/base/l10n/l10n_util.h" |
| 36 #include "ui/base/resource/resource_bundle.h" |
| 37 #include "webkit/plugins/webplugininfo.h" |
| 38 |
| 39 #if defined(OS_WIN) |
| 40 #include "base/win/windows_version.h" |
| 41 #endif |
| 42 |
| 43 using content::PluginService; |
| 44 using content::UserMetricsAction; |
| 45 using content::WebUIMessageHandler; |
| 46 |
| 47 namespace { |
| 48 |
| 49 ChromeWebUIDataSource* CreateNaClUIHTMLSource() { |
| 50 ChromeWebUIDataSource* source = |
| 51 new ChromeWebUIDataSource(chrome::kChromeUINaClHost); |
| 52 |
| 53 source->AddLocalizedString("loadingMessage", IDS_NACL_LOADING_MESSAGE); |
| 54 source->AddLocalizedString("naclLongTitle", IDS_NACL_TITLE_MESSAGE); |
| 55 source->set_json_path("strings.js"); |
| 56 source->add_resource_path("about_nacl.css", IDR_ABOUT_NACL_CSS); |
| 57 source->add_resource_path("about_nacl.js", IDR_ABOUT_NACL_JS); |
| 58 source->set_default_resource(IDR_ABOUT_NACL_HTML); |
| 59 return source; |
| 60 } |
| 61 |
| 62 //////////////////////////////////////////////////////////////////////////////// |
| 63 // |
| 64 // NaClDOMHandler |
| 65 // |
| 66 //////////////////////////////////////////////////////////////////////////////// |
| 67 |
| 68 // The handler for JavaScript messages for the about:flags page. |
| 69 class NaClDOMHandler : public WebUIMessageHandler { |
| 70 public: |
| 71 NaClDOMHandler(); |
| 72 virtual ~NaClDOMHandler(); |
| 73 |
| 74 // WebUIMessageHandler implementation. |
| 75 virtual void RegisterMessages() OVERRIDE; |
| 76 |
| 77 // Callback for the "requestNaClInfo" message. |
| 78 void HandleRequestNaClInfo(const ListValue* args); |
| 79 |
| 80 // Callback for the NaCl plugin information. |
| 81 void OnGotPlugins(const std::vector<webkit::WebPluginInfo>& plugins); |
| 82 |
| 83 private: |
| 84 // Called when enough information is gathered to return data back to the page. |
| 85 void MaybeRespondToPage(); |
| 86 |
| 87 // Helper for MaybeRespondToPage -- called after enough information |
| 88 // is gathered. |
| 89 void PopulatePageInformation(DictionaryValue* naclInfo); |
| 90 |
| 91 // Factory for the creating refs in callbacks. |
| 92 base::WeakPtrFactory<NaClDOMHandler> weak_ptr_factory_; |
| 93 |
| 94 // Whether the page has requested data. |
| 95 bool page_has_requested_data_; |
| 96 |
| 97 // Whether the plugin information is ready. |
| 98 bool has_plugin_info_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(NaClDOMHandler); |
| 101 }; |
| 102 |
| 103 NaClDOMHandler::NaClDOMHandler() |
| 104 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 105 page_has_requested_data_(false), |
| 106 has_plugin_info_(false) { |
| 107 PluginService::GetInstance()->GetPlugins(base::Bind( |
| 108 &NaClDOMHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr())); |
| 109 } |
| 110 |
| 111 NaClDOMHandler::~NaClDOMHandler() { |
| 112 } |
| 113 |
| 114 void NaClDOMHandler::RegisterMessages() { |
| 115 web_ui()->RegisterMessageCallback( |
| 116 "requestNaClInfo", |
| 117 base::Bind(&NaClDOMHandler::HandleRequestNaClInfo, |
| 118 base::Unretained(this))); |
| 119 } |
| 120 |
| 121 // Helper functions for collecting a list of key-value pairs that will |
| 122 // be displayed. |
| 123 void AddPair(ListValue* list, const string16& key, const string16& value) { |
| 124 DictionaryValue* results = new DictionaryValue(); |
| 125 results->SetString("key", key); |
| 126 results->SetString("value", value); |
| 127 list->Append(results); |
| 128 } |
| 129 |
| 130 // Generate an empty data-pair which acts as a line break. |
| 131 void AddLineBreak(ListValue* list) { |
| 132 AddPair(list, ASCIIToUTF16(""), ASCIIToUTF16("")); |
| 133 } |
| 134 |
| 135 // Check whether a commandline switch is turned on or off. |
| 136 void ListFlagStatus(ListValue* list, const std::string& flag_label, |
| 137 const std::string& flag_name) { |
| 138 if (CommandLine::ForCurrentProcess()->HasSwitch(flag_name)) |
| 139 AddPair(list, ASCIIToUTF16(flag_label), ASCIIToUTF16("On")); |
| 140 else |
| 141 AddPair(list, ASCIIToUTF16(flag_label), ASCIIToUTF16("Off")); |
| 142 } |
| 143 |
| 144 void NaClDOMHandler::HandleRequestNaClInfo(const ListValue* args) { |
| 145 page_has_requested_data_ = true; |
| 146 MaybeRespondToPage(); |
| 147 } |
| 148 |
| 149 void NaClDOMHandler::OnGotPlugins( |
| 150 const std::vector<webkit::WebPluginInfo>& plugins) { |
| 151 has_plugin_info_ = true; |
| 152 MaybeRespondToPage(); |
| 153 } |
| 154 |
| 155 void NaClDOMHandler::PopulatePageInformation(DictionaryValue* naclInfo) { |
| 156 // Store Key-Value pairs of about-information. |
| 157 scoped_ptr<ListValue> list(new ListValue()); |
| 158 |
| 159 // Obtain the Chrome version info. |
| 160 chrome::VersionInfo version_info; |
| 161 AddPair(list.get(), |
| 162 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), |
| 163 ASCIIToUTF16(version_info.Version() + " (" + |
| 164 chrome::VersionInfo::GetVersionStringModifier() + ")")); |
| 165 |
| 166 // OS version information. |
| 167 // TODO(jvoung): refactor this to share the extra windows labeling |
| 168 // with about:flash, or something. |
| 169 std::string os_label = version_info.OSType(); |
| 170 #if defined(OS_WIN) |
| 171 base::win::OSInfo* os = base::win::OSInfo::GetInstance(); |
| 172 switch (os->version()) { |
| 173 case base::win::VERSION_XP: os_label += " XP"; break; |
| 174 case base::win::VERSION_SERVER_2003: |
| 175 os_label += " Server 2003 or XP Pro 64 bit"; |
| 176 break; |
| 177 case base::win::VERSION_VISTA: os_label += " Vista or Server 2008"; break; |
| 178 case base::win::VERSION_WIN7: os_label += " 7 or Server 2008 R2"; break; |
| 179 case base::win::VERSION_WIN8: os_label += " 8 or Server 2012"; break; |
| 180 default: os_label += " UNKNOWN"; break; |
| 181 } |
| 182 os_label += " SP" + base::IntToString(os->service_pack().major); |
| 183 if (os->service_pack().minor > 0) |
| 184 os_label += "." + base::IntToString(os->service_pack().minor); |
| 185 if (os->architecture() == base::win::OSInfo::X64_ARCHITECTURE) |
| 186 os_label += " 64 bit"; |
| 187 #endif |
| 188 AddPair(list.get(), |
| 189 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_OS), |
| 190 ASCIIToUTF16(os_label)); |
| 191 |
| 192 AddLineBreak(list.get()); |
| 193 |
| 194 // Obtain the version of the NaCl plugin. |
| 195 std::vector<webkit::WebPluginInfo> info_array; |
| 196 PluginService::GetInstance()->GetPluginInfoArray( |
| 197 GURL(), "application/x-nacl", false, &info_array, NULL); |
| 198 string16 nacl_version; |
| 199 string16 nacl_key = ASCIIToUTF16("NaCl plugin"); |
| 200 if (info_array.empty()) { |
| 201 AddPair(list.get(), nacl_key, ASCIIToUTF16("Disabled")); |
| 202 } else { |
| 203 PluginPrefs* plugin_prefs = |
| 204 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())); |
| 205 |
| 206 // Only the 0th plugin is used. |
| 207 nacl_version = info_array[0].version + ASCIIToUTF16(" ") + |
| 208 info_array[0].path.LossyDisplayName(); |
| 209 if (!plugin_prefs->IsPluginEnabled(info_array[0])) { |
| 210 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)"); |
| 211 AddPair(list.get(), nacl_key, nacl_version); |
| 212 } |
| 213 |
| 214 AddPair(list.get(), nacl_key, nacl_version); |
| 215 |
| 216 // Mark the rest as not used. |
| 217 for (size_t i = 1; i < info_array.size(); ++i) { |
| 218 nacl_version = info_array[i].version + ASCIIToUTF16(" ") + |
| 219 info_array[i].path.LossyDisplayName(); |
| 220 nacl_version += ASCIIToUTF16(" (not used)"); |
| 221 if (!plugin_prefs->IsPluginEnabled(info_array[i])) |
| 222 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)"); |
| 223 AddPair(list.get(), nacl_key, nacl_version); |
| 224 } |
| 225 } |
| 226 |
| 227 // Check that commandline flags are enabled. |
| 228 ListFlagStatus(list.get(), "Flag '--enable-nacl'", switches::kEnableNaCl); |
| 229 |
| 230 AddLineBreak(list.get()); |
| 231 |
| 232 // Obtain the version of the PNaCl translator. |
| 233 FilePath pnacl_path; |
| 234 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); |
| 235 if (!got_path || pnacl_path.empty()) { |
| 236 AddPair(list.get(), |
| 237 ASCIIToUTF16("PNaCl translator"), |
| 238 ASCIIToUTF16("Not installed")); |
| 239 } else { |
| 240 AddPair(list.get(), |
| 241 ASCIIToUTF16("PNaCl translator path"), |
| 242 pnacl_path.LossyDisplayName()); |
| 243 AddPair(list.get(), |
| 244 ASCIIToUTF16("PNaCl translator version"), |
| 245 pnacl_path.BaseName().LossyDisplayName()); |
| 246 } |
| 247 |
| 248 ListFlagStatus(list.get(), "Flag '--enable-pnacl'", switches::kEnablePnacl); |
| 249 // naclInfo will take ownership of list, and clean it up on destruction. |
| 250 naclInfo->Set("naclInfo", list.release()); |
| 251 } |
| 252 |
| 253 void NaClDOMHandler::MaybeRespondToPage() { |
| 254 // Don't reply until everything is ready. The page will show a 'loading' |
| 255 // message until then. |
| 256 if (!page_has_requested_data_ || !has_plugin_info_) |
| 257 return; |
| 258 |
| 259 DictionaryValue naclInfo; |
| 260 PopulatePageInformation(&naclInfo); |
| 261 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo); |
| 262 } |
| 263 |
| 264 } // namespace |
| 265 |
| 266 /////////////////////////////////////////////////////////////////////////////// |
| 267 // |
| 268 // NaClUI |
| 269 // |
| 270 /////////////////////////////////////////////////////////////////////////////// |
| 271 |
| 272 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 273 content::RecordAction(UserMetricsAction("ViewAboutNaCl")); |
| 274 |
| 275 web_ui->AddMessageHandler(new NaClDOMHandler()); |
| 276 |
| 277 // Set up the about:nacl source. |
| 278 Profile* profile = Profile::FromWebUI(web_ui); |
| 279 ChromeURLDataManager::AddDataSource(profile, CreateNaClUIHTMLSource()); |
| 280 } |
OLD | NEW |