OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/nacl_ui.h" | 5 #include "chrome/browser/ui/webui/nacl_ui.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/json/json_file_value_serializer.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
16 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
19 #include "base/values.h" | 20 #include "base/values.h" |
20 #include "chrome/browser/plugins/plugin_prefs.h" | 21 #include "chrome/browser/plugins/plugin_prefs.h" |
21 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
23 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 71 |
71 // The handler for JavaScript messages for the about:flags page. | 72 // The handler for JavaScript messages for the about:flags page. |
72 class NaClDomHandler : public WebUIMessageHandler { | 73 class NaClDomHandler : public WebUIMessageHandler { |
73 public: | 74 public: |
74 NaClDomHandler(); | 75 NaClDomHandler(); |
75 virtual ~NaClDomHandler(); | 76 virtual ~NaClDomHandler(); |
76 | 77 |
77 // WebUIMessageHandler implementation. | 78 // WebUIMessageHandler implementation. |
78 virtual void RegisterMessages() OVERRIDE; | 79 virtual void RegisterMessages() OVERRIDE; |
79 | 80 |
| 81 private: |
80 // Callback for the "requestNaClInfo" message. | 82 // Callback for the "requestNaClInfo" message. |
81 void HandleRequestNaClInfo(const ListValue* args); | 83 void HandleRequestNaClInfo(const ListValue* args); |
82 | 84 |
83 // Callback for the NaCl plugin information. | 85 // Callback for the NaCl plugin information. |
84 void OnGotPlugins(const std::vector<content::WebPluginInfo>& plugins); | 86 void OnGotPlugins(const std::vector<content::WebPluginInfo>& plugins); |
85 | 87 |
86 // A helper callback that receives the result of checking if PNaCl path | 88 // A helper callback that receives the result of checking if PNaCl path |
87 // exists. |is_valid| is true if the PNaCl path that was returned by | 89 // exists and checking the PNaCl |version|. |is_valid| is true if the PNaCl |
88 // PathService is valid, and false otherwise. | 90 // path that was returned by PathService is valid, and false otherwise. |
89 void DidValidatePnaclPath(bool* is_valid); | 91 void DidCheckPathAndVersion(bool* is_valid, std::string* version); |
90 | 92 |
91 private: | |
92 // Called when enough information is gathered to return data back to the page. | 93 // Called when enough information is gathered to return data back to the page. |
93 void MaybeRespondToPage(); | 94 void MaybeRespondToPage(); |
94 | 95 |
95 // Helper for MaybeRespondToPage -- called after enough information | 96 // Helper for MaybeRespondToPage -- called after enough information |
96 // is gathered. | 97 // is gathered. |
97 void PopulatePageInformation(DictionaryValue* naclInfo); | 98 void PopulatePageInformation(DictionaryValue* naclInfo); |
98 | 99 |
99 // Factory for the creating refs in callbacks. | 100 // Factory for the creating refs in callbacks. |
100 base::WeakPtrFactory<NaClDomHandler> weak_ptr_factory_; | 101 base::WeakPtrFactory<NaClDomHandler> weak_ptr_factory_; |
101 | 102 |
102 // Returns whether the specified plugin is enabled. | 103 // Returns whether the specified plugin is enabled. |
103 bool isPluginEnabled(size_t plugin_index); | 104 bool isPluginEnabled(size_t plugin_index); |
104 | 105 |
105 // Adds information regarding the operating system and chrome version to list. | 106 // Adds information regarding the operating system and chrome version to list. |
106 void AddOperatingSystemInfo(ListValue* list); | 107 void AddOperatingSystemInfo(ListValue* list); |
107 | 108 |
108 // Adds the list of plugins for NaCl to list. | 109 // Adds the list of plugins for NaCl to list. |
109 void AddPluginList(ListValue* list); | 110 void AddPluginList(ListValue* list); |
110 | 111 |
111 // Adds the information relevant to PNaCl (e.g., enablement, paths) to list. | 112 // Adds the information relevant to PNaCl (e.g., enablement, paths, version) |
| 113 // to the list. |
112 void AddPnaclInfo(ListValue* list); | 114 void AddPnaclInfo(ListValue* list); |
113 | 115 |
114 // Adds the information relevant to NaCl to list. | 116 // Adds the information relevant to NaCl to list. |
115 void AddNaClInfo(ListValue* list); | 117 void AddNaClInfo(ListValue* list); |
116 | 118 |
117 // Whether the page has requested data. | 119 // Whether the page has requested data. |
118 bool page_has_requested_data_; | 120 bool page_has_requested_data_; |
119 | 121 |
120 // Whether the plugin information is ready. | 122 // Whether the plugin information is ready. |
121 bool has_plugin_info_; | 123 bool has_plugin_info_; |
122 | 124 |
123 // Whether PNaCl path was validated. PathService can return a path | 125 // Whether PNaCl path was validated. PathService can return a path |
124 // that does not exists, so it needs to be validated. | 126 // that does not exists, so it needs to be validated. |
125 bool pnacl_path_validated_; | 127 bool pnacl_path_validated_; |
126 bool pnacl_path_exists_; | 128 bool pnacl_path_exists_; |
| 129 std::string pnacl_version_string_; |
127 | 130 |
128 DISALLOW_COPY_AND_ASSIGN(NaClDomHandler); | 131 DISALLOW_COPY_AND_ASSIGN(NaClDomHandler); |
129 }; | 132 }; |
130 | 133 |
131 NaClDomHandler::NaClDomHandler() | 134 NaClDomHandler::NaClDomHandler() |
132 : weak_ptr_factory_(this), | 135 : weak_ptr_factory_(this), |
133 page_has_requested_data_(false), | 136 page_has_requested_data_(false), |
134 has_plugin_info_(false), | 137 has_plugin_info_(false), |
135 pnacl_path_validated_(false), | 138 pnacl_path_validated_(false), |
136 pnacl_path_exists_(false) { | 139 pnacl_path_exists_(false) { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 base::FilePath pnacl_path; | 261 base::FilePath pnacl_path; |
259 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); | 262 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); |
260 if (!got_path || pnacl_path.empty() || !pnacl_path_exists_) { | 263 if (!got_path || pnacl_path.empty() || !pnacl_path_exists_) { |
261 AddPair(list, | 264 AddPair(list, |
262 ASCIIToUTF16("PNaCl translator"), | 265 ASCIIToUTF16("PNaCl translator"), |
263 ASCIIToUTF16("Not installed")); | 266 ASCIIToUTF16("Not installed")); |
264 } else { | 267 } else { |
265 AddPair(list, | 268 AddPair(list, |
266 ASCIIToUTF16("PNaCl translator path"), | 269 ASCIIToUTF16("PNaCl translator path"), |
267 pnacl_path.LossyDisplayName()); | 270 pnacl_path.LossyDisplayName()); |
268 // Version string is part of the directory name: | |
269 // pnacl/<version>/_platform_specific/<arch>/[files] | |
270 // Keep in sync with pnacl_component_installer.cc. | |
271 AddPair(list, | 271 AddPair(list, |
272 ASCIIToUTF16("PNaCl translator version"), | 272 ASCIIToUTF16("PNaCl translator version"), |
273 pnacl_path.DirName().DirName().BaseName().LossyDisplayName()); | 273 ASCIIToUTF16(pnacl_version_string_)); |
274 } | 274 } |
275 AddLineBreak(list); | 275 AddLineBreak(list); |
276 } | 276 } |
277 | 277 |
278 void NaClDomHandler::AddNaClInfo(ListValue* list) { | 278 void NaClDomHandler::AddNaClInfo(ListValue* list) { |
279 string16 nacl_enabled_string = ASCIIToUTF16("Disabled"); | 279 string16 nacl_enabled_string = ASCIIToUTF16("Disabled"); |
280 if (isPluginEnabled(0) && | 280 if (isPluginEnabled(0) && |
281 CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableNaCl)) { | 281 CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableNaCl)) { |
282 nacl_enabled_string = ASCIIToUTF16("Enabled by flag '--enable-nacl'"); | 282 nacl_enabled_string = ASCIIToUTF16("Enabled by flag '--enable-nacl'"); |
283 } | 283 } |
284 AddPair(list, | 284 AddPair(list, |
285 ASCIIToUTF16("Native Client (non-portable, outside web store)"), | 285 ASCIIToUTF16("Native Client (non-portable, outside web store)"), |
286 nacl_enabled_string); | 286 nacl_enabled_string); |
287 AddLineBreak(list); | 287 AddLineBreak(list); |
288 } | 288 } |
289 | 289 |
290 void NaClDomHandler::HandleRequestNaClInfo(const ListValue* args) { | 290 void NaClDomHandler::HandleRequestNaClInfo(const ListValue* args) { |
291 page_has_requested_data_ = true; | 291 page_has_requested_data_ = true; |
292 // Force re-validation of pnacl's path in the next call to | 292 // Force re-validation of PNaCl's path in the next call to |
293 // MaybeRespondToPage(), in case PNaCl went from not-installed | 293 // MaybeRespondToPage(), in case PNaCl went from not-installed |
294 // to installed since the request. | 294 // to installed since the request. |
295 pnacl_path_validated_ = false; | 295 pnacl_path_validated_ = false; |
296 MaybeRespondToPage(); | 296 MaybeRespondToPage(); |
297 } | 297 } |
298 | 298 |
299 void NaClDomHandler::OnGotPlugins( | 299 void NaClDomHandler::OnGotPlugins( |
300 const std::vector<content::WebPluginInfo>& plugins) { | 300 const std::vector<content::WebPluginInfo>& plugins) { |
301 has_plugin_info_ = true; | 301 has_plugin_info_ = true; |
302 MaybeRespondToPage(); | 302 MaybeRespondToPage(); |
303 } | 303 } |
304 | 304 |
305 void NaClDomHandler::PopulatePageInformation(DictionaryValue* naclInfo) { | 305 void NaClDomHandler::PopulatePageInformation(DictionaryValue* naclInfo) { |
306 DCHECK(pnacl_path_validated_); | 306 DCHECK(pnacl_path_validated_); |
307 // Store Key-Value pairs of about-information. | 307 // Store Key-Value pairs of about-information. |
308 scoped_ptr<ListValue> list(new ListValue()); | 308 scoped_ptr<ListValue> list(new ListValue()); |
309 // Display the operating system and chrome version information. | 309 // Display the operating system and chrome version information. |
310 AddOperatingSystemInfo(list.get()); | 310 AddOperatingSystemInfo(list.get()); |
311 // Display the list of plugins serving NaCl. | 311 // Display the list of plugins serving NaCl. |
312 AddPluginList(list.get()); | 312 AddPluginList(list.get()); |
313 // Display information relevant to PNaCl. | 313 // Display information relevant to PNaCl. |
314 AddPnaclInfo(list.get()); | 314 AddPnaclInfo(list.get()); |
315 // Display information relevant to NaCl (non-portable. | 315 // Display information relevant to NaCl (non-portable. |
316 AddNaClInfo(list.get()); | 316 AddNaClInfo(list.get()); |
317 // naclInfo will take ownership of list, and clean it up on destruction. | 317 // naclInfo will take ownership of list, and clean it up on destruction. |
318 naclInfo->Set("naclInfo", list.release()); | 318 naclInfo->Set("naclInfo", list.release()); |
319 } | 319 } |
320 | 320 |
321 void NaClDomHandler::DidValidatePnaclPath(bool* is_valid) { | 321 void NaClDomHandler::DidCheckPathAndVersion(bool* is_valid, |
| 322 std::string* version) { |
322 pnacl_path_validated_ = true; | 323 pnacl_path_validated_ = true; |
323 pnacl_path_exists_ = *is_valid; | 324 pnacl_path_exists_ = *is_valid; |
| 325 pnacl_version_string_ = *version; |
324 MaybeRespondToPage(); | 326 MaybeRespondToPage(); |
325 } | 327 } |
326 | 328 |
327 void ValidatePnaclPath(bool* is_valid) { | 329 void CheckVersion(const base::FilePath& pnacl_path, std::string* version) { |
| 330 base::FilePath pnacl_json_path = |
| 331 pnacl_path.Append(FILE_PATH_LITERAL("pnacl_public_pnacl_json")); |
| 332 JSONFileValueSerializer serializer(pnacl_json_path); |
| 333 std::string error; |
| 334 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); |
| 335 if (!root || !root->IsType(base::Value::TYPE_DICTIONARY)) |
| 336 return; |
| 337 |
| 338 // Now try to get the field. This may leave version empty if the |
| 339 // the "get" fails (no key, or wrong type). |
| 340 static_cast<base::DictionaryValue*>(root.get())-> |
| 341 GetStringASCII("pnacl-version", version); |
| 342 } |
| 343 |
| 344 void CheckPathAndVersion(bool* is_valid, std::string* version) { |
328 base::FilePath pnacl_path; | 345 base::FilePath pnacl_path; |
329 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); | 346 bool got_path = PathService::Get(chrome::DIR_PNACL_COMPONENT, &pnacl_path); |
330 *is_valid = got_path && !pnacl_path.empty() && base::PathExists(pnacl_path); | 347 *is_valid = got_path && !pnacl_path.empty() && base::PathExists(pnacl_path); |
| 348 if (*is_valid) |
| 349 CheckVersion(pnacl_path, version); |
331 } | 350 } |
332 | 351 |
333 void NaClDomHandler::MaybeRespondToPage() { | 352 void NaClDomHandler::MaybeRespondToPage() { |
334 // Don't reply until everything is ready. The page will show a 'loading' | 353 // Don't reply until everything is ready. The page will show a 'loading' |
335 // message until then. | 354 // message until then. |
336 if (!page_has_requested_data_ || !has_plugin_info_) | 355 if (!page_has_requested_data_ || !has_plugin_info_) |
337 return; | 356 return; |
338 | 357 |
339 if (!pnacl_path_validated_) { | 358 if (!pnacl_path_validated_) { |
340 bool* is_valid = new bool; | 359 bool* is_valid = new bool; |
| 360 std::string* version_string = new std::string; |
341 BrowserThread::PostBlockingPoolTaskAndReply( | 361 BrowserThread::PostBlockingPoolTaskAndReply( |
342 FROM_HERE, | 362 FROM_HERE, |
343 base::Bind(&ValidatePnaclPath, is_valid), | 363 base::Bind(&CheckPathAndVersion, is_valid, version_string), |
344 base::Bind(&NaClDomHandler::DidValidatePnaclPath, | 364 base::Bind(&NaClDomHandler::DidCheckPathAndVersion, |
345 weak_ptr_factory_.GetWeakPtr(), | 365 weak_ptr_factory_.GetWeakPtr(), |
346 base::Owned(is_valid))); | 366 base::Owned(is_valid), |
| 367 base::Owned(version_string))); |
347 return; | 368 return; |
348 } | 369 } |
349 | 370 |
350 DictionaryValue naclInfo; | 371 DictionaryValue naclInfo; |
351 PopulatePageInformation(&naclInfo); | 372 PopulatePageInformation(&naclInfo); |
352 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo); | 373 web_ui()->CallJavascriptFunction("nacl.returnNaClInfo", naclInfo); |
353 } | 374 } |
354 | 375 |
355 } // namespace | 376 } // namespace |
356 | 377 |
357 /////////////////////////////////////////////////////////////////////////////// | 378 /////////////////////////////////////////////////////////////////////////////// |
358 // | 379 // |
359 // NaClUI | 380 // NaClUI |
360 // | 381 // |
361 /////////////////////////////////////////////////////////////////////////////// | 382 /////////////////////////////////////////////////////////////////////////////// |
362 | 383 |
363 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 384 NaClUI::NaClUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
364 content::RecordAction(UserMetricsAction("ViewAboutNaCl")); | 385 content::RecordAction(UserMetricsAction("ViewAboutNaCl")); |
365 | 386 |
366 web_ui->AddMessageHandler(new NaClDomHandler()); | 387 web_ui->AddMessageHandler(new NaClDomHandler()); |
367 | 388 |
368 // Set up the about:nacl source. | 389 // Set up the about:nacl source. |
369 Profile* profile = Profile::FromWebUI(web_ui); | 390 Profile* profile = Profile::FromWebUI(web_ui); |
370 content::WebUIDataSource::Add(profile, CreateNaClUIHTMLSource()); | 391 content::WebUIDataSource::Add(profile, CreateNaClUIHTMLSource()); |
371 } | 392 } |
OLD | NEW |