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/url_request_context_getter.h" | 21 #include "chrome/browser/net/url_request_context_getter.h" |
22 #include "chrome/browser/profile.h" | 22 #include "chrome/browser/profile.h" |
23 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
24 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
25 #include "net/base/escape.h" | 25 #include "net/base/escape.h" |
| 26 #include "net/base/host_resolver_impl.h" |
| 27 #include "net/base/net_errors.h" |
| 28 #include "net/base/net_util.h" |
| 29 #include "net/base/sys_addrinfo.h" |
26 #include "net/proxy/proxy_service.h" | 30 #include "net/proxy/proxy_service.h" |
27 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
28 | 32 |
29 namespace { | 33 namespace { |
30 | 34 |
31 // Formats |t| as a decimal number, in milliseconds. | 35 // Formats |t| as a decimal number, in milliseconds. |
32 std::string TickCountToString(const base::TimeTicks& t) { | 36 std::string TickCountToString(const base::TimeTicks& t) { |
33 return Int64ToString((t - base::TimeTicks()).InMilliseconds()); | 37 return Int64ToString((t - base::TimeTicks()).InMilliseconds()); |
34 } | 38 } |
35 | 39 |
| 40 // Returns the HostCache for |context|'s primary HostResolver, or NULL if |
| 41 // there is none. |
| 42 net::HostCache* GetHostResolverCache(URLRequestContext* context) { |
| 43 net::HostResolverImpl* host_resolver_impl = |
| 44 context->host_resolver()->GetAsHostResolverImpl(); |
| 45 |
| 46 if (!host_resolver_impl) |
| 47 return NULL; |
| 48 |
| 49 return host_resolver_impl->cache(); |
| 50 } |
| 51 |
36 // TODO(eroman): Bootstrap the net-internals page using the passively logged | 52 // TODO(eroman): Bootstrap the net-internals page using the passively logged |
37 // data. | 53 // data. |
38 | 54 |
39 class NetInternalsHTMLSource : public ChromeURLDataManager::DataSource { | 55 class NetInternalsHTMLSource : public ChromeURLDataManager::DataSource { |
40 public: | 56 public: |
41 NetInternalsHTMLSource(); | 57 NetInternalsHTMLSource(); |
42 | 58 |
43 // Called when the network layer has requested a resource underneath | 59 // Called when the network layer has requested a resource underneath |
44 // the path we registered. | 60 // the path we registered. |
45 virtual void StartDataRequest(const std::string& path, | 61 virtual void StartDataRequest(const std::string& path, |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 //-------------------------------- | 141 //-------------------------------- |
126 | 142 |
127 // This message is called after the webpage's onloaded handler has fired. | 143 // This message is called after the webpage's onloaded handler has fired. |
128 // it indicates that the renderer is ready to start receiving captured data. | 144 // it indicates that the renderer is ready to start receiving captured data. |
129 void OnRendererReady(const Value* value); | 145 void OnRendererReady(const Value* value); |
130 | 146 |
131 void OnGetProxySettings(const Value* value); | 147 void OnGetProxySettings(const Value* value); |
132 void OnReloadProxySettings(const Value* value); | 148 void OnReloadProxySettings(const Value* value); |
133 void OnGetBadProxies(const Value* value); | 149 void OnGetBadProxies(const Value* value); |
134 void OnClearBadProxies(const Value* value); | 150 void OnClearBadProxies(const Value* value); |
| 151 void OnGetHostResolverCache(const Value* value); |
| 152 void OnClearHostResolverCache(const Value* value); |
135 | 153 |
136 // ChromeNetLog::Observer implementation: | 154 // ChromeNetLog::Observer implementation: |
137 virtual void OnAddEntry(const net::NetLog::Entry& entry); | 155 virtual void OnAddEntry(const net::NetLog::Entry& entry); |
138 | 156 |
139 private: | 157 private: |
140 class CallbackHelper; | 158 class CallbackHelper; |
141 | 159 |
142 // Helper that runs |method| with |arg|, and deletes |arg| on completion. | 160 // Helper that runs |method| with |arg|, and deletes |arg| on completion. |
143 void DispatchToMessageHandler(Value* arg, MessageHandler method); | 161 void DispatchToMessageHandler(Value* arg, MessageHandler method); |
144 | 162 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 dom_ui_->RegisterMessageCallback("notifyReady", | 290 dom_ui_->RegisterMessageCallback("notifyReady", |
273 proxy_->CreateCallback(&IOThreadImpl::OnRendererReady)); | 291 proxy_->CreateCallback(&IOThreadImpl::OnRendererReady)); |
274 dom_ui_->RegisterMessageCallback("getProxySettings", | 292 dom_ui_->RegisterMessageCallback("getProxySettings", |
275 proxy_->CreateCallback(&IOThreadImpl::OnGetProxySettings)); | 293 proxy_->CreateCallback(&IOThreadImpl::OnGetProxySettings)); |
276 dom_ui_->RegisterMessageCallback("reloadProxySettings", | 294 dom_ui_->RegisterMessageCallback("reloadProxySettings", |
277 proxy_->CreateCallback(&IOThreadImpl::OnReloadProxySettings)); | 295 proxy_->CreateCallback(&IOThreadImpl::OnReloadProxySettings)); |
278 dom_ui_->RegisterMessageCallback("getBadProxies", | 296 dom_ui_->RegisterMessageCallback("getBadProxies", |
279 proxy_->CreateCallback(&IOThreadImpl::OnGetBadProxies)); | 297 proxy_->CreateCallback(&IOThreadImpl::OnGetBadProxies)); |
280 dom_ui_->RegisterMessageCallback("clearBadProxies", | 298 dom_ui_->RegisterMessageCallback("clearBadProxies", |
281 proxy_->CreateCallback(&IOThreadImpl::OnClearBadProxies)); | 299 proxy_->CreateCallback(&IOThreadImpl::OnClearBadProxies)); |
| 300 dom_ui_->RegisterMessageCallback("getHostResolverCache", |
| 301 proxy_->CreateCallback(&IOThreadImpl::OnGetHostResolverCache)); |
| 302 dom_ui_->RegisterMessageCallback("clearHostResolverCache", |
| 303 proxy_->CreateCallback(&IOThreadImpl::OnClearHostResolverCache)); |
282 } | 304 } |
283 | 305 |
284 void NetInternalsMessageHandler::CallJavascriptFunction( | 306 void NetInternalsMessageHandler::CallJavascriptFunction( |
285 const std::wstring& function_name, | 307 const std::wstring& function_name, |
286 const Value& value) { | 308 const Value& value) { |
287 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 309 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
288 dom_ui_->CallJavascriptFunction(function_name, value); | 310 dom_ui_->CallJavascriptFunction(function_name, value); |
289 } | 311 } |
290 | 312 |
291 //////////////////////////////////////////////////////////////////////////////// | 313 //////////////////////////////////////////////////////////////////////////////// |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 | 432 |
411 // Pass it as a string, since it may be too large to fit in an integer. | 433 // Pass it as a string, since it may be too large to fit in an integer. |
412 CallJavascriptFunction(L"g_browser.receivedTimeTickOffset", | 434 CallJavascriptFunction(L"g_browser.receivedTimeTickOffset", |
413 Value::CreateStringValue( | 435 Value::CreateStringValue( |
414 Int64ToString(tick_to_unix_time_ms))); | 436 Int64ToString(tick_to_unix_time_ms))); |
415 } | 437 } |
416 | 438 |
417 // Notify the client of the basic proxy data. | 439 // Notify the client of the basic proxy data. |
418 OnGetProxySettings(NULL); | 440 OnGetProxySettings(NULL); |
419 OnGetBadProxies(NULL); | 441 OnGetBadProxies(NULL); |
| 442 OnGetHostResolverCache(NULL); |
420 } | 443 } |
421 | 444 |
422 void NetInternalsMessageHandler::IOThreadImpl::OnGetProxySettings( | 445 void NetInternalsMessageHandler::IOThreadImpl::OnGetProxySettings( |
423 const Value* value) { | 446 const Value* value) { |
424 URLRequestContext* context = context_getter_->GetURLRequestContext(); | 447 URLRequestContext* context = context_getter_->GetURLRequestContext(); |
425 net::ProxyService* proxy_service = context->proxy_service(); | 448 net::ProxyService* proxy_service = context->proxy_service(); |
426 | 449 |
427 // TODO(eroman): send a dictionary rather than a flat string, so client can do | 450 // TODO(eroman): send a dictionary rather than a flat string, so client can do |
428 // its own presentation. | 451 // its own presentation. |
429 std::string settings_string; | 452 std::string settings_string; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 | 497 |
475 void NetInternalsMessageHandler::IOThreadImpl::OnClearBadProxies( | 498 void NetInternalsMessageHandler::IOThreadImpl::OnClearBadProxies( |
476 const Value* value) { | 499 const Value* value) { |
477 URLRequestContext* context = context_getter_->GetURLRequestContext(); | 500 URLRequestContext* context = context_getter_->GetURLRequestContext(); |
478 context->proxy_service()->ClearBadProxiesCache(); | 501 context->proxy_service()->ClearBadProxiesCache(); |
479 | 502 |
480 // Cause the renderer to be notified of the new values. | 503 // Cause the renderer to be notified of the new values. |
481 OnGetBadProxies(NULL); | 504 OnGetBadProxies(NULL); |
482 } | 505 } |
483 | 506 |
| 507 void NetInternalsMessageHandler::IOThreadImpl::OnGetHostResolverCache( |
| 508 const Value* value) { |
| 509 |
| 510 net::HostCache* cache = |
| 511 GetHostResolverCache(context_getter_->GetURLRequestContext()); |
| 512 |
| 513 if (!cache) { |
| 514 CallJavascriptFunction(L"g_browser.receivedHostResolverCache", NULL); |
| 515 return; |
| 516 } |
| 517 |
| 518 DictionaryValue* dict = new DictionaryValue(); |
| 519 |
| 520 dict->SetInteger(L"capacity", static_cast<int>(cache->max_entries())); |
| 521 dict->SetInteger( |
| 522 L"ttl_success_ms", |
| 523 static_cast<int>(cache->success_entry_ttl().InMilliseconds())); |
| 524 dict->SetInteger( |
| 525 L"ttl_failure_ms", |
| 526 static_cast<int>(cache->failure_entry_ttl().InMilliseconds())); |
| 527 |
| 528 ListValue* entry_list = new ListValue(); |
| 529 |
| 530 for (net::HostCache::EntryMap::const_iterator it = |
| 531 cache->entries().begin(); |
| 532 it != cache->entries().end(); |
| 533 ++it) { |
| 534 const net::HostCache::Key& key = it->first; |
| 535 const net::HostCache::Entry* entry = it->second.get(); |
| 536 |
| 537 DictionaryValue* entry_dict = new DictionaryValue(); |
| 538 |
| 539 entry_dict->SetString(L"hostname", key.hostname); |
| 540 entry_dict->SetInteger(L"address_family", |
| 541 static_cast<int>(key.address_family)); |
| 542 entry_dict->SetString(L"expiration", TickCountToString(entry->expiration)); |
| 543 |
| 544 if (entry->error != net::OK) { |
| 545 entry_dict->SetInteger(L"error", entry->error); |
| 546 } else { |
| 547 // Append all of the resolved addresses. |
| 548 ListValue* address_list = new ListValue(); |
| 549 const struct addrinfo* current_address = entry->addrlist.head(); |
| 550 while (current_address) { |
| 551 address_list->Append(Value::CreateStringValue( |
| 552 net::NetAddressToString(current_address))); |
| 553 current_address = current_address->ai_next; |
| 554 } |
| 555 entry_dict->Set(L"addresses", address_list); |
| 556 } |
| 557 |
| 558 entry_list->Append(entry_dict); |
| 559 } |
| 560 |
| 561 dict->Set(L"entries", entry_list); |
| 562 |
| 563 CallJavascriptFunction(L"g_browser.receivedHostResolverCache", dict); |
| 564 } |
| 565 |
| 566 void NetInternalsMessageHandler::IOThreadImpl::OnClearHostResolverCache( |
| 567 const Value* value) { |
| 568 net::HostCache* cache = |
| 569 GetHostResolverCache(context_getter_->GetURLRequestContext()); |
| 570 |
| 571 if (cache) |
| 572 cache->clear(); |
| 573 |
| 574 // Cause the renderer to be notified of the new values. |
| 575 OnGetHostResolverCache(NULL); |
| 576 } |
| 577 |
484 void NetInternalsMessageHandler::IOThreadImpl::OnAddEntry( | 578 void NetInternalsMessageHandler::IOThreadImpl::OnAddEntry( |
485 const net::NetLog::Entry& entry) { | 579 const net::NetLog::Entry& entry) { |
486 DCHECK(is_observing_log_); | 580 DCHECK(is_observing_log_); |
487 | 581 |
488 // JSONify the NetLog::Entry. | 582 // JSONify the NetLog::Entry. |
489 // TODO(eroman): Need a better format for this. | 583 // TODO(eroman): Need a better format for this. |
490 DictionaryValue* entry_dict = new DictionaryValue(); | 584 DictionaryValue* entry_dict = new DictionaryValue(); |
491 | 585 |
492 // Set the entry type. | 586 // Set the entry type. |
493 { | 587 { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource(); | 678 NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource(); |
585 | 679 |
586 // Set up the chrome://net-internals/ source. | 680 // Set up the chrome://net-internals/ source. |
587 ChromeThread::PostTask( | 681 ChromeThread::PostTask( |
588 ChromeThread::IO, FROM_HERE, | 682 ChromeThread::IO, FROM_HERE, |
589 NewRunnableMethod( | 683 NewRunnableMethod( |
590 Singleton<ChromeURLDataManager>::get(), | 684 Singleton<ChromeURLDataManager>::get(), |
591 &ChromeURLDataManager::AddDataSource, | 685 &ChromeURLDataManager::AddDataSource, |
592 make_scoped_refptr(html_source))); | 686 make_scoped_refptr(html_source))); |
593 } | 687 } |
OLD | NEW |