| 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 | 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 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 if (!url_request_->status().is_success()) | 234 if (!url_request_->status().is_success()) |
| 234 status->error = MakeNetworkError(url_request_->status().error()); | 235 status->error = MakeNetworkError(url_request_->status().error()); |
| 235 } else { | 236 } else { |
| 236 status->is_loading = false; | 237 status->is_loading = false; |
| 237 } | 238 } |
| 238 // TODO(darin): Populate more status fields. | 239 // TODO(darin): Populate more status fields. |
| 239 callback.Run(status.Pass()); | 240 callback.Run(status.Pass()); |
| 240 } | 241 } |
| 241 | 242 |
| 242 void URLLoaderImpl::OnReceivedRedirect(net::URLRequest* url_request, | 243 void URLLoaderImpl::OnReceivedRedirect(net::URLRequest* url_request, |
| 243 const GURL& new_url, | 244 const net::RedirectInfo& redirect_info, |
| 244 bool* defer_redirect) { | 245 bool* defer_redirect) { |
| 245 DCHECK(url_request == url_request_.get()); | 246 DCHECK(url_request == url_request_.get()); |
| 246 DCHECK(url_request->status().is_success()); | 247 DCHECK(url_request->status().is_success()); |
| 247 | 248 |
| 248 if (auto_follow_redirects_) | 249 if (auto_follow_redirects_) |
| 249 return; | 250 return; |
| 250 | 251 |
| 251 // Send the redirect response to the client, allowing them to inspect it and | 252 // Send the redirect response to the client, allowing them to inspect it and |
| 252 // optionally follow the redirect. | 253 // optionally follow the redirect. |
| 253 *defer_redirect = true; | 254 *defer_redirect = true; |
| 254 | 255 |
| 255 URLResponsePtr response = MakeURLResponse(url_request); | 256 URLResponsePtr response = MakeURLResponse(url_request); |
| 256 response->redirect_method = | 257 response->redirect_method = redirect_info.new_method; |
| 257 net::URLRequest::ComputeMethodForRedirect(url_request->method(), | 258 response->redirect_url = String::From(redirect_info.new_url); |
| 258 response->status_code); | |
| 259 response->redirect_url = String::From(new_url); | |
| 260 | 259 |
| 261 SendResponse(response.Pass()); | 260 SendResponse(response.Pass()); |
| 262 } | 261 } |
| 263 | 262 |
| 264 void URLLoaderImpl::OnResponseStarted(net::URLRequest* url_request) { | 263 void URLLoaderImpl::OnResponseStarted(net::URLRequest* url_request) { |
| 265 DCHECK(url_request == url_request_.get()); | 264 DCHECK(url_request == url_request_.get()); |
| 266 | 265 |
| 267 if (!url_request->status().is_success()) { | 266 if (!url_request->status().is_success()) { |
| 268 SendError(url_request->status().error(), callback_); | 267 SendError(url_request->status().error(), callback_); |
| 269 callback_ = Callback<void(URLResponsePtr)>(); | 268 callback_ = Callback<void(URLResponsePtr)>(); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 if (completed_synchronously) { | 372 if (completed_synchronously) { |
| 374 base::MessageLoop::current()->PostTask( | 373 base::MessageLoop::current()->PostTask( |
| 375 FROM_HERE, | 374 FROM_HERE, |
| 376 base::Bind(&URLLoaderImpl::ReadMore, weak_ptr_factory_.GetWeakPtr())); | 375 base::Bind(&URLLoaderImpl::ReadMore, weak_ptr_factory_.GetWeakPtr())); |
| 377 } else { | 376 } else { |
| 378 ReadMore(); | 377 ReadMore(); |
| 379 } | 378 } |
| 380 } | 379 } |
| 381 | 380 |
| 382 } // namespace mojo | 381 } // namespace mojo |
| OLD | NEW |