OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/dom_ui/net_internals_ui.h" | 5 #include "chrome/browser/dom_ui/net_internals_ui.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 | 8 |
9 #include "app/resource_bundle.h" | 9 #include "app/resource_bundle.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/singleton.h" | 12 #include "base/singleton.h" |
13 #include "base/string_piece.h" | 13 #include "base/string_piece.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/chrome_thread.h" | 17 #include "chrome/browser/chrome_thread.h" |
18 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" | 18 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
19 #include "chrome/browser/io_thread.h" | 19 #include "chrome/browser/io_thread.h" |
20 #include "chrome/browser/net/chrome_net_log.h" | 20 #include "chrome/browser/net/chrome_net_log.h" |
| 21 #include "chrome/browser/net/passive_log_collector.h" |
21 #include "chrome/browser/net/url_request_context_getter.h" | 22 #include "chrome/browser/net/url_request_context_getter.h" |
22 #include "chrome/browser/profile.h" | 23 #include "chrome/browser/profile.h" |
23 #include "chrome/common/chrome_paths.h" | 24 #include "chrome/common/chrome_paths.h" |
24 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
25 #include "net/base/escape.h" | 26 #include "net/base/escape.h" |
26 #include "net/base/host_resolver_impl.h" | 27 #include "net/base/host_resolver_impl.h" |
27 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
28 #include "net/base/net_util.h" | 29 #include "net/base/net_util.h" |
29 #include "net/base/sys_addrinfo.h" | 30 #include "net/base/sys_addrinfo.h" |
30 #include "net/proxy/proxy_service.h" | 31 #include "net/proxy/proxy_service.h" |
(...skipping 11 matching lines...) Expand all Loading... |
42 net::HostCache* GetHostResolverCache(URLRequestContext* context) { | 43 net::HostCache* GetHostResolverCache(URLRequestContext* context) { |
43 net::HostResolverImpl* host_resolver_impl = | 44 net::HostResolverImpl* host_resolver_impl = |
44 context->host_resolver()->GetAsHostResolverImpl(); | 45 context->host_resolver()->GetAsHostResolverImpl(); |
45 | 46 |
46 if (!host_resolver_impl) | 47 if (!host_resolver_impl) |
47 return NULL; | 48 return NULL; |
48 | 49 |
49 return host_resolver_impl->cache(); | 50 return host_resolver_impl->cache(); |
50 } | 51 } |
51 | 52 |
52 // TODO(eroman): Bootstrap the net-internals page using the passively logged | 53 // Serializes the specified event to a DictionaryValue. |
53 // data. | 54 Value* EntryToDictionaryValue(net::NetLog::EventType type, |
| 55 const base::TimeTicks& time, |
| 56 const net::NetLog::Source& source, |
| 57 net::NetLog::EventPhase phase, |
| 58 net::NetLog::EventParameters* extra_parameters) { |
| 59 DictionaryValue* entry_dict = new DictionaryValue(); |
| 60 |
| 61 // Set the entry time. (Note that we send it as a string since integers |
| 62 // might overflow). |
| 63 entry_dict->SetString(L"time", TickCountToString(time)); |
| 64 |
| 65 // Set the entry source. |
| 66 DictionaryValue* source_dict = new DictionaryValue(); |
| 67 source_dict->SetInteger(L"id", source.id); |
| 68 source_dict->SetInteger(L"type", static_cast<int>(source.type)); |
| 69 entry_dict->Set(L"source", source_dict); |
| 70 |
| 71 // Set the event info. |
| 72 entry_dict->SetInteger(L"type", static_cast<int>(type)); |
| 73 entry_dict->SetInteger(L"phase", static_cast<int>(phase)); |
| 74 |
| 75 // Set the event-specific parameters. |
| 76 if (extra_parameters) |
| 77 entry_dict->SetString(L"extra_parameters", extra_parameters->ToString()); |
| 78 |
| 79 return entry_dict; |
| 80 } |
54 | 81 |
55 class NetInternalsHTMLSource : public ChromeURLDataManager::DataSource { | 82 class NetInternalsHTMLSource : public ChromeURLDataManager::DataSource { |
56 public: | 83 public: |
57 NetInternalsHTMLSource(); | 84 NetInternalsHTMLSource(); |
58 | 85 |
59 // Called when the network layer has requested a resource underneath | 86 // Called when the network layer has requested a resource underneath |
60 // the path we registered. | 87 // the path we registered. |
61 virtual void StartDataRequest(const std::string& path, | 88 virtual void StartDataRequest(const std::string& path, |
62 bool is_off_the_record, | 89 bool is_off_the_record, |
63 int request_id); | 90 int request_id); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 // This message is called after the webpage's onloaded handler has fired. | 170 // This message is called after the webpage's onloaded handler has fired. |
144 // it indicates that the renderer is ready to start receiving captured data. | 171 // it indicates that the renderer is ready to start receiving captured data. |
145 void OnRendererReady(const Value* value); | 172 void OnRendererReady(const Value* value); |
146 | 173 |
147 void OnGetProxySettings(const Value* value); | 174 void OnGetProxySettings(const Value* value); |
148 void OnReloadProxySettings(const Value* value); | 175 void OnReloadProxySettings(const Value* value); |
149 void OnGetBadProxies(const Value* value); | 176 void OnGetBadProxies(const Value* value); |
150 void OnClearBadProxies(const Value* value); | 177 void OnClearBadProxies(const Value* value); |
151 void OnGetHostResolverCache(const Value* value); | 178 void OnGetHostResolverCache(const Value* value); |
152 void OnClearHostResolverCache(const Value* value); | 179 void OnClearHostResolverCache(const Value* value); |
| 180 void OnGetPassiveLogEntries(const Value* value); |
153 | 181 |
154 // ChromeNetLog::Observer implementation: | 182 // ChromeNetLog::Observer implementation: |
155 virtual void OnAddEntry(net::NetLog::EventType type, | 183 virtual void OnAddEntry(net::NetLog::EventType type, |
156 const base::TimeTicks& time, | 184 const base::TimeTicks& time, |
157 const net::NetLog::Source& source, | 185 const net::NetLog::Source& source, |
158 net::NetLog::EventPhase phase, | 186 net::NetLog::EventPhase phase, |
159 net::NetLog::EventParameters* extra_parameters); | 187 net::NetLog::EventParameters* extra_parameters); |
160 | 188 |
161 private: | 189 private: |
162 class CallbackHelper; | 190 class CallbackHelper; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 312 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
285 proxy_ = new IOThreadImpl(this->AsWeakPtr(), g_browser_process->io_thread(), | 313 proxy_ = new IOThreadImpl(this->AsWeakPtr(), g_browser_process->io_thread(), |
286 dom_ui->GetProfile()->GetRequestContext()); | 314 dom_ui->GetProfile()->GetRequestContext()); |
287 DOMMessageHandler* result = DOMMessageHandler::Attach(dom_ui); | 315 DOMMessageHandler* result = DOMMessageHandler::Attach(dom_ui); |
288 return result; | 316 return result; |
289 } | 317 } |
290 | 318 |
291 void NetInternalsMessageHandler::RegisterMessages() { | 319 void NetInternalsMessageHandler::RegisterMessages() { |
292 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 320 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
293 | 321 |
294 dom_ui_->RegisterMessageCallback("notifyReady", | 322 dom_ui_->RegisterMessageCallback( |
| 323 "notifyReady", |
295 proxy_->CreateCallback(&IOThreadImpl::OnRendererReady)); | 324 proxy_->CreateCallback(&IOThreadImpl::OnRendererReady)); |
296 dom_ui_->RegisterMessageCallback("getProxySettings", | 325 dom_ui_->RegisterMessageCallback( |
| 326 "getProxySettings", |
297 proxy_->CreateCallback(&IOThreadImpl::OnGetProxySettings)); | 327 proxy_->CreateCallback(&IOThreadImpl::OnGetProxySettings)); |
298 dom_ui_->RegisterMessageCallback("reloadProxySettings", | 328 dom_ui_->RegisterMessageCallback( |
| 329 "reloadProxySettings", |
299 proxy_->CreateCallback(&IOThreadImpl::OnReloadProxySettings)); | 330 proxy_->CreateCallback(&IOThreadImpl::OnReloadProxySettings)); |
300 dom_ui_->RegisterMessageCallback("getBadProxies", | 331 dom_ui_->RegisterMessageCallback( |
| 332 "getBadProxies", |
301 proxy_->CreateCallback(&IOThreadImpl::OnGetBadProxies)); | 333 proxy_->CreateCallback(&IOThreadImpl::OnGetBadProxies)); |
302 dom_ui_->RegisterMessageCallback("clearBadProxies", | 334 dom_ui_->RegisterMessageCallback( |
| 335 "clearBadProxies", |
303 proxy_->CreateCallback(&IOThreadImpl::OnClearBadProxies)); | 336 proxy_->CreateCallback(&IOThreadImpl::OnClearBadProxies)); |
304 dom_ui_->RegisterMessageCallback("getHostResolverCache", | 337 dom_ui_->RegisterMessageCallback( |
| 338 "getHostResolverCache", |
305 proxy_->CreateCallback(&IOThreadImpl::OnGetHostResolverCache)); | 339 proxy_->CreateCallback(&IOThreadImpl::OnGetHostResolverCache)); |
306 dom_ui_->RegisterMessageCallback("clearHostResolverCache", | 340 dom_ui_->RegisterMessageCallback( |
| 341 "clearHostResolverCache", |
307 proxy_->CreateCallback(&IOThreadImpl::OnClearHostResolverCache)); | 342 proxy_->CreateCallback(&IOThreadImpl::OnClearHostResolverCache)); |
| 343 dom_ui_->RegisterMessageCallback( |
| 344 "getPassiveLogEntries", |
| 345 proxy_->CreateCallback(&IOThreadImpl::OnGetPassiveLogEntries)); |
308 } | 346 } |
309 | 347 |
310 void NetInternalsMessageHandler::CallJavascriptFunction( | 348 void NetInternalsMessageHandler::CallJavascriptFunction( |
311 const std::wstring& function_name, | 349 const std::wstring& function_name, |
312 const Value& value) { | 350 const Value& value) { |
313 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 351 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
314 dom_ui_->CallJavascriptFunction(function_name, value); | 352 dom_ui_->CallJavascriptFunction(function_name, value); |
315 } | 353 } |
316 | 354 |
317 //////////////////////////////////////////////////////////////////////////////// | 355 //////////////////////////////////////////////////////////////////////////////// |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 // be part of the time library. | 459 // be part of the time library. |
422 const int64 kUnixEpochMs = 11644473600000LL; | 460 const int64 kUnixEpochMs = 11644473600000LL; |
423 int64 tick_to_unix_time_ms = tick_to_time_ms - kUnixEpochMs; | 461 int64 tick_to_unix_time_ms = tick_to_time_ms - kUnixEpochMs; |
424 | 462 |
425 // Pass it as a string, since it may be too large to fit in an integer. | 463 // Pass it as a string, since it may be too large to fit in an integer. |
426 CallJavascriptFunction(L"g_browser.receivedTimeTickOffset", | 464 CallJavascriptFunction(L"g_browser.receivedTimeTickOffset", |
427 Value::CreateStringValue( | 465 Value::CreateStringValue( |
428 Int64ToString(tick_to_unix_time_ms))); | 466 Int64ToString(tick_to_unix_time_ms))); |
429 } | 467 } |
430 | 468 |
431 // Notify the client of the basic proxy data. | 469 OnGetPassiveLogEntries(NULL); |
432 OnGetProxySettings(NULL); | 470 OnGetProxySettings(NULL); |
433 OnGetBadProxies(NULL); | 471 OnGetBadProxies(NULL); |
434 OnGetHostResolverCache(NULL); | 472 OnGetHostResolverCache(NULL); |
435 } | 473 } |
436 | 474 |
437 void NetInternalsMessageHandler::IOThreadImpl::OnGetProxySettings( | 475 void NetInternalsMessageHandler::IOThreadImpl::OnGetProxySettings( |
438 const Value* value) { | 476 const Value* value) { |
439 URLRequestContext* context = context_getter_->GetURLRequestContext(); | 477 URLRequestContext* context = context_getter_->GetURLRequestContext(); |
440 net::ProxyService* proxy_service = context->proxy_service(); | 478 net::ProxyService* proxy_service = context->proxy_service(); |
441 | 479 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 net::HostCache* cache = | 598 net::HostCache* cache = |
561 GetHostResolverCache(context_getter_->GetURLRequestContext()); | 599 GetHostResolverCache(context_getter_->GetURLRequestContext()); |
562 | 600 |
563 if (cache) | 601 if (cache) |
564 cache->clear(); | 602 cache->clear(); |
565 | 603 |
566 // Cause the renderer to be notified of the new values. | 604 // Cause the renderer to be notified of the new values. |
567 OnGetHostResolverCache(NULL); | 605 OnGetHostResolverCache(NULL); |
568 } | 606 } |
569 | 607 |
| 608 void NetInternalsMessageHandler::IOThreadImpl::OnGetPassiveLogEntries( |
| 609 const Value* value) { |
| 610 ChromeNetLog* net_log = io_thread_->globals()->net_log.get(); |
| 611 |
| 612 PassiveLogCollector::EntryList passive_entries; |
| 613 net_log->passive_collector()->GetAllCapturedEvents(&passive_entries); |
| 614 |
| 615 ListValue* list = new ListValue(); |
| 616 for (size_t i = 0; i < passive_entries.size(); ++i) { |
| 617 const PassiveLogCollector::Entry& e = passive_entries[i]; |
| 618 list->Append(EntryToDictionaryValue(e.type, |
| 619 e.time, |
| 620 e.source, |
| 621 e.phase, |
| 622 e.extra_parameters)); |
| 623 } |
| 624 |
| 625 CallJavascriptFunction(L"g_browser.receivedPassiveLogEntries", list); |
| 626 } |
| 627 |
570 void NetInternalsMessageHandler::IOThreadImpl::OnAddEntry( | 628 void NetInternalsMessageHandler::IOThreadImpl::OnAddEntry( |
571 net::NetLog::EventType type, | 629 net::NetLog::EventType type, |
572 const base::TimeTicks& time, | 630 const base::TimeTicks& time, |
573 const net::NetLog::Source& source, | 631 const net::NetLog::Source& source, |
574 net::NetLog::EventPhase phase, | 632 net::NetLog::EventPhase phase, |
575 net::NetLog::EventParameters* extra_parameters) { | 633 net::NetLog::EventParameters* extra_parameters) { |
576 DCHECK(is_observing_log_); | 634 DCHECK(is_observing_log_); |
577 | 635 |
578 // JSONify the NetLog::Entry. | 636 CallJavascriptFunction( |
579 // TODO(eroman): Need a better format for this. | 637 L"g_browser.receivedLogEntry", |
580 DictionaryValue* entry_dict = new DictionaryValue(); | 638 EntryToDictionaryValue(type, time, source, phase, extra_parameters)); |
581 | |
582 // Set the entry time. (Note that we send it as a string since integers | |
583 // might overflow). | |
584 entry_dict->SetString(L"time", TickCountToString(time)); | |
585 | |
586 // Set the entry source. | |
587 DictionaryValue* source_dict = new DictionaryValue(); | |
588 source_dict->SetInteger(L"id", source.id); | |
589 source_dict->SetInteger(L"type", static_cast<int>(source.type)); | |
590 entry_dict->Set(L"source", source_dict); | |
591 | |
592 // Set the event info. | |
593 entry_dict->SetInteger(L"type", static_cast<int>(type)); | |
594 entry_dict->SetInteger(L"phase", static_cast<int>(phase)); | |
595 | |
596 // Set the event-specific parameters. | |
597 if (extra_parameters) | |
598 entry_dict->SetString(L"extra_parameters", extra_parameters->ToString()); | |
599 | |
600 CallJavascriptFunction(L"g_browser.receivedLogEntry", entry_dict); | |
601 } | 639 } |
602 | 640 |
603 void NetInternalsMessageHandler::IOThreadImpl::DispatchToMessageHandler( | 641 void NetInternalsMessageHandler::IOThreadImpl::DispatchToMessageHandler( |
604 Value* arg, MessageHandler method) { | 642 Value* arg, MessageHandler method) { |
605 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 643 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
606 (this->*method)(arg); | 644 (this->*method)(arg); |
607 delete arg; | 645 delete arg; |
608 } | 646 } |
609 | 647 |
610 void NetInternalsMessageHandler::IOThreadImpl::CallJavascriptFunction( | 648 void NetInternalsMessageHandler::IOThreadImpl::CallJavascriptFunction( |
611 const std::wstring& function_name, | 649 const std::wstring& function_name, |
612 Value* arg) { | 650 Value* arg) { |
613 if (ChromeThread::CurrentlyOn(ChromeThread::UI)) { | 651 if (ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
614 if (handler_) { | 652 if (handler_) { |
615 // We check |handler_| in case it was deleted on the UI thread earlier | 653 // We check |handler_| in case it was deleted on the UI thread earlier |
616 // while we were running on the IO thread. | 654 // while we were running on the IO thread. |
617 handler_->CallJavascriptFunction(function_name, *arg); | 655 handler_->CallJavascriptFunction(function_name, *arg); |
618 } | 656 } |
619 delete arg; | 657 delete arg; |
620 return; | 658 return; |
621 } | 659 } |
622 | 660 |
623 | |
624 // Otherwise if we were called from the IO thread, bridge the request over to | 661 // Otherwise if we were called from the IO thread, bridge the request over to |
625 // the UI thread. | 662 // the UI thread. |
626 | 663 |
627 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 664 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
628 if (!ChromeThread::PostTask( | 665 if (!ChromeThread::PostTask( |
629 ChromeThread::UI, FROM_HERE, | 666 ChromeThread::UI, FROM_HERE, |
630 NewRunnableMethod( | 667 NewRunnableMethod( |
631 this, | 668 this, |
632 &IOThreadImpl::CallJavascriptFunction, | 669 &IOThreadImpl::CallJavascriptFunction, |
633 function_name, arg))) { | 670 function_name, arg))) { |
(...skipping 17 matching lines...) Expand all Loading... |
651 NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource(); | 688 NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource(); |
652 | 689 |
653 // Set up the chrome://net-internals/ source. | 690 // Set up the chrome://net-internals/ source. |
654 ChromeThread::PostTask( | 691 ChromeThread::PostTask( |
655 ChromeThread::IO, FROM_HERE, | 692 ChromeThread::IO, FROM_HERE, |
656 NewRunnableMethod( | 693 NewRunnableMethod( |
657 Singleton<ChromeURLDataManager>::get(), | 694 Singleton<ChromeURLDataManager>::get(), |
658 &ChromeURLDataManager::AddDataSource, | 695 &ChromeURLDataManager::AddDataSource, |
659 make_scoped_refptr(html_source))); | 696 make_scoped_refptr(html_source))); |
660 } | 697 } |
OLD | NEW |