Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / webui / nacl_ui.cc
blobe129cfd561584f182f98b996af3d2617ba7bc529
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.
5 #include "chrome/browser/ui/webui/nacl_ui.h"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/files/file_util.h"
14 #include "base/json/json_file_value_serializer.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/path_service.h"
17 #include "base/strings/string16.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/threading/sequenced_worker_pool.h"
21 #include "base/values.h"
22 #include "chrome/browser/plugins/plugin_prefs.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/common/channel_info.h"
25 #include "chrome/common/chrome_paths.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/url_constants.h"
28 #include "chrome/grit/chromium_strings.h"
29 #include "chrome/grit/generated_resources.h"
30 #include "components/version_info/version_info.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/plugin_service.h"
33 #include "content/public/browser/user_metrics.h"
34 #include "content/public/browser/web_ui.h"
35 #include "content/public/browser/web_ui_data_source.h"
36 #include "content/public/browser/web_ui_message_handler.h"
37 #include "content/public/common/webplugininfo.h"
38 #include "grit/browser_resources.h"
39 #include "ui/base/l10n/l10n_util.h"
41 #if defined(OS_WIN)
42 #include "base/win/windows_version.h"
43 #endif
45 using base::ASCIIToUTF16;
46 using base::UserMetricsAction;
47 using content::BrowserThread;
48 using content::PluginService;
49 using content::WebUIMessageHandler;
51 namespace {
53 content::WebUIDataSource* CreateNaClUIHTMLSource() {
54 content::WebUIDataSource* source =
55 content::WebUIDataSource::Create(chrome::kChromeUINaClHost);
57 source->SetJsonPath("strings.js");
58 source->AddResourcePath("about_nacl.css", IDR_ABOUT_NACL_CSS);
59 source->AddResourcePath("about_nacl.js", IDR_ABOUT_NACL_JS);
60 source->SetDefaultResource(IDR_ABOUT_NACL_HTML);
61 return source;
64 ////////////////////////////////////////////////////////////////////////////////
66 // NaClDomHandler
68 ////////////////////////////////////////////////////////////////////////////////
70 // The handler for JavaScript messages for the about:flags page.
71 class NaClDomHandler : public WebUIMessageHandler {
72 public:
73 NaClDomHandler();
74 ~NaClDomHandler() override;
76 // WebUIMessageHandler implementation.
77 void RegisterMessages() override;
79 private:
80 // Callback for the "requestNaClInfo" message.
81 void HandleRequestNaClInfo(const base::ListValue* args);
83 // Callback for the NaCl plugin information.
84 void OnGotPlugins(const std::vector<content::WebPluginInfo>& plugins);
86 // A helper callback that receives the result of checking if PNaCl path
87 // exists and checking the PNaCl |version|. |is_valid| is true if the PNaCl
88 // path that was returned by PathService is valid, and false otherwise.
89 void DidCheckPathAndVersion(const std::string* version, bool is_valid);
91 // Called when enough information is gathered to return data back to the page.
92 void MaybeRespondToPage();
94 // Helper for MaybeRespondToPage -- called after enough information
95 // is gathered.
96 void PopulatePageInformation(base::DictionaryValue* naclInfo);
98 // Returns whether the specified plugin is enabled.
99 bool isPluginEnabled(size_t plugin_index);
101 // Adds information regarding the operating system and chrome version to list.
102 void AddOperatingSystemInfo(base::ListValue* list);
104 // Adds the list of plugins for NaCl to list.
105 void AddPluginList(base::ListValue* list);
107 // Adds the information relevant to PNaCl (e.g., enablement, paths, version)
108 // to the list.
109 void AddPnaclInfo(base::ListValue* list);
111 // Adds the information relevant to NaCl to list.
112 void AddNaClInfo(base::ListValue* list);
114 // Whether the page has requested data.
115 bool page_has_requested_data_;
117 // Whether the plugin information is ready.
118 bool has_plugin_info_;
120 // Whether PNaCl path was validated. PathService can return a path
121 // that does not exists, so it needs to be validated.
122 bool pnacl_path_validated_;
123 bool pnacl_path_exists_;
124 std::string pnacl_version_string_;
126 // Factory for the creating refs in callbacks.
127 base::WeakPtrFactory<NaClDomHandler> weak_ptr_factory_;
129 DISALLOW_COPY_AND_ASSIGN(NaClDomHandler);
132 NaClDomHandler::NaClDomHandler()
133 : page_has_requested_data_(false),
134 has_plugin_info_(false),
135 pnacl_path_validated_(false),
136 pnacl_path_exists_(false),
137 weak_ptr_factory_(this) {
138 PluginService::GetInstance()->GetPlugins(base::Bind(
139 &NaClDomHandler::OnGotPlugins, weak_ptr_factory_.GetWeakPtr()));
142 NaClDomHandler::~NaClDomHandler() {
145 void NaClDomHandler::RegisterMessages() {
146 web_ui()->RegisterMessageCallback(
147 "requestNaClInfo",
148 base::Bind(&NaClDomHandler::HandleRequestNaClInfo,
149 base::Unretained(this)));
152 // Helper functions for collecting a list of key-value pairs that will
153 // be displayed.
154 void AddPair(base::ListValue* list,
155 const base::string16& key,
156 const base::string16& value) {
157 base::DictionaryValue* results = new base::DictionaryValue();
158 results->SetString("key", key);
159 results->SetString("value", value);
160 list->Append(results);
163 // Generate an empty data-pair which acts as a line break.
164 void AddLineBreak(base::ListValue* list) {
165 AddPair(list, ASCIIToUTF16(""), ASCIIToUTF16(""));
168 bool NaClDomHandler::isPluginEnabled(size_t plugin_index) {
169 std::vector<content::WebPluginInfo> info_array;
170 PluginService::GetInstance()->GetPluginInfoArray(
171 GURL(), "application/x-nacl", false, &info_array, NULL);
172 PluginPrefs* plugin_prefs =
173 PluginPrefs::GetForProfile(Profile::FromWebUI(web_ui())).get();
174 return (!info_array.empty() &&
175 plugin_prefs->IsPluginEnabled(info_array[plugin_index]));
178 void NaClDomHandler::AddOperatingSystemInfo(base::ListValue* list) {
179 // Obtain the Chrome version info.
180 AddPair(list,
181 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
182 ASCIIToUTF16(version_info::GetVersionNumber() + " (" +
183 chrome::GetChannelString() + ")"));
185 // OS version information.
186 // TODO(jvoung): refactor this to share the extra windows labeling
187 // with about:flash, or something.
188 std::string os_label = version_info::GetOSType();
189 #if defined(OS_WIN)
190 base::win::OSInfo* os = base::win::OSInfo::GetInstance();
191 switch (os->version()) {
192 case base::win::VERSION_XP: os_label += " XP"; break;
193 case base::win::VERSION_SERVER_2003:
194 os_label += " Server 2003 or XP Pro 64 bit";
195 break;
196 case base::win::VERSION_VISTA: os_label += " Vista or Server 2008"; break;
197 case base::win::VERSION_WIN7: os_label += " 7 or Server 2008 R2"; break;
198 case base::win::VERSION_WIN8: os_label += " 8 or Server 2012"; break;
199 default: os_label += " UNKNOWN"; break;
201 os_label += " SP" + base::IntToString(os->service_pack().major);
202 if (os->service_pack().minor > 0)
203 os_label += "." + base::IntToString(os->service_pack().minor);
204 if (os->architecture() == base::win::OSInfo::X64_ARCHITECTURE)
205 os_label += " 64 bit";
206 #endif
207 AddPair(list,
208 l10n_util::GetStringUTF16(IDS_ABOUT_VERSION_OS),
209 ASCIIToUTF16(os_label));
210 AddLineBreak(list);
213 void NaClDomHandler::AddPluginList(base::ListValue* list) {
214 // Obtain the version of the NaCl plugin.
215 std::vector<content::WebPluginInfo> info_array;
216 PluginService::GetInstance()->GetPluginInfoArray(
217 GURL(), "application/x-nacl", false, &info_array, NULL);
218 base::string16 nacl_version;
219 base::string16 nacl_key = ASCIIToUTF16("NaCl plugin");
220 if (info_array.empty()) {
221 AddPair(list, nacl_key, ASCIIToUTF16("Disabled"));
222 } else {
223 // Only the 0th plugin is used.
224 nacl_version = info_array[0].version + ASCIIToUTF16(" ") +
225 info_array[0].path.LossyDisplayName();
226 if (!isPluginEnabled(0)) {
227 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)");
230 AddPair(list, nacl_key, nacl_version);
232 // Mark the rest as not used.
233 for (size_t i = 1; i < info_array.size(); ++i) {
234 nacl_version = info_array[i].version + ASCIIToUTF16(" ") +
235 info_array[i].path.LossyDisplayName();
236 nacl_version += ASCIIToUTF16(" (not used)");
237 if (!isPluginEnabled(i)) {
238 nacl_version += ASCIIToUTF16(" (Disabled in profile prefs)");
240 AddPair(list, nacl_key, nacl_version);
243 AddLineBreak(list);
246 void NaClDomHandler::AddPnaclInfo(base::ListValue* list) {
247 // Display whether PNaCl is enabled.
248 base::string16 pnacl_enabled_string = ASCIIToUTF16("Enabled");
249 if (!isPluginEnabled(0)) {
250 pnacl_enabled_string = ASCIIToUTF16("Disabled in profile prefs");
252 AddPair(list,
253 ASCIIToUTF16("Portable Native Client (PNaCl)"),
254 pnacl_enabled_string);
256 // Obtain the version of the PNaCl translator.
257 base::FilePath pnacl_path;
258 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path);
259 if (!got_path || pnacl_path.empty() || !pnacl_path_exists_) {
260 AddPair(list,
261 ASCIIToUTF16("PNaCl translator"),
262 ASCIIToUTF16("Not installed"));
263 } else {
264 AddPair(list,
265 ASCIIToUTF16("PNaCl translator path"),
266 pnacl_path.LossyDisplayName());
267 AddPair(list,
268 ASCIIToUTF16("PNaCl translator version"),
269 ASCIIToUTF16(pnacl_version_string_));
271 AddLineBreak(list);
274 void NaClDomHandler::AddNaClInfo(base::ListValue* list) {
275 base::string16 nacl_enabled_string = ASCIIToUTF16("Disabled");
276 if (isPluginEnabled(0) &&
277 base::CommandLine::ForCurrentProcess()->HasSwitch(
278 switches::kEnableNaCl)) {
279 nacl_enabled_string = ASCIIToUTF16("Enabled by flag '--enable-nacl'");
281 AddPair(list,
282 ASCIIToUTF16("Native Client (non-portable, outside web store)"),
283 nacl_enabled_string);
284 AddLineBreak(list);
287 void NaClDomHandler::HandleRequestNaClInfo(const base::ListValue* args) {
288 page_has_requested_data_ = true;
289 // Force re-validation of PNaCl's path in the next call to
290 // MaybeRespondToPage(), in case PNaCl went from not-installed
291 // to installed since the request.
292 pnacl_path_validated_ = false;
293 MaybeRespondToPage();
296 void NaClDomHandler::OnGotPlugins(
297 const std::vector<content::WebPluginInfo>& plugins) {
298 has_plugin_info_ = true;
299 MaybeRespondToPage();
302 void NaClDomHandler::PopulatePageInformation(base::DictionaryValue* naclInfo) {
303 DCHECK(pnacl_path_validated_);
304 // Store Key-Value pairs of about-information.
305 scoped_ptr<base::ListValue> list(new base::ListValue());
306 // Display the operating system and chrome version information.
307 AddOperatingSystemInfo(list.get());
308 // Display the list of plugins serving NaCl.
309 AddPluginList(list.get());
310 // Display information relevant to PNaCl.
311 AddPnaclInfo(list.get());
312 // Display information relevant to NaCl (non-portable.
313 AddNaClInfo(list.get());
314 // naclInfo will take ownership of list, and clean it up on destruction.
315 naclInfo->Set("naclInfo", list.release());
318 void NaClDomHandler::DidCheckPathAndVersion(const std::string* version,
319 bool is_valid) {
320 pnacl_path_validated_ = true;
321 pnacl_path_exists_ = is_valid;
322 pnacl_version_string_ = *version;
323 MaybeRespondToPage();
326 void CheckVersion(const base::FilePath& pnacl_path, std::string* version) {
327 base::FilePath pnacl_json_path =
328 pnacl_path.AppendASCII("pnacl_public_pnacl_json");
329 JSONFileValueDeserializer deserializer(pnacl_json_path);
330 std::string error;
331 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, &error));
332 if (!root || !root->IsType(base::Value::TYPE_DICTIONARY))
333 return;
335 // Now try to get the field. This may leave version empty if the
336 // the "get" fails (no key, or wrong type).
337 static_cast<base::DictionaryValue*>(root.get())->GetStringASCII(
338 "pnacl-version", version);
341 bool CheckPathAndVersion(std::string* version) {
342 base::FilePath pnacl_path;
343 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path);
344 if (got_path && !pnacl_path.empty() && base::PathExists(pnacl_path)) {
345 CheckVersion(pnacl_path, version);
346 return true;
348 return false;
351 void NaClDomHandler::MaybeRespondToPage() {
352 // Don't reply until everything is ready. The page will show a 'loading'
353 // message until then.
354 if (!page_has_requested_data_ || !has_plugin_info_)
355 return;
357 if (!pnacl_path_validated_) {
358 std::string* version_string = new std::string;
359 base::PostTaskAndReplyWithResult(
360 BrowserThread::GetBlockingPool(),
361 FROM_HERE,
362 base::Bind(&CheckPathAndVersion, version_string),
363 base::Bind(&NaClDomHandler::DidCheckPathAndVersion,
364 weak_ptr_factory_.GetWeakPtr(),
365 base::Owned(version_string)));
366 return;
369 base::DictionaryValue naclInfo;
370 PopulatePageInformation(&naclInfo);
371 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo);
374 } // namespace
376 ///////////////////////////////////////////////////////////////////////////////
378 // NaClUI
380 ///////////////////////////////////////////////////////////////////////////////
382 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) {
383 content::RecordAction(UserMetricsAction("ViewAboutNaCl"));
385 web_ui->AddMessageHandler(new NaClDomHandler());
387 // Set up the about:nacl source.
388 Profile* profile = Profile::FromWebUI(web_ui);
389 content::WebUIDataSource::Add(profile, CreateNaClUIHTMLSource());