Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/loader/detachable_resource_handler.h" | 5 #include "content/browser/loader/detachable_resource_handler.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/metrics/histogram.h" | |
| 8 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 9 #include "content/browser/loader/resource_request_info_impl.h" | 10 #include "content/browser/loader/resource_request_info_impl.h" |
| 10 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/url_request/url_request_status.h" | 13 #include "net/url_request/url_request_status.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 16 | |
| 15 // This matches the maximum allocation size of AsyncResourceHandler. | 17 // This matches the maximum allocation size of AsyncResourceHandler. |
| 16 const int kReadBufSize = 32 * 1024; | 18 const int kReadBufSize = 32 * 1024; |
| 17 } | 19 |
| 20 // Enum type for <a ping> result histograms. Only add new values to the end. | |
| 21 enum UMAPingResultType { | |
| 22 // The ping request completed successfully. | |
| 23 UMA_PING_RESULT_TYPE_SUCCESS = 0, | |
| 24 // The ping request received a response, but did not consume the entire body. | |
| 25 UMA_PING_RESULT_TYPE_RESPONSE_STARTED = 1, | |
| 26 // The ping request was canceled due to the internal timeout. | |
| 27 UMA_PING_RESULT_TYPE_TIMEDOUT = 2, | |
| 28 // The ping request was canceled for some other reason. | |
| 29 UMA_PING_RESULT_TYPE_CANCELED = 3, | |
| 30 // The ping request failed for some reason. | |
| 31 UMA_PING_RESULT_TYPE_FAILED = 4, | |
| 32 // The ping request was aborted before OnResponseCompleted. | |
| 33 UMA_PING_RESULT_TYPE_UNCOMPLETED = 5, | |
| 34 | |
| 35 UMA_PING_RESULT_TYPE_MAX, | |
| 36 }; | |
| 37 | |
| 38 } // namespace | |
| 18 | 39 |
| 19 namespace content { | 40 namespace content { |
| 20 | 41 |
| 21 DetachableResourceHandler::DetachableResourceHandler( | 42 DetachableResourceHandler::DetachableResourceHandler( |
| 22 net::URLRequest* request, | 43 net::URLRequest* request, |
| 23 base::TimeDelta cancel_delay, | 44 base::TimeDelta cancel_delay, |
| 24 scoped_ptr<ResourceHandler> next_handler) | 45 scoped_ptr<ResourceHandler> next_handler) |
| 25 : ResourceHandler(request), | 46 : ResourceHandler(request), |
| 26 next_handler_(next_handler.Pass()), | 47 next_handler_(next_handler.Pass()), |
| 27 cancel_delay_(cancel_delay), | 48 cancel_delay_(cancel_delay), |
| 28 is_deferred_(false), | 49 is_deferred_(false), |
| 29 is_finished_(false) { | 50 is_finished_(false), |
| 51 timed_out_(false), | |
| 52 response_started_(false), | |
| 53 status_(net::URLRequestStatus::IO_PENDING) { | |
| 30 GetRequestInfo()->set_detachable_handler(this); | 54 GetRequestInfo()->set_detachable_handler(this); |
| 31 } | 55 } |
| 32 | 56 |
| 33 DetachableResourceHandler::~DetachableResourceHandler() { | 57 DetachableResourceHandler::~DetachableResourceHandler() { |
| 34 // Cleanup back-pointer stored on the request info. | 58 // Cleanup back-pointer stored on the request info. |
| 35 GetRequestInfo()->set_detachable_handler(NULL); | 59 GetRequestInfo()->set_detachable_handler(NULL); |
| 60 | |
| 61 // Record the status of <a ping> requests. | |
| 62 // http://crbug.com/302816 | |
| 63 if (GetRequestInfo()->GetResourceType() == ResourceType::PING) { | |
| 64 UMAPingResultType result_type = UMA_PING_RESULT_TYPE_MAX; | |
| 65 | |
| 66 if (status_ == net::URLRequestStatus::SUCCESS) { | |
| 67 result_type = UMA_PING_RESULT_TYPE_SUCCESS; | |
| 68 } else if (response_started_) { | |
| 69 // However the request ended, bucket this under RESPONSE_STARTED because | |
| 70 // OnResponseStarted was received. Note: OnResponseCompleted is also sent | |
| 71 // when a request is canceled before completion, so it is possible to | |
| 72 // receive OnResponseCompleted without OnResponseStarted. | |
| 73 result_type = UMA_PING_RESULT_TYPE_RESPONSE_STARTED; | |
| 74 } else if (status_ == net::URLRequestStatus::IO_PENDING) { | |
| 75 // The request was aborted without OnResponseCompleted before any response | |
|
mmenke
2014/01/28 20:02:47
optional: aborted -> deleted? ("Aborted" sounds t
davidben
2014/01/28 20:10:28
Done.
| |
| 76 // was received. | |
| 77 result_type = UMA_PING_RESULT_TYPE_UNCOMPLETED; | |
| 78 } else if (status_ == net::URLRequestStatus::CANCELED) { | |
| 79 if (timed_out_) { | |
| 80 result_type = UMA_PING_RESULT_TYPE_TIMEDOUT; | |
| 81 } else { | |
| 82 result_type = UMA_PING_RESULT_TYPE_CANCELED; | |
| 83 } | |
| 84 } else if (status_ == net::URLRequestStatus::FAILED) { | |
| 85 result_type = UMA_PING_RESULT_TYPE_FAILED; | |
| 86 } | |
| 87 | |
| 88 if (result_type < UMA_PING_RESULT_TYPE_MAX) { | |
| 89 UMA_HISTOGRAM_ENUMERATION("Net.Ping_Result", result_type, | |
| 90 UMA_PING_RESULT_TYPE_MAX); | |
| 91 } else { | |
| 92 NOTREACHED(); | |
| 93 } | |
| 94 } | |
| 36 } | 95 } |
| 37 | 96 |
| 38 void DetachableResourceHandler::Detach() { | 97 void DetachableResourceHandler::Detach() { |
| 39 if (is_detached()) | 98 if (is_detached()) |
| 40 return; | 99 return; |
| 41 | 100 |
| 42 if (!is_finished_) { | 101 if (!is_finished_) { |
| 43 // Simulate a cancel on the next handler before destroying it. | 102 // Simulate a cancel on the next handler before destroying it. |
| 44 net::URLRequestStatus status(net::URLRequestStatus::CANCELED, | 103 net::URLRequestStatus status(net::URLRequestStatus::CANCELED, |
| 45 net::ERR_ABORTED); | 104 net::ERR_ABORTED); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 58 // OnWillRead passes back a scoped_refptr, so downstream handler's buffer will | 117 // OnWillRead passes back a scoped_refptr, so downstream handler's buffer will |
| 59 // survive long enough to complete that read. From there, future reads will | 118 // survive long enough to complete that read. From there, future reads will |
| 60 // drain into |read_buffer_|. (If |next_handler_| is an AsyncResourceHandler, | 119 // drain into |read_buffer_|. (If |next_handler_| is an AsyncResourceHandler, |
| 61 // the net::IOBuffer takes a reference to the ResourceBuffer which owns the | 120 // the net::IOBuffer takes a reference to the ResourceBuffer which owns the |
| 62 // shared memory.) | 121 // shared memory.) |
| 63 next_handler_.reset(); | 122 next_handler_.reset(); |
| 64 | 123 |
| 65 // Time the request out if it takes too long. | 124 // Time the request out if it takes too long. |
| 66 detached_timer_.reset(new base::OneShotTimer<DetachableResourceHandler>()); | 125 detached_timer_.reset(new base::OneShotTimer<DetachableResourceHandler>()); |
| 67 detached_timer_->Start( | 126 detached_timer_->Start( |
| 68 FROM_HERE, cancel_delay_, this, &DetachableResourceHandler::Cancel); | 127 FROM_HERE, cancel_delay_, this, &DetachableResourceHandler::TimedOut); |
| 69 | 128 |
| 70 // Resume if necessary. The request may have been deferred, say, waiting on a | 129 // Resume if necessary. The request may have been deferred, say, waiting on a |
| 71 // full buffer in AsyncResourceHandler. Now that it has been detached, resume | 130 // full buffer in AsyncResourceHandler. Now that it has been detached, resume |
| 72 // and drain it. | 131 // and drain it. |
| 73 if (is_deferred_) | 132 if (is_deferred_) |
| 74 Resume(); | 133 Resume(); |
| 75 } | 134 } |
| 76 | 135 |
| 77 void DetachableResourceHandler::SetController(ResourceController* controller) { | 136 void DetachableResourceHandler::SetController(ResourceController* controller) { |
| 78 ResourceHandler::SetController(controller); | 137 ResourceHandler::SetController(controller); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 104 bool ret = next_handler_->OnRequestRedirected(request_id, url, response, | 163 bool ret = next_handler_->OnRequestRedirected(request_id, url, response, |
| 105 &is_deferred_); | 164 &is_deferred_); |
| 106 *defer = is_deferred_; | 165 *defer = is_deferred_; |
| 107 return ret; | 166 return ret; |
| 108 } | 167 } |
| 109 | 168 |
| 110 bool DetachableResourceHandler::OnResponseStarted(int request_id, | 169 bool DetachableResourceHandler::OnResponseStarted(int request_id, |
| 111 ResourceResponse* response, | 170 ResourceResponse* response, |
| 112 bool* defer) { | 171 bool* defer) { |
| 113 DCHECK(!is_deferred_); | 172 DCHECK(!is_deferred_); |
| 173 DCHECK(!response_started_); | |
| 174 response_started_ = true; | |
| 175 | |
| 176 // Record how long it takes for <a ping> to respond. | |
| 177 // http://crbug.com/302816 | |
| 178 if (GetRequestInfo()->GetResourceType() == ResourceType::PING) { | |
| 179 UMA_HISTOGRAM_MEDIUM_TIMES("Net.Ping_ResponseStartedTime", | |
| 180 time_since_start_.Elapsed()); | |
| 181 } | |
| 114 | 182 |
| 115 if (!next_handler_) | 183 if (!next_handler_) |
| 116 return true; | 184 return true; |
| 117 | 185 |
| 118 bool ret = | 186 bool ret = |
| 119 next_handler_->OnResponseStarted(request_id, response, &is_deferred_); | 187 next_handler_->OnResponseStarted(request_id, response, &is_deferred_); |
| 120 *defer = is_deferred_; | 188 *defer = is_deferred_; |
| 121 return ret; | 189 return ret; |
| 122 } | 190 } |
| 123 | 191 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 } | 245 } |
| 178 | 246 |
| 179 void DetachableResourceHandler::OnResponseCompleted( | 247 void DetachableResourceHandler::OnResponseCompleted( |
| 180 int request_id, | 248 int request_id, |
| 181 const net::URLRequestStatus& status, | 249 const net::URLRequestStatus& status, |
| 182 const std::string& security_info, | 250 const std::string& security_info, |
| 183 bool* defer) { | 251 bool* defer) { |
| 184 // No DCHECK(!is_deferred_) as the request may have been cancelled while | 252 // No DCHECK(!is_deferred_) as the request may have been cancelled while |
| 185 // deferred. | 253 // deferred. |
| 186 | 254 |
| 255 status_ = status.status(); | |
| 256 DCHECK_NE(net::URLRequestStatus::IO_PENDING, status_); | |
| 257 | |
| 187 if (!next_handler_) | 258 if (!next_handler_) |
| 188 return; | 259 return; |
| 189 | 260 |
| 190 is_finished_ = true; | 261 is_finished_ = true; |
| 191 | 262 |
| 192 next_handler_->OnResponseCompleted(request_id, status, security_info, | 263 next_handler_->OnResponseCompleted(request_id, status, security_info, |
| 193 &is_deferred_); | 264 &is_deferred_); |
| 194 *defer = is_deferred_; | 265 *defer = is_deferred_; |
| 195 } | 266 } |
| 196 | 267 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 213 } | 284 } |
| 214 | 285 |
| 215 void DetachableResourceHandler::CancelAndIgnore() { | 286 void DetachableResourceHandler::CancelAndIgnore() { |
| 216 controller()->CancelAndIgnore(); | 287 controller()->CancelAndIgnore(); |
| 217 } | 288 } |
| 218 | 289 |
| 219 void DetachableResourceHandler::CancelWithError(int error_code) { | 290 void DetachableResourceHandler::CancelWithError(int error_code) { |
| 220 controller()->CancelWithError(error_code); | 291 controller()->CancelWithError(error_code); |
| 221 } | 292 } |
| 222 | 293 |
| 294 void DetachableResourceHandler::TimedOut() { | |
| 295 timed_out_ = true; | |
| 296 controller()->Cancel(); | |
| 297 } | |
| 298 | |
| 223 } // namespace content | 299 } // namespace content |
| OLD | NEW |