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 // If the parent handler downloaded the resource to a file, grant the child | |
95 // read permissions on it. | |
96 if (!response->head.download_file_path.empty()) { | |
97 rdh_->RegisterDownloadedTempFile(info->GetChildID(), info->GetRequestID(), | |
98 response->head.download_file_path); | |
99 } | |
mmenke
2016/05/26 21:46:09
Seems like we shouldn't include this code until On
yhirano
2016/05/27 11:30:34
Done.
| |
100 | |
101 response->head.request_start = request()->creation_time(); | |
102 response->head.response_start = base::TimeTicks::Now(); | |
103 sent_received_response_message_ = true; | |
104 url_loader_client_->OnReceiveResponse(response->head); | |
105 return true; | |
106 } | |
107 | |
108 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { | |
109 return true; | |
110 } | |
111 | |
112 bool MojoAsyncResourceHandler::OnBeforeNetworkStart(const GURL& url, | |
113 bool* defer) { | |
114 return true; | |
115 } | |
116 | |
117 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
118 int* buf_size, | |
119 int min_size) { | |
120 DCHECK_EQ(-1, min_size); | |
121 | |
122 if (!CheckForSufficientResource()) | |
123 return false; | |
124 | |
125 void* buffer = nullptr; | |
126 uint32_t available = 0; | |
127 if (!writer_.is_valid()) { | |
128 MojoCreateDataPipeOptions options; | |
129 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
130 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
131 options.element_num_bytes = 1; | |
132 options.capacity_num_bytes = g_max_allocation_size; | |
133 mojo::DataPipe data_pipe(options); | |
134 | |
135 writer_ = std::move(data_pipe.producer_handle); | |
136 ResourceMessageFilter* filter = GetRequestInfo()->filter(); | |
137 if (filter) { | |
138 url_loader_client_->OnStartLoadingResponseBody( | |
139 std::move(data_pipe.consumer_handle)); | |
140 } | |
141 } | |
142 if (!writer_.is_valid()) { | |
143 controller()->CancelWithError(net::ERR_FAILED); | |
144 return false; | |
mmenke
2016/05/26 21:46:09
Returning false results in a cancel, as does calli
yhirano
2016/05/27 11:30:34
Done.
Does that mean CancelWithError in checkForS
mmenke
2016/05/27 15:39:26
Yes. It may make sense to return a network error
| |
145 } | |
146 | |
147 MojoResult result = mojo::BeginWriteDataRaw( | |
148 writer_.get(), &buffer, &available, MOJO_WRITE_DATA_FLAG_NONE); | |
149 // SHOULD_WAIT should be handled in OnReadCompleted. | |
mmenke
2016/05/26 21:46:09
I'm not following this comment. Docs seem to indi
yhirano
2016/05/27 11:30:34
Generally we don't treat MOJO_RESULT_SHOULD_WAIT a
mmenke
2016/05/27 15:39:26
I'm not following. The docs say nothing about whe
yhirano
2016/05/30 12:41:45
I expected so:
- After creating a data pipe, the
yzshen1
2016/05/31 21:07:38
Generally speaking, I think we should stick to wha
yhirano
2016/06/01 14:40:51
Thank you. Let me address this issue in a separate
| |
150 if (result == MOJO_RESULT_OK) { | |
151 *buf = new net::WrappedIOBuffer(static_cast<const char*>(buffer)); | |
152 *buf_size = available; | |
153 return true; | |
154 } | |
155 controller()->CancelWithError(net::ERR_FAILED); | |
mmenke
2016/05/26 21:46:09
remove this.
yhirano
2016/05/27 11:30:34
Done.
| |
156 return false; | |
mmenke
2016/05/26 21:46:09
Suggest the failure case first.
yhirano
2016/05/27 11:30:33
Done.
| |
157 } | |
158 | |
159 bool MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | |
160 DCHECK_GE(bytes_read, 0); | |
161 | |
162 if (!bytes_read) | |
163 return true; | |
164 | |
165 MojoResult result = mojo::EndWriteDataRaw(writer_.get(), bytes_read); | |
166 if (result != MOJO_RESULT_OK) | |
167 return false; | |
168 void* buffer = nullptr; | |
169 uint32_t available = 0; | |
170 // To see if the handle is still writable. | |
171 result = mojo::BeginWriteDataRaw(writer_.get(), &buffer, &available, | |
172 MOJO_WRITE_DATA_FLAG_NONE); | |
173 if (result == MOJO_RESULT_SHOULD_WAIT || | |
174 (result == MOJO_RESULT_OK && available == 0)) { | |
mmenke
2016/05/26 21:46:09
Can the second condition occur? Should it be able
yhirano
2016/05/27 11:30:34
The comment was not clear to me, but the actual im
| |
175 *defer = did_defer_ = true; | |
176 OnDefer(); | |
177 handle_watcher_.Start(writer_.get(), MOJO_HANDLE_SIGNAL_WRITABLE, | |
178 MOJO_DEADLINE_INDEFINITE, | |
179 base::Bind(&MojoAsyncResourceHandler::OnWritable, | |
180 base::Unretained(this))); | |
181 } | |
182 if (result == MOJO_RESULT_OK) | |
183 mojo::EndWriteDataRaw(writer_.get(), 0); | |
184 return true; | |
185 } | |
186 | |
187 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { | |
188 // Not implemented. | |
189 } | |
190 | |
191 void MojoAsyncResourceHandler::OnResponseCompleted( | |
192 const net::URLRequestStatus& status, | |
193 const std::string& security_info, | |
194 bool* defer) { | |
195 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
196 if (!info->filter()) | |
197 return; | |
198 | |
199 int error_code = status.error(); | |
200 bool was_ignored_by_handler = info->WasIgnoredByHandler(); | |
201 | |
202 DCHECK(status.status() != net::URLRequestStatus::IO_PENDING); | |
mmenke
2016/05/26 21:46:09
Please keep the (status.status() != net::URLReques
yhirano
2016/05/27 11:30:33
Done.
| |
203 // If this check fails, then we're in an inconsistent state because all | |
204 // requests ignored by the handler should be canceled (which should result in | |
205 // the ERR_ABORTED error code). | |
206 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED); | |
207 | |
208 // TODO(mkosiba): Fix up cases where we create a URLRequestStatus | |
209 // with a status() != SUCCESS and an error_code() == net::OK. | |
210 if (status.status() == net::URLRequestStatus::CANCELED && | |
211 error_code == net::OK) { | |
212 error_code = net::ERR_ABORTED; | |
213 } else if (status.status() == net::URLRequestStatus::FAILED && | |
214 error_code == net::OK) { | |
215 error_code = net::ERR_FAILED; | |
216 } | |
217 | |
218 ResourceRequestCompletionStatus request_complete_data; | |
219 request_complete_data.error_code = error_code; | |
220 request_complete_data.was_ignored_by_handler = was_ignored_by_handler; | |
221 request_complete_data.exists_in_cache = request()->response_info().was_cached; | |
222 request_complete_data.security_info = security_info; | |
223 request_complete_data.completion_time = base::TimeTicks::Now(); | |
224 request_complete_data.encoded_data_length = | |
225 request()->GetTotalReceivedBytes(); | |
226 | |
227 url_loader_client_->OnComplete(request_complete_data); | |
228 } | |
229 | |
230 void MojoAsyncResourceHandler::ResumeIfDeferred() { | |
231 if (did_defer_) { | |
232 did_defer_ = false; | |
233 request()->LogUnblocked(); | |
234 controller()->Resume(); | |
235 } | |
236 } | |
237 | |
238 void MojoAsyncResourceHandler::OnDefer() { | |
239 request()->LogBlockedBy("MojoAsyncResourceHandler"); | |
240 } | |
241 | |
242 bool MojoAsyncResourceHandler::CheckForSufficientResource() { | |
243 if (has_checked_for_sufficient_resources_) | |
244 return true; | |
245 has_checked_for_sufficient_resources_ = true; | |
246 | |
247 if (rdh_->HasSufficientResourcesForRequest(request())) | |
248 return true; | |
249 | |
250 controller()->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES); | |
251 return false; | |
252 } | |
253 | |
254 void MojoAsyncResourceHandler::OnWritable(MojoResult unused) { | |
255 ResumeIfDeferred(); | |
256 } | |
257 | |
258 } // namespace content | |
OLD | NEW |