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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_NET_LOAD_TIMING_OBSERVER_H_ 6 #define CHROME_BROWSER_NET_LOAD_TIMING_OBSERVER_H_ 7 #pragma once 8 9 #include "base/gtest_prod_util.h" 10 #include "base/hash_tables.h" 11 #include "base/time.h" 12 #include "chrome/browser/net/chrome_net_log.h" 13 #include "net/base/net_log.h" 14 #include "webkit/glue/resource_loader_bridge.h" 15 16 namespace net { 17 class URLRequest; 18 } // namespace net 19 20 struct ResourceResponse; 21 22 // LoadTimingObserver watches the NetLog event stream and collects the network 23 // timing information. 24 // 25 // LoadTimingObserver lives completely on the IOThread and ignores events from 26 // other threads. It is not safe to use from other threads. 27 class LoadTimingObserver : public ChromeNetLog::ThreadSafeObserver { 28 public: 29 struct URLRequestRecord { 30 URLRequestRecord(); 31 32 webkit_glue::ResourceLoadTimingInfo timing; 33 uint32 connect_job_id; 34 uint32 socket_log_id; 35 bool socket_reused; 36 base::TimeTicks base_ticks; 37 }; 38 39 struct HTTPStreamJobRecord { 40 HTTPStreamJobRecord(); 41 42 uint32 socket_log_id; 43 bool socket_reused; 44 base::TimeTicks connect_start; 45 base::TimeTicks connect_end; 46 base::TimeTicks dns_start; 47 base::TimeTicks dns_end; 48 base::TimeTicks ssl_start; 49 base::TimeTicks ssl_end; 50 }; 51 52 struct ConnectJobRecord { 53 base::TimeTicks dns_start; 54 base::TimeTicks dns_end; 55 }; 56 57 struct SocketRecord { 58 base::TimeTicks ssl_start; 59 base::TimeTicks ssl_end; 60 }; 61 62 LoadTimingObserver(); 63 ~LoadTimingObserver(); 64 65 URLRequestRecord* GetURLRequestRecord(uint32 source_id); 66 67 // ThreadSafeObserver implementation: 68 virtual void OnAddEntry(net::NetLog::EventType type, 69 const base::TimeTicks& time, 70 const net::NetLog::Source& source, 71 net::NetLog::EventPhase phase, 72 net::NetLog::EventParameters* params); 73 74 static void PopulateTimingInfo(net::URLRequest* request, 75 ResourceResponse* response); 76 77 private: 78 FRIEND_TEST_ALL_PREFIXES(LoadTimingObserverTest, 79 HTTPStreamJobRecord); 80 FRIEND_TEST_ALL_PREFIXES(LoadTimingObserverTest, 81 ConnectJobRecord); 82 FRIEND_TEST_ALL_PREFIXES(LoadTimingObserverTest, 83 SocketRecord); 84 85 void OnAddURLRequestEntry(net::NetLog::EventType type, 86 const base::TimeTicks& time, 87 const net::NetLog::Source& source, 88 net::NetLog::EventPhase phase, 89 net::NetLog::EventParameters* params); 90 91 void OnAddHTTPStreamJobEntry(net::NetLog::EventType type, 92 const base::TimeTicks& time, 93 const net::NetLog::Source& source, 94 net::NetLog::EventPhase phase, 95 net::NetLog::EventParameters* params); 96 97 void OnAddConnectJobEntry(net::NetLog::EventType type, 98 const base::TimeTicks& time, 99 const net::NetLog::Source& source, 100 net::NetLog::EventPhase phase, 101 net::NetLog::EventParameters* params); 102 103 void OnAddSocketEntry(net::NetLog::EventType type, 104 const base::TimeTicks& time, 105 const net::NetLog::Source& source, 106 net::NetLog::EventPhase phase, 107 net::NetLog::EventParameters* params); 108 109 URLRequestRecord* CreateURLRequestRecord(uint32 source_id); 110 void DeleteURLRequestRecord(uint32 source_id); 111 112 typedef base::hash_map<uint32, URLRequestRecord> URLRequestToRecordMap; 113 typedef base::hash_map<uint32, HTTPStreamJobRecord> HTTPStreamJobToRecordMap; 114 typedef base::hash_map<uint32, ConnectJobRecord> ConnectJobToRecordMap; 115 typedef base::hash_map<uint32, SocketRecord> SocketToRecordMap; 116 URLRequestToRecordMap url_request_to_record_; 117 HTTPStreamJobToRecordMap http_stream_job_to_record_; 118 ConnectJobToRecordMap connect_job_to_record_; 119 SocketToRecordMap socket_to_record_; 120 uint32 last_connect_job_id_; 121 ConnectJobRecord last_connect_job_record_; 122 123 DISALLOW_COPY_AND_ASSIGN(LoadTimingObserver); 124 }; 125 126 #endif // CHROME_BROWSER_NET_LOAD_TIMING_OBSERVER_H_ 127