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
3 // found in the LICENSE file.
5 #include "content/browser/loader/detachable_resource_handler.h"
7 #include "base/logging.h"
8 #include "base/time/time.h"
9 #include "content/browser/loader/resource_request_info_impl.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/url_request/url_request_status.h"
15 // This matches the maximum allocation size of AsyncResourceHandler.
16 const int kReadBufSize
= 32 * 1024;
21 DetachableResourceHandler::DetachableResourceHandler(
22 net::URLRequest
* request
,
23 base::TimeDelta cancel_delay
,
24 scoped_ptr
<ResourceHandler
> next_handler
)
25 : ResourceHandler(request
),
26 next_handler_(next_handler
.Pass()),
27 cancel_delay_(cancel_delay
),
30 GetRequestInfo()->set_detachable_handler(this);
33 DetachableResourceHandler::~DetachableResourceHandler() {
34 // Cleanup back-pointer stored on the request info.
35 GetRequestInfo()->set_detachable_handler(NULL
);
38 void DetachableResourceHandler::Detach() {
43 // Simulate a cancel on the next handler before destroying it.
44 net::URLRequestStatus
status(net::URLRequestStatus::CANCELED
,
46 bool defer_ignored
= false;
47 next_handler_
->OnResponseCompleted(status
, std::string(), &defer_ignored
);
48 DCHECK(!defer_ignored
);
49 // If |next_handler_| were to defer its shutdown in OnResponseCompleted,
50 // this would destroy it anyway. Fortunately, AsyncResourceHandler never
51 // does this anyway, so DCHECK it. BufferedResourceHandler and RVH shutdown
52 // already ignore deferred ResourceHandler shutdown, but
53 // DetachableResourceHandler and the detach-on-renderer-cancel logic
54 // introduces a case where this occurs when the renderer cancels a resource.
56 // A OnWillRead / OnReadCompleted pair may still be in progress, but
57 // OnWillRead passes back a scoped_refptr, so downstream handler's buffer will
58 // survive long enough to complete that read. From there, future reads will
59 // drain into |read_buffer_|. (If |next_handler_| is an AsyncResourceHandler,
60 // the net::IOBuffer takes a reference to the ResourceBuffer which owns the
62 next_handler_
.reset();
64 // Time the request out if it takes too long.
65 detached_timer_
.reset(new base::OneShotTimer
<DetachableResourceHandler
>());
66 detached_timer_
->Start(
67 FROM_HERE
, cancel_delay_
, this, &DetachableResourceHandler::Cancel
);
69 // Resume if necessary. The request may have been deferred, say, waiting on a
70 // full buffer in AsyncResourceHandler. Now that it has been detached, resume
76 void DetachableResourceHandler::SetController(ResourceController
* controller
) {
77 ResourceHandler::SetController(controller
);
79 // Intercept the ResourceController for downstream handlers to keep track of
80 // whether the request is deferred.
82 next_handler_
->SetController(this);
85 bool DetachableResourceHandler::OnUploadProgress(uint64 position
, uint64 size
) {
89 return next_handler_
->OnUploadProgress(position
, size
);
92 bool DetachableResourceHandler::OnRequestRedirected(const GURL
& url
,
93 ResourceResponse
* response
,
95 DCHECK(!is_deferred_
);
100 bool ret
= next_handler_
->OnRequestRedirected(url
, response
, &is_deferred_
);
101 *defer
= is_deferred_
;
105 bool DetachableResourceHandler::OnResponseStarted(ResourceResponse
* response
,
107 DCHECK(!is_deferred_
);
113 next_handler_
->OnResponseStarted(response
, &is_deferred_
);
114 *defer
= is_deferred_
;
118 bool DetachableResourceHandler::OnWillStart(const GURL
& url
, bool* defer
) {
119 DCHECK(!is_deferred_
);
124 bool ret
= next_handler_
->OnWillStart(url
, &is_deferred_
);
125 *defer
= is_deferred_
;
129 bool DetachableResourceHandler::OnBeforeNetworkStart(const GURL
& url
,
131 DCHECK(!is_deferred_
);
137 next_handler_
->OnBeforeNetworkStart(url
, &is_deferred_
);
138 *defer
= is_deferred_
;
142 bool DetachableResourceHandler::OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
145 if (!next_handler_
) {
146 DCHECK_EQ(-1, min_size
);
148 read_buffer_
= new net::IOBuffer(kReadBufSize
);
150 *buf_size
= kReadBufSize
;
154 return next_handler_
->OnWillRead(buf
, buf_size
, min_size
);
157 bool DetachableResourceHandler::OnReadCompleted(int bytes_read
, bool* defer
) {
158 DCHECK(!is_deferred_
);
164 next_handler_
->OnReadCompleted(bytes_read
, &is_deferred_
);
165 *defer
= is_deferred_
;
169 void DetachableResourceHandler::OnResponseCompleted(
170 const net::URLRequestStatus
& status
,
171 const std::string
& security_info
,
173 // No DCHECK(!is_deferred_) as the request may have been cancelled while
181 next_handler_
->OnResponseCompleted(status
, security_info
, &is_deferred_
);
182 *defer
= is_deferred_
;
185 void DetachableResourceHandler::OnDataDownloaded(int bytes_downloaded
) {
189 next_handler_
->OnDataDownloaded(bytes_downloaded
);
192 void DetachableResourceHandler::Resume() {
193 DCHECK(is_deferred_
);
194 is_deferred_
= false;
195 controller()->Resume();
198 void DetachableResourceHandler::Cancel() {
199 controller()->Cancel();
202 void DetachableResourceHandler::CancelAndIgnore() {
203 controller()->CancelAndIgnore();
206 void DetachableResourceHandler::CancelWithError(int error_code
) {
207 controller()->CancelWithError(error_code
);
210 } // namespace content