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 <algorithm> | 7 #include <algorithm> |
8 #include <sstream> | 8 #include <sstream> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "chrome/browser/net/passive_log_collector.h" | 26 #include "chrome/browser/net/passive_log_collector.h" |
27 #include "chrome/browser/net/url_request_context_getter.h" | 27 #include "chrome/browser/net/url_request_context_getter.h" |
28 #include "chrome/browser/profile.h" | 28 #include "chrome/browser/profile.h" |
29 #include "chrome/common/chrome_paths.h" | 29 #include "chrome/common/chrome_paths.h" |
30 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
31 #include "net/base/escape.h" | 31 #include "net/base/escape.h" |
32 #include "net/base/host_resolver_impl.h" | 32 #include "net/base/host_resolver_impl.h" |
33 #include "net/base/net_errors.h" | 33 #include "net/base/net_errors.h" |
34 #include "net/base/net_util.h" | 34 #include "net/base/net_util.h" |
35 #include "net/base/sys_addrinfo.h" | 35 #include "net/base/sys_addrinfo.h" |
| 36 #include "net/disk_cache/disk_cache.h" |
| 37 #include "net/http/http_cache.h" |
36 #include "net/proxy/proxy_service.h" | 38 #include "net/proxy/proxy_service.h" |
37 #include "net/url_request/url_request_context.h" | 39 #include "net/url_request/url_request_context.h" |
38 | 40 |
39 namespace { | 41 namespace { |
40 | 42 |
41 // Formats |t| as a decimal number, in milliseconds. | 43 // Formats |t| as a decimal number, in milliseconds. |
42 std::string TickCountToString(const base::TimeTicks& t) { | 44 std::string TickCountToString(const base::TimeTicks& t) { |
43 return Int64ToString((t - base::TimeTicks()).InMilliseconds()); | 45 return Int64ToString((t - base::TimeTicks()).InMilliseconds()); |
44 } | 46 } |
45 | 47 |
46 // Returns the HostCache for |context|'s primary HostResolver, or NULL if | 48 // Returns the HostCache for |context|'s primary HostResolver, or NULL if |
47 // there is none. | 49 // there is none. |
48 net::HostCache* GetHostResolverCache(URLRequestContext* context) { | 50 net::HostCache* GetHostResolverCache(URLRequestContext* context) { |
49 net::HostResolverImpl* host_resolver_impl = | 51 net::HostResolverImpl* host_resolver_impl = |
50 context->host_resolver()->GetAsHostResolverImpl(); | 52 context->host_resolver()->GetAsHostResolverImpl(); |
51 | 53 |
52 if (!host_resolver_impl) | 54 if (!host_resolver_impl) |
53 return NULL; | 55 return NULL; |
54 | 56 |
55 return host_resolver_impl->cache(); | 57 return host_resolver_impl->cache(); |
56 } | 58 } |
57 | 59 |
| 60 // Returns the disk cache backend for |context| if there is one, or NULL. |
| 61 disk_cache::Backend* GetDiskCacheBackend(URLRequestContext* context) { |
| 62 if (!context->http_transaction_factory()) |
| 63 return NULL; |
| 64 |
| 65 net::HttpCache* http_cache = context->http_transaction_factory()->GetCache(); |
| 66 if (!http_cache) |
| 67 return NULL; |
| 68 |
| 69 return http_cache->GetBackend(); |
| 70 } |
| 71 |
58 // Serializes the specified event to a DictionaryValue. | 72 // Serializes the specified event to a DictionaryValue. |
59 Value* EntryToDictionaryValue(net::NetLog::EventType type, | 73 Value* EntryToDictionaryValue(net::NetLog::EventType type, |
60 const base::TimeTicks& time, | 74 const base::TimeTicks& time, |
61 const net::NetLog::Source& source, | 75 const net::NetLog::Source& source, |
62 net::NetLog::EventPhase phase, | 76 net::NetLog::EventPhase phase, |
63 net::NetLog::EventParameters* extra_parameters) { | 77 net::NetLog::EventParameters* extra_parameters) { |
64 DictionaryValue* entry_dict = new DictionaryValue(); | 78 DictionaryValue* entry_dict = new DictionaryValue(); |
65 | 79 |
66 // Set the entry time. (Note that we send it as a string since integers | 80 // Set the entry time. (Note that we send it as a string since integers |
67 // might overflow). | 81 // might overflow). |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 void OnRendererReady(const Value* value); | 207 void OnRendererReady(const Value* value); |
194 | 208 |
195 void OnGetProxySettings(const Value* value); | 209 void OnGetProxySettings(const Value* value); |
196 void OnReloadProxySettings(const Value* value); | 210 void OnReloadProxySettings(const Value* value); |
197 void OnGetBadProxies(const Value* value); | 211 void OnGetBadProxies(const Value* value); |
198 void OnClearBadProxies(const Value* value); | 212 void OnClearBadProxies(const Value* value); |
199 void OnGetHostResolverCache(const Value* value); | 213 void OnGetHostResolverCache(const Value* value); |
200 void OnClearHostResolverCache(const Value* value); | 214 void OnClearHostResolverCache(const Value* value); |
201 void OnGetPassiveLogEntries(const Value* value); | 215 void OnGetPassiveLogEntries(const Value* value); |
202 void OnStartConnectionTests(const Value* value); | 216 void OnStartConnectionTests(const Value* value); |
| 217 void OnGetHttpCacheInfo(const Value* value); |
203 | 218 |
204 // ChromeNetLog::Observer implementation: | 219 // ChromeNetLog::Observer implementation: |
205 virtual void OnAddEntry(net::NetLog::EventType type, | 220 virtual void OnAddEntry(net::NetLog::EventType type, |
206 const base::TimeTicks& time, | 221 const base::TimeTicks& time, |
207 const net::NetLog::Source& source, | 222 const net::NetLog::Source& source, |
208 net::NetLog::EventPhase phase, | 223 net::NetLog::EventPhase phase, |
209 net::NetLog::EventParameters* extra_parameters); | 224 net::NetLog::EventParameters* extra_parameters); |
210 | 225 |
211 // ConnectionTester::Delegate implementation: | 226 // ConnectionTester::Delegate implementation: |
212 virtual void OnStartConnectionTestSuite(); | 227 virtual void OnStartConnectionTestSuite(); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 proxy_->CreateCallback(&IOThreadImpl::OnGetHostResolverCache)); | 388 proxy_->CreateCallback(&IOThreadImpl::OnGetHostResolverCache)); |
374 dom_ui_->RegisterMessageCallback( | 389 dom_ui_->RegisterMessageCallback( |
375 "clearHostResolverCache", | 390 "clearHostResolverCache", |
376 proxy_->CreateCallback(&IOThreadImpl::OnClearHostResolverCache)); | 391 proxy_->CreateCallback(&IOThreadImpl::OnClearHostResolverCache)); |
377 dom_ui_->RegisterMessageCallback( | 392 dom_ui_->RegisterMessageCallback( |
378 "getPassiveLogEntries", | 393 "getPassiveLogEntries", |
379 proxy_->CreateCallback(&IOThreadImpl::OnGetPassiveLogEntries)); | 394 proxy_->CreateCallback(&IOThreadImpl::OnGetPassiveLogEntries)); |
380 dom_ui_->RegisterMessageCallback( | 395 dom_ui_->RegisterMessageCallback( |
381 "startConnectionTests", | 396 "startConnectionTests", |
382 proxy_->CreateCallback(&IOThreadImpl::OnStartConnectionTests)); | 397 proxy_->CreateCallback(&IOThreadImpl::OnStartConnectionTests)); |
| 398 dom_ui_->RegisterMessageCallback( |
| 399 "getHttpCacheInfo", |
| 400 proxy_->CreateCallback(&IOThreadImpl::OnGetHttpCacheInfo)); |
383 } | 401 } |
384 | 402 |
385 void NetInternalsMessageHandler::CallJavascriptFunction( | 403 void NetInternalsMessageHandler::CallJavascriptFunction( |
386 const std::wstring& function_name, | 404 const std::wstring& function_name, |
387 const Value* value) { | 405 const Value* value) { |
388 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 406 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
389 if (value) { | 407 if (value) { |
390 dom_ui_->CallJavascriptFunction(function_name, *value); | 408 dom_ui_->CallJavascriptFunction(function_name, *value); |
391 } else { | 409 } else { |
392 dom_ui_->CallJavascriptFunction(function_name); | 410 dom_ui_->CallJavascriptFunction(function_name); |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 const ListValue* list = static_cast<const ListValue*>(value); | 696 const ListValue* list = static_cast<const ListValue*>(value); |
679 list->GetStringAsUTF16(0, &url_str); | 697 list->GetStringAsUTF16(0, &url_str); |
680 } | 698 } |
681 | 699 |
682 GURL url(url_str); | 700 GURL url(url_str); |
683 | 701 |
684 connection_tester_.reset(new ConnectionTester(this)); | 702 connection_tester_.reset(new ConnectionTester(this)); |
685 connection_tester_->RunAllTests(url); | 703 connection_tester_->RunAllTests(url); |
686 } | 704 } |
687 | 705 |
| 706 void NetInternalsMessageHandler::IOThreadImpl::OnGetHttpCacheInfo( |
| 707 const Value* value) { |
| 708 DictionaryValue* info_dict = new DictionaryValue(); |
| 709 ListValue* entry_keys = new ListValue(); |
| 710 DictionaryValue* stats_dict = new DictionaryValue(); |
| 711 |
| 712 disk_cache::Backend* disk_cache = GetDiskCacheBackend( |
| 713 context_getter_->GetURLRequestContext()); |
| 714 |
| 715 if (disk_cache) { |
| 716 // Iterate over all the entries in the cache, to discover the keys. |
| 717 // WARNING: this can be slow! (and will block the IO thread). |
| 718 void* iter = NULL; |
| 719 disk_cache::Entry* entry; |
| 720 while (disk_cache->OpenNextEntry(&iter, &entry)) { |
| 721 entry_keys->Append(Value::CreateStringValue(entry->GetKey())); |
| 722 entry->Close(); |
| 723 } |
| 724 |
| 725 // Extract the statistics key/value pairs from the backend. |
| 726 std::vector<std::pair<std::string, std::string> > stats; |
| 727 disk_cache->GetStats(&stats); |
| 728 for (size_t i = 0; i < stats.size(); ++i) { |
| 729 stats_dict->Set(ASCIIToWide(stats[i].first), |
| 730 Value::CreateStringValue(stats[i].second)); |
| 731 } |
| 732 } |
| 733 |
| 734 info_dict->Set(L"keys", entry_keys); |
| 735 info_dict->Set(L"stats", stats_dict); |
| 736 |
| 737 CallJavascriptFunction(L"g_browser.receivedHttpCacheInfo", info_dict); |
| 738 } |
| 739 |
688 void NetInternalsMessageHandler::IOThreadImpl::OnAddEntry( | 740 void NetInternalsMessageHandler::IOThreadImpl::OnAddEntry( |
689 net::NetLog::EventType type, | 741 net::NetLog::EventType type, |
690 const base::TimeTicks& time, | 742 const base::TimeTicks& time, |
691 const net::NetLog::Source& source, | 743 const net::NetLog::Source& source, |
692 net::NetLog::EventPhase phase, | 744 net::NetLog::EventPhase phase, |
693 net::NetLog::EventParameters* extra_parameters) { | 745 net::NetLog::EventParameters* extra_parameters) { |
694 DCHECK(is_observing_log_); | 746 DCHECK(is_observing_log_); |
695 | 747 |
696 CallJavascriptFunction( | 748 CallJavascriptFunction( |
697 L"g_browser.receivedLogEntry", | 749 L"g_browser.receivedLogEntry", |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource(); | 832 NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource(); |
781 | 833 |
782 // Set up the chrome://net-internals/ source. | 834 // Set up the chrome://net-internals/ source. |
783 ChromeThread::PostTask( | 835 ChromeThread::PostTask( |
784 ChromeThread::IO, FROM_HERE, | 836 ChromeThread::IO, FROM_HERE, |
785 NewRunnableMethod( | 837 NewRunnableMethod( |
786 Singleton<ChromeURLDataManager>::get(), | 838 Singleton<ChromeURLDataManager>::get(), |
787 &ChromeURLDataManager::AddDataSource, | 839 &ChromeURLDataManager::AddDataSource, |
788 make_scoped_refptr(html_source))); | 840 make_scoped_refptr(html_source))); |
789 } | 841 } |
OLD | NEW |