OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "mojo/examples/html_viewer/weburlloader_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "mojo/services/public/interfaces/network/network_service.mojom.h" |
| 10 #include "third_party/WebKit/public/platform/WebURLError.h" |
| 11 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" |
| 12 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 13 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 14 |
| 15 namespace mojo { |
| 16 namespace examples { |
| 17 namespace { |
| 18 |
| 19 blink::WebURLResponse ToWebURLResponse(const URLResponsePtr& url_response) { |
| 20 blink::WebURLResponse result; |
| 21 result.initialize(); |
| 22 result.setURL(GURL(url_response->url)); |
| 23 // TODO(darin): Copy other fields. |
| 24 return result; |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 WebURLLoaderImpl::WebURLLoaderImpl(NetworkService* network_service) |
| 30 : client_(NULL), |
| 31 weak_factory_(this) { |
| 32 network_service->CreateURLLoader(Get(&url_loader_)); |
| 33 url_loader_.set_client(this); |
| 34 } |
| 35 |
| 36 WebURLLoaderImpl::~WebURLLoaderImpl() { |
| 37 } |
| 38 |
| 39 void WebURLLoaderImpl::loadSynchronously( |
| 40 const blink::WebURLRequest& request, |
| 41 blink::WebURLResponse& response, |
| 42 blink::WebURLError& error, |
| 43 blink::WebData& data) { |
| 44 NOTIMPLEMENTED(); |
| 45 } |
| 46 |
| 47 void WebURLLoaderImpl::loadAsynchronously(const blink::WebURLRequest& request, |
| 48 blink::WebURLLoaderClient* client) { |
| 49 client_ = client; |
| 50 |
| 51 URLRequestPtr url_request(URLRequest::New()); |
| 52 url_request->url = request.url().spec(); |
| 53 url_request->auto_follow_redirects = false; |
| 54 // TODO(darin): Copy other fields. |
| 55 |
| 56 DataPipe pipe; |
| 57 url_loader_->Start(url_request.Pass(), pipe.producer_handle.Pass()); |
| 58 response_body_stream_ = pipe.consumer_handle.Pass(); |
| 59 } |
| 60 |
| 61 void WebURLLoaderImpl::cancel() { |
| 62 url_loader_.reset(); |
| 63 response_body_stream_.reset(); |
| 64 // TODO(darin): Need to asynchronously call didFail. |
| 65 } |
| 66 |
| 67 void WebURLLoaderImpl::setDefersLoading(bool defers_loading) { |
| 68 NOTIMPLEMENTED(); |
| 69 } |
| 70 |
| 71 void WebURLLoaderImpl::OnReceivedRedirect(URLResponsePtr url_response, |
| 72 const String& new_url, |
| 73 const String& new_method) { |
| 74 blink::WebURLRequest new_request; |
| 75 new_request.initialize(); |
| 76 new_request.setURL(GURL(new_url)); |
| 77 |
| 78 client_->willSendRequest(this, new_request, ToWebURLResponse(url_response)); |
| 79 // TODO(darin): Check if new_request was rejected. |
| 80 |
| 81 url_loader_->FollowRedirect(); |
| 82 } |
| 83 |
| 84 void WebURLLoaderImpl::OnReceivedResponse(URLResponsePtr url_response) { |
| 85 client_->didReceiveResponse(this, ToWebURLResponse(url_response)); |
| 86 |
| 87 // Start streaming data |
| 88 ReadMore(); |
| 89 } |
| 90 |
| 91 void WebURLLoaderImpl::OnReceivedError(NetworkErrorPtr error) { |
| 92 // TODO(darin): Construct a meaningful WebURLError. |
| 93 client_->didFail(this, blink::WebURLError()); |
| 94 } |
| 95 |
| 96 void WebURLLoaderImpl::OnReceivedEndOfResponseBody() { |
| 97 // This is the signal that the response body was not truncated. |
| 98 } |
| 99 |
| 100 void WebURLLoaderImpl::ReadMore() { |
| 101 const void* buf; |
| 102 uint32_t buf_size; |
| 103 MojoResult rv = BeginReadDataRaw(response_body_stream_.get(), |
| 104 &buf, |
| 105 &buf_size, |
| 106 MOJO_READ_DATA_FLAG_NONE); |
| 107 if (rv == MOJO_RESULT_OK) { |
| 108 client_->didReceiveData(this, static_cast<const char*>(buf), buf_size, -1); |
| 109 EndReadDataRaw(response_body_stream_.get(), buf_size); |
| 110 WaitToReadMore(); |
| 111 } else if (rv == MOJO_RESULT_SHOULD_WAIT) { |
| 112 WaitToReadMore(); |
| 113 } else if (rv == MOJO_RESULT_FAILED_PRECONDITION) { |
| 114 // We reached end-of-file. |
| 115 double finish_time = base::Time::Now().ToDoubleT(); |
| 116 client_->didFinishLoading( |
| 117 this, |
| 118 finish_time, |
| 119 blink::WebURLLoaderClient::kUnknownEncodedDataLength); |
| 120 } else { |
| 121 // TODO(darin): Oops! |
| 122 } |
| 123 } |
| 124 |
| 125 void WebURLLoaderImpl::WaitToReadMore() { |
| 126 handle_watcher_.Start( |
| 127 response_body_stream_.get(), |
| 128 MOJO_HANDLE_SIGNAL_READABLE, |
| 129 MOJO_DEADLINE_INDEFINITE, |
| 130 base::Bind(&WebURLLoaderImpl::OnResponseBodyStreamReady, |
| 131 weak_factory_.GetWeakPtr())); |
| 132 } |
| 133 |
| 134 void WebURLLoaderImpl::OnResponseBodyStreamReady(MojoResult result) { |
| 135 ReadMore(); |
| 136 } |
| 137 |
| 138 } // namespace examples |
| 139 } // namespace mojo |
OLD | NEW |