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