blob: ad924096a76d121bc54aae83094f03479d22990c [file] [log] [blame]
isherman@chromium.org2e4cd1a2012-01-12 08:51:031// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
initial.commit09911bf2008-07-26 23:55:295//------------------------------------------------------------------------------
6// Description of the life cycle of a instance of MetricsService.
7//
8// OVERVIEW
9//
10// A MetricsService instance is typically created at application startup. It
11// is the central controller for the acquisition of log data, and the automatic
12// transmission of that log data to an external server. Its major job is to
13// manage logs, grouping them for transmission, and transmitting them. As part
14// of its grouping, MS finalizes logs by including some just-in-time gathered
15// memory statistics, snapshotting the current stats of numerous histograms,
16// closing the logs, translating to XML text, and compressing the results for
17// transmission. Transmission includes submitting a compressed log as data in a
jar@chromium.org281d2882009-01-20 20:32:4218// URL-post, and retransmitting (or retaining at process termination) if the
initial.commit09911bf2008-07-26 23:55:2919// attempted transmission failed. Retention across process terminations is done
ziadh@chromium.org46f89e142010-07-19 08:00:4220// using the the PrefServices facilities. The retained logs (the ones that never
21// got transmitted) are compressed and base64-encoded before being persisted.
initial.commit09911bf2008-07-26 23:55:2922//
jar@chromium.org281d2882009-01-20 20:32:4223// Logs fall into one of two categories: "initial logs," and "ongoing logs."
24// There is at most one initial log sent for each complete run of the chromium
initial.commit09911bf2008-07-26 23:55:2925// product (from startup, to browser shutdown). An initial log is generally
26// transmitted some short time (1 minute?) after startup, and includes stats
27// such as recent crash info, the number and types of plugins, etc. The
jar@chromium.org281d2882009-01-20 20:32:4228// external server's response to the initial log conceptually tells this MS if
29// it should continue transmitting logs (during this session). The server
30// response can actually be much more detailed, and always includes (at a
31// minimum) how often additional ongoing logs should be sent.
initial.commit09911bf2008-07-26 23:55:2932//
33// After the above initial log, a series of ongoing logs will be transmitted.
34// The first ongoing log actually begins to accumulate information stating when
35// the MS was first constructed. Note that even though the initial log is
36// commonly sent a full minute after startup, the initial log does not include
37// much in the way of user stats. The most common interlog period (delay)
jar@google.com0b33f80b2008-12-17 21:34:3638// is 20 minutes. That time period starts when the first user action causes a
initial.commit09911bf2008-07-26 23:55:2939// logging event. This means that if there is no user action, there may be long
jar@chromium.org281d2882009-01-20 20:32:4240// periods without any (ongoing) log transmissions. Ongoing logs typically
initial.commit09911bf2008-07-26 23:55:2941// contain very detailed records of user activities (ex: opened tab, closed
42// tab, fetched URL, maximized window, etc.) In addition, just before an
43// ongoing log is closed out, a call is made to gather memory statistics. Those
44// memory statistics are deposited into a histogram, and the log finalization
45// code is then called. In the finalization, a call to a Histogram server
46// acquires a list of all local histograms that have been flagged for upload
jar@chromium.org281d2882009-01-20 20:32:4247// to the UMA server. The finalization also acquires a the most recent number
48// of page loads, along with any counts of renderer or plugin crashes.
initial.commit09911bf2008-07-26 23:55:2949//
50// When the browser shuts down, there will typically be a fragment of an ongoing
51// log that has not yet been transmitted. At shutdown time, that fragment
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:1052// is closed (including snapshotting histograms), and persisted, for
initial.commit09911bf2008-07-26 23:55:2953// potential transmission during a future run of the product.
54//
55// There are two slightly abnormal shutdown conditions. There is a
56// "disconnected scenario," and a "really fast startup and shutdown" scenario.
57// In the "never connected" situation, the user has (during the running of the
58// process) never established an internet connection. As a result, attempts to
59// transmit the initial log have failed, and a lot(?) of data has accumulated in
60// the ongoing log (which didn't yet get closed, because there was never even a
61// contemplation of sending it). There is also a kindred "lost connection"
62// situation, where a loss of connection prevented an ongoing log from being
63// transmitted, and a (still open) log was stuck accumulating a lot(?) of data,
64// while the earlier log retried its transmission. In both of these
65// disconnected situations, two logs need to be, and are, persistently stored
66// for future transmission.
67//
68// The other unusual shutdown condition, termed "really fast startup and
69// shutdown," involves the deliberate user termination of the process before
70// the initial log is even formed or transmitted. In that situation, no logging
71// is done, but the historical crash statistics remain (unlogged) for inclusion
72// in a future run's initial log. (i.e., we don't lose crash stats).
73//
74// With the above overview, we can now describe the state machine's various
75// stats, based on the State enum specified in the state_ member. Those states
76// are:
77//
78// INITIALIZED, // Constructor was called.
zelidrag@chromium.org85ed9d42010-06-08 22:37:4479// INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to complete.
80// INIT_TASK_DONE, // Waiting for timer to send initial log.
initial.commit09911bf2008-07-26 23:55:2981// INITIAL_LOG_READY, // Initial log generated, and waiting for reply.
initial.commit09911bf2008-07-26 23:55:2982// SENDING_OLD_LOGS, // Sending unsent logs from previous session.
83// SENDING_CURRENT_LOGS, // Sending standard current logs as they accrue.
84//
85// In more detail, we have:
86//
87// INITIALIZED, // Constructor was called.
88// The MS has been constructed, but has taken no actions to compose the
89// initial log.
90//
zelidrag@chromium.org85ed9d42010-06-08 22:37:4491// INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to complete.
initial.commit09911bf2008-07-26 23:55:2992// Typically about 30 seconds after startup, a task is sent to a second thread
zelidrag@chromium.org85ed9d42010-06-08 22:37:4493// (the file thread) to perform deferred (lower priority and slower)
94// initialization steps such as getting the list of plugins. That task will
95// (when complete) make an async callback (via a Task) to indicate the
96// completion.
initial.commit09911bf2008-07-26 23:55:2997//
zelidrag@chromium.org85ed9d42010-06-08 22:37:4498// INIT_TASK_DONE, // Waiting for timer to send initial log.
initial.commit09911bf2008-07-26 23:55:2999// The callback has arrived, and it is now possible for an initial log to be
100// created. This callback typically arrives back less than one second after
zelidrag@chromium.org85ed9d42010-06-08 22:37:44101// the deferred init task is dispatched.
initial.commit09911bf2008-07-26 23:55:29102//
103// INITIAL_LOG_READY, // Initial log generated, and waiting for reply.
104// This state is entered only after an initial log has been composed, and
105// prepared for transmission. It is also the case that any previously unsent
106// logs have been loaded into instance variables for possible transmission.
107//
initial.commit09911bf2008-07-26 23:55:29108// SENDING_OLD_LOGS, // Sending unsent logs from previous session.
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10109// This state indicates that the initial log for this session has been
110// successfully sent and it is now time to send any logs that were
111// saved from previous sessions. All such logs will be transmitted before
112// exiting this state, and proceeding with ongoing logs from the current session
113// (see next state).
initial.commit09911bf2008-07-26 23:55:29114//
115// SENDING_CURRENT_LOGS, // Sending standard current logs as they accrue.
jar@google.com0b33f80b2008-12-17 21:34:36116// Current logs are being accumulated. Typically every 20 minutes a log is
initial.commit09911bf2008-07-26 23:55:29117// closed and finalized for transmission, at the same time as a new log is
118// started.
119//
120// The progression through the above states is simple, and sequential, in the
121// most common use cases. States proceed from INITIAL to SENDING_CURRENT_LOGS,
122// and remain in the latter until shutdown.
123//
124// The one unusual case is when the user asks that we stop logging. When that
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10125// happens, any staged (transmission in progress) log is persisted, and any log
126// log that is currently accumulating is also finalized and persisted. We then
127// regress back to the SEND_OLD_LOGS state in case the user enables log
128// recording again during this session. This way anything we have persisted
129// will be sent automatically if/when we progress back to SENDING_CURRENT_LOG
130// state.
initial.commit09911bf2008-07-26 23:55:29131//
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10132// Also note that whenever we successfully send an old log, we mirror the list
133// of logs into the PrefService. This ensures that IF we crash, we won't start
134// up and retransmit our old logs again.
initial.commit09911bf2008-07-26 23:55:29135//
136// Due to race conditions, it is always possible that a log file could be sent
137// twice. For example, if a log file is sent, but not yet acknowledged by
138// the external server, and the user shuts down, then a copy of the log may be
139// saved for re-transmission. These duplicates could be filtered out server
jar@chromium.org281d2882009-01-20 20:32:42140// side, but are not expected to be a significant problem.
initial.commit09911bf2008-07-26 23:55:29141//
142//
143//------------------------------------------------------------------------------
144
maruel@chromium.org40bcc302009-03-02 20:50:39145#include "chrome/browser/metrics/metrics_service.h"
146
eroman@chromium.orgd7c1fa62012-06-15 23:35:30147#include <algorithm>
148
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16149#include "base/bind.h"
150#include "base/callback.h"
erg@google.com5d91c9e2010-07-28 17:25:28151#include "base/command_line.h"
akalin@chromium.org3dc1bc42012-06-19 08:20:53152#include "base/guid.h"
ziadh@chromium.org46f89e142010-07-19 08:00:42153#include "base/md5.h"
brettw@chromium.org835d7c82010-10-14 04:38:38154#include "base/metrics/histogram.h"
stevet@chromium.orge61003a2012-05-24 17:03:19155#include "base/rand_util.h"
brettw@chromium.org528c56d2010-07-30 19:28:44156#include "base/string_number_conversions.h"
brettw@chromium.orgce072a72010-12-31 20:02:16157#include "base/threading/platform_thread.h"
tfarina@chromium.orgb3841c502011-03-09 01:21:31158#include "base/threading/thread.h"
jam@chromium.org3a7b66d2012-04-26 16:34:16159#include "base/threading/thread_restrictions.h"
isherman@chromium.orged0fd002012-04-25 23:10:34160#include "base/tracked_objects.h"
viettrungluu@chromium.org440b37b22010-08-30 05:31:40161#include "base/utf_string_conversions.h"
erg@google.com679082052010-07-21 21:30:13162#include "base/values.h"
tfarina@chromium.org19871a92012-06-27 19:50:32163#include "chrome/browser/autocomplete/autocomplete_log.h"
sky@google.comd8e41ed2008-09-11 15:22:32164#include "chrome/browser/bookmarks/bookmark_model.h"
initial.commit09911bf2008-07-26 23:55:29165#include "chrome/browser/browser_process.h"
aa@chromium.org6f371442011-11-09 06:45:46166#include "chrome/browser/extensions/extension_service.h"
167#include "chrome/browser/extensions/process_map.h"
isherman@chromium.orgb8ddb052012-04-19 02:36:06168#include "chrome/browser/io_thread.h"
sail@chromium.org84c988a2011-04-19 17:56:33169#include "chrome/browser/memory_details.h"
phajdan.jr@chromium.org7c927b62010-02-24 09:54:13170#include "chrome/browser/metrics/histogram_synchronizer.h"
erg@google.com679082052010-07-21 21:30:13171#include "chrome/browser/metrics/metrics_log.h"
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10172#include "chrome/browser/metrics/metrics_log_serializer.h"
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16173#include "chrome/browser/metrics/metrics_reporting_scheduler.h"
isherman@chromium.orged0fd002012-04-25 23:10:34174#include "chrome/browser/metrics/tracking_synchronizer.h"
simonjam@chromium.orgadbb3762012-03-09 22:20:08175#include "chrome/browser/net/http_pipelining_compatibility_client.h"
rtenneti@chromium.orgd67d1052011-06-09 05:11:41176#include "chrome/browser/net/network_stats.h"
evan@chromium.org37858e52010-08-26 00:22:02177#include "chrome/browser/prefs/pref_service.h"
battre@chromium.orgf8628c22011-04-05 12:10:18178#include "chrome/browser/prefs/scoped_user_pref_update.h"
ben@chromium.org8ecad5e2010-12-02 21:18:33179#include "chrome/browser/profiles/profile.h"
erg@google.com8e5c89a2011-06-07 18:13:33180#include "chrome/browser/search_engines/template_url_service.h"
tfarina@chromium.org71b73f02011-04-06 15:57:29181#include "chrome/browser/ui/browser_list.h"
kuchhal@chromium.org157d5472009-11-05 22:31:03182#include "chrome/common/child_process_logging.h"
ananta@chromium.org432115822011-07-10 15:52:27183#include "chrome/common/chrome_notification_types.h"
eroman@chromium.orgd7c1fa62012-06-15 23:35:30184#include "chrome/common/chrome_result_codes.h"
jar@chromium.org92745242009-06-12 16:52:21185#include "chrome/common/chrome_switches.h"
isherman@chromium.org2e4cd1a2012-01-12 08:51:03186#include "chrome/common/metrics/metrics_log_manager.h"
simonjam@chromium.orgb4a72d842012-03-22 20:09:09187#include "chrome/common/net/test_server_locations.h"
initial.commit09911bf2008-07-26 23:55:29188#include "chrome/common/pref_names.h"
jam@chromium.orge09ba552009-02-05 03:26:29189#include "chrome/common/render_messages.h"
jam@chromium.org4967f792012-01-20 22:14:40190#include "content/public/browser/child_process_data.h"
tfarina@chromium.org09d31d52012-03-11 22:30:27191#include "content/public/browser/load_notification_details.h"
jam@chromium.orgad50def52011-10-19 23:17:07192#include "content/public/browser/notification_service.h"
jam@chromium.org3a5180ae2011-12-21 02:39:38193#include "content/public/browser/plugin_service.h"
ananta@chromium.orgf3b1a082011-11-18 00:34:30194#include "content/public/browser/render_process_host.h"
isherman@chromium.orgfe58acc22012-02-29 01:29:58195#include "net/base/load_flags.h"
akalin@chromium.org3dc1bc42012-06-19 08:20:53196#include "net/url_request/url_fetcher.h"
cpu@chromium.org91d9f3d2011-08-14 05:24:44197#include "webkit/plugins/webplugininfo.h"
initial.commit09911bf2008-07-26 23:55:29198
phajdan.jr@chromium.orge06131d2010-02-10 18:40:33199// TODO(port): port browser_distribution.h.
200#if !defined(OS_POSIX)
kuchhal@chromium.org79bf0b72009-04-27 21:30:55201#include "chrome/installer/util/browser_distribution.h"
paul@chromium.orgdc6f4962009-02-13 01:25:50202#endif
203
rvargas@google.com5ccaa412009-11-13 22:00:16204#if defined(OS_CHROMEOS)
stevenjb@chromium.orgdb342d52010-08-09 21:19:37205#include "chrome/browser/chromeos/cros/cros_library.h"
rvargas@google.com5ccaa412009-11-13 22:00:16206#include "chrome/browser/chromeos/external_metrics.h"
satorux@chromium.orgd43970a72011-07-10 06:24:52207#include "chrome/browser/chromeos/system/statistics_provider.h"
rvargas@google.com5ccaa412009-11-13 22:00:16208#endif
209
eroman@chromium.orgd7c1fa62012-06-15 23:35:30210#if defined(OS_WIN)
211#include <windows.h> // Needed for STATUS_* codes
212#endif
213
dsh@google.come1acf6f2008-10-27 20:43:33214using base::Time;
joi@chromium.org631bb742011-11-02 11:29:39215using content::BrowserThread;
jam@chromium.org4967f792012-01-20 22:14:40216using content::ChildProcessData;
tfarina@chromium.org09d31d52012-03-11 22:30:27217using content::LoadNotificationDetails;
jam@chromium.org3a5180ae2011-12-21 02:39:38218using content::PluginService;
dsh@google.come1acf6f2008-10-27 20:43:33219
isherman@chromium.orgfe58acc22012-02-29 01:29:58220namespace {
isherman@chromium.orgb2a4812d2012-02-28 05:31:31221
isherman@chromium.orgfe58acc22012-02-29 01:29:58222// Check to see that we're being called on only one thread.
223bool IsSingleThreaded() {
224 static base::PlatformThreadId thread_id = 0;
225 if (!thread_id)
226 thread_id = base::PlatformThread::CurrentId();
227 return base::PlatformThread::CurrentId() == thread_id;
228}
229
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16230// The delay, in seconds, after starting recording before doing expensive
231// initialization work.
isherman@chromium.orgfe58acc22012-02-29 01:29:58232const int kInitializationDelaySeconds = 30;
petersont@google.com252873ef2008-08-04 21:59:45233
jar@chromium.orgc9a3ef82009-05-28 22:02:46234// This specifies the amount of time to wait for all renderers to send their
235// data.
isherman@chromium.orgfe58acc22012-02-29 01:29:58236const int kMaxHistogramGatheringWaitDuration = 60000; // 60 seconds.
jar@chromium.orgc9a3ef82009-05-28 22:02:46237
stuartmorgan@chromium.org54702c92011-04-15 15:06:43238// The maximum number of events in a log uploaded to the UMA server.
isherman@chromium.orgfe58acc22012-02-29 01:29:58239const int kEventLimit = 2400;
jar@google.com68475e602008-08-22 03:21:15240
241// If an upload fails, and the transmission was over this byte count, then we
242// will discard the log, and not try to retransmit it. We also don't persist
243// the log to the prefs for transmission during the next chrome session if this
244// limit is exceeded.
isherman@chromium.orgfe58acc22012-02-29 01:29:58245const size_t kUploadLogAvoidRetransmitSize = 50000;
initial.commit09911bf2008-07-26 23:55:29246
tedvessenes@gmail.comfc4252a72012-01-12 21:58:47247// Interval, in minutes, between state saves.
isherman@chromium.orgfe58acc22012-02-29 01:29:58248const int kSaveStateIntervalMinutes = 5;
249
isherman@chromium.org4266def22012-05-17 01:02:40250enum ResponseStatus {
251 UNKNOWN_FAILURE,
252 SUCCESS,
253 BAD_REQUEST, // Invalid syntax or log too large.
isherman@chromium.org9f5c1ce82012-05-23 23:11:28254 NO_RESPONSE,
isherman@chromium.org4266def22012-05-17 01:02:40255 NUM_RESPONSE_STATUSES
256};
257
258ResponseStatus ResponseCodeToStatus(int response_code) {
259 switch (response_code) {
260 case 200:
261 return SUCCESS;
262 case 400:
263 return BAD_REQUEST;
isherman@chromium.org9f5c1ce82012-05-23 23:11:28264 case net::URLFetcher::RESPONSE_CODE_INVALID:
265 return NO_RESPONSE;
isherman@chromium.org4266def22012-05-17 01:02:40266 default:
267 return UNKNOWN_FAILURE;
268 }
269}
270
stevet@chromium.orge61003a2012-05-24 17:03:19271// The argument used to generate a non-identifying entropy source. We want no
272// more than 13 bits of entropy, so use this max to return a number between 1
273// and 2^13 = 8192 as the entropy source.
274const uint32 kMaxEntropySize = (1 << 13);
275
276// Generates a new non-identifying entropy source used to seed persistent
277// activities.
278int GenerateLowEntropySource() {
279 return base::RandInt(1, kMaxEntropySize);
280}
281
eroman@chromium.orgd7c1fa62012-06-15 23:35:30282// Converts an exit code into something that can be inserted into our
283// histograms (which expect non-negative numbers less than MAX_INT).
284int MapCrashExitCodeForHistogram(int exit_code) {
285#if defined(OS_WIN)
286 // Since |abs(STATUS_GUARD_PAGE_VIOLATION) == MAX_INT| it causes problems in
287 // histograms.cc. Solve this by remapping it to a smaller value, which
288 // hopefully doesn't conflict with other codes.
289 if (exit_code == STATUS_GUARD_PAGE_VIOLATION)
290 return 0x1FCF7EC3; // Randomly picked number.
291#endif
292
293 return std::abs(exit_code);
294}
295
296// Returns a list of all the likely exit codes for crashed renderer processes.
297// The exit codes are all positive, since that is what histograms expect.
298std::vector<int> GetAllCrashExitCodes() {
299 std::vector<int> codes;
300
301 // Chrome defines its own exit codes in the range
302 // 1 to RESULT_CODE_CHROME_LAST_CODE.
303 for (int i = 1; i < chrome::RESULT_CODE_CHROME_LAST_CODE; ++i)
304 codes.push_back(i);
305
306#if defined(OS_WIN)
307 // The exit code when crashing will be one of the Windows exception codes.
308 int kExceptionCodes[] = {
309 0xe06d7363, // C++ EH exception
310 STATUS_ACCESS_VIOLATION,
311 STATUS_ARRAY_BOUNDS_EXCEEDED,
312 STATUS_BREAKPOINT,
313 STATUS_CONTROL_C_EXIT,
314 STATUS_DATATYPE_MISALIGNMENT,
315 STATUS_FLOAT_DENORMAL_OPERAND,
316 STATUS_FLOAT_DIVIDE_BY_ZERO,
317 STATUS_FLOAT_INEXACT_RESULT,
318 STATUS_FLOAT_INVALID_OPERATION,
319 STATUS_FLOAT_OVERFLOW,
320 STATUS_FLOAT_STACK_CHECK,
321 STATUS_FLOAT_UNDERFLOW,
322 STATUS_GUARD_PAGE_VIOLATION,
323 STATUS_ILLEGAL_INSTRUCTION,
324 STATUS_INTEGER_DIVIDE_BY_ZERO,
325 STATUS_INTEGER_OVERFLOW,
326 STATUS_INVALID_DISPOSITION,
327 STATUS_INVALID_HANDLE,
328 STATUS_INVALID_PARAMETER,
329 STATUS_IN_PAGE_ERROR,
330 STATUS_NONCONTINUABLE_EXCEPTION,
331 STATUS_NO_MEMORY,
332 STATUS_PRIVILEGED_INSTRUCTION,
333 STATUS_SINGLE_STEP,
334 STATUS_STACK_OVERFLOW,
335 };
336
337 for (size_t i = 0; i < arraysize(kExceptionCodes); ++i)
338 codes.push_back(MapCrashExitCodeForHistogram(kExceptionCodes[i]));
339#endif
340
341 return codes;
342}
343
isherman@chromium.orgfe58acc22012-02-29 01:29:58344}
initial.commit09911bf2008-07-26 23:55:29345
jar@chromium.orgc0c55e92011-09-10 18:47:30346// static
347MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ =
348 MetricsService::CLEANLY_SHUTDOWN;
349
erg@google.com679082052010-07-21 21:30:13350// This is used to quickly log stats from child process related notifications in
351// MetricsService::child_stats_buffer_. The buffer's contents are transferred
352// out when Local State is periodically saved. The information is then
353// reported to the UMA server on next launch.
354struct MetricsService::ChildProcessStats {
355 public:
jam@chromium.orgbd5d6cf2011-12-01 00:39:12356 explicit ChildProcessStats(content::ProcessType type)
erg@google.com679082052010-07-21 21:30:13357 : process_launches(0),
358 process_crashes(0),
359 instances(0),
bauerb@chromium.orgcd937072012-07-02 09:00:29360 loading_errors(0),
erg@google.com679082052010-07-21 21:30:13361 process_type(type) {}
362
363 // This constructor is only used by the map to return some default value for
364 // an index for which no value has been assigned.
365 ChildProcessStats()
366 : process_launches(0),
pkasting@chromium.orgd88bf0a2011-08-30 23:55:57367 process_crashes(0),
368 instances(0),
bauerb@chromium.orgcd937072012-07-02 09:00:29369 loading_errors(0),
jam@chromium.orgbd5d6cf2011-12-01 00:39:12370 process_type(content::PROCESS_TYPE_UNKNOWN) {}
erg@google.com679082052010-07-21 21:30:13371
372 // The number of times that the given child process has been launched
373 int process_launches;
374
375 // The number of times that the given child process has crashed
376 int process_crashes;
377
378 // The number of instances of this child process that have been created.
379 // An instance is a DOM object rendered by this child process during a page
380 // load.
381 int instances;
382
bauerb@chromium.orgcd937072012-07-02 09:00:29383 // The number of times there was an error loading an instance of this child
384 // process.
385 int loading_errors;
386
jam@chromium.orgbd5d6cf2011-12-01 00:39:12387 content::ProcessType process_type;
erg@google.com679082052010-07-21 21:30:13388};
initial.commit09911bf2008-07-26 23:55:29389
sail@chromium.org84c988a2011-04-19 17:56:33390// Handles asynchronous fetching of memory details.
391// Will run the provided task after finished.
392class MetricsMemoryDetails : public MemoryDetails {
393 public:
dcheng@chromium.org2226c222011-11-22 00:08:40394 explicit MetricsMemoryDetails(const base::Closure& callback)
395 : callback_(callback) {}
sail@chromium.org84c988a2011-04-19 17:56:33396
397 virtual void OnDetailsAvailable() {
dcheng@chromium.org2226c222011-11-22 00:08:40398 MessageLoop::current()->PostTask(FROM_HERE, callback_);
sail@chromium.org84c988a2011-04-19 17:56:33399 }
400
401 private:
402 ~MetricsMemoryDetails() {}
403
dcheng@chromium.org2226c222011-11-22 00:08:40404 base::Closure callback_;
sail@chromium.org84c988a2011-04-19 17:56:33405 DISALLOW_COPY_AND_ASSIGN(MetricsMemoryDetails);
406};
407
initial.commit09911bf2008-07-26 23:55:29408// static
409void MetricsService::RegisterPrefs(PrefService* local_state) {
410 DCHECK(IsSingleThreaded());
estade@chromium.org20ce516d2010-06-18 02:20:04411 local_state->RegisterStringPref(prefs::kMetricsClientID, "");
stevet@chromium.orge61003a2012-05-24 17:03:19412 local_state->RegisterIntegerPref(prefs::kMetricsLowEntropySource, 0);
robertshield@google.com0bb1a622009-03-04 03:22:32413 local_state->RegisterInt64Pref(prefs::kMetricsClientIDTimestamp, 0);
414 local_state->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0);
415 local_state->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0);
estade@chromium.org20ce516d2010-06-18 02:20:04416 local_state->RegisterStringPref(prefs::kStabilityStatsVersion, "");
jar@chromium.org225c50842010-01-19 21:19:13417 local_state->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0);
initial.commit09911bf2008-07-26 23:55:29418 local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true);
419 local_state->RegisterBooleanPref(prefs::kStabilitySessionEndCompleted, true);
420 local_state->RegisterIntegerPref(prefs::kMetricsSessionID, -1);
421 local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0);
422 local_state->RegisterIntegerPref(prefs::kStabilityCrashCount, 0);
423 local_state->RegisterIntegerPref(prefs::kStabilityIncompleteSessionEndCount,
424 0);
425 local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0);
initial.commit09911bf2008-07-26 23:55:29426 local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0);
asargent@chromium.org1f085622009-12-04 05:33:45427 local_state->RegisterIntegerPref(prefs::kStabilityExtensionRendererCrashCount,
428 0);
initial.commit09911bf2008-07-26 23:55:29429 local_state->RegisterIntegerPref(prefs::kStabilityRendererHangCount, 0);
asargent@chromium.org1f085622009-12-04 05:33:45430 local_state->RegisterIntegerPref(prefs::kStabilityChildProcessCrashCount, 0);
cpu@google.come73c01972008-08-13 00:18:24431 local_state->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationFail,
432 0);
433 local_state->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationSuccess,
434 0);
435 local_state->RegisterIntegerPref(prefs::kStabilityDebuggerPresent, 0);
436 local_state->RegisterIntegerPref(prefs::kStabilityDebuggerNotPresent, 0);
petkov@chromium.orgc1834a92011-01-21 18:21:03437#if defined(OS_CHROMEOS)
438 local_state->RegisterIntegerPref(prefs::kStabilityOtherUserCrashCount, 0);
439 local_state->RegisterIntegerPref(prefs::kStabilityKernelCrashCount, 0);
440 local_state->RegisterIntegerPref(prefs::kStabilitySystemUncleanShutdownCount,
441 0);
442#endif // OS_CHROMEOS
cpu@google.come73c01972008-08-13 00:18:24443
initial.commit09911bf2008-07-26 23:55:29444 local_state->RegisterDictionaryPref(prefs::kProfileMetrics);
445 local_state->RegisterIntegerPref(prefs::kNumBookmarksOnBookmarkBar, 0);
446 local_state->RegisterIntegerPref(prefs::kNumFoldersOnBookmarkBar, 0);
447 local_state->RegisterIntegerPref(prefs::kNumBookmarksInOtherBookmarkFolder,
448 0);
449 local_state->RegisterIntegerPref(prefs::kNumFoldersInOtherBookmarkFolder, 0);
450 local_state->RegisterIntegerPref(prefs::kNumKeywords, 0);
isherman@chromium.orgfe58acc22012-02-29 01:29:58451 local_state->RegisterListPref(prefs::kMetricsInitialLogsXml);
452 local_state->RegisterListPref(prefs::kMetricsOngoingLogsXml);
453 local_state->RegisterListPref(prefs::kMetricsInitialLogsProto);
454 local_state->RegisterListPref(prefs::kMetricsOngoingLogsProto);
robertshield@google.com0bb1a622009-03-04 03:22:32455
456 local_state->RegisterInt64Pref(prefs::kUninstallMetricsPageLoadCount, 0);
457 local_state->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0);
robertshield@google.com6b5f21d2009-04-13 17:01:35458 local_state->RegisterInt64Pref(prefs::kUninstallMetricsInstallDate, 0);
robertshield@google.com0bb1a622009-03-04 03:22:32459 local_state->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0);
460 local_state->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0);
461 local_state->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0);
initial.commit09911bf2008-07-26 23:55:29462}
463
jar@chromium.org541f77922009-02-23 21:14:38464// static
465void MetricsService::DiscardOldStabilityStats(PrefService* local_state) {
466 local_state->SetBoolean(prefs::kStabilityExitedCleanly, true);
jar@chromium.orgc9abf242009-07-18 06:00:38467 local_state->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
jar@chromium.org541f77922009-02-23 21:14:38468
469 local_state->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
470 local_state->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
471 local_state->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
472 local_state->SetInteger(prefs::kStabilityDebuggerPresent, 0);
473 local_state->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
474
475 local_state->SetInteger(prefs::kStabilityLaunchCount, 0);
476 local_state->SetInteger(prefs::kStabilityCrashCount, 0);
477
478 local_state->SetInteger(prefs::kStabilityPageLoadCount, 0);
479 local_state->SetInteger(prefs::kStabilityRendererCrashCount, 0);
480 local_state->SetInteger(prefs::kStabilityRendererHangCount, 0);
481
jar@chromium.org9165f742010-03-10 22:55:01482 local_state->SetInt64(prefs::kStabilityLaunchTimeSec, 0);
483 local_state->SetInt64(prefs::kStabilityLastTimestampSec, 0);
jar@chromium.org541f77922009-02-23 21:14:38484
485 local_state->ClearPref(prefs::kStabilityPluginStats);
jar@chromium.orgae155cb92009-06-19 06:10:37486
isherman@chromium.orgfe58acc22012-02-29 01:29:58487 local_state->ClearPref(prefs::kMetricsInitialLogsXml);
488 local_state->ClearPref(prefs::kMetricsOngoingLogsXml);
489 local_state->ClearPref(prefs::kMetricsInitialLogsProto);
490 local_state->ClearPref(prefs::kMetricsOngoingLogsProto);
jar@chromium.org541f77922009-02-23 21:14:38491}
492
initial.commit09911bf2008-07-26 23:55:29493MetricsService::MetricsService()
petersont@google.comd01b8732008-10-16 02:18:07494 : recording_active_(false),
495 reporting_active_(false),
petersont@google.comd01b8732008-10-16 02:18:07496 state_(INITIALIZED),
stevet@chromium.orge61003a2012-05-24 17:03:19497 low_entropy_source_(0),
petersont@google.comd01b8732008-10-16 02:18:07498 idle_since_last_transmission_(false),
initial.commit09911bf2008-07-26 23:55:29499 next_window_id_(0),
dpolukhin@chromium.orgc94d7382012-02-28 08:43:40500 ALLOW_THIS_IN_INITIALIZER_LIST(self_ptr_factory_(this)),
maruel@chromium.org40bcc302009-03-02 20:50:39501 ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_factory_(this)),
isherman@chromium.orgd119f222012-06-08 02:33:27502 waiting_for_asynchronous_reporting_step_(false) {
initial.commit09911bf2008-07-26 23:55:29503 DCHECK(IsSingleThreaded());
504 InitializeMetricsState();
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16505
506 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:40507 self_ptr_factory_.GetWeakPtr());
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16508 scheduler_.reset(new MetricsReportingScheduler(callback));
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10509 log_manager_.set_log_serializer(new MetricsLogSerializer());
510 log_manager_.set_max_ongoing_log_store_size(kUploadLogAvoidRetransmitSize);
initial.commit09911bf2008-07-26 23:55:29511}
512
513MetricsService::~MetricsService() {
514 SetRecording(false);
515}
516
petersont@google.comd01b8732008-10-16 02:18:07517void MetricsService::Start() {
stuartmorgan@chromium.orgb1c8dc02011-04-13 18:32:04518 HandleIdleSinceLastTransmission(false);
petersont@google.comd01b8732008-10-16 02:18:07519 SetRecording(true);
520 SetReporting(true);
521}
522
523void MetricsService::StartRecordingOnly() {
524 SetRecording(true);
525 SetReporting(false);
526}
527
528void MetricsService::Stop() {
stuartmorgan@chromium.orgb1c8dc02011-04-13 18:32:04529 HandleIdleSinceLastTransmission(false);
petersont@google.comd01b8732008-10-16 02:18:07530 SetReporting(false);
531 SetRecording(false);
532}
533
joi@chromium.orgedafd4c2011-05-10 17:18:53534std::string MetricsService::GetClientId() {
535 return client_id_;
536}
537
stevet@chromium.orge61003a2012-05-24 17:03:19538std::string MetricsService::GetEntropySource() {
stevet@chromium.org29d81ee02012-05-25 05:45:42539 // For metrics reporting-enabled users, we combine the client ID and low
540 // entropy source to get the final entropy source. Otherwise, only use the low
541 // entropy source.
542 // This has two useful properties:
stevet@chromium.orge61003a2012-05-24 17:03:19543 // 1) It makes the entropy source less identifiable for parties that do not
544 // know the low entropy source.
545 // 2) It makes the final entropy source resettable.
stevet@chromium.org29d81ee02012-05-25 05:45:42546 std::string low_entropy_source = base::IntToString(GetLowEntropySource());
547 if (reporting_active())
548 return client_id_ + low_entropy_source;
549 return low_entropy_source;
stevet@chromium.orge61003a2012-05-24 17:03:19550}
551
jam@chromium.org5cbeeef72012-02-08 02:05:18552void MetricsService::ForceClientIdCreation() {
553 if (!client_id_.empty())
554 return;
555 PrefService* pref = g_browser_process->local_state();
556 client_id_ = pref->GetString(prefs::kMetricsClientID);
557 if (!client_id_.empty())
558 return;
559
560 client_id_ = GenerateClientID();
561 pref->SetString(prefs::kMetricsClientID, client_id_);
562
563 // Might as well make a note of how long this ID has existed
564 pref->SetString(prefs::kMetricsClientIDTimestamp,
565 base::Int64ToString(Time::Now().ToTimeT()));
566}
567
initial.commit09911bf2008-07-26 23:55:29568void MetricsService::SetRecording(bool enabled) {
569 DCHECK(IsSingleThreaded());
570
petersont@google.comd01b8732008-10-16 02:18:07571 if (enabled == recording_active_)
initial.commit09911bf2008-07-26 23:55:29572 return;
573
574 if (enabled) {
jam@chromium.org5cbeeef72012-02-08 02:05:18575 ForceClientIdCreation();
kuchhal@chromium.org157d5472009-11-05 22:31:03576 child_process_logging::SetClientId(client_id_);
initial.commit09911bf2008-07-26 23:55:29577 StartRecording();
pkasting@chromium.org005ef3e2009-05-22 20:55:46578
stuartmorgan@chromium.org3ffd3ae2011-03-17 22:17:52579 SetUpNotifications(&registrar_, this);
initial.commit09911bf2008-07-26 23:55:29580 } else {
pkasting@chromium.org005ef3e2009-05-22 20:55:46581 registrar_.RemoveAll();
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10582 PushPendingLogsToPersistentStorage();
583 DCHECK(!log_manager_.has_staged_log());
initial.commit09911bf2008-07-26 23:55:29584 }
petersont@google.comd01b8732008-10-16 02:18:07585 recording_active_ = enabled;
initial.commit09911bf2008-07-26 23:55:29586}
587
petersont@google.comd01b8732008-10-16 02:18:07588bool MetricsService::recording_active() const {
initial.commit09911bf2008-07-26 23:55:29589 DCHECK(IsSingleThreaded());
petersont@google.comd01b8732008-10-16 02:18:07590 return recording_active_;
initial.commit09911bf2008-07-26 23:55:29591}
592
petersont@google.comd01b8732008-10-16 02:18:07593void MetricsService::SetReporting(bool enable) {
594 if (reporting_active_ != enable) {
595 reporting_active_ = enable;
596 if (reporting_active_)
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16597 StartSchedulerIfNecessary();
initial.commit09911bf2008-07-26 23:55:29598 }
petersont@google.comd01b8732008-10-16 02:18:07599}
600
601bool MetricsService::reporting_active() const {
602 DCHECK(IsSingleThreaded());
603 return reporting_active_;
initial.commit09911bf2008-07-26 23:55:29604}
605
rtenneti@chromium.org87ef9ea2011-02-26 03:15:15606// static
jam@chromium.org6c2381d2011-10-19 02:52:53607void MetricsService::SetUpNotifications(
608 content::NotificationRegistrar* registrar,
609 content::NotificationObserver* observer) {
610 registrar->Add(observer, chrome::NOTIFICATION_BROWSER_OPENED,
jam@chromium.orgad50def52011-10-19 23:17:07611 content::NotificationService::AllBrowserContextsAndSources());
jam@chromium.org6c2381d2011-10-19 02:52:53612 registrar->Add(observer, chrome::NOTIFICATION_BROWSER_CLOSED,
jam@chromium.orgad50def52011-10-19 23:17:07613 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53614 registrar->Add(observer, content::NOTIFICATION_USER_ACTION,
jam@chromium.orgad50def52011-10-19 23:17:07615 content::NotificationService::AllSources());
avi@chromium.org884033e2012-04-16 19:38:42616 registrar->Add(observer, chrome::NOTIFICATION_TAB_PARENTED,
jam@chromium.orgad50def52011-10-19 23:17:07617 content::NotificationService::AllSources());
avi@chromium.org884033e2012-04-16 19:38:42618 registrar->Add(observer, chrome::NOTIFICATION_TAB_CLOSING,
jam@chromium.orgad50def52011-10-19 23:17:07619 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53620 registrar->Add(observer, content::NOTIFICATION_LOAD_START,
jam@chromium.orgad50def52011-10-19 23:17:07621 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53622 registrar->Add(observer, content::NOTIFICATION_LOAD_STOP,
jam@chromium.orgad50def52011-10-19 23:17:07623 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53624 registrar->Add(observer, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
jam@chromium.orgad50def52011-10-19 23:17:07625 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53626 registrar->Add(observer, content::NOTIFICATION_RENDERER_PROCESS_HANG,
jam@chromium.orgad50def52011-10-19 23:17:07627 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53628 registrar->Add(observer, content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED,
jam@chromium.orgad50def52011-10-19 23:17:07629 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53630 registrar->Add(observer, content::NOTIFICATION_CHILD_INSTANCE_CREATED,
jam@chromium.orgad50def52011-10-19 23:17:07631 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53632 registrar->Add(observer, content::NOTIFICATION_CHILD_PROCESS_CRASHED,
jam@chromium.orgad50def52011-10-19 23:17:07633 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53634 registrar->Add(observer, chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED,
jam@chromium.orgad50def52011-10-19 23:17:07635 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53636 registrar->Add(observer, chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
jam@chromium.orgad50def52011-10-19 23:17:07637 content::NotificationService::AllSources());
jam@chromium.org6c2381d2011-10-19 02:52:53638 registrar->Add(observer, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED,
jam@chromium.orgad50def52011-10-19 23:17:07639 content::NotificationService::AllBrowserContextsAndSources());
rtenneti@chromium.org87ef9ea2011-02-26 03:15:15640}
641
ananta@chromium.org432115822011-07-10 15:52:27642void MetricsService::Observe(int type,
jam@chromium.org6c2381d2011-10-19 02:52:53643 const content::NotificationSource& source,
644 const content::NotificationDetails& details) {
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10645 DCHECK(log_manager_.current_log());
initial.commit09911bf2008-07-26 23:55:29646 DCHECK(IsSingleThreaded());
647
648 if (!CanLogNotification(type, source, details))
649 return;
650
ananta@chromium.org432115822011-07-10 15:52:27651 switch (type) {
652 case content::NOTIFICATION_USER_ACTION:
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:10653 log_manager_.current_log()->RecordUserAction(
jam@chromium.org6c2381d2011-10-19 02:52:53654 *content::Details<const char*>(details).ptr());
initial.commit09911bf2008-07-26 23:55:29655 break;
656
ananta@chromium.org432115822011-07-10 15:52:27657 case chrome::NOTIFICATION_BROWSER_OPENED:
658 case chrome::NOTIFICATION_BROWSER_CLOSED:
initial.commit09911bf2008-07-26 23:55:29659 LogWindowChange(type, source, details);
660 break;
661
avi@chromium.org884033e2012-04-16 19:38:42662 case chrome::NOTIFICATION_TAB_PARENTED:
663 case chrome::NOTIFICATION_TAB_CLOSING:
initial.commit09911bf2008-07-26 23:55:29664 LogWindowChange(type, source, details);
665 break;
666
ananta@chromium.org432115822011-07-10 15:52:27667 case content::NOTIFICATION_LOAD_STOP:
initial.commit09911bf2008-07-26 23:55:29668 LogLoadComplete(type, source, details);
669 break;
670
ananta@chromium.org432115822011-07-10 15:52:27671 case content::NOTIFICATION_LOAD_START:
initial.commit09911bf2008-07-26 23:55:29672 LogLoadStarted();
673 break;
674
ananta@chromium.org432115822011-07-10 15:52:27675 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: {
ananta@chromium.orgf3b1a082011-11-18 00:34:30676 content::RenderProcessHost::RendererClosedDetails* process_details =
677 content::Details<
678 content::RenderProcessHost::RendererClosedDetails>(
679 details).ptr();
680 content::RenderProcessHost* host =
681 content::Source<content::RenderProcessHost>(source).ptr();
jar@chromium.orgc3721482012-03-23 16:21:48682 LogRendererCrash(
jam@chromium.orgf1675202012-07-09 15:18:00683 host, process_details->status, process_details->exit_code);
asargent@chromium.org1f085622009-12-04 05:33:45684 }
initial.commit09911bf2008-07-26 23:55:29685 break;
686
ananta@chromium.org432115822011-07-10 15:52:27687 case content::NOTIFICATION_RENDERER_PROCESS_HANG:
initial.commit09911bf2008-07-26 23:55:29688 LogRendererHang();
689 break;
690
ananta@chromium.org432115822011-07-10 15:52:27691 case content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED:
692 case content::NOTIFICATION_CHILD_PROCESS_CRASHED:
693 case content::NOTIFICATION_CHILD_INSTANCE_CREATED:
jam@chromium.orga27a9382009-02-11 23:55:10694 LogChildProcessChange(type, source, details);
initial.commit09911bf2008-07-26 23:55:29695 break;
696
ananta@chromium.org432115822011-07-10 15:52:27697 case chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED:
pkasting@chromium.org604d8a062012-03-16 21:37:46698 LogKeywordCount(content::Source<TemplateURLService>(
699 source)->GetTemplateURLs().size());
initial.commit09911bf2008-07-26 23:55:29700 break;
701
ananta@chromium.org432115822011-07-10 15:52:27702 case chrome::NOTIFICATION_OMNIBOX_OPENED_URL: {
isherman@chromium.org279703f2012-01-20 22:23:26703 MetricsLog* current_log =
704 static_cast<MetricsLog*>(log_manager_.current_log());
ananta@chromium.org1226abb2010-06-10 18:01:28705 DCHECK(current_log);
706 current_log->RecordOmniboxOpenedURL(
jam@chromium.org6c2381d2011-10-19 02:52:53707 *content::Details<AutocompleteLog>(details).ptr());
initial.commit09911bf2008-07-26 23:55:29708 break;
ananta@chromium.org1226abb2010-06-10 18:01:28709 }
initial.commit09911bf2008-07-26 23:55:29710
ananta@chromium.org432115822011-07-10 15:52:27711 case chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED: {
jam@chromium.org6c2381d2011-10-19 02:52:53712 Profile* p = content::Source<Profile>(source).ptr();
tim@chromium.orgb61236c62009-04-09 22:43:55713 if (p)
714 LogBookmarks(p->GetBookmarkModel());
initial.commit09911bf2008-07-26 23:55:29715 break;
tim@chromium.orgb61236c62009-04-09 22:43:55716 }
initial.commit09911bf2008-07-26 23:55:29717 default:
jar@chromium.orga063c102010-07-22 22:20:19718 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29719 break;
720 }
petersont@google.comd01b8732008-10-16 02:18:07721
722 HandleIdleSinceLastTransmission(false);
petersont@google.comd01b8732008-10-16 02:18:07723}
724
725void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) {
726 // If there wasn't a lot of action, maybe the computer was asleep, in which
727 // case, the log transmissions should have stopped. Here we start them up
728 // again.
pkasting@chromium.orgcac78842008-11-27 01:02:20729 if (!in_idle && idle_since_last_transmission_)
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16730 StartSchedulerIfNecessary();
pkasting@chromium.orgcac78842008-11-27 01:02:20731 idle_since_last_transmission_ = in_idle;
initial.commit09911bf2008-07-26 23:55:29732}
733
initial.commit09911bf2008-07-26 23:55:29734void MetricsService::RecordStartOfSessionEnd() {
stuartmorgan@chromium.org466f3c12011-03-23 21:20:38735 LogCleanShutdown();
initial.commit09911bf2008-07-26 23:55:29736 RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, false);
737}
738
739void MetricsService::RecordCompletedSessionEnd() {
stuartmorgan@chromium.org466f3c12011-03-23 21:20:38740 LogCleanShutdown();
initial.commit09911bf2008-07-26 23:55:29741 RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, true);
742}
743
dfalcantara@chromium.org117fbdf22012-06-26 18:36:39744#if defined(OS_ANDROID)
745void MetricsService::OnAppEnterBackground() {
746 scheduler_->Stop();
747
748 PrefService* pref = g_browser_process->local_state();
749 pref->SetBoolean(prefs::kStabilityExitedCleanly, true);
750
751 // At this point, there's no way of knowing when the process will be
752 // killed, so this has to be treated similar to a shutdown, closing and
753 // persisting all logs. Unlinke a shutdown, the state is primed to be ready
754 // to continue logging and uploading if the process does return.
755 if (recording_active() && state_ >= INITIAL_LOG_READY) {
756 PushPendingLogsToPersistentStorage();
757 if (state_ == SENDING_CURRENT_LOGS)
758 state_ = SENDING_OLD_LOGS;
759 // Persisting logs stops recording, so start recording a new log immediately
760 // to capture any background work that might be done before the process is
761 // killed.
762 StartRecording();
763 }
764
765 // Start writing right away (write happens on a different thread).
766 pref->CommitPendingWrite();
767}
768
769void MetricsService::OnAppEnterForeground() {
770 PrefService* pref = g_browser_process->local_state();
771 pref->SetBoolean(prefs::kStabilityExitedCleanly, false);
772
773 StartSchedulerIfNecessary();
774}
775#endif
776
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:16777void MetricsService::RecordBreakpadRegistration(bool success) {
jar@google.com68475e602008-08-22 03:21:15778 if (!success)
cpu@google.come73c01972008-08-13 00:18:24779 IncrementPrefValue(prefs::kStabilityBreakpadRegistrationFail);
780 else
781 IncrementPrefValue(prefs::kStabilityBreakpadRegistrationSuccess);
782}
783
784void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) {
785 if (!has_debugger)
786 IncrementPrefValue(prefs::kStabilityDebuggerNotPresent);
787 else
jar@google.com68475e602008-08-22 03:21:15788 IncrementPrefValue(prefs::kStabilityDebuggerPresent);
cpu@google.come73c01972008-08-13 00:18:24789}
790
initial.commit09911bf2008-07-26 23:55:29791//------------------------------------------------------------------------------
792// private methods
793//------------------------------------------------------------------------------
794
795
796//------------------------------------------------------------------------------
797// Initialization methods
798
799void MetricsService::InitializeMetricsState() {
kuchhal@chromium.org79bf0b72009-04-27 21:30:55800#if defined(OS_POSIX)
isherman@chromium.orgfe58acc22012-02-29 01:29:58801 server_url_xml_ = ASCIIToUTF16(kServerUrlXml);
802 server_url_proto_ = ASCIIToUTF16(kServerUrlProto);
simonjam@chromium.orgb4a72d842012-03-22 20:09:09803 network_stats_server_ = chrome_common_net::kEchoTestServerLocation;
804 http_pipelining_test_server_ = chrome_common_net::kPipelineTestServerBaseUrl;
kuchhal@chromium.org79bf0b72009-04-27 21:30:55805#else
806 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
isherman@chromium.orgfe58acc22012-02-29 01:29:58807 server_url_xml_ = dist->GetStatsServerURL();
808 server_url_proto_ = ASCIIToUTF16(kServerUrlProto);
rtenneti@chromium.orgd67d1052011-06-09 05:11:41809 network_stats_server_ = dist->GetNetworkStatsServer();
simonjam@chromium.orgadbb3762012-03-09 22:20:08810 http_pipelining_test_server_ = dist->GetHttpPipeliningTestServer();
kuchhal@chromium.org79bf0b72009-04-27 21:30:55811#endif
812
initial.commit09911bf2008-07-26 23:55:29813 PrefService* pref = g_browser_process->local_state();
814 DCHECK(pref);
815
jar@chromium.org225c50842010-01-19 21:19:13816 if ((pref->GetInt64(prefs::kStabilityStatsBuildTime)
817 != MetricsLog::GetBuildTime()) ||
estade@chromium.orgddd231e2010-06-29 20:35:19818 (pref->GetString(prefs::kStabilityStatsVersion)
jar@chromium.org225c50842010-01-19 21:19:13819 != MetricsLog::GetVersionString())) {
jar@chromium.org541f77922009-02-23 21:14:38820 // This is a new version, so we don't want to confuse the stats about the
821 // old version with info that we upload.
822 DiscardOldStabilityStats(pref);
823 pref->SetString(prefs::kStabilityStatsVersion,
estade@chromium.orgddd231e2010-06-29 20:35:19824 MetricsLog::GetVersionString());
jar@chromium.org225c50842010-01-19 21:19:13825 pref->SetInt64(prefs::kStabilityStatsBuildTime,
826 MetricsLog::GetBuildTime());
jar@chromium.org541f77922009-02-23 21:14:38827 }
828
initial.commit09911bf2008-07-26 23:55:29829 // Update session ID
830 session_id_ = pref->GetInteger(prefs::kMetricsSessionID);
831 ++session_id_;
832 pref->SetInteger(prefs::kMetricsSessionID, session_id_);
833
initial.commit09911bf2008-07-26 23:55:29834 // Stability bookkeeping
cpu@google.come73c01972008-08-13 00:18:24835 IncrementPrefValue(prefs::kStabilityLaunchCount);
initial.commit09911bf2008-07-26 23:55:29836
cpu@google.come73c01972008-08-13 00:18:24837 if (!pref->GetBoolean(prefs::kStabilityExitedCleanly)) {
838 IncrementPrefValue(prefs::kStabilityCrashCount);
jar@chromium.orgc0c55e92011-09-10 18:47:30839 // Reset flag, and wait until we call LogNeedForCleanShutdown() before
840 // monitoring.
841 pref->SetBoolean(prefs::kStabilityExitedCleanly, true);
initial.commit09911bf2008-07-26 23:55:29842 }
cpu@google.come73c01972008-08-13 00:18:24843
cpu@google.come73c01972008-08-13 00:18:24844 if (!pref->GetBoolean(prefs::kStabilitySessionEndCompleted)) {
845 IncrementPrefValue(prefs::kStabilityIncompleteSessionEndCount);
jar@chromium.orgc9abf242009-07-18 06:00:38846 // This is marked false when we get a WM_ENDSESSION.
847 pref->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
initial.commit09911bf2008-07-26 23:55:29848 }
initial.commit09911bf2008-07-26 23:55:29849
jar@chromium.org9165f742010-03-10 22:55:01850 // Initialize uptime counters.
851 int64 startup_uptime = MetricsLog::GetIncrementalUptime(pref);
mad@google.comae393ec702010-06-27 16:23:14852 DCHECK_EQ(0, startup_uptime);
jar@chromium.org9165f742010-03-10 22:55:01853 // For backwards compatibility, leave this intact in case Omaha is checking
854 // them. prefs::kStabilityLastTimestampSec may also be useless now.
855 // TODO(jar): Delete these if they have no uses.
robertshield@google.com0bb1a622009-03-04 03:22:32856 pref->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT());
857
858 // Bookkeeping for the uninstall metrics.
859 IncrementLongPrefsValue(prefs::kUninstallLaunchCount);
initial.commit09911bf2008-07-26 23:55:29860
861 // Save profile metrics.
862 PrefService* prefs = g_browser_process->local_state();
863 if (prefs) {
864 // Remove the current dictionary and store it for use when sending data to
865 // server. By removing the value we prune potentially dead profiles
866 // (and keys). All valid values are added back once services startup.
867 const DictionaryValue* profile_dictionary =
868 prefs->GetDictionary(prefs::kProfileMetrics);
869 if (profile_dictionary) {
870 // Do a deep copy of profile_dictionary since ClearPref will delete it.
871 profile_dictionary_.reset(static_cast<DictionaryValue*>(
872 profile_dictionary->DeepCopy()));
873 prefs->ClearPref(prefs::kProfileMetrics);
874 }
875 }
876
jar@chromium.org92745242009-06-12 16:52:21877 // Get stats on use of command line.
878 const CommandLine* command_line(CommandLine::ForCurrentProcess());
879 size_t common_commands = 0;
880 if (command_line->HasSwitch(switches::kUserDataDir)) {
881 ++common_commands;
882 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineDatDirCount", 1);
883 }
884
885 if (command_line->HasSwitch(switches::kApp)) {
886 ++common_commands;
887 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineAppModeCount", 1);
888 }
889
msw@chromium.org62b4e522011-07-13 21:46:32890 size_t switch_count = command_line->GetSwitches().size();
891 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineFlagCount", switch_count);
jar@chromium.org92745242009-06-12 16:52:21892 UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineUncommonFlagCount",
msw@chromium.org62b4e522011-07-13 21:46:32893 switch_count - common_commands);
jar@chromium.org92745242009-06-12 16:52:21894
initial.commit09911bf2008-07-26 23:55:29895 // Kick off the process of saving the state (so the uptime numbers keep
896 // getting updated) every n minutes.
897 ScheduleNextStateSave();
898}
899
dpolukhin@chromium.orgc94d7382012-02-28 08:43:40900// static
rsesek@chromium.orgd33e7cc2011-09-23 01:43:56901void MetricsService::InitTaskGetHardwareClass(
dpolukhin@chromium.orgc94d7382012-02-28 08:43:40902 base::WeakPtr<MetricsService> self,
rsesek@chromium.orgd33e7cc2011-09-23 01:43:56903 base::MessageLoopProxy* target_loop) {
rsesek@chromium.orgd33e7cc2011-09-23 01:43:56904 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
905
906 std::string hardware_class;
907#if defined(OS_CHROMEOS)
908 chromeos::system::StatisticsProvider::GetInstance()->GetMachineStatistic(
909 "hardware_class", &hardware_class);
910#endif // OS_CHROMEOS
911
912 target_loop->PostTask(FROM_HERE,
913 base::Bind(&MetricsService::OnInitTaskGotHardwareClass,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:40914 self, hardware_class));
rsesek@chromium.orgd33e7cc2011-09-23 01:43:56915}
916
917void MetricsService::OnInitTaskGotHardwareClass(
918 const std::string& hardware_class) {
isherman@chromium.orged0fd002012-04-25 23:10:34919 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
zelidrag@chromium.org85ed9d42010-06-08 22:37:44920 hardware_class_ = hardware_class;
rsesek@chromium.orgd33e7cc2011-09-23 01:43:56921
922 // Start the next part of the init task: loading plugin information.
923 PluginService::GetInstance()->GetPlugins(
924 base::Bind(&MetricsService::OnInitTaskGotPluginInfo,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:40925 self_ptr_factory_.GetWeakPtr()));
rsesek@chromium.orgd33e7cc2011-09-23 01:43:56926}
927
928void MetricsService::OnInitTaskGotPluginInfo(
929 const std::vector<webkit::WebPluginInfo>& plugins) {
isherman@chromium.orged0fd002012-04-25 23:10:34930 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
jam@chromium.org35fa6a22009-08-15 00:04:01931 plugins_ = plugins;
rsesek@chromium.orgd33e7cc2011-09-23 01:43:56932
ryanmyers@chromium.org197c0772012-05-14 23:50:51933 // Schedules a task on a blocking pool thread to gather Google Update
934 // statistics (requires Registry reads).
935 BrowserThread::PostBlockingPoolTask(
936 FROM_HERE,
937 base::Bind(&MetricsService::InitTaskGetGoogleUpdateData,
938 self_ptr_factory_.GetWeakPtr(),
939 MessageLoop::current()->message_loop_proxy()));
940}
941
942// static
943void MetricsService::InitTaskGetGoogleUpdateData(
944 base::WeakPtr<MetricsService> self,
945 base::MessageLoopProxy* target_loop) {
946 GoogleUpdateMetrics google_update_metrics;
947
948#if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
949 const bool system_install = GoogleUpdateSettings::IsSystemInstall();
950
951 google_update_metrics.is_system_install = system_install;
952 google_update_metrics.last_started_au =
953 GoogleUpdateSettings::GetGoogleUpdateLastStartedAU(system_install);
954 google_update_metrics.last_checked =
955 GoogleUpdateSettings::GetGoogleUpdateLastChecked(system_install);
956 GoogleUpdateSettings::GetUpdateDetailForGoogleUpdate(
957 system_install,
958 &google_update_metrics.google_update_data);
959 GoogleUpdateSettings::GetUpdateDetail(
960 system_install,
961 &google_update_metrics.product_data);
962#endif // defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
963
964 target_loop->PostTask(FROM_HERE,
965 base::Bind(&MetricsService::OnInitTaskGotGoogleUpdateData,
966 self, google_update_metrics));
967}
968
969void MetricsService::OnInitTaskGotGoogleUpdateData(
970 const GoogleUpdateMetrics& google_update_metrics) {
971 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
972
973 google_update_metrics_ = google_update_metrics;
974
isherman@chromium.orged0fd002012-04-25 23:10:34975 // Start the next part of the init task: fetching performance data. This will
976 // call into |FinishedReceivingProfilerData()| when the task completes.
977 chrome_browser_metrics::TrackingSynchronizer::FetchProfilerDataAsynchronously(
978 self_ptr_factory_.GetWeakPtr());
979}
980
981void MetricsService::ReceivedProfilerData(
982 const tracked_objects::ProcessDataSnapshot& process_data,
983 content::ProcessType process_type) {
984 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
985
986 // Upon the first callback, create the initial log so that we can immediately
987 // save the profiler data.
988 if (!initial_log_.get())
989 initial_log_.reset(new MetricsLog(client_id_, session_id_));
990
991 initial_log_->RecordProfilerData(process_data, process_type);
992}
993
994void MetricsService::FinishedReceivingProfilerData() {
995 DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
jam@chromium.org3a7b66d2012-04-26 16:34:16996 state_ = INIT_TASK_DONE;
initial.commit09911bf2008-07-26 23:55:29997}
998
stevet@chromium.orge61003a2012-05-24 17:03:19999int MetricsService::GetLowEntropySource() {
1000 // Note that the default value for the low entropy source and the default pref
1001 // value are both zero, which is used to identify if the value has been set
1002 // or not. Valid values start at 1.
1003 if (low_entropy_source_)
1004 return low_entropy_source_;
1005
1006 PrefService* pref = g_browser_process->local_state();
1007 const CommandLine* command_line(CommandLine::ForCurrentProcess());
1008 // Only try to load the value from prefs if the user did not request a reset.
1009 // Otherwise, skip to generating a new value.
1010 if (!command_line->HasSwitch(switches::kResetVariationState)) {
1011 low_entropy_source_ = pref->GetInteger(prefs::kMetricsLowEntropySource);
1012 if (low_entropy_source_)
1013 return low_entropy_source_;
1014 }
1015
1016 low_entropy_source_ = GenerateLowEntropySource();
1017 pref->SetInteger(prefs::kMetricsLowEntropySource, low_entropy_source_);
1018
1019 return low_entropy_source_;
1020}
1021
1022// static
initial.commit09911bf2008-07-26 23:55:291023std::string MetricsService::GenerateClientID() {
marja@chromium.org7e49ad32012-06-14 14:22:071024 return base::GenerateGUID();
initial.commit09911bf2008-07-26 23:55:291025}
1026
initial.commit09911bf2008-07-26 23:55:291027//------------------------------------------------------------------------------
1028// State save methods
1029
1030void MetricsService::ScheduleNextStateSave() {
isherman@chromium.org8454aeb2011-11-19 23:38:201031 state_saver_factory_.InvalidateWeakPtrs();
initial.commit09911bf2008-07-26 23:55:291032
1033 MessageLoop::current()->PostDelayedTask(FROM_HERE,
isherman@chromium.org8454aeb2011-11-19 23:38:201034 base::Bind(&MetricsService::SaveLocalState,
1035 state_saver_factory_.GetWeakPtr()),
tedvessenes@gmail.comfc4252a72012-01-12 21:58:471036 base::TimeDelta::FromMinutes(kSaveStateIntervalMinutes));
initial.commit09911bf2008-07-26 23:55:291037}
1038
1039void MetricsService::SaveLocalState() {
1040 PrefService* pref = g_browser_process->local_state();
1041 if (!pref) {
jar@chromium.orga063c102010-07-22 22:20:191042 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:291043 return;
1044 }
1045
1046 RecordCurrentState(pref);
initial.commit09911bf2008-07-26 23:55:291047
tedvessenes@gmail.comfc4252a72012-01-12 21:58:471048 // TODO(jar):110021 Does this run down the batteries????
initial.commit09911bf2008-07-26 23:55:291049 ScheduleNextStateSave();
1050}
1051
1052
1053//------------------------------------------------------------------------------
1054// Recording control methods
1055
1056void MetricsService::StartRecording() {
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101057 if (log_manager_.current_log())
initial.commit09911bf2008-07-26 23:55:291058 return;
1059
stuartmorgan@chromium.org29948262012-03-01 12:15:081060 log_manager_.BeginLoggingWithLog(new MetricsLog(client_id_, session_id_),
1061 MetricsLogManager::ONGOING_LOG);
initial.commit09911bf2008-07-26 23:55:291062 if (state_ == INITIALIZED) {
1063 // We only need to schedule that run once.
zelidrag@chromium.org85ed9d42010-06-08 22:37:441064 state_ = INIT_TASK_SCHEDULED;
initial.commit09911bf2008-07-26 23:55:291065
zelidrag@chromium.org85ed9d42010-06-08 22:37:441066 // Schedules a task on the file thread for execution of slower
1067 // initialization steps (such as plugin list generation) necessary
1068 // for sending the initial log. This avoids blocking the main UI
1069 // thread.
joi@chromium.orged10dd12011-12-07 12:03:421070 BrowserThread::PostDelayedTask(
1071 BrowserThread::FILE,
1072 FROM_HERE,
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561073 base::Bind(&MetricsService::InitTaskGetHardwareClass,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:401074 self_ptr_factory_.GetWeakPtr(),
rsesek@chromium.orgd33e7cc2011-09-23 01:43:561075 MessageLoop::current()->message_loop_proxy()),
tedvessenes@gmail.com7e560102012-03-08 20:58:421076 base::TimeDelta::FromSeconds(kInitializationDelaySeconds));
initial.commit09911bf2008-07-26 23:55:291077 }
1078}
1079
stuartmorgan@chromium.org024b5cd2011-05-27 03:29:381080void MetricsService::StopRecording() {
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101081 if (!log_manager_.current_log())
initial.commit09911bf2008-07-26 23:55:291082 return;
1083
jar@google.com68475e602008-08-22 03:21:151084 // TODO(jar): Integrate bounds on log recording more consistently, so that we
1085 // can stop recording logs that are too big much sooner.
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101086 if (log_manager_.current_log()->num_events() > kEventLimit) {
dsh@google.com553dba62009-02-24 19:08:231087 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events",
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101088 log_manager_.current_log()->num_events());
1089 log_manager_.DiscardCurrentLog();
jar@google.com68475e602008-08-22 03:21:151090 StartRecording(); // Start trivial log to hold our histograms.
1091 }
1092
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101093 // Adds to ongoing logs.
1094 log_manager_.current_log()->set_hardware_class(hardware_class_);
jar@chromium.orgaccdfa62011-09-20 01:56:521095
jar@google.com0b33f80b2008-12-17 21:34:361096 // Put incremental data (histogram deltas, and realtime stats deltas) at the
jar@google.com147bbc0b2009-01-06 19:37:401097 // end of all log transmissions (initial log handles this separately).
stuartmorgan@chromium.org024b5cd2011-05-27 03:29:381098 // RecordIncrementalStabilityElements only exists on the derived
1099 // MetricsLog class.
isherman@chromium.org279703f2012-01-20 22:23:261100 MetricsLog* current_log =
1101 static_cast<MetricsLog*>(log_manager_.current_log());
stuartmorgan@chromium.org024b5cd2011-05-27 03:29:381102 DCHECK(current_log);
ryanmyers@chromium.org197c0772012-05-14 23:50:511103 current_log->RecordEnvironmentProto(plugins_, google_update_metrics_);
isherman@chromium.orgfe58acc22012-02-29 01:29:581104 current_log->RecordIncrementalStabilityElements(plugins_);
stuartmorgan@chromium.org024b5cd2011-05-27 03:29:381105 RecordCurrentHistograms();
initial.commit09911bf2008-07-26 23:55:291106
stuartmorgan@chromium.org29948262012-03-01 12:15:081107 log_manager_.FinishCurrentLog();
initial.commit09911bf2008-07-26 23:55:291108}
1109
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101110void MetricsService::PushPendingLogsToPersistentStorage() {
initial.commit09911bf2008-07-26 23:55:291111 if (state_ < INITIAL_LOG_READY)
avi@google.com28ab7f92009-01-06 21:39:041112 return; // We didn't and still don't have time to get plugin list etc.
initial.commit09911bf2008-07-26 23:55:291113
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101114 if (log_manager_.has_staged_log()) {
stuartmorgan@chromium.org7d41ae6d2012-06-26 08:53:031115 // We may race here, and send second copy of the log later.
isherman@chromium.orgdc61fe92012-06-12 00:13:501116 MetricsLogManager::StoreType store_type;
1117 if (current_fetch_xml_.get() || current_fetch_proto_.get())
1118 store_type = MetricsLogManager::PROVISIONAL_STORE;
1119 else
1120 store_type = MetricsLogManager::NORMAL_STORE;
stuartmorgan@chromium.orge7508d82012-05-03 15:59:531121 log_manager_.StoreStagedLogAsUnsent(store_type);
initial.commit09911bf2008-07-26 23:55:291122 }
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101123 DCHECK(!log_manager_.has_staged_log());
stuartmorgan@chromium.org024b5cd2011-05-27 03:29:381124 StopRecording();
initial.commit09911bf2008-07-26 23:55:291125 StoreUnsentLogs();
stuartmorgan@chromium.org7d41ae6d2012-06-26 08:53:031126
1127 // If there was a staged and/or current log, then there is now at least one
1128 // log waiting to be uploaded.
1129 if (log_manager_.has_unsent_logs())
1130 state_ = SENDING_OLD_LOGS;
initial.commit09911bf2008-07-26 23:55:291131}
1132
1133//------------------------------------------------------------------------------
1134// Transmission of logs methods
1135
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161136void MetricsService::StartSchedulerIfNecessary() {
1137 if (reporting_active() && recording_active())
1138 scheduler_->Start();
initial.commit09911bf2008-07-26 23:55:291139}
1140
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161141void MetricsService::StartScheduledUpload() {
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471142 // If we're getting no notifications, then the log won't have much in it, and
1143 // it's possible the computer is about to go to sleep, so don't upload and
1144 // stop the scheduler.
1145 // Similarly, if logs should no longer be uploaded, stop here.
1146 // TODO(stuartmorgan): Call Stop() on the schedule when reporting and/or
1147 // recording are turned off instead of letting it fire and then aborting.
1148 if (idle_since_last_transmission_ ||
1149 !recording_active() || !reporting_active()) {
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161150 scheduler_->Stop();
1151 scheduler_->UploadCancelled();
1152 return;
1153 }
1154
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471155 // If there are unsent logs, send the next one. If not, start the asynchronous
1156 // process of finalizing the current log for upload.
1157 if (state_ == SENDING_OLD_LOGS) {
1158 DCHECK(log_manager_.has_unsent_logs());
1159 log_manager_.StageNextLogForUpload();
1160 SendStagedLog();
1161 } else {
1162 StartFinalLogInfoCollection();
1163 }
stuartmorgan@chromium.org29948262012-03-01 12:15:081164}
1165
1166void MetricsService::StartFinalLogInfoCollection() {
1167 // Begin the multi-step process of collecting memory usage histograms:
1168 // First spawn a task to collect the memory details; when that task is
1169 // finished, it will call OnMemoryDetailCollectionDone. That will in turn
1170 // call HistogramSynchronization to collect histograms from all renderers and
1171 // then call OnHistogramSynchronizationDone to continue processing.
isherman@chromium.orgd119f222012-06-08 02:33:271172 DCHECK(!waiting_for_asynchronous_reporting_step_);
1173 waiting_for_asynchronous_reporting_step_ = true;
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161174
dcheng@chromium.org2226c222011-11-22 00:08:401175 base::Closure callback =
1176 base::Bind(&MetricsService::OnMemoryDetailCollectionDone,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:401177 self_ptr_factory_.GetWeakPtr());
sail@chromium.org84c988a2011-04-19 17:56:331178
dcheng@chromium.org2226c222011-11-22 00:08:401179 scoped_refptr<MetricsMemoryDetails> details(
1180 new MetricsMemoryDetails(callback));
jamescook@chromium.org4306df72012-04-20 18:58:571181 details->StartFetch(MemoryDetails::UPDATE_USER_METRICS);
sail@chromium.org84c988a2011-04-19 17:56:331182
1183 // Collect WebCore cache information to put into a histogram.
ananta@chromium.orgf3b1a082011-11-18 00:34:301184 for (content::RenderProcessHost::iterator i(
1185 content::RenderProcessHost::AllHostsIterator());
sail@chromium.org84c988a2011-04-19 17:56:331186 !i.IsAtEnd(); i.Advance())
ananta@chromium.org2ccf45c2011-08-19 23:35:501187 i.GetCurrentValue()->Send(new ChromeViewMsg_GetCacheResourceStats());
sail@chromium.org84c988a2011-04-19 17:56:331188}
1189
1190void MetricsService::OnMemoryDetailCollectionDone() {
jar@chromium.orgc9a3ef82009-05-28 22:02:461191 DCHECK(IsSingleThreaded());
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161192 // This function should only be called as the callback from an ansynchronous
1193 // step.
isherman@chromium.orgd119f222012-06-08 02:33:271194 DCHECK(waiting_for_asynchronous_reporting_step_);
jar@chromium.orgc9a3ef82009-05-28 22:02:461195
jar@chromium.orgc9a3ef82009-05-28 22:02:461196 // Create a callback_task for OnHistogramSynchronizationDone.
dcheng@chromium.org2226c222011-11-22 00:08:401197 base::Closure callback = base::Bind(
1198 &MetricsService::OnHistogramSynchronizationDone,
dpolukhin@chromium.orgc94d7382012-02-28 08:43:401199 self_ptr_factory_.GetWeakPtr());
jar@chromium.orgc9a3ef82009-05-28 22:02:461200
rtenneti@chromium.org908de522011-10-20 00:55:001201 base::StatisticsRecorder::CollectHistogramStats("Browser");
1202
jar@chromium.orgc9a3ef82009-05-28 22:02:461203 // Set up the callback to task to call after we receive histograms from all
1204 // renderer processes. Wait time specifies how long to wait before absolutely
1205 // calling us back on the task.
1206 HistogramSynchronizer::FetchRendererHistogramsAsynchronously(
dcheng@chromium.org2226c222011-11-22 00:08:401207 MessageLoop::current(), callback,
tedvessenes@gmail.com7e560102012-03-08 20:58:421208 base::TimeDelta::FromMilliseconds(kMaxHistogramGatheringWaitDuration));
jar@chromium.orgc9a3ef82009-05-28 22:02:461209}
1210
1211void MetricsService::OnHistogramSynchronizationDone() {
initial.commit09911bf2008-07-26 23:55:291212 DCHECK(IsSingleThreaded());
stuartmorgan@chromium.org29948262012-03-01 12:15:081213 // This function should only be called as the callback from an ansynchronous
1214 // step.
isherman@chromium.orgd119f222012-06-08 02:33:271215 DCHECK(waiting_for_asynchronous_reporting_step_);
initial.commit09911bf2008-07-26 23:55:291216
isherman@chromium.orgd119f222012-06-08 02:33:271217 waiting_for_asynchronous_reporting_step_ = false;
stuartmorgan@chromium.org29948262012-03-01 12:15:081218 OnFinalLogInfoCollectionDone();
1219}
1220
1221void MetricsService::OnFinalLogInfoCollectionDone() {
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161222 // If somehow there is a fetch in progress, we return and hope things work
1223 // out. The scheduler isn't informed since if this happens, the scheduler
1224 // will get a response from the upload.
isherman@chromium.orgfe58acc22012-02-29 01:29:581225 DCHECK(!current_fetch_xml_.get());
1226 DCHECK(!current_fetch_proto_.get());
1227 if (current_fetch_xml_.get() || current_fetch_proto_.get())
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161228 return;
1229
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471230 // Abort if metrics were turned off during the final info gathering.
1231 if (!recording_active() || !reporting_active()) {
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161232 scheduler_->Stop();
1233 scheduler_->UploadCancelled();
petersont@google.comd01b8732008-10-16 02:18:071234 return;
1235 }
1236
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471237 StageNewLog();
stuartmorgan@chromium.org29948262012-03-01 12:15:081238 SendStagedLog();
1239}
1240
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471241void MetricsService::StageNewLog() {
stuartmorgan@chromium.org29948262012-03-01 12:15:081242 if (log_manager_.has_staged_log())
1243 return;
1244
1245 switch (state_) {
1246 case INITIALIZED:
1247 case INIT_TASK_SCHEDULED: // We should be further along by now.
isherman@chromium.orgdc61fe92012-06-12 00:13:501248 NOTREACHED();
stuartmorgan@chromium.org29948262012-03-01 12:15:081249 return;
1250
1251 case INIT_TASK_DONE:
stuartmorgan@chromium.org29948262012-03-01 12:15:081252 PrepareInitialLog();
isherman@chromium.orged0fd002012-04-25 23:10:341253 DCHECK_EQ(INIT_TASK_DONE, state_);
stuartmorgan@chromium.org29948262012-03-01 12:15:081254 log_manager_.LoadPersistedUnsentLogs();
1255 state_ = INITIAL_LOG_READY;
1256 break;
1257
1258 case SENDING_OLD_LOGS:
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471259 NOTREACHED(); // Shouldn't be staging a new log during old log sending.
1260 return;
stuartmorgan@chromium.org29948262012-03-01 12:15:081261
1262 case SENDING_CURRENT_LOGS:
1263 StopRecording();
1264 StartRecording();
1265 log_manager_.StageNextLogForUpload();
1266 break;
1267
1268 default:
1269 NOTREACHED();
1270 return;
1271 }
1272
1273 DCHECK(log_manager_.has_staged_log());
1274}
1275
1276void MetricsService::PrepareInitialLog() {
isherman@chromium.orged0fd002012-04-25 23:10:341277 DCHECK_EQ(INIT_TASK_DONE, state_);
stuartmorgan@chromium.org29948262012-03-01 12:15:081278
isherman@chromium.orged0fd002012-04-25 23:10:341279 DCHECK(initial_log_.get());
1280 initial_log_->set_hardware_class(hardware_class_);
ryanmyers@chromium.org197c0772012-05-14 23:50:511281 initial_log_->RecordEnvironment(plugins_, google_update_metrics_,
1282 profile_dictionary_.get());
stuartmorgan@chromium.org29948262012-03-01 12:15:081283
1284 // Histograms only get written to the current log, so make the new log current
1285 // before writing them.
1286 log_manager_.PauseCurrentLog();
isherman@chromium.orged0fd002012-04-25 23:10:341287 log_manager_.BeginLoggingWithLog(initial_log_.release(),
1288 MetricsLogManager::INITIAL_LOG);
stuartmorgan@chromium.org29948262012-03-01 12:15:081289 RecordCurrentHistograms();
1290 log_manager_.FinishCurrentLog();
1291 log_manager_.ResumePausedLog();
1292
1293 DCHECK(!log_manager_.has_staged_log());
1294 log_manager_.StageNextLogForUpload();
1295}
1296
1297void MetricsService::StoreUnsentLogs() {
1298 if (state_ < INITIAL_LOG_READY)
1299 return; // We never Recalled the prior unsent logs.
1300
1301 log_manager_.PersistUnsentLogs();
1302}
1303
1304void MetricsService::SendStagedLog() {
1305 DCHECK(log_manager_.has_staged_log());
1306
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101307 PrepareFetchWithStagedLog();
petersont@google.comd01b8732008-10-16 02:18:071308
isherman@chromium.orgd6bebb92012-06-13 23:14:551309 bool upload_created = current_fetch_xml_.get() || current_fetch_proto_.get();
1310 UMA_HISTOGRAM_BOOLEAN("UMA.UploadCreation", upload_created);
1311 if (!upload_created) {
petersont@google.comd01b8732008-10-16 02:18:071312 // Compression failed, and log discarded :-/.
isherman@chromium.orgdc61fe92012-06-12 00:13:501313 // Skip this upload and hope things work out next time.
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101314 log_manager_.DiscardStagedLog();
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161315 scheduler_->UploadCancelled();
petersont@google.comd01b8732008-10-16 02:18:071316 return;
1317 }
1318
isherman@chromium.orgd119f222012-06-08 02:33:271319 DCHECK(!waiting_for_asynchronous_reporting_step_);
isherman@chromium.orgd119f222012-06-08 02:33:271320 waiting_for_asynchronous_reporting_step_ = true;
isherman@chromium.orgdc61fe92012-06-12 00:13:501321
1322 if (current_fetch_xml_.get())
1323 current_fetch_xml_->Start();
isherman@chromium.orgfe58acc22012-02-29 01:29:581324 if (current_fetch_proto_.get())
1325 current_fetch_proto_->Start();
petersont@google.comd01b8732008-10-16 02:18:071326
1327 HandleIdleSinceLastTransmission(true);
1328}
1329
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101330void MetricsService::PrepareFetchWithStagedLog() {
isherman@chromium.orgdc61fe92012-06-12 00:13:501331 DCHECK(log_manager_.has_staged_log());
pkasting@chromium.orgcac78842008-11-27 01:02:201332
isherman@chromium.orgfe58acc22012-02-29 01:29:581333 // Prepare the XML version.
1334 DCHECK(!current_fetch_xml_.get());
isherman@chromium.orgdc61fe92012-06-12 00:13:501335 if (log_manager_.has_staged_log_xml()) {
akalin@chromium.org3dc1bc42012-06-19 08:20:531336 current_fetch_xml_.reset(net::URLFetcher::Create(
isherman@chromium.orgdc61fe92012-06-12 00:13:501337 GURL(server_url_xml_), net::URLFetcher::POST, this));
1338 current_fetch_xml_->SetRequestContext(
1339 g_browser_process->system_request_context());
1340 current_fetch_xml_->SetUploadData(kMimeTypeXml,
1341 log_manager_.staged_log_text().xml);
1342 // We already drop cookies server-side, but we might as well strip them out
1343 // client-side as well.
1344 current_fetch_xml_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
1345 net::LOAD_DO_NOT_SEND_COOKIES);
1346 }
isherman@chromium.orgfe58acc22012-02-29 01:29:581347
1348 // Prepare the protobuf version.
1349 DCHECK(!current_fetch_proto_.get());
1350 if (log_manager_.has_staged_log_proto()) {
akalin@chromium.org3dc1bc42012-06-19 08:20:531351 current_fetch_proto_.reset(net::URLFetcher::Create(
akalin@chromium.orgd3ec669b2012-05-23 07:12:141352 GURL(server_url_proto_), net::URLFetcher::POST, this));
isherman@chromium.orgfe58acc22012-02-29 01:29:581353 current_fetch_proto_->SetRequestContext(
1354 g_browser_process->system_request_context());
isherman@chromium.org448ca4d2012-05-09 19:53:121355 current_fetch_proto_->SetUploadData(kMimeTypeProto,
isherman@chromium.orgfe58acc22012-02-29 01:29:581356 log_manager_.staged_log_text().proto);
1357 // We already drop cookies server-side, but we might as well strip them out
1358 // client-side as well.
1359 current_fetch_proto_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
1360 net::LOAD_DO_NOT_SEND_COOKIES);
isherman@chromium.orgfe58acc22012-02-29 01:29:581361 }
initial.commit09911bf2008-07-26 23:55:291362}
1363
isherman@chromium.orgfe58acc22012-02-29 01:29:581364// We need to wait for two responses: the response to the XML upload, and the
isherman@chromium.orgdc61fe92012-06-12 00:13:501365// response to the protobuf upload. In the case that exactly one of the uploads
1366// fails and needs to be retried, we "zap" the other pipeline's staged log to
1367// avoid incorrectly re-uploading it as well.
akalin@chromium.org10c2d692012-05-11 05:32:231368void MetricsService::OnURLFetchComplete(const net::URLFetcher* source) {
isherman@chromium.orgd119f222012-06-08 02:33:271369 DCHECK(waiting_for_asynchronous_reporting_step_);
isherman@chromium.orgfe58acc22012-02-29 01:29:581370
1371 // We're not allowed to re-use the existing |URLFetcher|s, so free them here.
isherman@chromium.org4266def22012-05-17 01:02:401372 // Note however that |source| is aliased to one of these, so we should be
1373 // careful not to delete it too early.
akalin@chromium.org15fb2aa2012-05-22 22:52:591374 scoped_ptr<net::URLFetcher> s;
isherman@chromium.orgdc61fe92012-06-12 00:13:501375 bool is_xml;
isherman@chromium.orgfe58acc22012-02-29 01:29:581376 if (source == current_fetch_xml_.get()) {
1377 s.reset(current_fetch_xml_.release());
isherman@chromium.orgdc61fe92012-06-12 00:13:501378 is_xml = true;
isherman@chromium.orgfe58acc22012-02-29 01:29:581379 } else if (source == current_fetch_proto_.get()) {
1380 s.reset(current_fetch_proto_.release());
isherman@chromium.orgdc61fe92012-06-12 00:13:501381 is_xml = false;
isherman@chromium.orgfe58acc22012-02-29 01:29:581382 } else {
1383 NOTREACHED();
1384 return;
1385 }
1386
isherman@chromium.orgdc61fe92012-06-12 00:13:501387 int response_code = source->GetResponseCode();
isherman@chromium.orgfe58acc22012-02-29 01:29:581388
isherman@chromium.orgdc61fe92012-06-12 00:13:501389 // Log a histogram to track response success vs. failure rates.
1390 if (is_xml) {
1391 UMA_HISTOGRAM_ENUMERATION("UMA.UploadResponseStatus.XML",
1392 ResponseCodeToStatus(response_code),
1393 NUM_RESPONSE_STATUSES);
1394 } else {
1395 UMA_HISTOGRAM_ENUMERATION("UMA.UploadResponseStatus.Protobuf",
1396 ResponseCodeToStatus(response_code),
1397 NUM_RESPONSE_STATUSES);
1398 }
isherman@chromium.orgfe58acc22012-02-29 01:29:581399
stuartmorgan@chromium.orge7508d82012-05-03 15:59:531400 // If the upload was provisionally stored, drop it now that the upload is
1401 // known to have gone through.
1402 log_manager_.DiscardLastProvisionalStore();
initial.commit09911bf2008-07-26 23:55:291403
isherman@chromium.orgdc61fe92012-06-12 00:13:501404 bool upload_succeeded = response_code == 200;
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161405
jar@chromium.org0eb34fee2009-01-21 08:04:381406 // Provide boolean for error recovery (allow us to ignore response_code).
paul@chromium.orgdc6f4962009-02-13 01:25:501407 bool discard_log = false;
isherman@chromium.orgdc61fe92012-06-12 00:13:501408 const size_t log_size = is_xml ?
1409 log_manager_.staged_log_text().xml.length() :
1410 log_manager_.staged_log_text().proto.length();
1411 if (!upload_succeeded && log_size > kUploadLogAvoidRetransmitSize) {
1412 UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded",
1413 static_cast<int>(log_size));
jar@chromium.org0eb34fee2009-01-21 08:04:381414 discard_log = true;
isherman@chromium.orgdc61fe92012-06-12 00:13:501415 } else if (response_code == 400) {
jar@chromium.org0eb34fee2009-01-21 08:04:381416 // Bad syntax. Retransmission won't work.
jar@chromium.org0eb34fee2009-01-21 08:04:381417 discard_log = true;
jar@google.com68475e602008-08-22 03:21:151418 }
1419
isherman@chromium.orgbf02f922012-06-13 23:25:311420 if (upload_succeeded || discard_log) {
isherman@chromium.orgdc61fe92012-06-12 00:13:501421 if (is_xml)
1422 log_manager_.DiscardStagedLogXml();
1423 else
1424 log_manager_.DiscardStagedLogProto();
1425 }
1426
1427 // If we're still waiting for one of the responses, keep waiting...
1428 if (current_fetch_xml_.get() || current_fetch_proto_.get())
1429 return;
1430
1431 waiting_for_asynchronous_reporting_step_ = false;
1432
1433 if (!log_manager_.has_staged_log()) {
initial.commit09911bf2008-07-26 23:55:291434 switch (state_) {
1435 case INITIAL_LOG_READY:
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471436 state_ = log_manager_.has_unsent_logs() ? SENDING_OLD_LOGS
1437 : SENDING_CURRENT_LOGS;
initial.commit09911bf2008-07-26 23:55:291438 break;
1439
initial.commit09911bf2008-07-26 23:55:291440 case SENDING_OLD_LOGS:
stuartmorgan@chromium.orgd53e2232011-06-30 15:54:571441 // Store the updated list to disk now that the removed log is uploaded.
initial.commit09911bf2008-07-26 23:55:291442 StoreUnsentLogs();
stuartmorgan@chromium.orgcd1ac712012-06-26 08:26:471443 if (!log_manager_.has_unsent_logs())
1444 state_ = SENDING_CURRENT_LOGS;
initial.commit09911bf2008-07-26 23:55:291445 break;
1446
1447 case SENDING_CURRENT_LOGS:
1448 break;
1449
1450 default:
jar@chromium.orga063c102010-07-22 22:20:191451 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:291452 break;
1453 }
petersont@google.comd01b8732008-10-16 02:18:071454
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101455 if (log_manager_.has_unsent_logs())
isherman@chromium.orged0fd002012-04-25 23:10:341456 DCHECK_LT(state_, SENDING_CURRENT_LOGS);
initial.commit09911bf2008-07-26 23:55:291457 }
petersont@google.com252873ef2008-08-04 21:59:451458
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161459 // Error 400 indicates a problem with the log, not with the server, so
1460 // don't consider that a sign that the server is in trouble.
isherman@chromium.orgdc61fe92012-06-12 00:13:501461 bool server_is_healthy = upload_succeeded || response_code == 400;
stuartmorgan@chromium.org7f7f1962011-04-20 15:58:161462
isherman@chromium.orgdc61fe92012-06-12 00:13:501463 // Note that we are essentially randomly choosing to report either the XML or
1464 // the protobuf server's health status, but this is ok: In the case that the
1465 // two statuses are not the same, one of the uploads succeeded but the other
1466 // did not. In this case, we'll re-try only the upload that failed. This first
1467 // re-try might ignore the server's unhealthiness; but the response to the
1468 // re-tried upload will correctly propagate the server's health status for any
1469 // subsequent re-tries. Hence, we'll at most delay slowing the upload rate by
1470 // one re-try, which is fine.
1471 scheduler_->UploadFinished(server_is_healthy, log_manager_.has_unsent_logs());
rtenneti@chromium.orgd67d1052011-06-09 05:11:411472
1473 // Collect network stats if UMA upload succeeded.
isherman@chromium.orgb8ddb052012-04-19 02:36:061474 IOThread* io_thread = g_browser_process->io_thread();
1475 if (server_is_healthy && io_thread) {
1476 chrome_browser_net::CollectNetworkStats(network_stats_server_, io_thread);
simonjam@chromium.orgadbb3762012-03-09 22:20:081477 chrome_browser_net::CollectPipeliningCapabilityStatsOnUIThread(
isherman@chromium.orgb8ddb052012-04-19 02:36:061478 http_pipelining_test_server_, io_thread);
simonjam@chromium.orgadbb3762012-03-09 22:20:081479 }
initial.commit09911bf2008-07-26 23:55:291480}
1481
jam@chromium.org6c2381d2011-10-19 02:52:531482void MetricsService::LogWindowChange(
1483 int type,
1484 const content::NotificationSource& source,
1485 const content::NotificationDetails& details) {
brettw@google.com534e54b2008-08-13 15:40:091486 int controller_id = -1;
1487 uintptr_t window_or_tab = source.map_key();
initial.commit09911bf2008-07-26 23:55:291488 MetricsLog::WindowEventType window_type;
1489
1490 // Note: since we stop all logging when a single OTR session is active, it is
1491 // possible that we start getting notifications about a window that we don't
1492 // know about.
brettw@google.com534e54b2008-08-13 15:40:091493 if (window_map_.find(window_or_tab) == window_map_.end()) {
1494 controller_id = next_window_id_++;
1495 window_map_[window_or_tab] = controller_id;
initial.commit09911bf2008-07-26 23:55:291496 } else {
brettw@google.com534e54b2008-08-13 15:40:091497 controller_id = window_map_[window_or_tab];
initial.commit09911bf2008-07-26 23:55:291498 }
jar@chromium.org92745242009-06-12 16:52:211499 DCHECK_NE(controller_id, -1);
initial.commit09911bf2008-07-26 23:55:291500
ananta@chromium.org432115822011-07-10 15:52:271501 switch (type) {
avi@chromium.org884033e2012-04-16 19:38:421502 case chrome::NOTIFICATION_TAB_PARENTED:
ananta@chromium.org432115822011-07-10 15:52:271503 case chrome::NOTIFICATION_BROWSER_OPENED:
initial.commit09911bf2008-07-26 23:55:291504 window_type = MetricsLog::WINDOW_CREATE;
1505 break;
1506
avi@chromium.org884033e2012-04-16 19:38:421507 case chrome::NOTIFICATION_TAB_CLOSING:
ananta@chromium.org432115822011-07-10 15:52:271508 case chrome::NOTIFICATION_BROWSER_CLOSED:
brettw@google.com534e54b2008-08-13 15:40:091509 window_map_.erase(window_map_.find(window_or_tab));
initial.commit09911bf2008-07-26 23:55:291510 window_type = MetricsLog::WINDOW_DESTROY;
1511 break;
1512
1513 default:
jar@chromium.orga063c102010-07-22 22:20:191514 NOTREACHED();
paul@chromium.org68d74f02009-02-13 01:36:501515 return;
initial.commit09911bf2008-07-26 23:55:291516 }
1517
brettw@google.com534e54b2008-08-13 15:40:091518 // TODO(brettw) we should have some kind of ID for the parent.
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101519 log_manager_.current_log()->RecordWindowEvent(window_type, controller_id, 0);
initial.commit09911bf2008-07-26 23:55:291520}
1521
jam@chromium.org6c2381d2011-10-19 02:52:531522void MetricsService::LogLoadComplete(
1523 int type,
1524 const content::NotificationSource& source,
1525 const content::NotificationDetails& details) {
jam@chromium.orgad50def52011-10-19 23:17:071526 if (details == content::NotificationService::NoDetails())
initial.commit09911bf2008-07-26 23:55:291527 return;
1528
jar@google.com68475e602008-08-22 03:21:151529 // TODO(jar): There is a bug causing this to be called too many times, and
1530 // the log overflows. For now, we won't record these events.
dsh@google.com553dba62009-02-24 19:08:231531 UMA_HISTOGRAM_COUNTS("UMA.LogLoadComplete called", 1);
jar@google.com68475e602008-08-22 03:21:151532 return;
1533
jam@chromium.org6c2381d2011-10-19 02:52:531534 const content::Details<LoadNotificationDetails> load_details(details);
brettw@google.com534e54b2008-08-13 15:40:091535 int controller_id = window_map_[details.map_key()];
stuartmorgan@chromium.orgcac267c2011-09-29 15:18:101536 log_manager_.current_log()->RecordLoadEvent(controller_id,
tfarina@chromium.org09d31d52012-03-11 22:30:271537 load_details->url,
1538 load_details->origin,
1539 load_details->session_index,
1540 load_details->load_time);
initial.commit09911bf2008-07-26 23:55:291541}
1542
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511543void MetricsService::IncrementPrefValue(const char* path) {
cpu@google.come73c01972008-08-13 00:18:241544 PrefService* pref = g_browser_process->local_state();
1545 DCHECK(pref);
1546 int value = pref->GetInteger(path);
1547 pref->SetInteger(path, value + 1);
1548}
1549
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511550void MetricsService::IncrementLongPrefsValue(const char* path) {
robertshield@google.com0bb1a622009-03-04 03:22:321551 PrefService* pref = g_browser_process->local_state();
1552 DCHECK(pref);
1553 int64 value = pref->GetInt64(path);
jar@chromium.orgb42c5e42010-06-03 20:43:251554 pref->SetInt64(path, value + 1);
robertshield@google.com0bb1a622009-03-04 03:22:321555}
1556
initial.commit09911bf2008-07-26 23:55:291557void MetricsService::LogLoadStarted() {
jar@chromium.orgdd8d12a2011-09-02 02:10:151558 HISTOGRAM_ENUMERATION("Chrome.UmaPageloadCounter", 1, 2);
cpu@google.come73c01972008-08-13 00:18:241559 IncrementPrefValue(prefs::kStabilityPageLoadCount);
robertshield@google.com0bb1a622009-03-04 03:22:321560 IncrementLongPrefsValue(prefs::kUninstallMetricsPageLoadCount);
jar@google.com0b33f80b2008-12-17 21:34:361561 // We need to save the prefs, as page load count is a critical stat, and it
1562 // might be lost due to a crash :-(.
initial.commit09911bf2008-07-26 23:55:291563}
1564
jar@chromium.orgc3721482012-03-23 16:21:481565void MetricsService::LogRendererCrash(content::RenderProcessHost* host,
1566 base::TerminationStatus status,
jam@chromium.orgf1675202012-07-09 15:18:001567 int exit_code) {
ananta@chromium.orgf3b1a082011-11-18 00:34:301568 Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext());
aa@chromium.org6f371442011-11-09 06:45:461569 ExtensionService* service = profile->GetExtensionService();
1570 bool was_extension_process =
ananta@chromium.orgf3b1a082011-11-18 00:34:301571 service && service->process_map()->Contains(host->GetID());
jar@chromium.orgc3721482012-03-23 16:21:481572 if (status == base::TERMINATION_STATUS_PROCESS_CRASHED ||
1573 status == base::TERMINATION_STATUS_ABNORMAL_TERMINATION) {
eroman@chromium.orgd7c1fa62012-06-15 23:35:301574 if (was_extension_process) {
jochen@chromium.org718eab62011-10-05 21:16:521575 IncrementPrefValue(prefs::kStabilityExtensionRendererCrashCount);
eroman@chromium.orgd7c1fa62012-06-15 23:35:301576
1577 UMA_HISTOGRAM_CUSTOM_ENUMERATION("CrashExitCodes.Extension",
1578 MapCrashExitCodeForHistogram(exit_code),
1579 GetAllCrashExitCodes());
1580 } else {
jochen@chromium.org718eab62011-10-05 21:16:521581 IncrementPrefValue(prefs::kStabilityRendererCrashCount);
initial.commit09911bf2008-07-26 23:55:291582
eroman@chromium.orgd7c1fa62012-06-15 23:35:301583 UMA_HISTOGRAM_CUSTOM_ENUMERATION("CrashExitCodes.Renderer",
1584 MapCrashExitCodeForHistogram(exit_code),
1585 GetAllCrashExitCodes());
1586 }
1587
jochen@chromium.org718eab62011-10-05 21:16:521588 UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.ChildCrashes",
1589 was_extension_process ? 2 : 1);
jar@chromium.orgc3721482012-03-23 16:21:481590 } else if (status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED) {
jochen@chromium.org718eab62011-10-05 21:16:521591 UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.ChildKills",
1592 was_extension_process ? 2 : 1);
jam@chromium.orgf1675202012-07-09 15:18:001593 } else if (status == base::TERMINATION_STATUS_STILL_RUNNING) {
1594 UMA_HISTOGRAM_PERCENTAGE("BrowserRenderProcessHost.DisconnectedAlive",
jochen@chromium.org718eab62011-10-05 21:16:521595 was_extension_process ? 2 : 1);
1596 }
1597 }
asargent@chromium.org1f085622009-12-04 05:33:451598
initial.commit09911bf2008-07-26 23:55:291599void MetricsService::LogRendererHang() {
cpu@google.come73c01972008-08-13 00:18:241600 IncrementPrefValue(prefs::kStabilityRendererHangCount);
initial.commit09911bf2008-07-26 23:55:291601}
1602
jar@chromium.orgc0c55e92011-09-10 18:47:301603void MetricsService::LogNeedForCleanShutdown() {
1604 PrefService* pref = g_browser_process->local_state();
1605 pref->SetBoolean(prefs::kStabilityExitedCleanly, false);
1606 // Redundant setting to be sure we call for a clean shutdown.
1607 clean_shutdown_status_ = NEED_TO_SHUTDOWN;
1608}
1609
1610bool MetricsService::UmaMetricsProperlyShutdown() {
1611 CHECK(clean_shutdown_status_ == CLEANLY_SHUTDOWN ||
1612 clean_shutdown_status_ == NEED_TO_SHUTDOWN);
1613 return clean_shutdown_status_ == CLEANLY_SHUTDOWN;
1614}
1615
jar@chromium.org67c4e952011-09-17 00:44:271616// For use in hack in LogCleanShutdown.
1617static void Signal(base::WaitableEvent* event) {
1618 event->Signal();
1619}
1620
stuartmorgan@chromium.org466f3c12011-03-23 21:20:381621void MetricsService::LogCleanShutdown() {
jar@chromium.orgacd55b32011-09-05 17:35:311622 // Redundant hack to write pref ASAP.
1623 PrefService* pref = g_browser_process->local_state();
1624 pref->SetBoolean(prefs::kStabilityExitedCleanly, true);
bauerb@chromium.orgfbe17c8a2011-12-27 16:41:481625 pref->CommitPendingWrite();
jar@chromium.org67c4e952011-09-17 00:44:271626 // Hack: TBD: Remove this wait.
1627 // We are so concerned that the pref gets written, we are now willing to stall
1628 // the UI thread until we get assurance that a pref-writing task has
1629 // completed.
1630 base::WaitableEvent done_writing(false, false);
1631 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
isherman@chromium.org8454aeb2011-11-19 23:38:201632 base::Bind(Signal, &done_writing));
jam@chromium.org3a7b66d2012-04-26 16:34:161633 // http://crbug.com/124954
1634 base::ThreadRestrictions::ScopedAllowWait allow_wait;
jar@chromium.org67c4e952011-09-17 00:44:271635 done_writing.TimedWait(base::TimeDelta::FromHours(1));
1636
jar@chromium.orgc0c55e92011-09-10 18:47:301637 // Redundant setting to assure that we always reset this value at shutdown
1638 // (and that we don't use some alternate path, and not call LogCleanShutdown).
1639 clean_shutdown_status_ = CLEANLY_SHUTDOWN;
jar@chromium.orgacd55b32011-09-05 17:35:311640
stuartmorgan@chromium.org466f3c12011-03-23 21:20:381641 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true);
1642}
1643
petkov@chromium.orgc1834a92011-01-21 18:21:031644#if defined(OS_CHROMEOS)
1645void MetricsService::LogChromeOSCrash(const std::string &crash_type) {
1646 if (crash_type == "user")
1647 IncrementPrefValue(prefs::kStabilityOtherUserCrashCount);
1648 else if (crash_type == "kernel")
1649 IncrementPrefValue(prefs::kStabilityKernelCrashCount);
1650 else if (crash_type == "uncleanshutdown")
1651 IncrementPrefValue(prefs::kStabilitySystemUncleanShutdownCount);
1652 else
1653 NOTREACHED() << "Unexpected Chrome OS crash type " << crash_type;
1654 // Wake up metrics logs sending if necessary now that new
1655 // log data is available.
1656 HandleIdleSinceLastTransmission(false);
1657}
1658#endif // OS_CHROMEOS
1659
bauerb@chromium.orgcd937072012-07-02 09:00:291660void MetricsService::LogPluginLoadingError(const FilePath& plugin_path) {
1661 webkit::WebPluginInfo plugin;
1662 bool success =
1663 content::PluginService::GetInstance()->GetPluginInfoByPath(plugin_path,
1664 &plugin);
1665 DCHECK(success);
1666 ChildProcessStats& stats = child_process_stats_buffer_[plugin.name];
1667 // Initialize the type if this entry is new.
1668 if (stats.process_type == content::PROCESS_TYPE_UNKNOWN) {
1669 // The plug-in process might not actually of type PLUGIN (which means
1670 // NPAPI), but we only care that it is *a* plug-in process.
1671 stats.process_type = content::PROCESS_TYPE_PLUGIN;
1672 } else {
1673 DCHECK(IsPluginProcess(stats.process_type));
1674 }
1675 stats.loading_errors++;
1676}
1677
jam@chromium.orga27a9382009-02-11 23:55:101678void MetricsService::LogChildProcessChange(
ananta@chromium.org432115822011-07-10 15:52:271679 int type,
jam@chromium.org6c2381d2011-10-19 02:52:531680 const content::NotificationSource& source,
1681 const content::NotificationDetails& details) {
jam@chromium.org4967f792012-01-20 22:14:401682 content::Details<ChildProcessData> child_details(details);
jam@chromium.org4306c3792011-12-02 01:57:531683 const string16& child_name = child_details->name;
gregoryd@google.com0d84c5d2009-10-09 01:10:421684
jam@chromium.orga27a9382009-02-11 23:55:101685 if (child_process_stats_buffer_.find(child_name) ==
1686 child_process_stats_buffer_.end()) {
gregoryd@google.com0d84c5d2009-10-09 01:10:421687 child_process_stats_buffer_[child_name] =
jam@chromium.org4306c3792011-12-02 01:57:531688 ChildProcessStats(child_details->type);
initial.commit09911bf2008-07-26 23:55:291689 }
1690
jam@chromium.orga27a9382009-02-11 23:55:101691 ChildProcessStats& stats = child_process_stats_buffer_[child_name];
ananta@chromium.org432115822011-07-10 15:52:271692 switch (type) {
1693 case content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED:
initial.commit09911bf2008-07-26 23:55:291694 stats.process_launches++;
1695 break;
1696
ananta@chromium.org432115822011-07-10 15:52:271697 case content::NOTIFICATION_CHILD_INSTANCE_CREATED:
initial.commit09911bf2008-07-26 23:55:291698 stats.instances++;
1699 break;
1700
ananta@chromium.org432115822011-07-10 15:52:271701 case content::NOTIFICATION_CHILD_PROCESS_CRASHED:
initial.commit09911bf2008-07-26 23:55:291702 stats.process_crashes++;
asargent@chromium.org1f085622009-12-04 05:33:451703 // Exclude plugin crashes from the count below because we report them via
1704 // a separate UMA metric.
jam@chromium.org4306c3792011-12-02 01:57:531705 if (!IsPluginProcess(child_details->type)) {
asargent@chromium.org1f085622009-12-04 05:33:451706 IncrementPrefValue(prefs::kStabilityChildProcessCrashCount);
1707 }
initial.commit09911bf2008-07-26 23:55:291708 break;
1709
1710 default:
ananta@chromium.org432115822011-07-10 15:52:271711 NOTREACHED() << "Unexpected notification type " << type;
initial.commit09911bf2008-07-26 23:55:291712 return;
1713 }
1714}
1715
1716// Recursively counts the number of bookmarks and folders in node.
munjal@chromium.orgb3c33d462009-06-26 22:29:201717static void CountBookmarks(const BookmarkNode* node,
1718 int* bookmarks,
1719 int* folders) {
tfarina@chromium.org0890e60e2011-06-27 14:55:211720 if (node->is_url())
initial.commit09911bf2008-07-26 23:55:291721 (*bookmarks)++;
1722 else
1723 (*folders)++;
tfarina@chromium.org9c1a75a2011-03-10 02:38:121724 for (int i = 0; i < node->child_count(); ++i)
initial.commit09911bf2008-07-26 23:55:291725 CountBookmarks(node->GetChild(i), bookmarks, folders);
1726}
1727
munjal@chromium.orgb3c33d462009-06-26 22:29:201728void MetricsService::LogBookmarks(const BookmarkNode* node,
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511729 const char* num_bookmarks_key,
1730 const char* num_folders_key) {
initial.commit09911bf2008-07-26 23:55:291731 DCHECK(node);
1732 int num_bookmarks = 0;
1733 int num_folders = 0;
1734 CountBookmarks(node, &num_bookmarks, &num_folders);
1735 num_folders--; // Don't include the root folder in the count.
1736
1737 PrefService* pref = g_browser_process->local_state();
1738 DCHECK(pref);
1739 pref->SetInteger(num_bookmarks_key, num_bookmarks);
1740 pref->SetInteger(num_folders_key, num_folders);
1741}
1742
sky@google.comd8e41ed2008-09-11 15:22:321743void MetricsService::LogBookmarks(BookmarkModel* model) {
initial.commit09911bf2008-07-26 23:55:291744 DCHECK(model);
tfarina@chromium.org72bdcfe2011-07-22 17:21:581745 LogBookmarks(model->bookmark_bar_node(),
initial.commit09911bf2008-07-26 23:55:291746 prefs::kNumBookmarksOnBookmarkBar,
1747 prefs::kNumFoldersOnBookmarkBar);
1748 LogBookmarks(model->other_node(),
1749 prefs::kNumBookmarksInOtherBookmarkFolder,
1750 prefs::kNumFoldersInOtherBookmarkFolder);
1751 ScheduleNextStateSave();
1752}
1753
pkasting@chromium.org604d8a062012-03-16 21:37:461754void MetricsService::LogKeywordCount(size_t keyword_count) {
initial.commit09911bf2008-07-26 23:55:291755 PrefService* pref = g_browser_process->local_state();
1756 DCHECK(pref);
pkasting@chromium.org604d8a062012-03-16 21:37:461757 pref->SetInteger(prefs::kNumKeywords, static_cast<int>(keyword_count));
initial.commit09911bf2008-07-26 23:55:291758 ScheduleNextStateSave();
1759}
1760
1761void MetricsService::RecordPluginChanges(PrefService* pref) {
battre@chromium.orgf8628c22011-04-05 12:10:181762 ListPrefUpdate update(pref, prefs::kStabilityPluginStats);
1763 ListValue* plugins = update.Get();
initial.commit09911bf2008-07-26 23:55:291764 DCHECK(plugins);
1765
1766 for (ListValue::iterator value_iter = plugins->begin();
1767 value_iter != plugins->end(); ++value_iter) {
1768 if (!(*value_iter)->IsType(Value::TYPE_DICTIONARY)) {
jar@chromium.orga063c102010-07-22 22:20:191769 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:291770 continue;
1771 }
1772
1773 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*value_iter);
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511774 std::string plugin_name;
nsylvain@chromium.org8e50b602009-03-03 22:59:431775 plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name);
nsylvain@chromium.org6470ee8f2009-03-03 20:46:401776 if (plugin_name.empty()) {
jar@chromium.orga063c102010-07-22 22:20:191777 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:291778 continue;
1779 }
1780
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511781 // TODO(viettrungluu): remove conversions
evan@chromium.org68b9e72b2011-08-05 23:08:221782 string16 name16 = UTF8ToUTF16(plugin_name);
1783 if (child_process_stats_buffer_.find(name16) ==
1784 child_process_stats_buffer_.end()) {
initial.commit09911bf2008-07-26 23:55:291785 continue;
evan@chromium.org68b9e72b2011-08-05 23:08:221786 }
initial.commit09911bf2008-07-26 23:55:291787
evan@chromium.org68b9e72b2011-08-05 23:08:221788 ChildProcessStats stats = child_process_stats_buffer_[name16];
initial.commit09911bf2008-07-26 23:55:291789 if (stats.process_launches) {
1790 int launches = 0;
nsylvain@chromium.org8e50b602009-03-03 22:59:431791 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
initial.commit09911bf2008-07-26 23:55:291792 launches += stats.process_launches;
nsylvain@chromium.org8e50b602009-03-03 22:59:431793 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, launches);
initial.commit09911bf2008-07-26 23:55:291794 }
1795 if (stats.process_crashes) {
1796 int crashes = 0;
nsylvain@chromium.org8e50b602009-03-03 22:59:431797 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
initial.commit09911bf2008-07-26 23:55:291798 crashes += stats.process_crashes;
nsylvain@chromium.org8e50b602009-03-03 22:59:431799 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, crashes);
initial.commit09911bf2008-07-26 23:55:291800 }
1801 if (stats.instances) {
1802 int instances = 0;
nsylvain@chromium.org8e50b602009-03-03 22:59:431803 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
initial.commit09911bf2008-07-26 23:55:291804 instances += stats.instances;
nsylvain@chromium.org8e50b602009-03-03 22:59:431805 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, instances);
initial.commit09911bf2008-07-26 23:55:291806 }
bauerb@chromium.orgcd937072012-07-02 09:00:291807 if (stats.loading_errors) {
1808 int loading_errors = 0;
1809 plugin_dict->GetInteger(prefs::kStabilityPluginLoadingErrors,
1810 &loading_errors);
1811 loading_errors += stats.loading_errors;
1812 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors,
1813 loading_errors);
1814 }
initial.commit09911bf2008-07-26 23:55:291815
evan@chromium.org68b9e72b2011-08-05 23:08:221816 child_process_stats_buffer_.erase(name16);
initial.commit09911bf2008-07-26 23:55:291817 }
1818
1819 // Now go through and add dictionaries for plugins that didn't already have
1820 // reports in Local State.
evan@chromium.org68b9e72b2011-08-05 23:08:221821 for (std::map<string16, ChildProcessStats>::iterator cache_iter =
jam@chromium.orga27a9382009-02-11 23:55:101822 child_process_stats_buffer_.begin();
1823 cache_iter != child_process_stats_buffer_.end(); ++cache_iter) {
jam@chromium.orga27a9382009-02-11 23:55:101824 ChildProcessStats stats = cache_iter->second;
gregoryd@google.com0d84c5d2009-10-09 01:10:421825
1826 // Insert only plugins information into the plugins list.
petkov@chromium.org8d5f1dae2011-11-11 14:30:411827 if (!IsPluginProcess(stats.process_type))
gregoryd@google.com0d84c5d2009-10-09 01:10:421828 continue;
1829
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511830 // TODO(viettrungluu): remove conversion
evan@chromium.org68b9e72b2011-08-05 23:08:221831 std::string plugin_name = UTF16ToUTF8(cache_iter->first);
gregoryd@google.com0d84c5d2009-10-09 01:10:421832
initial.commit09911bf2008-07-26 23:55:291833 DictionaryValue* plugin_dict = new DictionaryValue;
1834
nsylvain@chromium.org8e50b602009-03-03 22:59:431835 plugin_dict->SetString(prefs::kStabilityPluginName, plugin_name);
1836 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches,
initial.commit09911bf2008-07-26 23:55:291837 stats.process_launches);
nsylvain@chromium.org8e50b602009-03-03 22:59:431838 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes,
initial.commit09911bf2008-07-26 23:55:291839 stats.process_crashes);
nsylvain@chromium.org8e50b602009-03-03 22:59:431840 plugin_dict->SetInteger(prefs::kStabilityPluginInstances,
initial.commit09911bf2008-07-26 23:55:291841 stats.instances);
bauerb@chromium.orgcd937072012-07-02 09:00:291842 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors,
1843 stats.loading_errors);
initial.commit09911bf2008-07-26 23:55:291844 plugins->Append(plugin_dict);
1845 }
jam@chromium.orga27a9382009-02-11 23:55:101846 child_process_stats_buffer_.clear();
initial.commit09911bf2008-07-26 23:55:291847}
1848
jam@chromium.org6c2381d2011-10-19 02:52:531849bool MetricsService::CanLogNotification(
1850 int type,
1851 const content::NotificationSource& source,
1852 const content::NotificationDetails& details) {
akalin@chromium.org2c910b72011-03-08 21:16:321853 // We simply don't log anything to UMA if there is a single incognito
initial.commit09911bf2008-07-26 23:55:291854 // session visible. The problem is that we always notify using the orginal
1855 // profile in order to simplify notification processing.
1856 return !BrowserList::IsOffTheRecordSessionActive();
1857}
1858
viettrungluu@chromium.org57ecc4b2010-08-11 03:02:511859void MetricsService::RecordBooleanPrefValue(const char* path, bool value) {
initial.commit09911bf2008-07-26 23:55:291860 DCHECK(IsSingleThreaded());
1861
1862 PrefService* pref = g_browser_process->local_state();
1863 DCHECK(pref);
1864
1865 pref->SetBoolean(path, value);
1866 RecordCurrentState(pref);
1867}
1868
1869void MetricsService::RecordCurrentState(PrefService* pref) {
robertshield@google.com0bb1a622009-03-04 03:22:321870 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT());
initial.commit09911bf2008-07-26 23:55:291871
1872 RecordPluginChanges(pref);
1873}
1874
petkov@chromium.org8d5f1dae2011-11-11 14:30:411875// static
jam@chromium.orgbd5d6cf2011-12-01 00:39:121876bool MetricsService::IsPluginProcess(content::ProcessType type) {
bauerb@chromium.orgcd937072012-07-02 09:00:291877 return (type == content::PROCESS_TYPE_PLUGIN ||
1878 type == content::PROCESS_TYPE_PPAPI_PLUGIN ||
1879 type == content::PROCESS_TYPE_PPAPI_BROKER);
petkov@chromium.org8d5f1dae2011-11-11 14:30:411880}
1881
rvargas@google.com5ccaa412009-11-13 22:00:161882#if defined(OS_CHROMEOS)
sky@chromium.org29cf16772010-04-21 15:13:471883void MetricsService::StartExternalMetrics() {
rvargas@google.com5ccaa412009-11-13 22:00:161884 external_metrics_ = new chromeos::ExternalMetrics;
sky@chromium.org29cf16772010-04-21 15:13:471885 external_metrics_->Start();
rvargas@google.com5ccaa412009-11-13 22:00:161886}
1887#endif
sreeram@chromium.org3819f2ee2011-08-21 09:44:381888
1889// static
1890bool MetricsServiceHelper::IsMetricsReportingEnabled() {
1891 bool result = false;
1892 const PrefService* local_state = g_browser_process->local_state();
1893 if (local_state) {
1894 const PrefService::Preference* uma_pref =
1895 local_state->FindPreference(prefs::kMetricsReportingEnabled);
1896 if (uma_pref) {
1897 bool success = uma_pref->GetValue()->GetAsBoolean(&result);
1898 DCHECK(success);
1899 }
1900 }
1901 return result;
1902}