Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Side by Side Diff: chrome/browser/dom_ui/net_internals_ui.cc

Issue 4118004: Update NetLog to be thread safe. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Response to comments Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 private: 145 private:
146 class IOThreadImpl; 146 class IOThreadImpl;
147 147
148 // This is the "real" message handler, which lives on the IO thread. 148 // This is the "real" message handler, which lives on the IO thread.
149 scoped_refptr<IOThreadImpl> proxy_; 149 scoped_refptr<IOThreadImpl> proxy_;
150 150
151 DISALLOW_COPY_AND_ASSIGN(NetInternalsMessageHandler); 151 DISALLOW_COPY_AND_ASSIGN(NetInternalsMessageHandler);
152 }; 152 };
153 153
154 // This class is the "real" message handler. With the exception of being 154 // This class is the "real" message handler. With the exception of being
155 // allocated and destroyed on the UI thread, its methods are expected to be 155 // allocated and destroyed on the UI thread, its methods are expected to be
eroman 2010/11/17 05:59:02 Could you updating this language to mention the On
156 // called from the IO thread. 156 // called from the IO thread.
157 class NetInternalsMessageHandler::IOThreadImpl 157 class NetInternalsMessageHandler::IOThreadImpl
158 : public base::RefCountedThreadSafe< 158 : public base::RefCountedThreadSafe<
159 NetInternalsMessageHandler::IOThreadImpl, 159 NetInternalsMessageHandler::IOThreadImpl,
160 BrowserThread::DeleteOnUIThread>, 160 BrowserThread::DeleteOnUIThread>,
161 public ChromeNetLog::Observer, 161 public ChromeNetLog::Observer,
162 public ConnectionTester::Delegate { 162 public ConnectionTester::Delegate {
163 public: 163 public:
164 // Type for methods that can be used as MessageHandler callbacks. 164 // Type for methods that can be used as MessageHandler callbacks.
165 typedef void (IOThreadImpl::*MessageHandler)(const ListValue*); 165 typedef void (IOThreadImpl::*MessageHandler)(const ListValue*);
(...skipping 13 matching lines...) Expand all
179 // Creates a callback that will run |method| on the IO thread. 179 // Creates a callback that will run |method| on the IO thread.
180 // 180 //
181 // This can be used with DOMUI::RegisterMessageCallback() to bind to a method 181 // This can be used with DOMUI::RegisterMessageCallback() to bind to a method
182 // on the IO thread. 182 // on the IO thread.
183 DOMUI::MessageCallback* CreateCallback(MessageHandler method); 183 DOMUI::MessageCallback* CreateCallback(MessageHandler method);
184 184
185 // Called once the DOMUI has been deleted (i.e. renderer went away), on the 185 // Called once the DOMUI has been deleted (i.e. renderer went away), on the
186 // IO thread. 186 // IO thread.
187 void Detach(); 187 void Detach();
188 188
189 // Sends all passive log entries in |entries| to Javascript handler.
eroman 2010/11/17 05:59:02 nit: "to the javascript handler" (added "the").
190 void SendPassiveLogEntries(const ChromeNetLog::EntryList& passive_entries);
191
189 //-------------------------------- 192 //--------------------------------
190 // Javascript message handlers: 193 // Javascript message handlers:
191 //-------------------------------- 194 //--------------------------------
192 195
193 // This message is called after the webpage's onloaded handler has fired.
194 // it indicates that the renderer is ready to start receiving captured data.
195 void OnRendererReady(const ListValue* list); 196 void OnRendererReady(const ListValue* list);
196 197
197 void OnGetProxySettings(const ListValue* list); 198 void OnGetProxySettings(const ListValue* list);
198 void OnReloadProxySettings(const ListValue* list); 199 void OnReloadProxySettings(const ListValue* list);
199 void OnGetBadProxies(const ListValue* list); 200 void OnGetBadProxies(const ListValue* list);
200 void OnClearBadProxies(const ListValue* list); 201 void OnClearBadProxies(const ListValue* list);
201 void OnGetHostResolverInfo(const ListValue* list); 202 void OnGetHostResolverInfo(const ListValue* list);
202 void OnClearHostResolverCache(const ListValue* list); 203 void OnClearHostResolverCache(const ListValue* list);
203 void OnEnableIPv6(const ListValue* list); 204 void OnEnableIPv6(const ListValue* list);
204 void OnGetPassiveLogEntries(const ListValue* list);
205 void OnStartConnectionTests(const ListValue* list); 205 void OnStartConnectionTests(const ListValue* list);
206 void OnGetHttpCacheInfo(const ListValue* list); 206 void OnGetHttpCacheInfo(const ListValue* list);
207 void OnGetSocketPoolInfo(const ListValue* list); 207 void OnGetSocketPoolInfo(const ListValue* list);
208 void OnGetSpdySessionInfo(const ListValue* list); 208 void OnGetSpdySessionInfo(const ListValue* list);
209 #ifdef OS_WIN 209 #ifdef OS_WIN
210 void OnGetServiceProviders(const ListValue* list); 210 void OnGetServiceProviders(const ListValue* list);
211 #endif 211 #endif
212 212
213 void OnSetLogLevel(const ListValue* list); 213 void OnSetLogLevel(const ListValue* list);
214 214
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 dom_ui_->RegisterMessageCallback( 379 dom_ui_->RegisterMessageCallback(
380 "getHostResolverInfo", 380 "getHostResolverInfo",
381 proxy_->CreateCallback(&IOThreadImpl::OnGetHostResolverInfo)); 381 proxy_->CreateCallback(&IOThreadImpl::OnGetHostResolverInfo));
382 dom_ui_->RegisterMessageCallback( 382 dom_ui_->RegisterMessageCallback(
383 "clearHostResolverCache", 383 "clearHostResolverCache",
384 proxy_->CreateCallback(&IOThreadImpl::OnClearHostResolverCache)); 384 proxy_->CreateCallback(&IOThreadImpl::OnClearHostResolverCache));
385 dom_ui_->RegisterMessageCallback( 385 dom_ui_->RegisterMessageCallback(
386 "enableIPv6", 386 "enableIPv6",
387 proxy_->CreateCallback(&IOThreadImpl::OnEnableIPv6)); 387 proxy_->CreateCallback(&IOThreadImpl::OnEnableIPv6));
388 dom_ui_->RegisterMessageCallback( 388 dom_ui_->RegisterMessageCallback(
389 "getPassiveLogEntries",
390 proxy_->CreateCallback(&IOThreadImpl::OnGetPassiveLogEntries));
391 dom_ui_->RegisterMessageCallback(
392 "startConnectionTests", 389 "startConnectionTests",
393 proxy_->CreateCallback(&IOThreadImpl::OnStartConnectionTests)); 390 proxy_->CreateCallback(&IOThreadImpl::OnStartConnectionTests));
394 dom_ui_->RegisterMessageCallback( 391 dom_ui_->RegisterMessageCallback(
395 "getHttpCacheInfo", 392 "getHttpCacheInfo",
396 proxy_->CreateCallback(&IOThreadImpl::OnGetHttpCacheInfo)); 393 proxy_->CreateCallback(&IOThreadImpl::OnGetHttpCacheInfo));
397 dom_ui_->RegisterMessageCallback( 394 dom_ui_->RegisterMessageCallback(
398 "getSocketPoolInfo", 395 "getSocketPoolInfo",
399 proxy_->CreateCallback(&IOThreadImpl::OnGetSocketPoolInfo)); 396 proxy_->CreateCallback(&IOThreadImpl::OnGetSocketPoolInfo));
400 dom_ui_->RegisterMessageCallback( 397 dom_ui_->RegisterMessageCallback(
401 "getSpdySessionInfo", 398 "getSpdySessionInfo",
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 NetInternalsMessageHandler::IOThreadImpl::CreateCallback( 445 NetInternalsMessageHandler::IOThreadImpl::CreateCallback(
449 MessageHandler method) { 446 MessageHandler method) {
450 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 447 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
451 return new CallbackHelper(this, method); 448 return new CallbackHelper(this, method);
452 } 449 }
453 450
454 void NetInternalsMessageHandler::IOThreadImpl::Detach() { 451 void NetInternalsMessageHandler::IOThreadImpl::Detach() {
455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 452 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
456 // Unregister with network stack to observe events. 453 // Unregister with network stack to observe events.
457 if (is_observing_log_) 454 if (is_observing_log_)
458 io_thread_->globals()->net_log->RemoveObserver(this); 455 io_thread_->net_log()->RemoveObserver(this);
459 456
460 // Cancel any in-progress connection tests. 457 // Cancel any in-progress connection tests.
461 connection_tester_.reset(); 458 connection_tester_.reset();
462 } 459 }
463 460
464 void NetInternalsMessageHandler::IOThreadImpl::OnRendererReady( 461 void NetInternalsMessageHandler::IOThreadImpl::OnRendererReady(
465 const ListValue* list) { 462 const ListValue* list) {
466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
467 DCHECK(!is_observing_log_) << "notifyReady called twice"; 464 DCHECK(!is_observing_log_) << "notifyReady called twice";
468 465
469 // Register with network stack to observe events.
470 is_observing_log_ = true;
471 io_thread_->globals()->net_log->AddObserver(this);
472
473 // Tell the javascript about the relationship between event type enums and 466 // Tell the javascript about the relationship between event type enums and
474 // their symbolic name. 467 // their symbolic name.
475 { 468 {
476 std::vector<net::NetLog::EventType> event_types = 469 std::vector<net::NetLog::EventType> event_types =
477 net::NetLog::GetAllEventTypes(); 470 net::NetLog::GetAllEventTypes();
478 471
479 DictionaryValue* dict = new DictionaryValue(); 472 DictionaryValue* dict = new DictionaryValue();
480 473
481 for (size_t i = 0; i < event_types.size(); ++i) { 474 for (size_t i = 0; i < event_types.size(); ++i) {
482 const char* name = net::NetLog::EventTypeToString(event_types[i]); 475 const char* name = net::NetLog::EventTypeToString(event_types[i]);
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 // be part of the time library. 603 // be part of the time library.
611 const int64 kUnixEpochMs = 11644473600000LL; 604 const int64 kUnixEpochMs = 11644473600000LL;
612 int64 tick_to_unix_time_ms = tick_to_time_ms - kUnixEpochMs; 605 int64 tick_to_unix_time_ms = tick_to_time_ms - kUnixEpochMs;
613 606
614 // Pass it as a string, since it may be too large to fit in an integer. 607 // Pass it as a string, since it may be too large to fit in an integer.
615 CallJavascriptFunction(L"g_browser.receivedTimeTickOffset", 608 CallJavascriptFunction(L"g_browser.receivedTimeTickOffset",
616 Value::CreateStringValue( 609 Value::CreateStringValue(
617 base::Int64ToString(tick_to_unix_time_ms))); 610 base::Int64ToString(tick_to_unix_time_ms)));
618 } 611 }
619 612
620 OnGetPassiveLogEntries(NULL); 613 // Register with network stack to observe events.
614 is_observing_log_ = true;
615 ChromeNetLog::EntryList entries;
616 io_thread_->net_log()->AddObserverAndGetAllCapturedEvents(this, &entries);
eroman 2010/11/17 05:59:02 The impact of threading here is fairly subtle. I
mmenke 2010/11/17 21:42:14 Thanks for catching that. I hadn't thought about
617 SendPassiveLogEntries(entries);
621 } 618 }
622 619
623 void NetInternalsMessageHandler::IOThreadImpl::OnGetProxySettings( 620 void NetInternalsMessageHandler::IOThreadImpl::OnGetProxySettings(
624 const ListValue* list) { 621 const ListValue* list) {
625 URLRequestContext* context = context_getter_->GetURLRequestContext(); 622 URLRequestContext* context = context_getter_->GetURLRequestContext();
626 net::ProxyService* proxy_service = context->proxy_service(); 623 net::ProxyService* proxy_service = context->proxy_service();
627 624
628 DictionaryValue* dict = new DictionaryValue(); 625 DictionaryValue* dict = new DictionaryValue();
629 if (proxy_service->fetched_config().is_valid()) 626 if (proxy_service->fetched_config().is_valid())
630 dict->Set("original", proxy_service->fetched_config().ToValue()); 627 dict->Set("original", proxy_service->fetched_config().ToValue());
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
767 764
768 if (host_resolver_impl) { 765 if (host_resolver_impl) {
769 host_resolver_impl->SetDefaultAddressFamily( 766 host_resolver_impl->SetDefaultAddressFamily(
770 net::ADDRESS_FAMILY_UNSPECIFIED); 767 net::ADDRESS_FAMILY_UNSPECIFIED);
771 } 768 }
772 769
773 // Cause the renderer to be notified of the new value. 770 // Cause the renderer to be notified of the new value.
774 OnGetHostResolverInfo(NULL); 771 OnGetHostResolverInfo(NULL);
775 } 772 }
776 773
777 void NetInternalsMessageHandler::IOThreadImpl::OnGetPassiveLogEntries( 774 void NetInternalsMessageHandler::IOThreadImpl::SendPassiveLogEntries(
778 const ListValue* list) { 775 const ChromeNetLog::EntryList& passive_entries) {
779 ChromeNetLog* net_log = io_thread_->globals()->net_log.get();
780
781 PassiveLogCollector::EntryList passive_entries;
782 net_log->passive_collector()->GetAllCapturedEvents(&passive_entries);
783
784 ListValue* dict_list = new ListValue(); 776 ListValue* dict_list = new ListValue();
785 for (size_t i = 0; i < passive_entries.size(); ++i) { 777 for (size_t i = 0; i < passive_entries.size(); ++i) {
786 const PassiveLogCollector::Entry& e = passive_entries[i]; 778 const ChromeNetLog::Entry& e = passive_entries[i];
787 dict_list->Append(net::NetLog::EntryToDictionaryValue(e.type, 779 dict_list->Append(net::NetLog::EntryToDictionaryValue(e.type,
788 e.time, 780 e.time,
789 e.source, 781 e.source,
790 e.phase, 782 e.phase,
791 e.params, 783 e.params,
792 false)); 784 false));
793 } 785 }
794 786
795 CallJavascriptFunction(L"g_browser.receivedPassiveLogEntries", dict_list); 787 CallJavascriptFunction(L"g_browser.receivedPassiveLogEntries", dict_list);
796 } 788 }
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 int log_level; 896 int log_level;
905 std::string log_level_string; 897 std::string log_level_string;
906 if (!list->GetString(0, &log_level_string) || 898 if (!list->GetString(0, &log_level_string) ||
907 !base::StringToInt(log_level_string, &log_level)) { 899 !base::StringToInt(log_level_string, &log_level)) {
908 NOTREACHED(); 900 NOTREACHED();
909 return; 901 return;
910 } 902 }
911 903
912 DCHECK_GE(log_level, net::NetLog::LOG_ALL); 904 DCHECK_GE(log_level, net::NetLog::LOG_ALL);
913 DCHECK_LE(log_level, net::NetLog::LOG_BASIC); 905 DCHECK_LE(log_level, net::NetLog::LOG_BASIC);
914 set_log_level(static_cast<net::NetLog::LogLevel>(log_level)); 906 SetLogLevel(static_cast<net::NetLog::LogLevel>(log_level),
907 io_thread_->net_log());
eroman 2010/11/17 05:59:02 indentation is off by one.
915 } 908 }
916 909
917 void NetInternalsMessageHandler::IOThreadImpl::OnAddEntry( 910 void NetInternalsMessageHandler::IOThreadImpl::OnAddEntry(
918 net::NetLog::EventType type, 911 net::NetLog::EventType type,
919 const base::TimeTicks& time, 912 const base::TimeTicks& time,
920 const net::NetLog::Source& source, 913 const net::NetLog::Source& source,
921 net::NetLog::EventPhase phase, 914 net::NetLog::EventPhase phase,
922 net::NetLog::EventParameters* params) { 915 net::NetLog::EventParameters* params) {
eroman 2010/11/17 05:59:02 Could you add a big comment in this function notin
923 DCHECK(is_observing_log_); 916 DCHECK(is_observing_log_);
eroman 2010/11/17 05:59:02 I wander if we need a memory barrier or something
mmenke 2010/11/17 21:42:14 I don't think we do, since we set it before adding
924 917
925 CallJavascriptFunction( 918 CallJavascriptFunction(
926 L"g_browser.receivedLogEntry", 919 L"g_browser.receivedLogEntry",
927 net::NetLog::EntryToDictionaryValue(type, time, source, phase, params, 920 net::NetLog::EntryToDictionaryValue(type, time, source, phase, params,
928 false)); 921 false));
929 } 922 }
930 923
931 void NetInternalsMessageHandler::IOThreadImpl::OnStartConnectionTestSuite() { 924 void NetInternalsMessageHandler::IOThreadImpl::OnStartConnectionTestSuite() {
932 CallJavascriptFunction(L"g_browser.receivedStartConnectionTestSuite", NULL); 925 CallJavascriptFunction(L"g_browser.receivedStartConnectionTestSuite", NULL);
933 } 926 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { 966 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
974 if (handler_) { 967 if (handler_) {
975 // We check |handler_| in case it was deleted on the UI thread earlier 968 // We check |handler_| in case it was deleted on the UI thread earlier
976 // while we were running on the IO thread. 969 // while we were running on the IO thread.
977 handler_->CallJavascriptFunction(function_name, arg); 970 handler_->CallJavascriptFunction(function_name, arg);
978 } 971 }
979 delete arg; 972 delete arg;
980 return; 973 return;
981 } 974 }
982 975
983 // Otherwise if we were called from the IO thread, bridge the request over to
984 // the UI thread.
985
986 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
987 if (!BrowserThread::PostTask( 976 if (!BrowserThread::PostTask(
988 BrowserThread::UI, FROM_HERE, 977 BrowserThread::UI, FROM_HERE,
989 NewRunnableMethod( 978 NewRunnableMethod(
990 this, 979 this,
991 &IOThreadImpl::CallJavascriptFunction, 980 &IOThreadImpl::CallJavascriptFunction,
992 function_name, arg))) { 981 function_name, arg))) {
993 // Failed posting the task, avoid leaking. 982 // Failed posting the task, avoid leaking.
994 delete arg; 983 delete arg;
995 } 984 }
996 } 985 }
(...skipping 13 matching lines...) Expand all
1010 NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource(); 999 NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource();
1011 1000
1012 // Set up the chrome://net-internals/ source. 1001 // Set up the chrome://net-internals/ source.
1013 BrowserThread::PostTask( 1002 BrowserThread::PostTask(
1014 BrowserThread::IO, FROM_HERE, 1003 BrowserThread::IO, FROM_HERE,
1015 NewRunnableMethod( 1004 NewRunnableMethod(
1016 Singleton<ChromeURLDataManager>::get(), 1005 Singleton<ChromeURLDataManager>::get(),
1017 &ChromeURLDataManager::AddDataSource, 1006 &ChromeURLDataManager::AddDataSource,
1018 make_scoped_refptr(html_source))); 1007 make_scoped_refptr(html_source)));
1019 } 1008 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698