Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / cronet / android / url_request_peer.cc
bloba3b4aa787493fb59f35afbaef84f01aeb2aa9ffd
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.
5 #include "url_request_peer.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "net/base/load_flags.h"
9 #include "net/http/http_status_code.h"
11 namespace cronet {
13 static const size_t kBufferSizeIncrement = 8192;
15 // Fragment automatically inserted in the User-Agent header to indicate
16 // that the request is coming from this network stack.
17 static const char kUserAgentFragment[] = "; ChromiumJNI/";
19 URLRequestPeer::URLRequestPeer(URLRequestContextPeer* context,
20 URLRequestPeerDelegate* delegate,
21 GURL url,
22 net::RequestPriority priority)
23 : method_("GET"),
24 url_request_(NULL),
25 read_buffer_(new net::GrowableIOBuffer()),
26 bytes_read_(0),
27 total_bytes_read_(0),
28 error_code_(0),
29 http_status_code_(0),
30 canceled_(false),
31 expected_size_(0),
32 streaming_upload_(false) {
33 context_ = context;
34 delegate_ = delegate;
35 url_ = url;
36 priority_ = priority;
39 URLRequestPeer::~URLRequestPeer() { CHECK(url_request_ == NULL); }
41 void URLRequestPeer::SetMethod(const std::string& method) { method_ = method; }
43 void URLRequestPeer::AddHeader(const std::string& name,
44 const std::string& value) {
45 headers_.SetHeader(name, value);
48 void URLRequestPeer::SetPostContent(const char* bytes, int bytes_len) {
49 if (!upload_data_stream_) {
50 upload_data_stream_.reset(
51 new net::UploadDataStream(net::UploadDataStream::CHUNKED, 0));
53 upload_data_stream_->AppendChunk(bytes, bytes_len, true /* is_last_chunk */);
56 void URLRequestPeer::EnableStreamingUpload() { streaming_upload_ = true; }
58 void URLRequestPeer::AppendChunk(const char* bytes,
59 int bytes_len,
60 bool is_last_chunk) {
61 VLOG(context_->logging_level()) << "AppendChunk, len: " << bytes_len
62 << ", last: " << is_last_chunk;
64 context_->GetNetworkTaskRunner()->PostTask(
65 FROM_HERE,
66 base::Bind(&URLRequestPeer::OnAppendChunk,
67 base::Unretained(this),
68 bytes,
69 bytes_len,
70 is_last_chunk));
73 void URLRequestPeer::Start() {
74 context_->GetNetworkTaskRunner()->PostTask(
75 FROM_HERE,
76 base::Bind(&URLRequestPeer::OnInitiateConnection,
77 base::Unretained(this)));
80 void URLRequestPeer::OnAppendChunk(const char* bytes,
81 int bytes_len,
82 bool is_last_chunk) {
83 if (url_request_ != NULL) {
84 url_request_->AppendChunkToUpload(bytes, bytes_len, is_last_chunk);
85 delegate_->OnAppendChunkCompleted(this);
89 void URLRequestPeer::OnInitiateConnection() {
90 if (canceled_) {
91 return;
94 VLOG(context_->logging_level())
95 << "Starting chromium request: " << url_.possibly_invalid_spec().c_str()
96 << " priority: " << RequestPriorityToString(priority_);
97 url_request_ = new net::URLRequest(
98 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext());
99 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE |
100 net::LOAD_DO_NOT_SAVE_COOKIES |
101 net::LOAD_DO_NOT_SEND_COOKIES);
102 url_request_->set_method(method_);
103 url_request_->SetExtraRequestHeaders(headers_);
104 std::string user_agent;
105 if (headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) {
106 headers_.GetHeader(net::HttpRequestHeaders::kUserAgent, &user_agent);
107 } else {
108 user_agent = context_->GetUserAgent(url_);
110 size_t pos = user_agent.find(')');
111 if (pos != std::string::npos) {
112 user_agent.insert(pos, context_->version());
113 user_agent.insert(pos, kUserAgentFragment);
115 url_request_->SetExtraRequestHeaderByName(
116 net::HttpRequestHeaders::kUserAgent, user_agent, true /* override */);
118 VLOG(context_->logging_level()) << "User agent: " << user_agent;
120 if (upload_data_stream_) {
121 url_request_->set_upload(make_scoped_ptr(upload_data_stream_.release()));
122 } else if (streaming_upload_) {
123 url_request_->EnableChunkedUpload();
126 url_request_->SetPriority(priority_);
128 url_request_->Start();
131 void URLRequestPeer::Cancel() {
132 if (canceled_) {
133 return;
136 canceled_ = true;
138 context_->GetNetworkTaskRunner()->PostTask(
139 FROM_HERE,
140 base::Bind(&URLRequestPeer::OnCancelRequest, base::Unretained(this)));
143 void URLRequestPeer::OnCancelRequest() {
144 VLOG(context_->logging_level())
145 << "Canceling chromium request: " << url_.possibly_invalid_spec();
147 if (url_request_ != NULL) {
148 url_request_->Cancel();
151 OnRequestCanceled();
154 void URLRequestPeer::Destroy() {
155 context_->GetNetworkTaskRunner()->PostTask(
156 FROM_HERE, base::Bind(&URLRequestPeer::OnDestroyRequest, this));
159 // static
160 void URLRequestPeer::OnDestroyRequest(URLRequestPeer* self) {
161 VLOG(self->context_->logging_level())
162 << "Destroying chromium request: " << self->url_.possibly_invalid_spec();
163 delete self;
166 void URLRequestPeer::OnResponseStarted(net::URLRequest* request) {
167 if (request->status().status() != net::URLRequestStatus::SUCCESS) {
168 OnRequestFailed();
169 return;
172 http_status_code_ = request->GetResponseCode();
173 VLOG(context_->logging_level())
174 << "Response started with status: " << http_status_code_;
176 request->GetResponseHeaderByName("Content-Type", &content_type_);
177 expected_size_ = request->GetExpectedContentSize();
178 delegate_->OnResponseStarted(this);
180 Read();
183 // Reads all available data or starts an asynchronous read.
184 void URLRequestPeer::Read() {
185 while (true) {
186 if (read_buffer_->RemainingCapacity() == 0) {
187 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement;
188 read_buffer_->SetCapacity(new_capacity);
191 int bytes_read;
192 if (url_request_->Read(
193 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) {
194 if (bytes_read == 0) {
195 OnRequestSucceeded();
196 break;
199 VLOG(context_->logging_level()) << "Synchronously read: " << bytes_read
200 << " bytes";
201 OnBytesRead(bytes_read);
202 } else if (url_request_->status().status() ==
203 net::URLRequestStatus::IO_PENDING) {
204 if (bytes_read_ != 0) {
205 VLOG(context_->logging_level()) << "Flushing buffer: " << bytes_read_
206 << " bytes";
208 delegate_->OnBytesRead(this);
209 read_buffer_->set_offset(0);
210 bytes_read_ = 0;
212 VLOG(context_->logging_level()) << "Started async read";
213 break;
214 } else {
215 OnRequestFailed();
216 break;
221 void URLRequestPeer::OnReadCompleted(net::URLRequest* request, int bytes_read) {
222 VLOG(context_->logging_level()) << "Asynchronously read: " << bytes_read
223 << " bytes";
224 if (bytes_read < 0) {
225 OnRequestFailed();
226 return;
227 } else if (bytes_read == 0) {
228 OnRequestSucceeded();
229 return;
232 OnBytesRead(bytes_read);
233 Read();
236 void URLRequestPeer::OnBytesRead(int bytes_read) {
237 read_buffer_->set_offset(read_buffer_->offset() + bytes_read);
238 bytes_read_ += bytes_read;
239 total_bytes_read_ += bytes_read;
242 void URLRequestPeer::OnRequestSucceeded() {
243 if (canceled_) {
244 return;
247 VLOG(context_->logging_level())
248 << "Request completed with HTTP status: " << http_status_code_
249 << ". Total bytes read: " << total_bytes_read_;
251 OnRequestCompleted();
254 void URLRequestPeer::OnRequestFailed() {
255 if (canceled_) {
256 return;
259 error_code_ = url_request_->status().error();
260 VLOG(context_->logging_level())
261 << "Request failed with status: " << url_request_->status().status()
262 << " and error: " << net::ErrorToString(error_code_);
263 OnRequestCompleted();
266 void URLRequestPeer::OnRequestCanceled() { OnRequestCompleted(); }
268 void URLRequestPeer::OnRequestCompleted() {
269 VLOG(context_->logging_level())
270 << "Completed: " << url_.possibly_invalid_spec();
271 if (url_request_ != NULL) {
272 delete url_request_;
273 url_request_ = NULL;
276 delegate_->OnBytesRead(this);
277 delegate_->OnRequestFinished(this);
280 unsigned char* URLRequestPeer::Data() const {
281 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer());
284 } // namespace cronet