OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "url_request_adapter.h" | 5 #include "url_request_adapter.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "components/cronet/android/url_request_context_adapter.h" | 8 #include "components/cronet/android/url_request_context_adapter.h" |
9 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" | 9 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" |
10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
11 #include "net/base/upload_bytes_element_reader.h" | 11 #include "net/base/upload_bytes_element_reader.h" |
12 #include "net/http/http_status_code.h" | 12 #include "net/http/http_status_code.h" |
13 | 13 |
14 namespace cronet { | 14 namespace cronet { |
15 | 15 |
16 static const size_t kBufferSizeIncrement = 8192; | 16 static const size_t kBufferSizeIncrement = 8192; |
17 | 17 |
18 URLRequestAdapter::URLRequestAdapter(URLRequestContextAdapter* context, | 18 URLRequestAdapter::URLRequestAdapter(URLRequestContextAdapter* context, |
19 URLRequestAdapterDelegate* delegate, | 19 URLRequestAdapterDelegate* delegate, |
20 GURL url, | 20 GURL url, |
21 net::RequestPriority priority) | 21 net::RequestPriority priority) |
22 : method_("GET"), | 22 : method_("GET"), |
23 url_request_(NULL), | 23 url_request_(NULL), |
24 read_buffer_(new net::GrowableIOBuffer()), | 24 read_buffer_(new net::GrowableIOBuffer()), |
25 bytes_read_(0), | 25 bytes_read_(0), |
26 total_bytes_read_(0), | 26 total_bytes_read_(0), |
27 error_code_(0), | 27 error_code_(0), |
28 http_status_code_(0), | 28 http_status_code_(0), |
29 canceled_(false), | 29 canceled_(false), |
30 expected_size_(0) { | 30 expected_size_(0), |
31 chunked_upload_(false) { | |
31 context_ = context; | 32 context_ = context; |
32 delegate_ = delegate; | 33 delegate_ = delegate; |
33 url_ = url; | 34 url_ = url; |
34 priority_ = priority; | 35 priority_ = priority; |
35 } | 36 } |
36 | 37 |
37 URLRequestAdapter::~URLRequestAdapter() { | 38 URLRequestAdapter::~URLRequestAdapter() { |
38 CHECK(url_request_ == NULL); | 39 CHECK(url_request_ == NULL); |
39 } | 40 } |
40 | 41 |
(...skipping 14 matching lines...) Expand all Loading... | |
55 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)); | 56 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)); |
56 } | 57 } |
57 | 58 |
58 void URLRequestAdapter::SetUploadChannel(JNIEnv* env, int64 content_length) { | 59 void URLRequestAdapter::SetUploadChannel(JNIEnv* env, int64 content_length) { |
59 scoped_ptr<net::UploadElementReader> reader( | 60 scoped_ptr<net::UploadElementReader> reader( |
60 new WrappedChannelElementReader(delegate_, content_length)); | 61 new WrappedChannelElementReader(delegate_, content_length)); |
61 upload_data_stream_.reset( | 62 upload_data_stream_.reset( |
62 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)); | 63 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)); |
63 } | 64 } |
64 | 65 |
66 void URLRequestAdapter::EnableChunkedUpload() { | |
67 chunked_upload_ = true; | |
68 } | |
69 | |
70 void URLRequestAdapter::AppendChunk(const char* bytes, | |
71 int bytes_len, | |
72 bool is_last_chunk) { | |
73 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk; | |
74 | |
75 context_->GetNetworkTaskRunner()->PostTask( | |
76 FROM_HERE, | |
77 base::Bind(&URLRequestAdapter::OnAppendChunk, | |
78 base::Unretained(this), | |
79 bytes, | |
80 bytes_len, | |
81 is_last_chunk)); | |
82 } | |
83 | |
65 std::string URLRequestAdapter::GetHeader(const std::string& name) const { | 84 std::string URLRequestAdapter::GetHeader(const std::string& name) const { |
66 std::string value; | 85 std::string value; |
67 if (url_request_ != NULL) { | 86 if (url_request_ != NULL) { |
68 url_request_->GetResponseHeaderByName(name, &value); | 87 url_request_->GetResponseHeaderByName(name, &value); |
69 } | 88 } |
70 return value; | 89 return value; |
71 } | 90 } |
72 | 91 |
73 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const { | 92 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const { |
74 if (url_request_ == NULL) { | 93 if (url_request_ == NULL) { |
75 return NULL; | 94 return NULL; |
76 } | 95 } |
77 return url_request_->response_headers(); | 96 return url_request_->response_headers(); |
78 } | 97 } |
79 | 98 |
99 bool URLRequestAdapter::GetFullRequestHeaders( | |
mef
2014/08/15 15:42:52
Do you need this on the Java side?
mdumitrescu
2014/08/15 15:50:20
I was looking into this. It seems we don't right n
| |
100 net::HttpRequestHeaders* headers) const { | |
101 if (url_request_ == NULL) { | |
102 return false; | |
103 } | |
104 return url_request_->GetFullRequestHeaders(headers); | |
105 } | |
106 | |
80 void URLRequestAdapter::Start() { | 107 void URLRequestAdapter::Start() { |
81 context_->GetNetworkTaskRunner()->PostTask( | 108 context_->GetNetworkTaskRunner()->PostTask( |
82 FROM_HERE, | 109 FROM_HERE, |
83 base::Bind(&URLRequestAdapter::OnInitiateConnection, | 110 base::Bind(&URLRequestAdapter::OnInitiateConnection, |
84 base::Unretained(this))); | 111 base::Unretained(this))); |
85 } | 112 } |
86 | 113 |
114 void URLRequestAdapter::OnAppendChunk(const char* bytes, | |
115 int bytes_len, | |
116 bool is_last_chunk) { | |
mef
2014/08/15 13:30:22
nit: align with 'const'.
mdumitrescu
2014/08/15 15:23:13
Done.
| |
117 if (url_request_ != NULL) { | |
118 url_request_->AppendChunkToUpload(bytes, bytes_len, is_last_chunk); | |
119 delegate_->OnAppendChunkCompleted(this); | |
120 } | |
121 } | |
122 | |
87 void URLRequestAdapter::OnInitiateConnection() { | 123 void URLRequestAdapter::OnInitiateConnection() { |
88 if (canceled_) { | 124 if (canceled_) { |
89 return; | 125 return; |
90 } | 126 } |
91 | 127 |
92 VLOG(1) << "Starting chromium request: " | 128 VLOG(1) << "Starting chromium request: " |
93 << url_.possibly_invalid_spec().c_str() | 129 << url_.possibly_invalid_spec().c_str() |
94 << " priority: " << RequestPriorityToString(priority_); | 130 << " priority: " << RequestPriorityToString(priority_); |
95 url_request_ = new net::URLRequest( | 131 url_request_ = new net::URLRequest( |
96 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext()); | 132 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext()); |
97 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | 133 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
98 net::LOAD_DO_NOT_SAVE_COOKIES | | 134 net::LOAD_DO_NOT_SAVE_COOKIES | |
99 net::LOAD_DO_NOT_SEND_COOKIES); | 135 net::LOAD_DO_NOT_SEND_COOKIES); |
100 url_request_->set_method(method_); | 136 url_request_->set_method(method_); |
101 url_request_->SetExtraRequestHeaders(headers_); | 137 url_request_->SetExtraRequestHeaders(headers_); |
102 if (!headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { | 138 if (!headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { |
103 std::string user_agent; | 139 std::string user_agent; |
104 user_agent = context_->GetUserAgent(url_); | 140 user_agent = context_->GetUserAgent(url_); |
105 url_request_->SetExtraRequestHeaderByName( | 141 url_request_->SetExtraRequestHeaderByName( |
106 net::HttpRequestHeaders::kUserAgent, user_agent, true /* override */); | 142 net::HttpRequestHeaders::kUserAgent, user_agent, true /* override */); |
107 } | 143 } |
108 | 144 |
109 if (upload_data_stream_) | 145 if (upload_data_stream_) { |
110 url_request_->set_upload(upload_data_stream_.Pass()); | 146 url_request_->set_upload(make_scoped_ptr(upload_data_stream_.release())); |
mef
2014/08/15 13:30:22
Pass() is fine here.
mdumitrescu
2014/08/15 15:23:13
Oops. Unintended change.
| |
147 } else if (chunked_upload_) { | |
148 url_request_->EnableChunkedUpload(); | |
149 } | |
111 | 150 |
112 url_request_->SetPriority(priority_); | 151 url_request_->SetPriority(priority_); |
113 | 152 |
114 url_request_->Start(); | 153 url_request_->Start(); |
115 } | 154 } |
116 | 155 |
117 void URLRequestAdapter::Cancel() { | 156 void URLRequestAdapter::Cancel() { |
118 if (canceled_) { | 157 if (canceled_) { |
119 return; | 158 return; |
120 } | 159 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 | 295 |
257 delegate_->OnBytesRead(this); | 296 delegate_->OnBytesRead(this); |
258 delegate_->OnRequestFinished(this); | 297 delegate_->OnRequestFinished(this); |
259 } | 298 } |
260 | 299 |
261 unsigned char* URLRequestAdapter::Data() const { | 300 unsigned char* URLRequestAdapter::Data() const { |
262 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); | 301 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); |
263 } | 302 } |
264 | 303 |
265 } // namespace cronet | 304 } // namespace cronet |
OLD | NEW |