| 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 "base/thread_task_runner_handle.h" | |
| 10 #include "mojo/common/common_type_converters.h" | |
| 11 #include "mojo/services/public/interfaces/network/network_service.mojom.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "third_party/WebKit/public/platform/WebURLError.h" | |
| 14 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h" | |
| 15 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" | |
| 16 #include "third_party/WebKit/public/platform/WebURLResponse.h" | |
| 17 | |
| 18 namespace mojo { | |
| 19 namespace examples { | |
| 20 namespace { | |
| 21 | |
| 22 blink::WebURLResponse ToWebURLResponse(const URLResponsePtr& url_response) { | |
| 23 blink::WebURLResponse result; | |
| 24 result.initialize(); | |
| 25 result.setURL(GURL(url_response->url)); | |
| 26 result.setMIMEType(blink::WebString::fromUTF8(url_response->mime_type)); | |
| 27 result.setTextEncodingName(blink::WebString::fromUTF8(url_response->charset)); | |
| 28 result.setHTTPStatusCode(url_response->status_code); | |
| 29 | |
| 30 // TODO(darin): Initialize timing properly. | |
| 31 blink::WebURLLoadTiming timing; | |
| 32 timing.initialize(); | |
| 33 result.setLoadTiming(timing); | |
| 34 | |
| 35 // TODO(darin): Copy other fields. | |
| 36 return result; | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 WebURLRequestExtraData::WebURLRequestExtraData() { | |
| 42 } | |
| 43 | |
| 44 WebURLRequestExtraData::~WebURLRequestExtraData() { | |
| 45 } | |
| 46 | |
| 47 WebURLLoaderImpl::WebURLLoaderImpl(NetworkService* network_service) | |
| 48 : client_(NULL), | |
| 49 weak_factory_(this) { | |
| 50 network_service->CreateURLLoader(Get(&url_loader_)); | |
| 51 } | |
| 52 | |
| 53 WebURLLoaderImpl::~WebURLLoaderImpl() { | |
| 54 } | |
| 55 | |
| 56 void WebURLLoaderImpl::loadSynchronously( | |
| 57 const blink::WebURLRequest& request, | |
| 58 blink::WebURLResponse& response, | |
| 59 blink::WebURLError& error, | |
| 60 blink::WebData& data) { | |
| 61 NOTIMPLEMENTED(); | |
| 62 } | |
| 63 | |
| 64 void WebURLLoaderImpl::loadAsynchronously(const blink::WebURLRequest& request, | |
| 65 blink::WebURLLoaderClient* client) { | |
| 66 client_ = client; | |
| 67 url_ = request.url(); | |
| 68 | |
| 69 URLRequestPtr url_request(URLRequest::New()); | |
| 70 url_request->url = String::From(url_); | |
| 71 url_request->auto_follow_redirects = false; | |
| 72 // TODO(darin): Copy other fields. | |
| 73 | |
| 74 if (request.extraData()) { | |
| 75 WebURLRequestExtraData* extra_data = | |
| 76 static_cast<WebURLRequestExtraData*>(request.extraData()); | |
| 77 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 78 FROM_HERE, | |
| 79 base::Bind(&WebURLLoaderImpl::OnReceivedResponse, | |
| 80 weak_factory_.GetWeakPtr(), | |
| 81 base::Passed(&extra_data->synthetic_response))); | |
| 82 } else { | |
| 83 url_loader_->Start(url_request.Pass(), | |
| 84 base::Bind(&WebURLLoaderImpl::OnReceivedResponse, | |
| 85 weak_factory_.GetWeakPtr())); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void WebURLLoaderImpl::cancel() { | |
| 90 url_loader_.reset(); | |
| 91 response_body_stream_.reset(); | |
| 92 | |
| 93 URLResponsePtr failed_response(URLResponse::New()); | |
| 94 failed_response->url = String::From(url_); | |
| 95 failed_response->error = NetworkError::New(); | |
| 96 failed_response->error->code = net::ERR_ABORTED; | |
| 97 | |
| 98 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 99 FROM_HERE, | |
| 100 base::Bind(&WebURLLoaderImpl::OnReceivedResponse, | |
| 101 weak_factory_.GetWeakPtr(), | |
| 102 base::Passed(&failed_response))); | |
| 103 } | |
| 104 | |
| 105 void WebURLLoaderImpl::setDefersLoading(bool defers_loading) { | |
| 106 NOTIMPLEMENTED(); | |
| 107 } | |
| 108 | |
| 109 void WebURLLoaderImpl::OnReceivedResponse(URLResponsePtr url_response) { | |
| 110 url_ = GURL(url_response->url); | |
| 111 | |
| 112 if (url_response->error) { | |
| 113 OnReceivedError(url_response.Pass()); | |
| 114 } else if (url_response->redirect_url) { | |
| 115 OnReceivedRedirect(url_response.Pass()); | |
| 116 } else { | |
| 117 client_->didReceiveResponse(this, ToWebURLResponse(url_response)); | |
| 118 | |
| 119 // Start streaming data | |
| 120 response_body_stream_ = url_response->body.Pass(); | |
| 121 ReadMore(); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 void WebURLLoaderImpl::OnReceivedError(URLResponsePtr url_response) { | |
| 126 blink::WebURLError web_error; | |
| 127 web_error.domain = blink::WebString::fromUTF8(net::kErrorDomain); | |
| 128 web_error.reason = url_response->error->code; | |
| 129 web_error.unreachableURL = GURL(url_response->url); | |
| 130 web_error.staleCopyInCache = false; | |
| 131 web_error.isCancellation = | |
| 132 url_response->error->code == net::ERR_ABORTED ? true : false; | |
| 133 | |
| 134 client_->didFail(this, web_error); | |
| 135 } | |
| 136 | |
| 137 void WebURLLoaderImpl::OnReceivedRedirect(URLResponsePtr url_response) { | |
| 138 blink::WebURLRequest new_request; | |
| 139 new_request.initialize(); | |
| 140 new_request.setURL(GURL(url_response->redirect_url)); | |
| 141 new_request.setHTTPMethod( | |
| 142 blink::WebString::fromUTF8(url_response->redirect_method)); | |
| 143 | |
| 144 client_->willSendRequest(this, new_request, ToWebURLResponse(url_response)); | |
| 145 // TODO(darin): Check if new_request was rejected. | |
| 146 | |
| 147 url_loader_->FollowRedirect( | |
| 148 base::Bind(&WebURLLoaderImpl::OnReceivedResponse, | |
| 149 weak_factory_.GetWeakPtr())); | |
| 150 } | |
| 151 | |
| 152 void WebURLLoaderImpl::ReadMore() { | |
| 153 const void* buf; | |
| 154 uint32_t buf_size; | |
| 155 MojoResult rv = BeginReadDataRaw(response_body_stream_.get(), | |
| 156 &buf, | |
| 157 &buf_size, | |
| 158 MOJO_READ_DATA_FLAG_NONE); | |
| 159 if (rv == MOJO_RESULT_OK) { | |
| 160 client_->didReceiveData(this, static_cast<const char*>(buf), buf_size, -1); | |
| 161 EndReadDataRaw(response_body_stream_.get(), buf_size); | |
| 162 WaitToReadMore(); | |
| 163 } else if (rv == MOJO_RESULT_SHOULD_WAIT) { | |
| 164 WaitToReadMore(); | |
| 165 } else if (rv == MOJO_RESULT_FAILED_PRECONDITION) { | |
| 166 // We reached end-of-file. | |
| 167 double finish_time = base::Time::Now().ToDoubleT(); | |
| 168 client_->didFinishLoading( | |
| 169 this, | |
| 170 finish_time, | |
| 171 blink::WebURLLoaderClient::kUnknownEncodedDataLength); | |
| 172 } else { | |
| 173 // TODO(darin): Oops! | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 void WebURLLoaderImpl::WaitToReadMore() { | |
| 178 handle_watcher_.Start( | |
| 179 response_body_stream_.get(), | |
| 180 MOJO_HANDLE_SIGNAL_READABLE, | |
| 181 MOJO_DEADLINE_INDEFINITE, | |
| 182 base::Bind(&WebURLLoaderImpl::OnResponseBodyStreamReady, | |
| 183 weak_factory_.GetWeakPtr())); | |
| 184 } | |
| 185 | |
| 186 void WebURLLoaderImpl::OnResponseBodyStreamReady(MojoResult result) { | |
| 187 ReadMore(); | |
| 188 } | |
| 189 | |
| 190 } // namespace examples | |
| 191 } // namespace mojo | |
| OLD | NEW |