Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "content/browser/loader/mojo_async_resource_handler.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/containers/hash_tables.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/time/time.h" | |
| 15 #include "content/browser/loader/netlog_observer.h" | |
| 16 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
| 17 #include "content/browser/loader/resource_message_filter.h" | |
| 18 #include "content/browser/loader/resource_request_info_impl.h" | |
| 19 #include "content/common/resource_request_completion_status.h" | |
| 20 #include "content/public/browser/resource_dispatcher_host_delegate.h" | |
| 21 #include "content/public/common/resource_response.h" | |
| 22 #include "net/base/io_buffer.h" | |
| 23 #include "net/base/load_flags.h" | |
| 24 #include "net/log/net_log.h" | |
| 25 #include "net/url_request/redirect_info.h" | |
| 26 | |
| 27 namespace content { | |
| 28 namespace { | |
| 29 | |
| 30 int g_max_allocation_size = 1024 * 32; | |
| 31 | |
| 32 void GetNumericArg(const std::string& name, int* result) { | |
| 33 const std::string& value = | |
| 34 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(name); | |
| 35 if (!value.empty()) | |
| 36 base::StringToInt(value, result); | |
| 37 } | |
| 38 | |
| 39 void InitializeResourceBufferConstants() { | |
| 40 static bool did_init = false; | |
| 41 if (did_init) | |
| 42 return; | |
| 43 did_init = true; | |
| 44 | |
| 45 GetNumericArg("resource-buffer-max-allocation-size", &g_max_allocation_size); | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 MojoAsyncResourceHandler::MojoAsyncResourceHandler( | |
| 51 net::URLRequest* request, | |
| 52 ResourceDispatcherHostImpl* rdh, | |
| 53 std::unique_ptr<mojom::URLLoader> url_loader, | |
| 54 mojom::URLLoaderClientPtr url_loader_client) | |
| 55 : ResourceHandler(request), | |
| 56 rdh_(rdh), | |
| 57 url_loader_(std::move(url_loader)), | |
| 58 url_loader_client_(std::move(url_loader_client)) { | |
| 59 DCHECK(url_loader_); | |
| 60 DCHECK(url_loader_client_); | |
| 61 InitializeResourceBufferConstants(); | |
| 62 } | |
| 63 | |
| 64 MojoAsyncResourceHandler::~MojoAsyncResourceHandler() { | |
| 65 if (has_checked_for_sufficient_resources_) | |
| 66 rdh_->FinishedWithResourcesForRequest(request()); | |
| 67 } | |
| 68 | |
| 69 void MojoAsyncResourceHandler::OnFollowRedirect(int request_id) { | |
| 70 NOTREACHED(); | |
| 71 } | |
| 72 | |
| 73 bool MojoAsyncResourceHandler::OnRequestRedirected( | |
| 74 const net::RedirectInfo& redirect_info, | |
| 75 ResourceResponse* response, | |
| 76 bool* defer) { | |
| 77 // Not implemented. | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, | |
| 82 bool* defer) { | |
| 83 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
| 84 if (!info->filter()) | |
| 85 return false; | |
| 86 | |
| 87 if (rdh_->delegate()) { | |
| 88 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), response, | |
| 89 info->filter()); | |
| 90 } | |
| 91 | |
| 92 NetLogObserver::PopulateResponseInfo(request(), response); | |
| 93 | |
| 94 response->head.request_start = request()->creation_time(); | |
| 95 response->head.response_start = base::TimeTicks::Now(); | |
| 96 sent_received_response_message_ = true; | |
| 97 url_loader_client_->OnReceiveResponse(response->head); | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 bool MojoAsyncResourceHandler::OnBeforeNetworkStart(const GURL& url, | |
| 106 bool* defer) { | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
| 111 int* buf_size, | |
| 112 int min_size) { | |
| 113 DCHECK_EQ(-1, min_size); | |
| 114 | |
| 115 if (!CheckForSufficientResource()) | |
| 116 return false; | |
| 117 | |
| 118 void* buffer = nullptr; | |
| 119 uint32_t available = 0; | |
| 120 if (!writer_.is_valid()) { | |
| 121 MojoCreateDataPipeOptions options; | |
| 122 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
| 123 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
| 124 options.element_num_bytes = 1; | |
| 125 options.capacity_num_bytes = g_max_allocation_size; | |
| 126 mojo::DataPipe data_pipe(options); | |
| 127 | |
| 128 writer_ = std::move(data_pipe.producer_handle); | |
| 129 ResourceMessageFilter* filter = GetRequestInfo()->filter(); | |
| 130 if (filter) { | |
| 131 url_loader_client_->OnStartLoadingResponseBody( | |
| 132 std::move(data_pipe.consumer_handle)); | |
| 133 } | |
| 134 } | |
| 135 if (!writer_.is_valid()) | |
| 136 return false; | |
| 137 | |
| 138 MojoResult result = mojo::BeginWriteDataRaw( | |
|
yhirano
2016/06/03 11:27:44
I've just noticed that this breaks OnWillRead's po
yhirano
2016/06/06 22:54:03
Done.
| |
| 139 writer_.get(), &buffer, &available, MOJO_WRITE_DATA_FLAG_NONE); | |
| 140 // TODO(yhirano): Take care of SHOULD_WAIT. | |
| 141 if (result != MOJO_RESULT_OK) | |
| 142 return false; | |
| 143 | |
| 144 *buf = new net::WrappedIOBuffer(static_cast<const char*>(buffer)); | |
| 145 *buf_size = available; | |
| 146 return true; | |
| 147 } | |
| 148 | |
| 149 bool MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | |
| 150 DCHECK_GE(bytes_read, 0); | |
| 151 | |
| 152 if (!bytes_read) | |
| 153 return true; | |
| 154 | |
| 155 MojoResult result = mojo::EndWriteDataRaw(writer_.get(), bytes_read); | |
| 156 if (result != MOJO_RESULT_OK) | |
| 157 return false; | |
| 158 void* buffer = nullptr; | |
| 159 uint32_t available = 0; | |
| 160 // To see if the handle is still writable. | |
| 161 result = mojo::BeginWriteDataRaw(writer_.get(), &buffer, &available, | |
| 162 MOJO_WRITE_DATA_FLAG_NONE); | |
| 163 if (result == MOJO_RESULT_SHOULD_WAIT) { | |
| 164 *defer = did_defer_ = true; | |
| 165 OnDefer(); | |
| 166 handle_watcher_.Start(writer_.get(), MOJO_HANDLE_SIGNAL_WRITABLE, | |
| 167 MOJO_DEADLINE_INDEFINITE, | |
| 168 base::Bind(&MojoAsyncResourceHandler::OnWritable, | |
| 169 base::Unretained(this))); | |
| 170 } | |
| 171 if (result == MOJO_RESULT_OK) | |
| 172 mojo::EndWriteDataRaw(writer_.get(), 0); | |
| 173 return true; | |
| 174 } | |
| 175 | |
| 176 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { | |
| 177 // Not implemented. | |
| 178 } | |
| 179 | |
| 180 void MojoAsyncResourceHandler::OnResponseCompleted( | |
| 181 const net::URLRequestStatus& status, | |
| 182 const std::string& security_info, | |
| 183 bool* defer) { | |
| 184 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
| 185 if (!info->filter()) | |
| 186 return; | |
| 187 | |
| 188 // TODO(gavinp): Remove this CHECK when we figure out the cause of | |
| 189 // http://crbug.com/124680 . This check mirrors closely check in | |
| 190 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore | |
| 191 // ResourceHandleInternal which asserts on its state and crashes. By crashing | |
| 192 // when the message is sent, we should get better crash reports. | |
| 193 CHECK(status.status() != net::URLRequestStatus::SUCCESS || | |
| 194 sent_received_response_message_); | |
| 195 | |
| 196 int error_code = status.error(); | |
| 197 bool was_ignored_by_handler = info->WasIgnoredByHandler(); | |
| 198 | |
| 199 DCHECK(status.status() != net::URLRequestStatus::IO_PENDING); | |
| 200 // If this check fails, then we're in an inconsistent state because all | |
| 201 // requests ignored by the handler should be canceled (which should result in | |
| 202 // the ERR_ABORTED error code). | |
| 203 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED); | |
| 204 | |
| 205 // TODO(mkosiba): Fix up cases where we create a URLRequestStatus | |
| 206 // with a status() != SUCCESS and an error_code() == net::OK. | |
| 207 if (status.status() == net::URLRequestStatus::CANCELED && | |
| 208 error_code == net::OK) { | |
| 209 error_code = net::ERR_ABORTED; | |
| 210 } else if (status.status() == net::URLRequestStatus::FAILED && | |
| 211 error_code == net::OK) { | |
| 212 error_code = net::ERR_FAILED; | |
| 213 } | |
| 214 | |
| 215 ResourceRequestCompletionStatus request_complete_data; | |
| 216 request_complete_data.error_code = error_code; | |
| 217 request_complete_data.was_ignored_by_handler = was_ignored_by_handler; | |
| 218 request_complete_data.exists_in_cache = request()->response_info().was_cached; | |
| 219 request_complete_data.security_info = security_info; | |
| 220 request_complete_data.completion_time = base::TimeTicks::Now(); | |
| 221 request_complete_data.encoded_data_length = | |
| 222 request()->GetTotalReceivedBytes(); | |
| 223 | |
| 224 url_loader_client_->OnComplete(request_complete_data); | |
| 225 } | |
| 226 | |
| 227 void MojoAsyncResourceHandler::ResumeIfDeferred() { | |
| 228 if (did_defer_) { | |
| 229 did_defer_ = false; | |
| 230 request()->LogUnblocked(); | |
| 231 controller()->Resume(); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 void MojoAsyncResourceHandler::OnDefer() { | |
| 236 request()->LogBlockedBy("MojoAsyncResourceHandler"); | |
| 237 } | |
| 238 | |
| 239 bool MojoAsyncResourceHandler::CheckForSufficientResource() { | |
| 240 if (has_checked_for_sufficient_resources_) | |
| 241 return true; | |
| 242 has_checked_for_sufficient_resources_ = true; | |
| 243 | |
| 244 if (rdh_->HasSufficientResourcesForRequest(request())) | |
| 245 return true; | |
| 246 | |
| 247 controller()->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES); | |
| 248 return false; | |
| 249 } | |
| 250 | |
| 251 void MojoAsyncResourceHandler::OnWritable(MojoResult unused) { | |
| 252 ResumeIfDeferred(); | |
| 253 } | |
| 254 | |
| 255 } // namespace content | |
| OLD | NEW |