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 "mojo/services/network/url_loader_impl.h" | 5 #include "mojo/services/network/url_loader_impl.h" |
6 | 6 |
7 #include "base/memory/scoped_vector.h" | 7 #include "base/memory/scoped_vector.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "mojo/common/common_type_converters.h" | 9 #include "mojo/common/common_type_converters.h" |
10 #include "mojo/services/network/network_context.h" | 10 #include "mojo/services/network/network_context.h" |
11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
12 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
13 #include "net/base/upload_bytes_element_reader.h" | 13 #include "net/base/upload_bytes_element_reader.h" |
14 #include "net/base/upload_data_stream.h" | 14 #include "net/base/upload_data_stream.h" |
15 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 16 #include "net/url_request/url_request_context.h" |
16 | 17 |
17 namespace mojo { | 18 namespace mojo { |
18 namespace { | 19 namespace { |
19 | 20 |
20 const uint32_t kMaxReadSize = 64 * 1024; | 21 const uint32_t kMaxReadSize = 64 * 1024; |
21 | 22 |
22 // Generates an URLResponsePtr from the response state of a net::URLRequest. | 23 // Generates an URLResponsePtr from the response state of a net::URLRequest. |
23 URLResponsePtr MakeURLResponse(const net::URLRequest* url_request) { | 24 URLResponsePtr MakeURLResponse(const net::URLRequest* url_request) { |
24 URLResponsePtr response(URLResponse::New()); | 25 URLResponsePtr response(URLResponse::New()); |
25 response->url = String::From(url_request->url()); | 26 response->url = String::From(url_request->url()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 virtual uint64 BytesRemaining() const OVERRIDE { | 76 virtual uint64 BytesRemaining() const OVERRIDE { |
76 return num_bytes_ - offset_; | 77 return num_bytes_ - offset_; |
77 } | 78 } |
78 virtual bool IsInMemory() const OVERRIDE { | 79 virtual bool IsInMemory() const OVERRIDE { |
79 return false; | 80 return false; |
80 } | 81 } |
81 virtual int Read(net::IOBuffer* buf, | 82 virtual int Read(net::IOBuffer* buf, |
82 int buf_length, | 83 int buf_length, |
83 const net::CompletionCallback& callback) OVERRIDE { | 84 const net::CompletionCallback& callback) OVERRIDE { |
84 uint32_t bytes_read = | 85 uint32_t bytes_read = |
85 std::min(BytesRemaining(), static_cast<uint64>(buf_length)); | 86 std::min(static_cast<uint32_t>(BytesRemaining()), |
| 87 static_cast<uint32_t>(buf_length)); |
86 if (bytes_read > 0) { | 88 if (bytes_read > 0) { |
87 ReadDataRaw(pipe_.get(), buf->data(), &bytes_read, | 89 ReadDataRaw(pipe_.get(), buf->data(), &bytes_read, |
88 MOJO_READ_DATA_FLAG_NONE); | 90 MOJO_READ_DATA_FLAG_NONE); |
89 } | 91 } |
90 | 92 |
91 offset_ += bytes_read; | 93 offset_ += bytes_read; |
92 return bytes_read; | 94 return bytes_read; |
93 } | 95 } |
94 | 96 |
95 private: | 97 private: |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 if (url_request_) { | 172 if (url_request_) { |
171 SendError(net::ERR_UNEXPECTED, callback); | 173 SendError(net::ERR_UNEXPECTED, callback); |
172 return; | 174 return; |
173 } | 175 } |
174 | 176 |
175 if (!request) { | 177 if (!request) { |
176 SendError(net::ERR_INVALID_ARGUMENT, callback); | 178 SendError(net::ERR_INVALID_ARGUMENT, callback); |
177 return; | 179 return; |
178 } | 180 } |
179 | 181 |
180 url_request_.reset( | 182 url_request_ = context_->url_request_context()->CreateRequest( |
181 new net::URLRequest(GURL(request->url), | 183 GURL(request->url), |
182 net::DEFAULT_PRIORITY, | 184 net::DEFAULT_PRIORITY, |
183 this, | 185 this, |
184 context_->url_request_context())); | 186 NULL); |
185 url_request_->set_method(request->method); | 187 url_request_->set_method(request->method); |
186 if (request->headers) { | 188 if (request->headers) { |
187 net::HttpRequestHeaders headers; | 189 net::HttpRequestHeaders headers; |
188 for (size_t i = 0; i < request->headers.size(); ++i) | 190 for (size_t i = 0; i < request->headers.size(); ++i) |
189 headers.AddHeaderFromString(request->headers[i].To<base::StringPiece>()); | 191 headers.AddHeaderFromString(request->headers[i].To<base::StringPiece>()); |
190 url_request_->SetExtraRequestHeaders(headers); | 192 url_request_->SetExtraRequestHeaders(headers); |
191 } | 193 } |
192 if (request->body) { | 194 if (request->body) { |
193 ScopedVector<net::UploadElementReader> element_readers; | 195 ScopedVector<net::UploadElementReader> element_readers; |
194 for (size_t i = 0; i < request->body.size(); ++i) { | 196 for (size_t i = 0; i < request->body.size(); ++i) { |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 if (completed_synchronously) { | 375 if (completed_synchronously) { |
374 base::MessageLoop::current()->PostTask( | 376 base::MessageLoop::current()->PostTask( |
375 FROM_HERE, | 377 FROM_HERE, |
376 base::Bind(&URLLoaderImpl::ReadMore, weak_ptr_factory_.GetWeakPtr())); | 378 base::Bind(&URLLoaderImpl::ReadMore, weak_ptr_factory_.GetWeakPtr())); |
377 } else { | 379 } else { |
378 ReadMore(); | 380 ReadMore(); |
379 } | 381 } |
380 } | 382 } |
381 | 383 |
382 } // namespace mojo | 384 } // namespace mojo |
OLD | NEW |