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 #include "cronet_url_request_adapter.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/logging.h" | |
10 #include "components/cronet/android/cronet_url_request_context_adapter.h" | |
11 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" | |
12 #include "net/base/io_buffer.h" | |
13 #include "net/base/load_flags.h" | |
14 #include "net/http/http_status_code.h" | |
15 #include "net/url_request/redirect_info.h" | |
16 #include "net/url_request/url_request_context.h" | |
17 | |
18 namespace cronet { | |
19 | |
20 static const size_t kBufferSizeIncrement = 32768; | |
21 | |
22 CronetURLRequestAdapter::CronetURLRequestAdapter( | |
23 CronetURLRequestContextAdapter* context, | |
24 CronetURLRequestAdapterDelegate* delegate, | |
25 const GURL& url, | |
26 net::RequestPriority priority) | |
27 : initial_method_("GET"), | |
28 read_buffer_(new net::IOBufferWithSize(kBufferSizeIncrement)), | |
29 called_to_delegate_(false), | |
30 destroy_soon_(false) { | |
31 context_ = context; | |
32 delegate_ = delegate; | |
33 initial_url_ = url; | |
34 initial_priority_ = priority; | |
35 } | |
36 | |
37 CronetURLRequestAdapter::~CronetURLRequestAdapter() { | |
38 url_request_.reset(); | |
39 } | |
40 | |
41 void CronetURLRequestAdapter::SetMethod(const std::string& method) { | |
42 initial_method_ = method; | |
43 } | |
44 | |
45 void CronetURLRequestAdapter::AddRequestHeader(const std::string& name, | |
46 const std::string& value) { | |
47 initial_request_headers_.SetHeader(name, value); | |
48 } | |
49 | |
50 net::HttpResponseHeaders* CronetURLRequestAdapter::GetResponseHeaders() const { | |
51 DCHECK(IsOnNetworkThread()); | |
52 if (url_request_ == NULL) | |
53 return NULL; | |
54 return url_request_->response_headers(); | |
55 } | |
56 | |
57 std::string CronetURLRequestAdapter::GetNegotiatedProtocol() const { | |
58 DCHECK(IsOnNetworkThread()); | |
59 if (url_request_ == NULL) | |
60 return std::string(); | |
61 return url_request_->response_info().npn_negotiated_protocol; | |
62 } | |
63 | |
64 bool CronetURLRequestAdapter::GetWasCached() const { | |
65 DCHECK(IsOnNetworkThread()); | |
66 if (url_request_ == NULL) | |
67 return false; | |
68 return url_request_->response_info().was_cached; | |
69 } | |
70 | |
71 int64 CronetURLRequestAdapter::GetTotalReceivedBytes() const { | |
72 DCHECK(IsOnNetworkThread()); | |
73 if (url_request_ == NULL) | |
74 return 0; | |
75 return url_request_->GetTotalReceivedBytes(); | |
76 } | |
77 | |
78 void CronetURLRequestAdapter::Start() { | |
79 DCHECK(!IsOnNetworkThread()); | |
80 context_->GetNetworkTaskRunner()->PostTask( | |
81 FROM_HERE, | |
82 base::Bind(&CronetURLRequestAdapter::StartOnNetworkThread, | |
83 base::Unretained(this))); | |
84 } | |
85 | |
86 void CronetURLRequestAdapter::FollowDeferredRedirect() { | |
87 DCHECK(!IsOnNetworkThread()); | |
88 context_->GetNetworkTaskRunner()->PostTask( | |
89 FROM_HERE, | |
90 base::Bind( | |
91 &CronetURLRequestAdapter::FollowDeferredRedirectOnNetworkThread, | |
92 base::Unretained(this))); | |
93 } | |
94 | |
95 // Receive more data. | |
96 void CronetURLRequestAdapter::ReadData() { | |
97 DCHECK(!IsOnNetworkThread()); | |
98 context_->GetNetworkTaskRunner()->PostTask( | |
99 FROM_HERE, | |
100 base::Bind(&CronetURLRequestAdapter::ReadDataOnNetworkThread, | |
101 base::Unretained(this))); | |
102 } | |
103 | |
104 void CronetURLRequestAdapter::Destroy() { | |
105 DCHECK(!IsOnNetworkThread()); | |
106 context_->GetNetworkTaskRunner()->PostTask( | |
107 FROM_HERE, | |
108 base::Bind(&CronetURLRequestAdapter::DestroyOnNetworkThread, | |
109 base::Unretained(this))); | |
110 } | |
111 | |
112 // net::URLRequest::Delegate overrides (called on network thread). | |
xunjieli
2014/10/28 14:24:19
Add a DCHECK in this method, since you expect it t
mef
2014/10/28 16:29:00
Done. net::URLRequest::Delegate overrides are call
| |
113 void CronetURLRequestAdapter::OnReceivedRedirect( | |
114 net::URLRequest* request, | |
115 const net::RedirectInfo& redirect_info, | |
116 bool* defer_redirect) { | |
117 DCHECK(request->status().is_success()); | |
118 called_to_delegate_ = true; | |
119 delegate_->OnRedirect(redirect_info.new_url, redirect_info.status_code); | |
120 *defer_redirect = true; | |
121 } | |
122 | |
123 void CronetURLRequestAdapter::OnResponseStarted(net::URLRequest* request) { | |
124 if (!CheckStatus(request)) | |
125 return; | |
126 called_to_delegate_ = true; | |
127 delegate_->OnResponseStarted(request->GetResponseCode()); | |
128 } | |
129 | |
130 void CronetURLRequestAdapter::OnReadCompleted(net::URLRequest* request, | |
131 int bytes_read) { | |
132 if (!CheckStatus(request)) | |
133 return; | |
134 called_to_delegate_ = true; | |
135 if (bytes_read != 0) { | |
136 delegate_->OnBytesRead(Data(), bytes_read); | |
137 } else { | |
138 delegate_->OnRequestFinished(); | |
139 } | |
140 } | |
141 bool CronetURLRequestAdapter::IsOnNetworkThread() const { | |
142 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); | |
143 } | |
144 | |
145 void CronetURLRequestAdapter::StartOnNetworkThread() { | |
146 DCHECK(IsOnNetworkThread()); | |
147 if (destroy_soon_) | |
148 return; | |
149 | |
150 VLOG(1) << "Starting chromium request: " | |
151 << initial_url_.possibly_invalid_spec().c_str() | |
152 << " priority: " << RequestPriorityToString(initial_priority_); | |
153 url_request_ = context_->GetURLRequestContext()->CreateRequest( | |
154 initial_url_, net::DEFAULT_PRIORITY, this, NULL); | |
155 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | |
156 net::LOAD_DO_NOT_SAVE_COOKIES | | |
157 net::LOAD_DO_NOT_SEND_COOKIES); | |
158 url_request_->set_method(initial_method_); | |
159 url_request_->SetExtraRequestHeaders(initial_request_headers_); | |
160 url_request_->SetPriority(initial_priority_); | |
161 url_request_->Start(); | |
162 } | |
163 | |
164 void CronetURLRequestAdapter::FollowDeferredRedirectOnNetworkThread() { | |
165 DCHECK(IsOnNetworkThread()); | |
166 if (WasDestroyedWhileCalledToDelegate()) | |
167 return; | |
168 | |
169 url_request_->FollowDeferredRedirect(); | |
170 } | |
171 | |
172 // Reads all available data or starts an asynchronous read. | |
173 void CronetURLRequestAdapter::ReadDataOnNetworkThread() { | |
174 DCHECK(IsOnNetworkThread()); | |
175 if (WasDestroyedWhileCalledToDelegate()) | |
176 return; | |
177 int bytes_read = 0; | |
178 // If read completes synchronously, pass data to delegate. | |
179 if (url_request_->Read( | |
180 read_buffer_.get(), read_buffer_->size(), &bytes_read)) { | |
181 OnReadCompleted(url_request_.get(), bytes_read); | |
182 } else if (url_request_->status().status() != | |
183 net::URLRequestStatus::IO_PENDING) { | |
184 OnReadCompleted(url_request_.get(), -1); | |
185 } | |
186 } | |
187 | |
188 void CronetURLRequestAdapter::DestroyOnNetworkThread() { | |
189 DCHECK(IsOnNetworkThread()); | |
190 VLOG(1) << "Destroy chromium request: " << | |
191 initial_url_.possibly_invalid_spec(); | |
192 | |
193 if (called_to_delegate_) { | |
194 destroy_soon_ = true; | |
195 return; | |
196 } | |
197 | |
198 if (url_request_ != NULL) | |
199 url_request_->Cancel(); | |
200 | |
201 delete this; | |
202 } | |
203 | |
204 bool CronetURLRequestAdapter::WasDestroyedWhileCalledToDelegate() { | |
205 DCHECK(IsOnNetworkThread()); | |
206 DCHECK(called_to_delegate_); | |
207 called_to_delegate_ = false; | |
208 if (destroy_soon_) { | |
209 DestroyOnNetworkThread(); | |
210 return true; | |
211 } | |
212 return false; | |
213 } | |
214 | |
215 bool CronetURLRequestAdapter::CheckStatus(net::URLRequest* request) { | |
216 DCHECK_NE(net::URLRequestStatus::IO_PENDING, url_request_->status().status()); | |
217 DCHECK_EQ(request, url_request_); | |
218 if (url_request_->status().is_success()) | |
219 return true; | |
220 VLOG(1) << "Error " << url_request_->status().error() | |
221 << " on chromium request: " << initial_url_.possibly_invalid_spec(); | |
222 if (!called_to_delegate_) { | |
223 called_to_delegate_ = true; | |
224 delegate_->OnError(url_request_->status().error()); | |
225 } | |
226 return false; | |
227 } | |
228 | |
229 unsigned char* CronetURLRequestAdapter::Data() const { | |
230 return reinterpret_cast<unsigned char*>(read_buffer_->data()); | |
231 } | |
232 | |
233 } // namespace cronet | |
OLD | NEW |