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_request_info_impl.h" | |
18 #include "content/common/resource_request_completion_status.h" | |
19 #include "content/public/browser/resource_dispatcher_host_delegate.h" | |
20 #include "content/public/common/resource_response.h" | |
21 #include "mojo/public/c/system/data_pipe.h" | |
22 #include "mojo/public/cpp/system/data_pipe.h" | |
23 #include "net/base/io_buffer.h" | |
24 #include "net/base/load_flags.h" | |
25 #include "net/base/mime_sniffer.h" | |
26 #include "net/log/net_log.h" | |
27 #include "net/url_request/redirect_info.h" | |
28 | |
29 namespace content { | |
30 namespace { | |
31 | |
32 int g_max_allocation_size = 1024 * 32; | |
33 // MimeTypeResourceHandler *implicitly* requires that the buffer size | |
34 // returned from OnWillRead should be larger than certain size. | |
35 // TODO(yhirano): Fix MimeTypeResourceHandler. | |
36 size_t kMinAllocationSize = 2 * net::kMaxBytesToSniff; | |
37 | |
38 void GetNumericArg(const std::string& name, int* result) { | |
39 const std::string& value = | |
40 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(name); | |
41 if (!value.empty()) | |
42 base::StringToInt(value, result); | |
43 } | |
44 | |
45 void InitializeResourceBufferConstants() { | |
46 static bool did_init = false; | |
47 if (did_init) | |
48 return; | |
49 did_init = true; | |
50 | |
51 GetNumericArg("resource-buffer-max-allocation-size", &g_max_allocation_size); | |
52 } | |
53 | |
54 } // namespace | |
55 | |
56 // This class is for sharing the ownership of a ScopedDataPipeProducerHandle. | |
57 class MojoAsyncResourceHandler::SharedWriter final | |
58 : public base::RefCountedThreadSafe<SharedWriter> { | |
59 public: | |
60 explicit SharedWriter(mojo::ScopedDataPipeProducerHandle writer) | |
61 : writer_(std::move(writer)) {} | |
62 mojo::DataPipeProducerHandle writer() { return writer_.get(); } | |
63 | |
64 private: | |
65 friend class base::RefCountedThreadSafe<SharedWriter>; | |
66 ~SharedWriter() {} | |
67 | |
68 const mojo::ScopedDataPipeProducerHandle writer_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(SharedWriter); | |
71 }; | |
72 | |
73 // This class is a IOBuffer subclass for data gotten from a | |
74 // ScopedDataPipeProducerHandle. | |
75 class MojoAsyncResourceHandler::WriterIOBuffer final | |
mmenke
2016/07/14 21:23:04
Hrm...Wonder if it would be cleaner to merge this
yhirano
2016/07/15 11:47:55
Are you proposing to return the same IOBuffer poin
mmenke
2016/07/15 14:54:07
Yea, that's what I was thinking. Currently CopyRe
| |
76 : public net::IOBufferWithSize { | |
77 public: | |
78 // |data| and |size| should be gotten from |writer| via BeginWriteDataRaw. | |
79 // They will be accesible via IOBuffer methods. As |writer| is stored in this | |
80 // instance, |data| will be kept valid as long as the following conditions | |
81 // hold: | |
82 // 1. |data| is not invalidated via EndWriteDataRaw. | |
83 // 2. |this| instance is alive. | |
84 WriterIOBuffer(scoped_refptr<MojoAsyncResourceHandler::SharedWriter> writer, | |
85 void* data, | |
86 size_t size) | |
87 : net::IOBufferWithSize(static_cast<char*>(data), size), | |
88 writer_(std::move(writer)) {} | |
89 | |
90 private: | |
91 ~WriterIOBuffer() override { | |
92 // Avoid deleting |data_| in the IOBuffer destructor. | |
93 data_ = nullptr; | |
94 } | |
95 | |
96 // This member is for keeping the writer alive. | |
97 scoped_refptr<MojoAsyncResourceHandler::SharedWriter> writer_; | |
98 | |
99 DISALLOW_COPY_AND_ASSIGN(WriterIOBuffer); | |
100 }; | |
101 | |
102 MojoAsyncResourceHandler::MojoAsyncResourceHandler( | |
103 net::URLRequest* request, | |
104 ResourceDispatcherHostImpl* rdh, | |
105 mojo::InterfaceRequest<mojom::URLLoader> mojo_request, | |
106 mojom::URLLoaderClientPtr url_loader_client) | |
107 : ResourceHandler(request), | |
108 rdh_(rdh), | |
109 binding_(this, std::move(mojo_request)), | |
110 url_loader_client_(std::move(url_loader_client)) { | |
111 DCHECK(url_loader_client_); | |
112 InitializeResourceBufferConstants(); | |
113 } | |
114 | |
115 MojoAsyncResourceHandler::~MojoAsyncResourceHandler() { | |
116 if (has_checked_for_sufficient_resources_) | |
117 rdh_->FinishedWithResourcesForRequest(request()); | |
118 } | |
119 | |
120 bool MojoAsyncResourceHandler::OnRequestRedirected( | |
121 const net::RedirectInfo& redirect_info, | |
122 ResourceResponse* response, | |
123 bool* defer) { | |
124 // Not implemented. | |
125 return false; | |
126 } | |
127 | |
128 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, | |
129 bool* defer) { | |
130 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
131 | |
132 if (rdh_->delegate()) { | |
133 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), | |
134 response); | |
135 } | |
136 | |
137 NetLogObserver::PopulateResponseInfo(request(), response); | |
138 | |
139 response->head.request_start = request()->creation_time(); | |
140 response->head.response_start = base::TimeTicks::Now(); | |
141 sent_received_response_message_ = true; | |
142 url_loader_client_->OnReceiveResponse(response->head); | |
143 return true; | |
144 } | |
145 | |
146 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { | |
147 return true; | |
148 } | |
149 | |
150 bool MojoAsyncResourceHandler::OnBeforeNetworkStart(const GURL& url, | |
151 bool* defer) { | |
mmenke
2016/07/14 21:23:04
note: This method had been removed from the inter
yhirano
2016/07/19 09:42:47
OnBeforeNetworkStart was removed from URLRequest::
| |
152 return true; | |
153 } | |
154 | |
155 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
156 int* buf_size, | |
157 int min_size) { | |
158 DCHECK_EQ(-1, min_size); | |
159 | |
160 if (!CheckForSufficientResource()) | |
161 return false; | |
162 | |
163 if (!shared_writer_) { | |
164 MojoCreateDataPipeOptions options; | |
165 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
166 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
167 options.element_num_bytes = 1; | |
168 options.capacity_num_bytes = g_max_allocation_size; | |
mmenke
2016/07/14 21:23:04
This is the total capacity of the channel, not the
yhirano
2016/07/19 09:42:47
I renamed it to g_allocation_size and changed the
mmenke
2016/07/19 15:50:28
It's not about memory, it's about performance. Le
mmenke
2016/07/19 20:50:26
It's not necessarily the case that a bunch of 32k
yhirano
2016/07/20 13:38:36
Thanks, I see. I added kMaxChunkSize.
| |
169 mojo::DataPipe data_pipe(options); | |
170 | |
171 url_loader_client_->OnStartLoadingResponseBody( | |
172 std::move(data_pipe.consumer_handle)); | |
173 if (!data_pipe.producer_handle.is_valid()) | |
174 return false; | |
175 | |
176 shared_writer_ = new SharedWriter(std::move(data_pipe.producer_handle)); | |
177 | |
178 void* data = nullptr; | |
179 uint32_t available = 0; | |
180 MojoResult result = BeginWrite(&data, &available); | |
181 | |
182 if (result != MOJO_RESULT_OK && result != MOJO_RESULT_SHOULD_WAIT) | |
183 return false; | |
184 if (result == MOJO_RESULT_OK && available >= kMinAllocationSize) { | |
185 *buf = buffer_ = new WriterIOBuffer(shared_writer_, data, available); | |
186 *buf_size = buffer_->size(); | |
187 return true; | |
188 } | |
189 | |
190 if (result == MOJO_RESULT_OK) { | |
191 // The allocated buffer is too small. | |
192 if (EndWrite(0) != MOJO_RESULT_OK) | |
193 return false; | |
194 } | |
195 DCHECK(!is_using_io_buffer_not_from_writer_); | |
196 is_using_io_buffer_not_from_writer_ = true; | |
197 buffer_ = new net::IOBufferWithSize(kMinAllocationSize); | |
198 } | |
199 | |
200 if (is_using_io_buffer_not_from_writer_) { | |
201 DCHECK_EQ(0u, buffer_offset_); | |
202 *buf = new net::WrappedIOBuffer(buffer_->data()); | |
mmenke
2016/07/15 14:54:07
Hrm...Why can't we just return buffer_, like we do
yhirano
2016/07/19 09:42:47
You're right. fixed.
| |
203 *buf_size = buffer_->size(); | |
204 return true; | |
205 } | |
206 | |
207 *buf = buffer_; | |
208 *buf_size = buffer_->size(); | |
209 return true; | |
210 } | |
211 | |
212 bool MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | |
mmenke
2016/07/14 21:23:05
I think most branches here could use some document
yhirano
2016/07/15 11:47:55
Personally PS46 version is much easier to explain
mmenke
2016/07/15 14:54:07
I find them both extremely obscure, largely becaus
yhirano
2016/07/19 09:42:47
I agree that both are hard to understand and chang
| |
213 DCHECK_GE(bytes_read, 0); | |
214 DCHECK(buffer_); | |
215 | |
216 if (!bytes_read) | |
217 return true; | |
218 | |
219 if (is_using_io_buffer_not_from_writer_) { | |
220 // Couldn't allocate a buffer on the data pipe in OnWillRead. | |
221 DCHECK(!did_defer_); | |
222 DCHECK_EQ(0u, buffer_bytes_read_); | |
223 buffer_bytes_read_ = bytes_read; | |
224 if (!CopyReadData(defer)) | |
225 return false; | |
226 } else { | |
227 if (EndWrite(bytes_read) != MOJO_RESULT_OK) | |
228 return false; | |
229 } | |
230 if (!is_using_io_buffer_not_from_writer_) { | |
231 DCHECK(!*defer); | |
232 if (!AllocateBuffer(defer)) | |
233 return false; | |
234 } | |
235 if (*defer) { | |
236 did_defer_ = true; | |
237 OnDefer(); | |
238 } | |
239 return true; | |
240 } | |
241 | |
242 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { | |
243 // Not implemented. | |
244 } | |
245 | |
246 void MojoAsyncResourceHandler::FollowRedirect() { | |
247 NOTIMPLEMENTED(); | |
248 } | |
249 | |
250 void MojoAsyncResourceHandler::Cancel() { | |
251 NOTIMPLEMENTED(); | |
252 } | |
253 | |
254 void MojoAsyncResourceHandler::SetAllocationSizeForTesting(size_t size) { | |
255 g_max_allocation_size = size; | |
mmenke
2016/07/14 21:23:05
Can we just set this via the command line flag? E
yhirano
2016/07/19 09:42:47
I think some test cases share the same process. I
| |
256 } | |
257 | |
258 MojoResult MojoAsyncResourceHandler::BeginWrite(void** data, | |
259 uint32_t* available) { | |
260 return mojo::BeginWriteDataRaw(shared_writer_->writer(), data, available, | |
261 MOJO_WRITE_DATA_FLAG_NONE); | |
262 } | |
263 | |
264 MojoResult MojoAsyncResourceHandler::EndWrite(uint32_t written) { | |
265 return mojo::EndWriteDataRaw(shared_writer_->writer(), written); | |
266 } | |
267 | |
268 void MojoAsyncResourceHandler::OnResponseCompleted( | |
269 const net::URLRequestStatus& status, | |
270 const std::string& security_info, | |
271 bool* defer) { | |
272 shared_writer_ = nullptr; | |
273 buffer_ = nullptr; | |
274 handle_watcher_.Stop(); | |
275 | |
276 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
277 | |
278 // TODO(gavinp): Remove this CHECK when we figure out the cause of | |
279 // http://crbug.com/124680 . This check mirrors closely check in | |
280 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore | |
281 // ResourceHandleInternal which asserts on its state and crashes. By crashing | |
282 // when the message is sent, we should get better crash reports. | |
283 CHECK(status.status() != net::URLRequestStatus::SUCCESS || | |
284 sent_received_response_message_); | |
285 | |
286 int error_code = status.error(); | |
287 bool was_ignored_by_handler = info->WasIgnoredByHandler(); | |
288 | |
289 DCHECK(status.status() != net::URLRequestStatus::IO_PENDING); | |
290 // If this check fails, then we're in an inconsistent state because all | |
291 // requests ignored by the handler should be canceled (which should result in | |
292 // the ERR_ABORTED error code). | |
293 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED); | |
294 | |
295 ResourceRequestCompletionStatus request_complete_data; | |
296 request_complete_data.error_code = error_code; | |
297 request_complete_data.was_ignored_by_handler = was_ignored_by_handler; | |
298 request_complete_data.exists_in_cache = request()->response_info().was_cached; | |
299 request_complete_data.security_info = security_info; | |
300 request_complete_data.completion_time = base::TimeTicks::Now(); | |
301 request_complete_data.encoded_data_length = | |
302 request()->GetTotalReceivedBytes(); | |
303 | |
304 url_loader_client_->OnComplete(request_complete_data); | |
305 } | |
306 | |
307 bool MojoAsyncResourceHandler::CopyReadData(bool* defer) { | |
308 while (true) { | |
309 if (buffer_bytes_read_ == 0) { | |
310 // All bytes are copied. | |
311 buffer_offset_ = 0; | |
312 is_using_io_buffer_not_from_writer_ = false; | |
313 buffer_ = nullptr; | |
314 return true; | |
315 } | |
316 void* data = nullptr; | |
317 uint32_t available = 0; | |
318 MojoResult result = BeginWrite(&data, &available); | |
319 if (result == MOJO_RESULT_SHOULD_WAIT) { | |
320 *defer = true; | |
321 return true; | |
322 } | |
323 if (result != MOJO_RESULT_OK) | |
324 return false; | |
325 | |
326 size_t copied_size = | |
327 std::min(static_cast<uint32_t>(buffer_bytes_read_), available); | |
328 memcpy(data, buffer_->data() + buffer_offset_, copied_size); | |
329 buffer_offset_ += copied_size; | |
330 buffer_bytes_read_ -= copied_size; | |
331 result = EndWrite(copied_size); | |
332 if (result != MOJO_RESULT_OK) | |
333 return false; | |
334 } | |
335 } | |
336 | |
337 bool MojoAsyncResourceHandler::AllocateBuffer(bool* defer) { | |
338 void* data = nullptr; | |
339 uint32_t available = 0; | |
340 MojoResult result = BeginWrite(&data, &available); | |
341 if (result == MOJO_RESULT_SHOULD_WAIT) { | |
342 *defer = true; | |
343 return true; | |
344 } | |
345 if (result != MOJO_RESULT_OK) | |
346 return false; | |
347 buffer_ = new WriterIOBuffer(shared_writer_, data, available); | |
348 return true; | |
349 } | |
350 | |
351 void MojoAsyncResourceHandler::ResumeIfDeferred() { | |
mmenke
2016/07/14 21:23:05
I think most branches here could use some document
yhirano
2016/07/19 09:42:47
Done.
| |
352 DCHECK(did_defer_); | |
353 // did_defer_ is set only in OnReadCompleted (and here). | |
354 did_defer_ = false; | |
355 if (is_using_io_buffer_not_from_writer_) { | |
356 DCHECK_GT(buffer_bytes_read_, 0u); | |
357 if (!CopyReadData(&did_defer_)) { | |
358 controller()->CancelWithError(net::ERR_FAILED); | |
359 return; | |
360 } | |
361 } | |
362 if (!is_using_io_buffer_not_from_writer_) { | |
363 DCHECK(!did_defer_); | |
364 if (!AllocateBuffer(&did_defer_)) { | |
365 controller()->CancelWithError(net::ERR_FAILED); | |
366 return; | |
367 } | |
368 } | |
369 if (did_defer_) { | |
370 // Continue waiting. | |
371 handle_watcher_.Start(shared_writer_->writer(), MOJO_HANDLE_SIGNAL_WRITABLE, | |
372 MOJO_DEADLINE_INDEFINITE, | |
373 base::Bind(&MojoAsyncResourceHandler::OnWritable, | |
374 base::Unretained(this))); | |
375 return; | |
376 } | |
377 request()->LogUnblocked(); | |
378 controller()->Resume(); | |
379 } | |
380 | |
381 void MojoAsyncResourceHandler::OnDefer() { | |
382 request()->LogBlockedBy("MojoAsyncResourceHandler"); | |
383 handle_watcher_.Start(shared_writer_->writer(), MOJO_HANDLE_SIGNAL_WRITABLE, | |
384 MOJO_DEADLINE_INDEFINITE, | |
385 base::Bind(&MojoAsyncResourceHandler::OnWritable, | |
386 base::Unretained(this))); | |
387 } | |
388 | |
389 bool MojoAsyncResourceHandler::CheckForSufficientResource() { | |
390 if (has_checked_for_sufficient_resources_) | |
391 return true; | |
392 has_checked_for_sufficient_resources_ = true; | |
393 | |
394 if (rdh_->HasSufficientResourcesForRequest(request())) | |
395 return true; | |
396 | |
397 controller()->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES); | |
398 return false; | |
399 } | |
400 | |
401 void MojoAsyncResourceHandler::OnWritable(MojoResult unused) { | |
402 ResumeIfDeferred(); | |
403 } | |
404 | |
405 } // namespace content | |
OLD | NEW |