OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ | |
6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/location.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "net/base/request_priority.h" | |
15 #include "net/http/http_request_headers.h" | |
16 #include "net/url_request/url_request.h" | |
17 #include "url/gurl.h" | |
18 | |
19 namespace base { | |
20 class SingleThreadTaskRunner; | |
21 } // namespace base | |
22 | |
23 namespace net { | |
24 class GrowableIOBuffer; | |
25 class HttpResponseHeaders; | |
26 } // namespace net | |
27 | |
28 namespace cronet { | |
29 | |
30 class CronetURLRequestContextAdapter; | |
31 | |
32 // An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest. | |
33 // Created and configured from random thread. Start is posted to network | |
34 // thread and all callbacks into CronetURLRequestAdapterDelegate are done on | |
35 // network thread. Each delegate callback is expected to initiate next step like | |
36 // FollowDeferredRedirect, ReadData or Destroy. All methods except those needed | |
37 // to set up a request must be called exclusively on the network thread. | |
38 class CronetURLRequestAdapter : public net::URLRequest::Delegate { | |
39 public: | |
40 // The delegate which is called when the request adapter completes a step. | |
41 // Always called on network thread. | |
42 class CronetURLRequestAdapterDelegate { | |
43 public: | |
44 // Called on redirect. Consumer should either destroy the request, or | |
45 // call FollowDeferredRedirect. | |
46 virtual void OnRedirect(const GURL& newLocation, int http_status_code) = 0; | |
47 // Called when response has started and headers have been received. Consumer | |
48 // should either destroy the request, or call ReadData. | |
49 virtual void OnResponseStarted(int http_status_code) = 0; | |
50 // Called when response bytes were read. Consumer should consume data and | |
51 // either destroy the request, or call ReadData. The request may only be | |
52 // destroyed after the embedder is done with |bytes||, as deleting the | |
mmenke
2014/11/07 16:28:07
nit: Remove extra "|"
xunjieli
2014/11/07 16:28:40
nit: exra pipe?
mef
2014/11/07 16:33:40
Done.
| |
53 // request frees the buffer. | |
54 virtual void OnBytesRead(unsigned char* bytes, | |
55 int bytes_read) = 0; | |
56 // Called when response has finished successfully. Consumer should destroy | |
57 // the request. | |
58 virtual void OnRequestFinished() = 0; | |
59 // Called when response has finished with error. Consumer should destroy | |
60 // the request. | |
61 virtual void OnError(int net_error) = 0; | |
62 | |
63 virtual ~CronetURLRequestAdapterDelegate() {} | |
64 }; | |
65 | |
66 CronetURLRequestAdapter( | |
67 CronetURLRequestContextAdapter* context, | |
68 scoped_ptr<CronetURLRequestAdapterDelegate> delegate, | |
69 const GURL& url, | |
70 net::RequestPriority priority); | |
71 ~CronetURLRequestAdapter() override; | |
72 | |
73 // Methods called prior to Start are never called on network thread. | |
74 | |
75 // Sets the request method GET, POST etc. | |
76 void set_method(const std::string& method) { | |
77 initial_method_ = method; | |
78 } | |
79 | |
80 // Adds a header to the request before it starts. | |
81 void AddRequestHeader(const std::string& name, const std::string& value); | |
82 | |
83 // Methods called on any thread. | |
84 | |
85 // Posts tasks to network thread. | |
86 bool PostTaskToNetworkThread(const tracked_objects::Location& from_here, | |
87 const base::Closure& task); | |
88 | |
89 // Returns true if called on network thread. | |
90 bool IsOnNetworkThread() const; | |
91 | |
92 // Methods called only on network thread. | |
93 | |
94 // Starts the request. | |
95 void Start(); | |
96 | |
97 // Follows redirect. | |
98 void FollowDeferredRedirect(); | |
99 | |
100 // Reads more data. | |
101 void ReadData(); | |
102 | |
103 // Releases all resources for the request and deletes the object itself. | |
104 void Destroy(); | |
105 | |
106 // When called during a OnRedirect or OnResponseStarted callback, these | |
107 // methods return the corresponding response information. | |
108 | |
109 // Gets all response headers, as a HttpResponseHeaders object. Returns NULL | |
110 // if the last attempted request received no headers. | |
111 const net::HttpResponseHeaders* GetResponseHeaders() const; | |
112 | |
113 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. | |
114 const std::string& GetNegotiatedProtocol() const; | |
115 | |
116 // Returns true if response is coming from the cache. | |
117 bool GetWasCached() const; | |
118 | |
119 // Gets the total amount of body data received from network after | |
120 // SSL/SPDY/QUIC decoding and proxy handling. Basically the size of the body | |
121 // prior to decompression. | |
122 int64 GetTotalReceivedBytes() const; | |
123 | |
124 // net::URLRequest::Delegate overrides. | |
125 void OnReceivedRedirect(net::URLRequest* request, | |
126 const net::RedirectInfo& redirect_info, | |
127 bool* defer_redirect) override; | |
128 void OnResponseStarted(net::URLRequest* request) override; | |
129 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; | |
130 | |
131 private: | |
132 // Checks status of the request_adapter, return false if |is_success()| is | |
133 // true, otherwise report error and cancel request_adapter. | |
134 bool MaybeReportError(net::URLRequest* request) const; | |
135 | |
136 CronetURLRequestContextAdapter* context_; | |
137 scoped_ptr<CronetURLRequestAdapterDelegate> delegate_; | |
138 | |
139 const GURL initial_url_; | |
140 const net::RequestPriority initial_priority_; | |
141 std::string initial_method_; | |
142 net::HttpRequestHeaders initial_request_headers_; | |
143 | |
144 scoped_refptr<net::IOBufferWithSize> read_buffer_; | |
145 scoped_ptr<net::URLRequest> url_request_; | |
146 | |
147 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter); | |
148 }; | |
149 | |
150 } // namespace cronet | |
151 | |
152 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ | |
OLD | NEW |