Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / loader / async_resource_handler.cc
blob87d82ca1424f637a7fd3d53532d6b8c9472766d0
1 // Copyright (c) 2012 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.
5 #include "content/browser/loader/async_resource_handler.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/command_line.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/debug/alias.h"
13 #include "base/logging.h"
14 #include "base/memory/shared_memory.h"
15 #include "base/metrics/histogram.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "content/browser/devtools/devtools_netlog_observer.h"
18 #include "content/browser/host_zoom_map_impl.h"
19 #include "content/browser/loader/resource_buffer.h"
20 #include "content/browser/loader/resource_dispatcher_host_impl.h"
21 #include "content/browser/loader/resource_message_filter.h"
22 #include "content/browser/loader/resource_request_info_impl.h"
23 #include "content/browser/resource_context_impl.h"
24 #include "content/common/resource_messages.h"
25 #include "content/common/view_messages.h"
26 #include "content/public/browser/resource_dispatcher_host_delegate.h"
27 #include "content/public/common/resource_response.h"
28 #include "net/base/io_buffer.h"
29 #include "net/base/load_flags.h"
30 #include "net/base/net_log.h"
31 #include "net/base/net_util.h"
32 #include "net/url_request/redirect_info.h"
34 using base::TimeTicks;
36 namespace content {
37 namespace {
39 static int kBufferSize = 1024 * 512;
40 static int kMinAllocationSize = 1024 * 4;
41 static int kMaxAllocationSize = 1024 * 32;
43 void GetNumericArg(const std::string& name, int* result) {
44 const std::string& value =
45 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(name);
46 if (!value.empty())
47 base::StringToInt(value, result);
50 void InitializeResourceBufferConstants() {
51 static bool did_init = false;
52 if (did_init)
53 return;
54 did_init = true;
56 GetNumericArg("resource-buffer-size", &kBufferSize);
57 GetNumericArg("resource-buffer-min-allocation-size", &kMinAllocationSize);
58 GetNumericArg("resource-buffer-max-allocation-size", &kMaxAllocationSize);
61 int CalcUsedPercentage(int bytes_read, int buffer_size) {
62 double ratio = static_cast<double>(bytes_read) / buffer_size;
63 return static_cast<int>(ratio * 100.0 + 0.5); // Round to nearest integer.
66 } // namespace
68 class DependentIOBuffer : public net::WrappedIOBuffer {
69 public:
70 DependentIOBuffer(ResourceBuffer* backing, char* memory)
71 : net::WrappedIOBuffer(memory),
72 backing_(backing) {
74 private:
75 virtual ~DependentIOBuffer() {}
76 scoped_refptr<ResourceBuffer> backing_;
79 AsyncResourceHandler::AsyncResourceHandler(
80 net::URLRequest* request,
81 ResourceDispatcherHostImpl* rdh)
82 : ResourceHandler(request),
83 ResourceMessageDelegate(request),
84 rdh_(rdh),
85 pending_data_count_(0),
86 allocation_size_(0),
87 did_defer_(false),
88 has_checked_for_sufficient_resources_(false),
89 sent_received_response_msg_(false),
90 sent_first_data_msg_(false),
91 reported_transfer_size_(0) {
92 InitializeResourceBufferConstants();
95 AsyncResourceHandler::~AsyncResourceHandler() {
96 if (has_checked_for_sufficient_resources_)
97 rdh_->FinishedWithResourcesForRequest(request());
100 bool AsyncResourceHandler::OnMessageReceived(const IPC::Message& message) {
101 bool handled = true;
102 IPC_BEGIN_MESSAGE_MAP(AsyncResourceHandler, message)
103 IPC_MESSAGE_HANDLER(ResourceHostMsg_FollowRedirect, OnFollowRedirect)
104 IPC_MESSAGE_HANDLER(ResourceHostMsg_DataReceived_ACK, OnDataReceivedACK)
105 IPC_MESSAGE_UNHANDLED(handled = false)
106 IPC_END_MESSAGE_MAP()
107 return handled;
110 void AsyncResourceHandler::OnFollowRedirect(int request_id) {
111 if (!request()->status().is_success()) {
112 DVLOG(1) << "OnFollowRedirect for invalid request";
113 return;
116 ResumeIfDeferred();
119 void AsyncResourceHandler::OnDataReceivedACK(int request_id) {
120 if (pending_data_count_) {
121 --pending_data_count_;
123 buffer_->RecycleLeastRecentlyAllocated();
124 if (buffer_->CanAllocate())
125 ResumeIfDeferred();
129 bool AsyncResourceHandler::OnUploadProgress(uint64 position,
130 uint64 size) {
131 ResourceMessageFilter* filter = GetFilter();
132 if (!filter)
133 return false;
134 return filter->Send(
135 new ResourceMsg_UploadProgress(GetRequestID(), position, size));
138 bool AsyncResourceHandler::OnRequestRedirected(
139 const net::RedirectInfo& redirect_info,
140 ResourceResponse* response,
141 bool* defer) {
142 const ResourceRequestInfoImpl* info = GetRequestInfo();
143 if (!info->filter())
144 return false;
146 *defer = did_defer_ = true;
147 OnDefer();
149 if (rdh_->delegate()) {
150 rdh_->delegate()->OnRequestRedirected(
151 redirect_info.new_url, request(), info->GetContext(), response);
154 DevToolsNetLogObserver::PopulateResponseInfo(request(), response);
155 response->head.encoded_data_length = request()->GetTotalReceivedBytes();
156 reported_transfer_size_ = 0;
157 response->head.request_start = request()->creation_time();
158 response->head.response_start = TimeTicks::Now();
159 // TODO(davidben): Is it necessary to pass the new first party URL for
160 // cookies? The only case where it can change is top-level navigation requests
161 // and hopefully those will eventually all be owned by the browser. It's
162 // possible this is still needed while renderer-owned ones exist.
163 return info->filter()->Send(new ResourceMsg_ReceivedRedirect(
164 GetRequestID(), redirect_info, response->head));
167 bool AsyncResourceHandler::OnResponseStarted(ResourceResponse* response,
168 bool* defer) {
169 // For changes to the main frame, inform the renderer of the new URL's
170 // per-host settings before the request actually commits. This way the
171 // renderer will be able to set these precisely at the time the
172 // request commits, avoiding the possibility of e.g. zooming the old content
173 // or of having to layout the new content twice.
175 const ResourceRequestInfoImpl* info = GetRequestInfo();
176 if (!info->filter())
177 return false;
179 if (rdh_->delegate()) {
180 rdh_->delegate()->OnResponseStarted(
181 request(), info->GetContext(), response, info->filter());
184 DevToolsNetLogObserver::PopulateResponseInfo(request(), response);
186 HostZoomMap* host_zoom_map =
187 GetHostZoomMapForResourceContext(info->GetContext());
189 if (info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME && host_zoom_map) {
190 const GURL& request_url = request()->url();
191 info->filter()->Send(new ViewMsg_SetZoomLevelForLoadingURL(
192 info->GetRouteID(),
193 request_url, host_zoom_map->GetZoomLevelForHostAndScheme(
194 request_url.scheme(),
195 net::GetHostOrSpecFromURL(request_url))));
198 // If the parent handler downloaded the resource to a file, grant the child
199 // read permissions on it.
200 if (!response->head.download_file_path.empty()) {
201 rdh_->RegisterDownloadedTempFile(
202 info->GetChildID(), info->GetRequestID(),
203 response->head.download_file_path);
206 response->head.request_start = request()->creation_time();
207 response->head.response_start = TimeTicks::Now();
208 info->filter()->Send(new ResourceMsg_ReceivedResponse(GetRequestID(),
209 response->head));
210 sent_received_response_msg_ = true;
212 if (request()->response_info().metadata.get()) {
213 std::vector<char> copy(request()->response_info().metadata->data(),
214 request()->response_info().metadata->data() +
215 request()->response_info().metadata->size());
216 info->filter()->Send(new ResourceMsg_ReceivedCachedMetadata(GetRequestID(),
217 copy));
220 return true;
223 bool AsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) {
224 return true;
227 bool AsyncResourceHandler::OnBeforeNetworkStart(const GURL& url, bool* defer) {
228 return true;
231 bool AsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
232 int* buf_size,
233 int min_size) {
234 DCHECK_EQ(-1, min_size);
236 if (!EnsureResourceBufferIsInitialized())
237 return false;
239 DCHECK(buffer_->CanAllocate());
240 char* memory = buffer_->Allocate(&allocation_size_);
241 CHECK(memory);
243 *buf = new DependentIOBuffer(buffer_.get(), memory);
244 *buf_size = allocation_size_;
246 UMA_HISTOGRAM_CUSTOM_COUNTS(
247 "Net.AsyncResourceHandler_SharedIOBuffer_Alloc",
248 *buf_size, 0, kMaxAllocationSize, 100);
249 return true;
252 bool AsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
253 DCHECK_GE(bytes_read, 0);
255 if (!bytes_read)
256 return true;
258 ResourceMessageFilter* filter = GetFilter();
259 if (!filter)
260 return false;
262 buffer_->ShrinkLastAllocation(bytes_read);
264 UMA_HISTOGRAM_CUSTOM_COUNTS(
265 "Net.AsyncResourceHandler_SharedIOBuffer_Used",
266 bytes_read, 0, kMaxAllocationSize, 100);
267 UMA_HISTOGRAM_PERCENTAGE(
268 "Net.AsyncResourceHandler_SharedIOBuffer_UsedPercentage",
269 CalcUsedPercentage(bytes_read, allocation_size_));
271 if (!sent_first_data_msg_) {
272 base::SharedMemoryHandle handle;
273 int size;
274 if (!buffer_->ShareToProcess(filter->PeerHandle(), &handle, &size))
275 return false;
276 filter->Send(new ResourceMsg_SetDataBuffer(
277 GetRequestID(), handle, size, filter->peer_pid()));
278 sent_first_data_msg_ = true;
281 int data_offset = buffer_->GetLastAllocationOffset();
283 int64_t current_transfer_size = request()->GetTotalReceivedBytes();
284 int encoded_data_length = current_transfer_size - reported_transfer_size_;
285 reported_transfer_size_ = current_transfer_size;
287 filter->Send(new ResourceMsg_DataReceived(
288 GetRequestID(), data_offset, bytes_read, encoded_data_length));
289 ++pending_data_count_;
290 UMA_HISTOGRAM_CUSTOM_COUNTS(
291 "Net.AsyncResourceHandler_PendingDataCount",
292 pending_data_count_, 0, 100, 100);
294 if (!buffer_->CanAllocate()) {
295 UMA_HISTOGRAM_CUSTOM_COUNTS(
296 "Net.AsyncResourceHandler_PendingDataCount_WhenFull",
297 pending_data_count_, 0, 100, 100);
298 *defer = did_defer_ = true;
299 OnDefer();
302 return true;
305 void AsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) {
306 int64_t current_transfer_size = request()->GetTotalReceivedBytes();
307 int encoded_data_length = current_transfer_size - reported_transfer_size_;
308 reported_transfer_size_ = current_transfer_size;
310 ResourceMessageFilter* filter = GetFilter();
311 if (filter) {
312 filter->Send(new ResourceMsg_DataDownloaded(
313 GetRequestID(), bytes_downloaded, encoded_data_length));
317 void AsyncResourceHandler::OnResponseCompleted(
318 const net::URLRequestStatus& status,
319 const std::string& security_info,
320 bool* defer) {
321 const ResourceRequestInfoImpl* info = GetRequestInfo();
322 if (!info->filter())
323 return;
325 // If we crash here, figure out what URL the renderer was requesting.
326 // http://crbug.com/107692
327 char url_buf[128];
328 base::strlcpy(url_buf, request()->url().spec().c_str(), arraysize(url_buf));
329 base::debug::Alias(url_buf);
331 // TODO(gavinp): Remove this CHECK when we figure out the cause of
332 // http://crbug.com/124680 . This check mirrors closely check in
333 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore
334 // ResourceHandleInternal which asserts on its state and crashes. By crashing
335 // when the message is sent, we should get better crash reports.
336 CHECK(status.status() != net::URLRequestStatus::SUCCESS ||
337 sent_received_response_msg_);
339 int error_code = status.error();
340 bool was_ignored_by_handler = info->WasIgnoredByHandler();
342 DCHECK(status.status() != net::URLRequestStatus::IO_PENDING);
343 // If this check fails, then we're in an inconsistent state because all
344 // requests ignored by the handler should be canceled (which should result in
345 // the ERR_ABORTED error code).
346 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED);
348 // TODO(mkosiba): Fix up cases where we create a URLRequestStatus
349 // with a status() != SUCCESS and an error_code() == net::OK.
350 if (status.status() == net::URLRequestStatus::CANCELED &&
351 error_code == net::OK) {
352 error_code = net::ERR_ABORTED;
353 } else if (status.status() == net::URLRequestStatus::FAILED &&
354 error_code == net::OK) {
355 error_code = net::ERR_FAILED;
358 ResourceMsg_RequestCompleteData request_complete_data;
359 request_complete_data.error_code = error_code;
360 request_complete_data.was_ignored_by_handler = was_ignored_by_handler;
361 request_complete_data.exists_in_cache = request()->response_info().was_cached;
362 request_complete_data.security_info = security_info;
363 request_complete_data.completion_time = TimeTicks::Now();
364 request_complete_data.encoded_data_length =
365 request()->GetTotalReceivedBytes();
366 info->filter()->Send(
367 new ResourceMsg_RequestComplete(GetRequestID(), request_complete_data));
370 bool AsyncResourceHandler::EnsureResourceBufferIsInitialized() {
371 if (buffer_.get() && buffer_->IsInitialized())
372 return true;
374 if (!has_checked_for_sufficient_resources_) {
375 has_checked_for_sufficient_resources_ = true;
376 if (!rdh_->HasSufficientResourcesForRequest(request())) {
377 controller()->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES);
378 return false;
382 buffer_ = new ResourceBuffer();
383 return buffer_->Initialize(kBufferSize,
384 kMinAllocationSize,
385 kMaxAllocationSize);
388 void AsyncResourceHandler::ResumeIfDeferred() {
389 if (did_defer_) {
390 did_defer_ = false;
391 request()->LogUnblocked();
392 controller()->Resume();
396 void AsyncResourceHandler::OnDefer() {
397 request()->LogBlockedBy("AsyncResourceHandler");
400 } // namespace content