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; | |
mmenke
2014/10/31 19:16:22
size_t -> int (IOBuffers use integer sizes - resul
mmenke
2014/10/31 19:16:22
nit: Maybe kReadBufferSize? We never increment t
mef
2014/10/31 20:39:15
Done.
mef
2014/10/31 20:39:15
Done.
| |
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 context_ = context; | |
30 delegate_ = delegate; | |
31 initial_url_ = url; | |
32 initial_priority_ = priority; | |
mmenke
2014/10/31 19:16:22
These should all be in the initializer list (And m
mef
2014/10/31 20:39:15
Done.
| |
33 } | |
34 | |
35 CronetURLRequestAdapter::~CronetURLRequestAdapter() { | |
36 url_request_.reset(); | |
mmenke
2014/10/31 19:16:22
Not needed - that's what scoped_ptrs are for. :)
mef
2014/10/31 20:39:15
Done.
| |
37 } | |
38 | |
39 void CronetURLRequestAdapter::SetMethod(const std::string& method) { | |
40 initial_method_ = method; | |
41 } | |
42 | |
43 void CronetURLRequestAdapter::AddRequestHeader(const std::string& name, | |
44 const std::string& value) { | |
45 initial_request_headers_.SetHeader(name, value); | |
46 } | |
47 | |
48 net::HttpResponseHeaders* CronetURLRequestAdapter::GetResponseHeaders() const { | |
49 DCHECK(IsOnNetworkThread()); | |
50 if (url_request_ == NULL) | |
51 return NULL; | |
mmenke
2014/10/31 19:16:22
Do we ever call any of these when the request is N
mef
2014/10/31 20:39:15
Done.
| |
52 return url_request_->response_headers(); | |
53 } | |
54 | |
55 std::string CronetURLRequestAdapter::GetNegotiatedProtocol() const { | |
56 DCHECK(IsOnNetworkThread()); | |
57 if (url_request_ == NULL) | |
58 return std::string(); | |
59 return url_request_->response_info().npn_negotiated_protocol; | |
60 } | |
61 | |
62 bool CronetURLRequestAdapter::GetWasCached() const { | |
63 DCHECK(IsOnNetworkThread()); | |
64 if (url_request_ == NULL) | |
65 return false; | |
66 return url_request_->response_info().was_cached; | |
67 } | |
68 | |
69 int64 CronetURLRequestAdapter::GetTotalReceivedBytes() const { | |
70 DCHECK(IsOnNetworkThread()); | |
71 if (url_request_ == NULL) | |
72 return 0; | |
73 return url_request_->GetTotalReceivedBytes(); | |
74 } | |
75 | |
76 void CronetURLRequestAdapter::Start() { | |
77 DCHECK(!IsOnNetworkThread()); | |
78 context_->GetNetworkTaskRunner()->PostTask( | |
79 FROM_HERE, | |
80 base::Bind(&CronetURLRequestAdapter::StartOnNetworkThread, | |
81 base::Unretained(this))); | |
82 } | |
83 | |
84 void CronetURLRequestAdapter::FollowDeferredRedirect() { | |
85 DCHECK(!IsOnNetworkThread()); | |
86 context_->GetNetworkTaskRunner()->PostTask( | |
87 FROM_HERE, | |
88 base::Bind( | |
89 &CronetURLRequestAdapter::FollowDeferredRedirectOnNetworkThread, | |
90 base::Unretained(this))); | |
91 } | |
92 | |
93 // Receive more data. | |
94 void CronetURLRequestAdapter::ReadData() { | |
95 DCHECK(!IsOnNetworkThread()); | |
96 context_->GetNetworkTaskRunner()->PostTask( | |
97 FROM_HERE, | |
98 base::Bind(&CronetURLRequestAdapter::ReadDataOnNetworkThread, | |
99 base::Unretained(this))); | |
100 } | |
101 | |
102 void CronetURLRequestAdapter::Destroy() { | |
103 DCHECK(!IsOnNetworkThread()); | |
104 context_->GetNetworkTaskRunner()->PostTask( | |
105 FROM_HERE, | |
106 base::Bind(&CronetURLRequestAdapter::DestroyOnNetworkThread, | |
107 base::Unretained(this))); | |
108 } | |
109 | |
110 // net::URLRequest::Delegate overrides (called on network thread). | |
mmenke
2014/10/31 19:16:22
nit: This applies to multiple methods, so should
mef
2014/10/31 20:39:15
Done.
| |
111 void CronetURLRequestAdapter::OnReceivedRedirect( | |
112 net::URLRequest* request, | |
113 const net::RedirectInfo& redirect_info, | |
114 bool* defer_redirect) { | |
115 DCHECK(IsOnNetworkThread()); | |
116 DCHECK(request->status().is_success()); | |
117 delegate_->OnRedirect(redirect_info.new_url, redirect_info.status_code); | |
118 *defer_redirect = true; | |
119 } | |
120 | |
121 void CronetURLRequestAdapter::OnResponseStarted(net::URLRequest* request) { | |
122 DCHECK(IsOnNetworkThread()); | |
123 if (!CheckStatus(request)) | |
124 return; | |
125 delegate_->OnResponseStarted(request->GetResponseCode()); | |
126 } | |
127 | |
128 void CronetURLRequestAdapter::OnReadCompleted(net::URLRequest* request, | |
129 int bytes_read) { | |
130 DCHECK(IsOnNetworkThread()); | |
131 if (!CheckStatus(request)) | |
132 return; | |
133 if (bytes_read != 0) { | |
134 delegate_->OnBytesRead(Data(), bytes_read); | |
135 } else { | |
136 delegate_->OnRequestFinished(); | |
137 } | |
138 } | |
139 | |
140 bool CronetURLRequestAdapter::IsOnNetworkThread() const { | |
141 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); | |
142 } | |
143 | |
144 void CronetURLRequestAdapter::StartOnNetworkThread() { | |
145 DCHECK(IsOnNetworkThread()); | |
146 VLOG(1) << "Starting chromium request: " | |
147 << initial_url_.possibly_invalid_spec().c_str() | |
148 << " priority: " << RequestPriorityToString(initial_priority_); | |
149 url_request_ = context_->GetURLRequestContext()->CreateRequest( | |
150 initial_url_, net::DEFAULT_PRIORITY, this, NULL); | |
151 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | |
152 net::LOAD_DO_NOT_SAVE_COOKIES | | |
153 net::LOAD_DO_NOT_SEND_COOKIES); | |
154 url_request_->set_method(initial_method_); | |
155 url_request_->SetExtraRequestHeaders(initial_request_headers_); | |
156 url_request_->SetPriority(initial_priority_); | |
157 url_request_->Start(); | |
158 } | |
159 | |
160 void CronetURLRequestAdapter::FollowDeferredRedirectOnNetworkThread() { | |
161 DCHECK(IsOnNetworkThread()); | |
162 | |
163 url_request_->FollowDeferredRedirect(); | |
164 } | |
165 | |
166 // Reads all available data or starts an asynchronous read. | |
167 void CronetURLRequestAdapter::ReadDataOnNetworkThread() { | |
168 DCHECK(IsOnNetworkThread()); | |
169 int bytes_read = 0; | |
170 // If read completes synchronously, pass data to delegate. | |
171 if (url_request_->Read( | |
172 read_buffer_.get(), read_buffer_->size(), &bytes_read)) { | |
173 OnReadCompleted(url_request_.get(), bytes_read); | |
174 } else if (url_request_->status().status() != | |
175 net::URLRequestStatus::IO_PENDING) { | |
176 OnReadCompleted(url_request_.get(), -1); | |
177 } | |
178 } | |
179 | |
180 void CronetURLRequestAdapter::DestroyOnNetworkThread() { | |
181 DCHECK(IsOnNetworkThread()); | |
182 VLOG(1) << "Destroy chromium request: " << | |
183 initial_url_.possibly_invalid_spec(); | |
184 | |
185 if (url_request_ != NULL) | |
186 url_request_->Cancel(); | |
mmenke
2014/10/31 19:16:22
This isn't needed - requests call cancel on destru
mef
2014/10/31 20:39:15
Done.
| |
187 | |
188 delete this; | |
189 } | |
190 | |
191 bool CronetURLRequestAdapter::CheckStatus(net::URLRequest* request) { | |
mmenke
2014/10/31 19:16:22
Maybe call this MaybeReportError (Or failure?) No
mef
2014/10/31 20:39:15
Done.
| |
192 DCHECK_NE(net::URLRequestStatus::IO_PENDING, url_request_->status().status()); | |
193 DCHECK_EQ(request, url_request_); | |
194 if (url_request_->status().is_success()) | |
195 return true; | |
196 VLOG(1) << "Error " << url_request_->status().error() | |
197 << " on chromium request: " << initial_url_.possibly_invalid_spec(); | |
198 delegate_->OnError(url_request_->status().error()); | |
199 return false; | |
200 } | |
201 | |
202 unsigned char* CronetURLRequestAdapter::Data() const { | |
203 return reinterpret_cast<unsigned char*>(read_buffer_->data()); | |
204 } | |
205 | |
206 } // namespace cronet | |
OLD | NEW |