| 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/net/load_timing_observer.h" | 5 #include "chrome/browser/net/load_timing_observer.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "chrome/browser/net/chrome_net_log.h" | 9 #include "chrome/browser/net/chrome_net_log.h" |
| 10 #include "chrome/common/resource_response.h" | 10 #include "chrome/common/resource_response.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 tick_to_time_offset); | 37 tick_to_time_offset); |
| 38 } | 38 } |
| 39 | 39 |
| 40 static int32 TimeTicksToOffset( | 40 static int32 TimeTicksToOffset( |
| 41 const TimeTicks& time_ticks, | 41 const TimeTicks& time_ticks, |
| 42 LoadTimingObserver::URLRequestRecord* record) { | 42 LoadTimingObserver::URLRequestRecord* record) { |
| 43 return static_cast<int32>( | 43 return static_cast<int32>( |
| 44 (time_ticks - record->base_ticks).InMillisecondsRoundedUp()); | 44 (time_ticks - record->base_ticks).InMillisecondsRoundedUp()); |
| 45 } | 45 } |
| 46 | 46 |
| 47 } | 47 } // namespace |
| 48 | 48 |
| 49 LoadTimingObserver::URLRequestRecord::URLRequestRecord() | 49 LoadTimingObserver::URLRequestRecord::URLRequestRecord() |
| 50 : connect_job_id(net::NetLog::Source::kInvalidId), | 50 : connect_job_id(net::NetLog::Source::kInvalidId), |
| 51 socket_log_id(net::NetLog::Source::kInvalidId), | 51 socket_log_id(net::NetLog::Source::kInvalidId), |
| 52 socket_reused(false) { | 52 socket_reused(false) { |
| 53 } | 53 } |
| 54 | 54 |
| 55 LoadTimingObserver::LoadTimingObserver() | 55 LoadTimingObserver::LoadTimingObserver() |
| 56 : Observer(net::NetLog::LOG_BASIC), | 56 : ThreadSafeObserver(net::NetLog::LOG_BASIC), |
| 57 last_connect_job_id_(net::NetLog::Source::kInvalidId) { | 57 last_connect_job_id_(net::NetLog::Source::kInvalidId) { |
| 58 } | 58 } |
| 59 | 59 |
| 60 LoadTimingObserver::~LoadTimingObserver() { | 60 LoadTimingObserver::~LoadTimingObserver() { |
| 61 } | 61 } |
| 62 | 62 |
| 63 LoadTimingObserver::URLRequestRecord* | 63 LoadTimingObserver::URLRequestRecord* |
| 64 LoadTimingObserver::GetURLRequestRecord(uint32 source_id) { | 64 LoadTimingObserver::GetURLRequestRecord(uint32 source_id) { |
| 65 URLRequestToRecordMap::iterator it = url_request_to_record_.find(source_id); | 65 URLRequestToRecordMap::iterator it = url_request_to_record_.find(source_id); |
| 66 if (it != url_request_to_record_.end()) | 66 if (it != url_request_to_record_.end()) |
| 67 return &it->second; | 67 return &it->second; |
| 68 return NULL; | 68 return NULL; |
| 69 } | 69 } |
| 70 | 70 |
| 71 void LoadTimingObserver::OnAddEntry(net::NetLog::EventType type, | 71 void LoadTimingObserver::OnAddEntry(net::NetLog::EventType type, |
| 72 const base::TimeTicks& time, | 72 const base::TimeTicks& time, |
| 73 const net::NetLog::Source& source, | 73 const net::NetLog::Source& source, |
| 74 net::NetLog::EventPhase phase, | 74 net::NetLog::EventPhase phase, |
| 75 net::NetLog::EventParameters* params) { | 75 net::NetLog::EventParameters* params) { |
| 76 // The events that the Observer is interested in only occur on the IO thread. |
| 77 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) |
| 78 return; |
| 76 if (source.type == net::NetLog::SOURCE_URL_REQUEST) | 79 if (source.type == net::NetLog::SOURCE_URL_REQUEST) |
| 77 OnAddURLRequestEntry(type, time, source, phase, params); | 80 OnAddURLRequestEntry(type, time, source, phase, params); |
| 78 else if (source.type == net::NetLog::SOURCE_CONNECT_JOB) | 81 else if (source.type == net::NetLog::SOURCE_CONNECT_JOB) |
| 79 OnAddConnectJobEntry(type, time, source, phase, params); | 82 OnAddConnectJobEntry(type, time, source, phase, params); |
| 80 else if (source.type == net::NetLog::SOURCE_SOCKET) | 83 else if (source.type == net::NetLog::SOURCE_SOCKET) |
| 81 OnAddSocketEntry(type, time, source, phase, params); | 84 OnAddSocketEntry(type, time, source, phase, params); |
| 82 } | 85 } |
| 83 | 86 |
| 84 // static | 87 // static |
| 85 void LoadTimingObserver::PopulateTimingInfo(URLRequest* request, | 88 void LoadTimingObserver::PopulateTimingInfo(URLRequest* request, |
| 86 ResourceResponse* response) { | 89 ResourceResponse* response) { |
| 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 87 if (!(request->load_flags() & net::LOAD_ENABLE_LOAD_TIMING)) | 91 if (!(request->load_flags() & net::LOAD_ENABLE_LOAD_TIMING)) |
| 88 return; | 92 return; |
| 89 | 93 |
| 90 ChromeNetLog* chrome_net_log = static_cast<ChromeNetLog*>( | 94 ChromeNetLog* chrome_net_log = static_cast<ChromeNetLog*>( |
| 91 request->net_log().net_log()); | 95 request->net_log().net_log()); |
| 92 if (chrome_net_log == NULL) | 96 if (chrome_net_log == NULL) |
| 93 return; | 97 return; |
| 94 | 98 |
| 95 uint32 source_id = request->net_log().source().id; | 99 uint32 source_id = request->net_log().source().id; |
| 96 LoadTimingObserver* observer = chrome_net_log->load_timing_observer(); | 100 LoadTimingObserver* observer = chrome_net_log->load_timing_observer(); |
| 97 LoadTimingObserver::URLRequestRecord* record = | 101 LoadTimingObserver::URLRequestRecord* record = |
| 98 observer->GetURLRequestRecord(source_id); | 102 observer->GetURLRequestRecord(source_id); |
| 99 if (record) { | 103 if (record) { |
| 100 response->response_head.connection_id = record->socket_log_id; | 104 response->response_head.connection_id = record->socket_log_id; |
| 101 response->response_head.connection_reused = record->socket_reused; | 105 response->response_head.connection_reused = record->socket_reused; |
| 102 response->response_head.load_timing = record->timing; | 106 response->response_head.load_timing = record->timing; |
| 103 } | 107 } |
| 104 } | 108 } |
| 105 | 109 |
| 106 void LoadTimingObserver::OnAddURLRequestEntry( | 110 void LoadTimingObserver::OnAddURLRequestEntry( |
| 107 net::NetLog::EventType type, | 111 net::NetLog::EventType type, |
| 108 const base::TimeTicks& time, | 112 const base::TimeTicks& time, |
| 109 const net::NetLog::Source& source, | 113 const net::NetLog::Source& source, |
| 110 net::NetLog::EventPhase phase, | 114 net::NetLog::EventPhase phase, |
| 111 net::NetLog::EventParameters* params) { | 115 net::NetLog::EventParameters* params) { |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 117 |
| 112 bool is_begin = phase == net::NetLog::PHASE_BEGIN; | 118 bool is_begin = phase == net::NetLog::PHASE_BEGIN; |
| 113 bool is_end = phase == net::NetLog::PHASE_END; | 119 bool is_end = phase == net::NetLog::PHASE_END; |
| 114 | 120 |
| 115 if (type == net::NetLog::TYPE_URL_REQUEST_START_JOB) { | 121 if (type == net::NetLog::TYPE_URL_REQUEST_START_JOB) { |
| 116 if (is_begin) { | 122 if (is_begin) { |
| 117 // Only record timing for entries with corresponding flag. | 123 // Only record timing for entries with corresponding flag. |
| 118 int load_flags = static_cast<URLRequestStartEventParameters*>(params)-> | 124 int load_flags = static_cast<URLRequestStartEventParameters*>(params)-> |
| 119 load_flags(); | 125 load_flags(); |
| 120 if (!(load_flags & net::LOAD_ENABLE_LOAD_TIMING)) | 126 if (!(load_flags & net::LOAD_ENABLE_LOAD_TIMING)) |
| 121 return; | 127 return; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 break; | 209 break; |
| 204 } | 210 } |
| 205 } | 211 } |
| 206 | 212 |
| 207 void LoadTimingObserver::OnAddConnectJobEntry( | 213 void LoadTimingObserver::OnAddConnectJobEntry( |
| 208 net::NetLog::EventType type, | 214 net::NetLog::EventType type, |
| 209 const base::TimeTicks& time, | 215 const base::TimeTicks& time, |
| 210 const net::NetLog::Source& source, | 216 const net::NetLog::Source& source, |
| 211 net::NetLog::EventPhase phase, | 217 net::NetLog::EventPhase phase, |
| 212 net::NetLog::EventParameters* params) { | 218 net::NetLog::EventParameters* params) { |
| 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 220 |
| 213 bool is_begin = phase == net::NetLog::PHASE_BEGIN; | 221 bool is_begin = phase == net::NetLog::PHASE_BEGIN; |
| 214 bool is_end = phase == net::NetLog::PHASE_END; | 222 bool is_end = phase == net::NetLog::PHASE_END; |
| 215 | 223 |
| 216 // Manage record lifetime based on the SOCKET_POOL_CONNECT_JOB entry. | 224 // Manage record lifetime based on the SOCKET_POOL_CONNECT_JOB entry. |
| 217 if (type == net::NetLog::TYPE_SOCKET_POOL_CONNECT_JOB) { | 225 if (type == net::NetLog::TYPE_SOCKET_POOL_CONNECT_JOB) { |
| 218 if (is_begin) { | 226 if (is_begin) { |
| 219 // Prevents us from passively growing the memory memory unbounded in case | 227 // Prevents us from passively growing the memory memory unbounded in case |
| 220 // something went wrong. Should not happen. | 228 // something went wrong. Should not happen. |
| 221 if (connect_job_to_record_.size() > kMaxNumEntries) { | 229 if (connect_job_to_record_.size() > kMaxNumEntries) { |
| 222 LOG(WARNING) << "The load timing observer connect job count has grown " | 230 LOG(WARNING) << "The load timing observer connect job count has grown " |
| (...skipping 23 matching lines...) Expand all Loading... |
| 246 } | 254 } |
| 247 } | 255 } |
| 248 } | 256 } |
| 249 | 257 |
| 250 void LoadTimingObserver::OnAddSocketEntry( | 258 void LoadTimingObserver::OnAddSocketEntry( |
| 251 net::NetLog::EventType type, | 259 net::NetLog::EventType type, |
| 252 const base::TimeTicks& time, | 260 const base::TimeTicks& time, |
| 253 const net::NetLog::Source& source, | 261 const net::NetLog::Source& source, |
| 254 net::NetLog::EventPhase phase, | 262 net::NetLog::EventPhase phase, |
| 255 net::NetLog::EventParameters* params) { | 263 net::NetLog::EventParameters* params) { |
| 264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 265 |
| 256 bool is_begin = phase == net::NetLog::PHASE_BEGIN; | 266 bool is_begin = phase == net::NetLog::PHASE_BEGIN; |
| 257 bool is_end = phase == net::NetLog::PHASE_END; | 267 bool is_end = phase == net::NetLog::PHASE_END; |
| 258 | 268 |
| 259 // Manage record lifetime based on the SOCKET_ALIVE entry. | 269 // Manage record lifetime based on the SOCKET_ALIVE entry. |
| 260 if (type == net::NetLog::TYPE_SOCKET_ALIVE) { | 270 if (type == net::NetLog::TYPE_SOCKET_ALIVE) { |
| 261 if (is_begin) { | 271 if (is_begin) { |
| 262 // Prevents us from passively growing the memory memory unbounded in case | 272 // Prevents us from passively growing the memory memory unbounded in case |
| 263 // something went wrong. Should not happen. | 273 // something went wrong. Should not happen. |
| 264 if (socket_to_record_.size() > kMaxNumEntries) { | 274 if (socket_to_record_.size() > kMaxNumEntries) { |
| 265 LOG(WARNING) << "The load timing observer socket count has grown " | 275 LOG(WARNING) << "The load timing observer socket count has grown " |
| (...skipping 12 matching lines...) Expand all Loading... |
| 278 if (it == socket_to_record_.end()) | 288 if (it == socket_to_record_.end()) |
| 279 return; | 289 return; |
| 280 | 290 |
| 281 if (type == net::NetLog::TYPE_SSL_CONNECT) { | 291 if (type == net::NetLog::TYPE_SSL_CONNECT) { |
| 282 if (is_begin) | 292 if (is_begin) |
| 283 it->second.ssl_start = time; | 293 it->second.ssl_start = time; |
| 284 else if (is_end) | 294 else if (is_end) |
| 285 it->second.ssl_end = time; | 295 it->second.ssl_end = time; |
| 286 } | 296 } |
| 287 } | 297 } |
| OLD | NEW |