1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_LOADER_DETACHABLE_RESOURCE_HANDLER_H_
#define CONTENT_BROWSER_LOADER_DETACHABLE_RESOURCE_HANDLER_H_
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/timer/timer.h"
#include "content/browser/loader/resource_handler.h"
#include "content/public/browser/resource_controller.h"
#include "net/url_request/url_request_status.h"
namespace net {
class IOBuffer;
class URLRequest;
} // namespace net
namespace content {
// A ResourceHandler which delegates all calls to the next handler, unless
// detached. Once detached, it drives the request to completion itself. This is
// used for requests which outlive the owning renderer, such as <link
// rel=prefetch> and <a ping>. Requests do not start out detached so, e.g.,
// prefetches appear in DevTools and get placed in the renderer's local
// cache. If the request does not complete after a timeout on detach, it is
// cancelled.
//
// Note that, once detached, the request continues without the original next
// handler, so any policy decisions in that handler are skipped.
class DetachableResourceHandler : public ResourceHandler,
public ResourceController {
public:
DetachableResourceHandler(net::URLRequest* request,
base::TimeDelta cancel_delay,
scoped_ptr<ResourceHandler> next_handler);
virtual ~DetachableResourceHandler();
bool is_detached() const { return next_handler_ == NULL; }
void Detach();
void set_cancel_delay(base::TimeDelta cancel_delay) {
cancel_delay_ = cancel_delay;
}
// ResourceHandler implementation:
virtual void SetController(ResourceController* controller) OVERRIDE;
virtual bool OnUploadProgress(int request_id, uint64 position,
uint64 size) OVERRIDE;
virtual bool OnRequestRedirected(int request_id, const GURL& url,
ResourceResponse* response,
bool* defer) OVERRIDE;
virtual bool OnResponseStarted(int request_id,
ResourceResponse* response,
bool* defer) OVERRIDE;
virtual bool OnWillStart(int request_id, const GURL& url,
bool* defer) OVERRIDE;
virtual bool OnBeforeNetworkStart(int request_id,
const GURL& url,
bool* defer) OVERRIDE;
virtual bool OnWillRead(int request_id,
scoped_refptr<net::IOBuffer>* buf,
int* buf_size,
int min_size) OVERRIDE;
virtual bool OnReadCompleted(int request_id, int bytes_read,
bool* defer) OVERRIDE;
virtual void OnResponseCompleted(int request_id,
const net::URLRequestStatus& status,
const std::string& security_info,
bool* defer) OVERRIDE;
virtual void OnDataDownloaded(int request_id, int bytes_downloaded) OVERRIDE;
// ResourceController implementation:
virtual void Resume() OVERRIDE;
virtual void Cancel() OVERRIDE;
virtual void CancelAndIgnore() OVERRIDE;
virtual void CancelWithError(int error_code) OVERRIDE;
private:
void TimedOut();
scoped_ptr<ResourceHandler> next_handler_;
scoped_refptr<net::IOBuffer> read_buffer_;
scoped_ptr<base::OneShotTimer<DetachableResourceHandler> > detached_timer_;
base::TimeDelta cancel_delay_;
bool is_deferred_;
bool is_finished_;
bool timed_out_;
bool response_started_;
base::ElapsedTimer time_since_start_;
// The status recorded from OnResponseCompleted. Value is
// net::URLRequestStatus::IO_PENDING if OnResponseCompleted is never received.
net::URLRequestStatus::Status status_;
DISALLOW_COPY_AND_ASSIGN(DetachableResourceHandler);
};
} // namespace content
#endif // CONTENT_BROWSER_LOADER_DETACHABLE_RESOURCE_HANDLER_H_
|