download_stats.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4//
5// Holds helpers for gathering UMA stats about downloads.
6
7#ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_STATS_H_
8#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_STATS_H_
9
10#include <string>
11
12#include "base/basictypes.h"
13#include "content/common/content_export.h"
14#include "content/public/browser/download_interrupt_reasons.h"
15
16namespace base {
17class Time;
18class TimeDelta;
19class TimeTicks;
20}
21
22namespace content {
23
24// We keep a count of how often various events occur in the
25// histogram "Download.Counts".
26enum DownloadCountTypes {
27  // Stale enum values left around so that values passed to UMA don't
28  // change.
29  DOWNLOAD_COUNT_UNUSED_0 = 0,
30  DOWNLOAD_COUNT_UNUSED_1,
31  DOWNLOAD_COUNT_UNUSED_2,
32  DOWNLOAD_COUNT_UNUSED_3,
33  DOWNLOAD_COUNT_UNUSED_4,
34
35  // Downloads that made it to DownloadResourceHandler
36  UNTHROTTLED_COUNT,
37
38  // Downloads that actually complete.
39  COMPLETED_COUNT,
40
41  // Downloads that are cancelled before completion (user action or error).
42  CANCELLED_COUNT,
43
44  // Downloads that are started. Should be equal to UNTHROTTLED_COUNT.
45  START_COUNT,
46
47  // Downloads that were interrupted by the OS.
48  INTERRUPTED_COUNT,
49
50  // Write sizes for downloads.
51  WRITE_SIZE_COUNT,
52
53  // Counts iterations of the BaseFile::AppendDataToFile() loop.
54  WRITE_LOOP_COUNT,
55
56  // Counts interruptions that happened at the end of the download.
57  INTERRUPTED_AT_END_COUNT,
58
59  // Counts errors due to writes to BaseFiles that have been detached already.
60  // This can happen when saving web pages as complete packages. It happens
61  // when we get messages to append data to files that have already finished and
62  // been detached, but haven't yet been removed from the list of files in
63  // progress.
64  APPEND_TO_DETACHED_FILE_COUNT,
65
66  // Counts the number of instances where the downloaded file is missing after a
67  // successful invocation of ScanAndSaveDownloadedFile().
68  FILE_MISSING_AFTER_SUCCESSFUL_SCAN_COUNT,
69
70  DOWNLOAD_COUNT_TYPES_LAST_ENTRY
71};
72
73enum DownloadSource {
74  // The download was initiated when the SavePackage system rejected
75  // a Save Page As ... by returning false from
76  // SavePackage::IsSaveableContents().
77  INITIATED_BY_SAVE_PACKAGE_ON_NON_HTML = 0,
78
79  // The download was initiated by a drag and drop from a drag-and-drop
80  // enabled web application.
81  INITIATED_BY_DRAG_N_DROP,
82
83  // The download was initiated by explicit RPC from the renderer process
84  // (e.g. by Alt-click) through the IPC ViewHostMsg_DownloadUrl.
85  INITIATED_BY_RENDERER,
86
87  // The download was initiated by a renderer or plugin process through
88  // the IPC ViewHostMsg_SaveURLAs; currently this is only used by the
89  // Pepper plugin API.
90  INITIATED_BY_PEPPER_SAVE,
91
92  DOWNLOAD_SOURCE_LAST_ENTRY
93};
94
95// Increment one of the above counts.
96void RecordDownloadCount(DownloadCountTypes type);
97
98// Record initiation of a download from a specific source.
99void RecordDownloadSource(DownloadSource source);
100
101// Record COMPLETED_COUNT and how long the download took.
102void RecordDownloadCompleted(const base::TimeTicks& start, int64 download_len);
103
104// Record INTERRUPTED_COUNT, |reason|, |received| and |total| bytes.
105void RecordDownloadInterrupted(DownloadInterruptReason reason,
106                               int64 received,
107                               int64 total);
108
109// Records the mime type of the download.
110void RecordDownloadMimeType(const std::string& mime_type);
111
112// Records usage of Content-Disposition header.
113void RecordDownloadContentDisposition(const std::string& content_disposition);
114
115// Record WRITE_SIZE_COUNT and data_len.
116void RecordDownloadWriteSize(size_t data_len);
117
118// Record WRITE_LOOP_COUNT and number of loops.
119void RecordDownloadWriteLoopCount(int count);
120
121// Record the number of buffers piled up by the IO thread
122// before the file thread gets to draining them.
123void RecordFileThreadReceiveBuffers(size_t num_buffers);
124
125// Record the bandwidth seen in DownloadResourceHandler
126// |actual_bandwidth| and |potential_bandwidth| are in bytes/second.
127void RecordBandwidth(double actual_bandwidth, double potential_bandwidth);
128
129// Record the time of both the first open and all subsequent opens since the
130// download completed.
131void RecordOpen(const base::Time& end, bool first);
132
133// Record whether or not the server accepts ranges, and the download size.
134void RecordAcceptsRanges(const std::string& accepts_ranges, int64 download_len);
135
136// Record the number of downloads removed by ClearAll.
137void RecordClearAllSize(int size);
138
139// Record the number of completed unopened downloads when a download is opened.
140void RecordOpensOutstanding(int size);
141
142// Record how long we block the file thread at a time.
143void RecordContiguousWriteTime(base::TimeDelta time_blocked);
144
145// Record the percentage of time we had to block the network (i.e.
146// how often, for each download, something other than the network
147// was the bottleneck).
148void RecordNetworkBlockage(base::TimeDelta resource_handler_lifetime,
149                           base::TimeDelta resource_handler_blocked_time);
150
151// Record overall bandwidth stats at the file end.
152void RecordFileBandwidth(size_t length,
153                         base::TimeDelta disk_write_time,
154                         base::TimeDelta elapsed_time);
155
156enum SavePackageEvent {
157  // The user has started to save a page as a package.
158  SAVE_PACKAGE_STARTED,
159
160  // The save package operation was cancelled.
161  SAVE_PACKAGE_CANCELLED,
162
163  // The save package operation finished without being cancelled.
164  SAVE_PACKAGE_FINISHED,
165
166  // The save package tried to write to an already completed file.
167  SAVE_PACKAGE_WRITE_TO_COMPLETED,
168
169  // The save package tried to write to an already failed file.
170  SAVE_PACKAGE_WRITE_TO_FAILED,
171
172  SAVE_PACKAGE_LAST_ENTRY
173};
174
175void RecordSavePackageEvent(SavePackageEvent event);
176
177}  // namespace content
178
179#endif  // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_STATS_H_
180