1f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Description of the life cycle of a instance of MetricsService.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  OVERVIEW
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// A MetricsService instance is typically created at application startup.  It is
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// the central controller for the acquisition of log data, and the automatic
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// transmission of that log data to an external server.  Its major job is to
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// manage logs, grouping them for transmission, and transmitting them.  As part
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of its grouping, MS finalizes logs by including some just-in-time gathered
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// memory statistics, snapshotting the current stats of numerous histograms,
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// closing the logs, translating to protocol buffer format, and compressing the
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// results for transmission.  Transmission includes submitting a compressed log
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// as data in a URL-post, and retransmitting (or retaining at process
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// termination) if the attempted transmission failed.  Retention across process
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// terminations is done using the the PrefServices facilities. The retained logs
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// (the ones that never got transmitted) are compressed and base64-encoded
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// before being persisted.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Logs fall into one of two categories: "initial logs," and "ongoing logs."
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// There is at most one initial log sent for each complete run of Chrome (from
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// startup, to browser shutdown).  An initial log is generally transmitted some
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// short time (1 minute?) after startup, and includes stats such as recent crash
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// info, the number and types of plugins, etc.  The external server's response
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// to the initial log conceptually tells this MS if it should continue
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// transmitting logs (during this session). The server response can actually be
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// much more detailed, and always includes (at a minimum) how often additional
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// ongoing logs should be sent.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// After the above initial log, a series of ongoing logs will be transmitted.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The first ongoing log actually begins to accumulate information stating when
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the MS was first constructed.  Note that even though the initial log is
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// commonly sent a full minute after startup, the initial log does not include
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// much in the way of user stats.   The most common interlog period (delay)
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// is 30 minutes. That time period starts when the first user action causes a
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// logging event.  This means that if there is no user action, there may be long
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// periods without any (ongoing) log transmissions.  Ongoing logs typically
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contain very detailed records of user activities (ex: opened tab, closed
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// tab, fetched URL, maximized window, etc.)  In addition, just before an
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ongoing log is closed out, a call is made to gather memory statistics.  Those
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// memory statistics are deposited into a histogram, and the log finalization
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// code is then called.  In the finalization, a call to a Histogram server
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// acquires a list of all local histograms that have been flagged for upload
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// to the UMA server.  The finalization also acquires the most recent number
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of page loads, along with any counts of renderer or plugin crashes.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When the browser shuts down, there will typically be a fragment of an ongoing
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// log that has not yet been transmitted.  At shutdown time, that fragment is
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// closed (including snapshotting histograms), and persisted, for potential
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// transmission during a future run of the product.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There are two slightly abnormal shutdown conditions.  There is a
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "disconnected scenario," and a "really fast startup and shutdown" scenario.
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In the "never connected" situation, the user has (during the running of the
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// process) never established an internet connection.  As a result, attempts to
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// transmit the initial log have failed, and a lot(?) of data has accumulated in
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the ongoing log (which didn't yet get closed, because there was never even a
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contemplation of sending it).  There is also a kindred "lost connection"
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// situation, where a loss of connection prevented an ongoing log from being
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// transmitted, and a (still open) log was stuck accumulating a lot(?) of data,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// while the earlier log retried its transmission.  In both of these
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// disconnected situations, two logs need to be, and are, persistently stored
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for future transmission.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The other unusual shutdown condition, termed "really fast startup and
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// shutdown," involves the deliberate user termination of the process before
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the initial log is even formed or transmitted. In that situation, no logging
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is done, but the historical crash statistics remain (unlogged) for inclusion
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in a future run's initial log.  (i.e., we don't lose crash stats).
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// With the above overview, we can now describe the state machine's various
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// states, based on the State enum specified in the state_ member.  Those states
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are:
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//  INITIALIZED,                   // Constructor was called.
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//  INIT_TASK_SCHEDULED,           // Waiting for deferred init tasks to finish.
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//  INIT_TASK_DONE,                // Waiting for timer to send initial log.
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//  SENDING_INITIAL_STABILITY_LOG, // Initial stability log being sent.
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//  SENDING_INITIAL_METRICS_LOG,   // Initial metrics log being sent.
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//  SENDING_OLD_LOGS,              // Sending unsent logs from previous session.
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//  SENDING_CURRENT_LOGS,          // Sending ongoing logs as they acrue.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In more detail, we have:
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    INITIALIZED,            // Constructor was called.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The MS has been constructed, but has taken no actions to compose the
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// initial log.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//    INIT_TASK_SCHEDULED,    // Waiting for deferred init tasks to finish.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Typically about 30 seconds after startup, a task is sent to a second thread
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (the file thread) to perform deferred (lower priority and slower)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// initialization steps such as getting the list of plugins.  That task will
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (when complete) make an async callback (via a Task) to indicate the
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// completion.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    INIT_TASK_DONE,         // Waiting for timer to send initial log.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The callback has arrived, and it is now possible for an initial log to be
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// created.  This callback typically arrives back less than one second after
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the deferred init task is dispatched.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//    SENDING_INITIAL_STABILITY_LOG,  // Initial stability log being sent.
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// During initialization, if a crash occurred during the previous session, an
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// initial stability log will be generated and registered with the log manager.
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This state will be entered if a stability log was prepared during metrics
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// service initialization (in InitializeMetricsRecordingState()) and is waiting
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// to be transmitted when it's time to send up the first log (per the reporting
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// scheduler).  If there is no initial stability log (e.g. there was no previous
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// crash), then this state will be skipped and the state will advance to
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// SENDING_INITIAL_METRICS_LOG.
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//    SENDING_INITIAL_METRICS_LOG,  // Initial metrics log being sent.
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This state is entered after the initial metrics log has been composed, and
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// prepared for transmission.  This happens after SENDING_INITIAL_STABILITY_LOG
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// if there was an initial stability log (see above).  It is also the case that
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// any previously unsent logs have been loaded into instance variables for
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// possible transmission.
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    SENDING_OLD_LOGS,       // Sending unsent logs from previous session.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This state indicates that the initial log for this session has been
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// successfully sent and it is now time to send any logs that were
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// saved from previous sessions.  All such logs will be transmitted before
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// exiting this state, and proceeding with ongoing logs from the current session
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (see next state).
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    SENDING_CURRENT_LOGS,   // Sending standard current logs as they accrue.
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Current logs are being accumulated.  Typically every 20 minutes a log is
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// closed and finalized for transmission, at the same time as a new log is
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// started.
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The progression through the above states is simple, and sequential, in the
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// most common use cases.  States proceed from INITIAL to SENDING_CURRENT_LOGS,
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and remain in the latter until shutdown.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The one unusual case is when the user asks that we stop logging.  When that
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// happens, any staged (transmission in progress) log is persisted, and any log
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that is currently accumulating is also finalized and persisted.  We then
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// regress back to the SEND_OLD_LOGS state in case the user enables log
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// recording again during this session.  This way anything we have persisted
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// will be sent automatically if/when we progress back to SENDING_CURRENT_LOG
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// state.
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Another similar case is on mobile, when the application is backgrounded and
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// then foregrounded again. Backgrounding created new "old" stored logs, so the
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// state drops back from SENDING_CURRENT_LOGS to SENDING_OLD_LOGS so those logs
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// will be sent.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Also note that whenever we successfully send an old log, we mirror the list
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of logs into the PrefService. This ensures that IF we crash, we won't start
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// up and retransmit our old logs again.
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Due to race conditions, it is always possible that a log file could be sent
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// twice.  For example, if a log file is sent, but not yet acknowledged by
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the external server, and the user shuts down, then a copy of the log may be
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// saved for re-transmission.  These duplicates could be filtered out server
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// side, but are not expected to be a significant problem.
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
164f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/metrics/metrics_service.h"
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h"
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
171cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/metrics/histogram_base.h"
172cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/metrics/histogram_samples.h"
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/metrics/sparse_histogram.h"
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/statistics_recorder.h"
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_registry_simple.h"
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/platform_thread.h"
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread.h"
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread_restrictions.h"
182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/time/time.h"
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/tracked_objects.h"
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
18546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/metrics/metrics_log.h"
186010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "components/metrics/metrics_log_manager.h"
18746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/metrics/metrics_log_uploader.h"
188cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/metrics/metrics_pref_names.h"
189cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/metrics/metrics_reporting_scheduler.h"
190cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/metrics/metrics_service_client.h"
19146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/metrics/metrics_state_manager.h"
192424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "components/variations/entropy_provider.h"
1931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace metrics {
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Check to see that we're being called on only one thread.
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsSingleThreaded() {
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static base::PlatformThreadId thread_id = 0;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!thread_id)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    thread_id = base::PlatformThread::CurrentId();
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base::PlatformThread::CurrentId() == thread_id;
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The delay, in seconds, after starting recording before doing expensive
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// initialization work.
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_ANDROID) || defined(OS_IOS)
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// On mobile devices, a significant portion of sessions last less than a minute.
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use a shorter timer on these platforms to avoid losing data.
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(dfalcantara): To avoid delaying startup, tighten up initialization so
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                    that it occurs after the user gets their initial page.
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kInitializationDelaySeconds = 5;
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kInitializationDelaySeconds = 30;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The maximum number of events in a log uploaded to the UMA server.
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kEventLimit = 2400;
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If an upload fails, and the transmission was over this byte count, then we
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// will discard the log, and not try to retransmit it.  We also don't persist
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the log to the prefs for transmission during the next chrome session if this
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// limit is exceeded.
2256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)const size_t kUploadLogAvoidRetransmitSize = 100 * 1024;
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Interval, in minutes, between state saves.
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kSaveStateIntervalMinutes = 5;
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
230cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// The metrics server's URL.
231cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)const char kServerUrl[] = "https://clients4.google.com/uma/v2";
232cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
233cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// The MIME type for the uploaded metrics data.
234cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)const char kMimeType[] = "application/vnd.chrome.uma";
235cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum ResponseStatus {
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UNKNOWN_FAILURE,
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SUCCESS,
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BAD_REQUEST,  // Invalid syntax or log too large.
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NO_RESPONSE,
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NUM_RESPONSE_STATUSES
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ResponseStatus ResponseCodeToStatus(int response_code) {
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (response_code) {
24646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    case -1:
24746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      return NO_RESPONSE;
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case 200:
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return SUCCESS;
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case 400:
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return BAD_REQUEST;
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return UNKNOWN_FAILURE;
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid MarkAppCleanShutdownAndCommit(CleanExitBeacon* clean_exit_beacon,
2581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                   PrefService* local_state) {
2591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  clean_exit_beacon->WriteBeaconValue(true);
260f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  local_state->SetInteger(metrics::prefs::kStabilityExecutionPhase,
261cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                          MetricsService::SHUTDOWN_COMPLETE);
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Start writing right away (write happens on a different thread).
263cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  local_state->CommitPendingWrite();
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
268f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
269a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)SyntheticTrialGroup::SyntheticTrialGroup(uint32 trial, uint32 group) {
270f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  id.name = trial;
271f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  id.group = group;
272f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
273f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
274f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)SyntheticTrialGroup::~SyntheticTrialGroup() {
275f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
276f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ =
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MetricsService::CLEANLY_SHUTDOWN;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)MetricsService::ExecutionPhase MetricsService::execution_phase_ =
282f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    MetricsService::UNINITIALIZED_PHASE;
2831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MetricsService::RegisterPrefs(PrefRegistrySimple* registry) {
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsSingleThreaded());
287010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  metrics::MetricsStateManager::RegisterPrefs(registry);
28846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MetricsLog::RegisterPrefs(registry);
289010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
290116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  registry->RegisterInt64Pref(metrics::prefs::kInstallDate, 0);
291116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
292f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  registry->RegisterInt64Pref(metrics::prefs::kStabilityLaunchTimeSec, 0);
293f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  registry->RegisterInt64Pref(metrics::prefs::kStabilityLastTimestampSec, 0);
294f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  registry->RegisterStringPref(metrics::prefs::kStabilityStatsVersion,
295f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                               std::string());
296f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  registry->RegisterInt64Pref(metrics::prefs::kStabilityStatsBuildTime, 0);
297f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  registry->RegisterBooleanPref(metrics::prefs::kStabilityExitedCleanly, true);
298f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  registry->RegisterIntegerPref(metrics::prefs::kStabilityExecutionPhase,
299f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                                UNINITIALIZED_PHASE);
300f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  registry->RegisterBooleanPref(metrics::prefs::kStabilitySessionEndCompleted,
301f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                true);
30246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  registry->RegisterIntegerPref(metrics::prefs::kMetricsSessionID, -1);
303a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
304cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs);
305cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs);
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
307f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  registry->RegisterInt64Pref(metrics::prefs::kUninstallLaunchCount, 0);
308f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  registry->RegisterInt64Pref(metrics::prefs::kUninstallMetricsUptimeSec, 0);
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
311cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)MetricsService::MetricsService(metrics::MetricsStateManager* state_manager,
312cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                               metrics::MetricsServiceClient* client,
313cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                               PrefService* local_state)
314cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : log_manager_(local_state, kUploadLogAvoidRetransmitSize),
315cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      histogram_snapshot_manager_(this),
316cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      state_manager_(state_manager),
317cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      client_(client),
318cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      local_state_(local_state),
3191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      clean_exit_beacon_(client->GetRegistryBackupKey(), local_state),
320effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      recording_active_(false),
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      reporting_active_(false),
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      test_mode_active_(false),
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      state_(INITIALIZED),
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      has_initial_stability_log_(false),
32546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      log_upload_in_progress_(false),
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      idle_since_last_transmission_(false),
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      session_id_(-1),
328c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      self_ptr_factory_(this),
32946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      state_saver_factory_(this) {
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsSingleThreaded());
331010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(state_manager_);
332cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(client_);
333cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(local_state_);
334116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
335116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Set the install date if this is our first run.
336116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int64 install_date = local_state_->GetInt64(metrics::prefs::kInstallDate);
337116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (install_date == 0) {
338116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    local_state_->SetInt64(metrics::prefs::kInstallDate,
339116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                           base::Time::Now().ToTimeT());
340116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MetricsService::~MetricsService() {
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DisableRecording();
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
347010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void MetricsService::InitializeMetricsRecordingState() {
348010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  InitializeMetricsState();
3495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload,
3515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      self_ptr_factory_.GetWeakPtr());
3525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scheduler_.reset(new MetricsReportingScheduler(callback));
3535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::Start() {
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HandleIdleSinceLastTransmission(false);
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EnableRecording();
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EnableReporting();
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
361010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool MetricsService::StartIfMetricsReportingEnabled() {
362010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  const bool enabled = state_manager_->IsMetricsReportingEnabled();
363010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (enabled)
364010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Start();
365010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return enabled;
366010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
367010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::StartRecordingForTests() {
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  test_mode_active_ = true;
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EnableRecording();
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DisableReporting();
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::Stop() {
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HandleIdleSinceLastTransmission(false);
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DisableReporting();
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DisableRecording();
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::EnableReporting() {
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (reporting_active_)
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  reporting_active_ = true;
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartSchedulerIfNecessary();
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::DisableReporting() {
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  reporting_active_ = false;
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string MetricsService::GetClientId() {
392010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return state_manager_->client_id();
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint64 MetricsService::GetInstallDate() {
396116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return local_state_->GetInt64(metrics::prefs::kInstallDate);
397116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
398116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_ptr<const base::FieldTrial::EntropyProvider>
400010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)MetricsService::CreateEntropyProvider() {
401010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // TODO(asvitkine): Refactor the code so that MetricsService does not expose
402010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // this method.
403010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return state_manager_->CreateEntropyProvider();
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::EnableRecording() {
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsSingleThreaded());
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (recording_active_)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  recording_active_ = true;
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
413010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  state_manager_->ForceClientIdCreation();
414116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  client_->SetMetricsClientId(state_manager_->client_id());
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!log_manager_.current_log())
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OpenNewLog();
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
418cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for (size_t i = 0; i < metrics_providers_.size(); ++i)
419cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    metrics_providers_[i]->OnRecordingEnabled();
420cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
4215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RemoveActionCallback(action_callback_);
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  action_callback_ = base::Bind(&MetricsService::OnUserAction,
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                base::Unretained(this));
4245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::AddActionCallback(action_callback_);
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::DisableRecording() {
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsSingleThreaded());
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!recording_active_)
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  recording_active_ = false;
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RemoveActionCallback(action_callback_);
435cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
436cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for (size_t i = 0; i < metrics_providers_.size(); ++i)
437cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    metrics_providers_[i]->OnRecordingDisabled();
438cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PushPendingLogsToPersistentStorage();
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MetricsService::recording_active() const {
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsSingleThreaded());
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return recording_active_;
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MetricsService::reporting_active() const {
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsSingleThreaded());
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return reporting_active_;
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
452cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::RecordDelta(const base::HistogramBase& histogram,
453cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                 const base::HistogramSamples& snapshot) {
454cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  log_manager_.current_log()->RecordHistogramDelta(histogram.histogram_name(),
455cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                   snapshot);
456cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
458cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::InconsistencyDetected(
459cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::HistogramBase::Inconsistency problem) {
460cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  UMA_HISTOGRAM_ENUMERATION("Histogram.InconsistenciesBrowser",
461cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            problem, base::HistogramBase::NEVER_EXCEEDED_VALUE);
462cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
463a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
464cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::UniqueInconsistencyDetected(
465cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::HistogramBase::Inconsistency problem) {
466cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  UMA_HISTOGRAM_ENUMERATION("Histogram.InconsistenciesBrowserUnique",
467cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            problem, base::HistogramBase::NEVER_EXCEEDED_VALUE);
468cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
469a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
470cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::InconsistencyDetectedInLoggedCount(int amount) {
471cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  UMA_HISTOGRAM_COUNTS("Histogram.InconsistentSnapshotBrowser",
472cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       std::abs(amount));
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) {
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If there wasn't a lot of action, maybe the computer was asleep, in which
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // case, the log transmissions should have stopped.  Here we start them up
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // again.
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!in_idle && idle_since_last_transmission_)
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StartSchedulerIfNecessary();
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  idle_since_last_transmission_ = in_idle;
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
484cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::OnApplicationNotIdle() {
485cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (recording_active_)
486cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    HandleIdleSinceLastTransmission(false);
487cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
488cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::RecordStartOfSessionEnd() {
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogCleanShutdown();
491f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  RecordBooleanPrefValue(metrics::prefs::kStabilitySessionEndCompleted, false);
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::RecordCompletedSessionEnd() {
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogCleanShutdown();
496f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  RecordBooleanPrefValue(metrics::prefs::kStabilitySessionEndCompleted, true);
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_ANDROID) || defined(OS_IOS)
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::OnAppEnterBackground() {
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scheduler_->Stop();
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  MarkAppCleanShutdownAndCommit(&clean_exit_beacon_, local_state_);
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // At this point, there's no way of knowing when the process will be
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // killed, so this has to be treated similar to a shutdown, closing and
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // persisting all logs. Unlinke a shutdown, the state is primed to be ready
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to continue logging and uploading if the process does return.
5095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (recording_active() && state_ >= SENDING_INITIAL_STABILITY_LOG) {
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PushPendingLogsToPersistentStorage();
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Persisting logs closes the current log, so start recording a new log
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // immediately to capture any background work that might be done before the
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // process is killed.
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OpenNewLog();
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::OnAppEnterForeground() {
5191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  clean_exit_beacon_.WriteBeaconValue(false);
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartSchedulerIfNecessary();
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
5231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid MetricsService::LogNeedForCleanShutdown() {
5241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  clean_exit_beacon_.WriteBeaconValue(false);
5252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Redundant setting to be sure we call for a clean shutdown.
5262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  clean_shutdown_status_ = NEED_TO_SHUTDOWN;
5272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // defined(OS_ANDROID) || defined(OS_IOS)
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
530f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// static
531cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::SetExecutionPhase(ExecutionPhase execution_phase,
532cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                       PrefService* local_state) {
533f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  execution_phase_ = execution_phase;
534f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  local_state->SetInteger(metrics::prefs::kStabilityExecutionPhase,
535f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                          execution_phase_);
536f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
537f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::RecordBreakpadRegistration(bool success) {
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!success)
54046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    IncrementPrefValue(metrics::prefs::kStabilityBreakpadRegistrationFail);
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else
54246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    IncrementPrefValue(metrics::prefs::kStabilityBreakpadRegistrationSuccess);
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) {
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!has_debugger)
54746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    IncrementPrefValue(metrics::prefs::kStabilityDebuggerNotPresent);
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else
54946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    IncrementPrefValue(metrics::prefs::kStabilityDebuggerPresent);
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// private methods
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Initialization methods
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
560010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void MetricsService::InitializeMetricsState() {
5611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  const int64 buildtime = MetricsLog::GetBuildTime();
5621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  const std::string version = client_->GetVersionString();
5631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool version_changed = false;
5641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (local_state_->GetInt64(prefs::kStabilityStatsBuildTime) != buildtime ||
5651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      local_state_->GetString(prefs::kStabilityStatsVersion) != version) {
5661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    local_state_->SetString(metrics::prefs::kStabilityStatsVersion, version);
5671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    local_state_->SetInt64(metrics::prefs::kStabilityStatsBuildTime, buildtime);
5681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    version_changed = true;
5691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
571116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  log_manager_.LoadPersistedUnsentLogs();
572116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
57346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  session_id_ = local_state_->GetInteger(metrics::prefs::kMetricsSessionID);
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!clean_exit_beacon_.exited_cleanly()) {
57646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    IncrementPrefValue(metrics::prefs::kStabilityCrashCount);
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Reset flag, and wait until we call LogNeedForCleanShutdown() before
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // monitoring.
5791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    clean_exit_beacon_.WriteBeaconValue(true);
5801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
5811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!clean_exit_beacon_.exited_cleanly() || ProvidersHaveStabilityMetrics()) {
5831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // TODO(rtenneti): On windows, consider saving/getting execution_phase from
5841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // the registry.
585cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    int execution_phase =
586f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        local_state_->GetInteger(metrics::prefs::kStabilityExecutionPhase);
587f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    UMA_HISTOGRAM_SPARSE_SLOWLY("Chrome.Browser.CrashedExecutionPhase",
5881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                execution_phase);
5895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // If the previous session didn't exit cleanly, or if any provider
5911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // explicitly requests it, prepare an initial stability log -
5921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // provided UMA is enabled.
593010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (state_manager_->IsMetricsReportingEnabled())
5945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PrepareInitialStabilityLog();
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // If no initial stability log was generated and there was a version upgrade,
5981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // clear the stability stats from the previous version (so that they don't get
5991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // attributed to the current version). This could otherwise happen due to a
6001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // number of different edge cases, such as if the last version crashed before
6011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // it could save off a system profile or if UMA reporting is disabled (which
6021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // normally results in stats being accumulated).
6031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!has_initial_stability_log_ && version_changed) {
6041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    for (size_t i = 0; i < metrics_providers_.size(); ++i)
6051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      metrics_providers_[i]->ClearSavedStabilityMetrics();
6061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // Reset the prefs that are managed by MetricsService/MetricsLog directly.
6081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    local_state_->SetInteger(prefs::kStabilityCrashCount, 0);
6091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    local_state_->SetInteger(prefs::kStabilityExecutionPhase,
6101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             UNINITIALIZED_PHASE);
6111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    local_state_->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
6121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    local_state_->SetInteger(prefs::kStabilityLaunchCount, 0);
6131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
6141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
6151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Update session ID.
6175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ++session_id_;
61846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  local_state_->SetInteger(metrics::prefs::kMetricsSessionID, session_id_);
6195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
6205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Stability bookkeeping
62146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  IncrementPrefValue(metrics::prefs::kStabilityLaunchCount);
6225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
623f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK_EQ(UNINITIALIZED_PHASE, execution_phase_);
624cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetExecutionPhase(START_METRICS_RECORDING, local_state_);
6253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
626f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!local_state_->GetBoolean(
627f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          metrics::prefs::kStabilitySessionEndCompleted)) {
62846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    IncrementPrefValue(metrics::prefs::kStabilityIncompleteSessionEndCount);
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This is marked false when we get a WM_ENDSESSION.
630f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    local_state_->SetBoolean(metrics::prefs::kStabilitySessionEndCompleted,
631f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                             true);
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
634a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Call GetUptimes() for the first time, thus allowing all later calls
635a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // to record incremental uptimes accurately.
636a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::TimeDelta ignored_uptime_parameter;
637a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::TimeDelta startup_uptime;
638cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GetUptimes(local_state_, &startup_uptime, &ignored_uptime_parameter);
6394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  DCHECK_EQ(0, startup_uptime.InMicroseconds());
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For backwards compatibility, leave this intact in case Omaha is checking
641f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // them.  metrics::prefs::kStabilityLastTimestampSec may also be useless now.
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(jar): Delete these if they have no uses.
643f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  local_state_->SetInt64(metrics::prefs::kStabilityLaunchTimeSec,
6441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                         base::Time::Now().ToTimeT());
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Bookkeeping for the uninstall metrics.
647f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  IncrementLongPrefsValue(metrics::prefs::kUninstallLaunchCount);
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Kick off the process of saving the state (so the uptime numbers keep
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // getting updated) every n minutes.
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ScheduleNextStateSave();
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MetricsService::OnUserAction(const std::string& action) {
655a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!ShouldLogEvents())
6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
6572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
658c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  log_manager_.current_log()->RecordUserAction(action);
6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HandleIdleSinceLastTransmission(false);
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
662f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void MetricsService::FinishedGatheringInitialMetrics() {
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_EQ(INIT_TASK_SCHEDULED, state_);
6644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  state_ = INIT_TASK_DONE;
66546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
66646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Create the initial log.
66746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (!initial_metrics_log_.get()) {
66846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG);
66946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    NotifyOnDidCreateMetricsLog();
67046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
67146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
672a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  scheduler_->InitTaskComplete();
6734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
6744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
675a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void MetricsService::GetUptimes(PrefService* pref,
676a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                base::TimeDelta* incremental_uptime,
677a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                base::TimeDelta* uptime) {
6784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::TimeTicks now = base::TimeTicks::Now();
679a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If this is the first call, init |first_updated_time_| and
680a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // |last_updated_time_|.
681a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (last_updated_time_.is_null()) {
682a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    first_updated_time_ = now;
6834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    last_updated_time_ = now;
684a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
685a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  *incremental_uptime = now - last_updated_time_;
686a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  *uptime = now - first_updated_time_;
6874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  last_updated_time_ = now;
6884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
689a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const int64 incremental_time_secs = incremental_uptime->InSeconds();
6904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (incremental_time_secs > 0) {
691f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    int64 metrics_uptime =
692f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        pref->GetInt64(metrics::prefs::kUninstallMetricsUptimeSec);
6934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    metrics_uptime += incremental_time_secs;
694f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    pref->SetInt64(metrics::prefs::kUninstallMetricsUptimeSec, metrics_uptime);
6954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
698cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::NotifyOnDidCreateMetricsLog() {
6991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(IsSingleThreaded());
700cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for (size_t i = 0; i < metrics_providers_.size(); ++i)
701cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    metrics_providers_[i]->OnDidCreateMetricsLog();
702cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
703cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// State save methods
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::ScheduleNextStateSave() {
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  state_saver_factory_.InvalidateWeakPtrs();
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
71090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&MetricsService::SaveLocalState,
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 state_saver_factory_.GetWeakPtr()),
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::TimeDelta::FromMinutes(kSaveStateIntervalMinutes));
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::SaveLocalState() {
717cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  RecordCurrentState(local_state_);
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(jar):110021 Does this run down the batteries????
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ScheduleNextStateSave();
7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Recording control methods
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::OpenNewLog() {
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!log_manager_.current_log());
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
73046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  log_manager_.BeginLoggingWithLog(CreateLog(MetricsLog::ONGOING_LOG));
731cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NotifyOnDidCreateMetricsLog();
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (state_ == INITIALIZED) {
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We only need to schedule that run once.
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    state_ = INIT_TASK_SCHEDULED;
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
736f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    base::MessageLoop::current()->PostDelayedTask(
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        FROM_HERE,
738cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        base::Bind(&MetricsService::StartGatheringMetrics,
739cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   self_ptr_factory_.GetWeakPtr()),
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::TimeDelta::FromSeconds(kInitializationDelaySeconds));
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
744cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::StartGatheringMetrics() {
745cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  client_->StartGatheringMetrics(
746f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      base::Bind(&MetricsService::FinishedGatheringInitialMetrics,
747cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 self_ptr_factory_.GetWeakPtr()));
748cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
749cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::CloseCurrentLog() {
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!log_manager_.current_log())
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(jar): Integrate bounds on log recording more consistently, so that we
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // can stop recording logs that are too big much sooner.
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (log_manager_.current_log()->num_events() > kEventLimit) {
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events",
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         log_manager_.current_log()->num_events());
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    log_manager_.DiscardCurrentLog();
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OpenNewLog();  // Start trivial log to hold our histograms.
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Put incremental data (histogram deltas, and realtime stats deltas) at the
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // end of all log transmissions (initial log handles this separately).
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // RecordIncrementalStabilityElements only exists on the derived
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // MetricsLog class.
767116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  MetricsLog* current_log = log_manager_.current_log();
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(current_log);
769cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::vector<variations::ActiveGroupId> synthetic_trials;
770f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  GetCurrentSyntheticFieldTrials(&synthetic_trials);
771116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  current_log->RecordEnvironment(
772116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      metrics_providers_.get(), synthetic_trials, GetInstallDate());
773a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::TimeDelta incremental_uptime;
774a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::TimeDelta uptime;
775cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GetUptimes(local_state_, &incremental_uptime, &uptime);
776cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  current_log->RecordStabilityMetrics(metrics_providers_.get(),
777cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                      incremental_uptime, uptime);
778f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
779cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  current_log->RecordGeneralMetrics(metrics_providers_.get());
780ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch  RecordCurrentHistograms();
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  log_manager_.FinishCurrentLog();
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::PushPendingLogsToPersistentStorage() {
7865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (state_ < SENDING_INITIAL_STABILITY_LOG)
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;  // We didn't and still don't have time to get plugin list etc.
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CloseCurrentLog();
7905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  log_manager_.PersistUnsentLogs();
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If there was a staged and/or current log, then there is now at least one
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // log waiting to be uploaded.
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (log_manager_.has_unsent_logs())
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    state_ = SENDING_OLD_LOGS;
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Transmission of logs methods
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::StartSchedulerIfNecessary() {
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Never schedule cutting or uploading of logs in test mode.
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (test_mode_active_)
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Even if reporting is disabled, the scheduler is needed to trigger the
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // creation of the initial log, which must be done in order for any logs to be
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // persisted on shutdown or backgrounding.
8095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (recording_active() &&
8105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      (reporting_active() || state_ < SENDING_INITIAL_STABILITY_LOG)) {
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scheduler_->Start();
8125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::StartScheduledUpload() {
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If we're getting no notifications, then the log won't have much in it, and
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it's possible the computer is about to go to sleep, so don't upload and
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // stop the scheduler.
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If recording has been turned off, the scheduler doesn't need to run.
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If reporting is off, proceed if the initial log hasn't been created, since
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that has to happen in order for logs to be cut and stored when persisting.
822cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // TODO(stuartmorgan): Call Stop() on the scheduler when reporting and/or
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // recording are turned off instead of letting it fire and then aborting.
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (idle_since_last_transmission_ ||
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !recording_active() ||
8265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      (!reporting_active() && state_ >= SENDING_INITIAL_STABILITY_LOG)) {
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scheduler_->Stop();
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scheduler_->UploadCancelled();
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the callback was to upload an old log, but there no longer is one,
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // just report success back to the scheduler to begin the ongoing log
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callbacks.
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(stuartmorgan): Consider removing the distinction between
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SENDING_OLD_LOGS and SENDING_CURRENT_LOGS to simplify the state machine
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // now that the log upload flow is the same for both modes.
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (state_ == SENDING_OLD_LOGS && !log_manager_.has_unsent_logs()) {
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    state_ = SENDING_CURRENT_LOGS;
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scheduler_->UploadFinished(true /* healthy */, false /* no unsent logs */);
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If there are unsent logs, send the next one. If not, start the asynchronous
8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // process of finalizing the current log for upload.
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (state_ == SENDING_OLD_LOGS) {
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(log_manager_.has_unsent_logs());
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    log_manager_.StageNextLogForUpload();
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SendStagedLog();
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
850cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    client_->CollectFinalMetrics(
851cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        base::Bind(&MetricsService::OnFinalLogInfoCollectionDone,
852cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   self_ptr_factory_.GetWeakPtr()));
8531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::OnFinalLogInfoCollectionDone() {
85746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // If somehow there is a log upload in progress, we return and hope things
85846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // work out. The scheduler isn't informed since if this happens, the scheduler
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will get a response from the upload.
86046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(!log_upload_in_progress_);
86146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (log_upload_in_progress_)
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Abort if metrics were turned off during the final info gathering.
8655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!recording_active()) {
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scheduler_->Stop();
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scheduler_->UploadCancelled();
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StageNewLog();
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If logs shouldn't be uploaded, stop here. It's important that this check
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be after StageNewLog(), otherwise the previous logs will never be loaded,
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and thus the open log won't be persisted.
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(stuartmorgan): This is unnecessarily complicated; restructure loading
8775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of previous logs to not require running part of the upload logic.
8785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://crbug.com/157337
8795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!reporting_active()) {
8805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scheduler_->Stop();
8815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scheduler_->UploadCancelled();
8825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SendStagedLog();
8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::StageNewLog() {
8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (log_manager_.has_staged_log())
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (state_) {
8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case INITIALIZED:
8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case INIT_TASK_SCHEDULED:  // We should be further along by now.
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case INIT_TASK_DONE:
8995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (has_initial_stability_log_) {
9005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        // There's an initial stability log, ready to send.
9015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        log_manager_.StageNextLogForUpload();
9025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        has_initial_stability_log_ = false;
9035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        state_ = SENDING_INITIAL_STABILITY_LOG;
9045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      } else {
905c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        PrepareInitialMetricsLog();
9065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        state_ = SENDING_INITIAL_METRICS_LOG;
9075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case SENDING_OLD_LOGS:
9115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();  // Shouldn't be staging a new log during old log sending.
9125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case SENDING_CURRENT_LOGS:
9155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CloseCurrentLog();
9165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      OpenNewLog();
9175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      log_manager_.StageNextLogForUpload();
9185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
9195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
9235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(log_manager_.has_staged_log());
9265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool MetricsService::ProvidersHaveStabilityMetrics() {
9291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Check whether any metrics provider has stability metrics.
9301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  for (size_t i = 0; i < metrics_providers_.size(); ++i) {
9311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (metrics_providers_[i]->HasStabilityMetrics())
9321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return true;
9331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
9341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
9351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return false;
9361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
9371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
9385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void MetricsService::PrepareInitialStabilityLog() {
9395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK_EQ(INITIALIZED, state_);
9405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
9415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<MetricsLog> initial_stability_log(
942cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      CreateLog(MetricsLog::INITIAL_STABILITY_LOG));
943cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
944cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Do not call NotifyOnDidCreateMetricsLog here because the stability
945cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // log describes stats from the _previous_ session.
946cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
9475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!initial_stability_log->LoadSavedEnvironmentFromPrefs())
9485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
949cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
9505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  log_manager_.PauseCurrentLog();
95146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  log_manager_.BeginLoggingWithLog(initial_stability_log.Pass());
952cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
953cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Note: Some stability providers may record stability stats via histograms,
954cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //       so this call has to be after BeginLoggingWithLog().
95546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  log_manager_.current_log()->RecordStabilityMetrics(
95646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      metrics_providers_.get(), base::TimeDelta(), base::TimeDelta());
9575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RecordCurrentStabilityHistograms();
958cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
959cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Note: RecordGeneralMetrics() intentionally not called since this log is for
960cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //       stability stats from a previous session only.
961cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
9625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  log_manager_.FinishCurrentLog();
9635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  log_manager_.ResumePausedLog();
9645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
9655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Store unsent logs, including the stability log that was just saved, so
9665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // that they're not lost in case of a crash before upload time.
9675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  log_manager_.PersistUnsentLogs();
9685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
9695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  has_initial_stability_log_ = true;
9705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
972c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochvoid MetricsService::PrepareInitialMetricsLog() {
9735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(state_ == INIT_TASK_DONE || state_ == SENDING_INITIAL_STABILITY_LOG);
974f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
975cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::vector<variations::ActiveGroupId> synthetic_trials;
976f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  GetCurrentSyntheticFieldTrials(&synthetic_trials);
977cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  initial_metrics_log_->RecordEnvironment(metrics_providers_.get(),
978116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                          synthetic_trials,
979116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                          GetInstallDate());
980a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::TimeDelta incremental_uptime;
981a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::TimeDelta uptime;
982cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GetUptimes(local_state_, &incremental_uptime, &uptime);
9835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Histograms only get written to the current log, so make the new log current
9855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // before writing them.
9865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  log_manager_.PauseCurrentLog();
98746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  log_manager_.BeginLoggingWithLog(initial_metrics_log_.Pass());
988cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
989cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Note: Some stability providers may record stability stats via histograms,
990cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //       so this call has to be after BeginLoggingWithLog().
991116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  MetricsLog* current_log = log_manager_.current_log();
992cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  current_log->RecordStabilityMetrics(metrics_providers_.get(),
993cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                      base::TimeDelta(), base::TimeDelta());
994cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  current_log->RecordGeneralMetrics(metrics_providers_.get());
995ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch  RecordCurrentHistograms();
996cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
9975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  log_manager_.FinishCurrentLog();
9985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  log_manager_.ResumePausedLog();
9995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1000116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Store unsent logs, including the initial log that was just saved, so
1001116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // that they're not lost in case of a crash before upload time.
1002116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  log_manager_.PersistUnsentLogs();
1003116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
10045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!log_manager_.has_staged_log());
10055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  log_manager_.StageNextLogForUpload();
10065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::SendStagedLog() {
10095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(log_manager_.has_staged_log());
101046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (!log_manager_.has_staged_log())
101146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return;
10125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
101346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(!log_upload_in_progress_);
101446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  log_upload_in_progress_ = true;
10155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
101646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (!log_uploader_) {
101746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    log_uploader_ = client_->CreateUploader(
101846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        kServerUrl, kMimeType,
101946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        base::Bind(&MetricsService::OnLogUploadComplete,
102046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                   self_ptr_factory_.GetWeakPtr()));
102146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
102246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
102346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const std::string hash =
102446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      base::HexEncode(log_manager_.staged_log_hash().data(),
102546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                      log_manager_.staged_log_hash().size());
102646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool success = log_uploader_->UploadLog(log_manager_.staged_log(), hash);
102746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  UMA_HISTOGRAM_BOOLEAN("UMA.UploadCreation", success);
102846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (!success) {
10295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Skip this upload and hope things work out next time.
10305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    log_manager_.DiscardStagedLog();
10315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scheduler_->UploadCancelled();
103246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    log_upload_in_progress_ = false;
10335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
10345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HandleIdleSinceLastTransmission(true);
10375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
104046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void MetricsService::OnLogUploadComplete(int response_code) {
104146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(log_upload_in_progress_);
104246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  log_upload_in_progress_ = false;
10435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Log a histogram to track response success vs. failure rates.
1045c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  UMA_HISTOGRAM_ENUMERATION("UMA.UploadResponseStatus.Protobuf",
1046c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            ResponseCodeToStatus(response_code),
1047c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            NUM_RESPONSE_STATUSES);
10485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool upload_succeeded = response_code == 200;
10505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Provide boolean for error recovery (allow us to ignore response_code).
10525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool discard_log = false;
1053cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const size_t log_size = log_manager_.staged_log().length();
10541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (upload_succeeded) {
10551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    UMA_HISTOGRAM_COUNTS_10000("UMA.LogSize.OnSuccess", log_size / 1024);
10561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  } else if (log_size > kUploadLogAvoidRetransmitSize) {
10575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded",
10585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         static_cast<int>(log_size));
10595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    discard_log = true;
10605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (response_code == 400) {
10615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Bad syntax.  Retransmission won't work.
10625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    discard_log = true;
10635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (upload_succeeded || discard_log) {
1066c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    log_manager_.DiscardStagedLog();
1067116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Store the updated list to disk now that the removed log is uploaded.
1068116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    log_manager_.PersistUnsentLogs();
1069116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
10705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!log_manager_.has_staged_log()) {
10725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    switch (state_) {
10735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case SENDING_INITIAL_STABILITY_LOG:
1074c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        PrepareInitialMetricsLog();
10755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        SendStagedLog();
10765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        state_ = SENDING_INITIAL_METRICS_LOG;
10775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        break;
10785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case SENDING_INITIAL_METRICS_LOG:
10805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        state_ = log_manager_.has_unsent_logs() ? SENDING_OLD_LOGS
10815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                : SENDING_CURRENT_LOGS;
10825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
10835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case SENDING_OLD_LOGS:
10855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (!log_manager_.has_unsent_logs())
10865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          state_ = SENDING_CURRENT_LOGS;
10875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
10885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case SENDING_CURRENT_LOGS:
10905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
10915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      default:
10935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
10945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
10955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
10965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (log_manager_.has_unsent_logs())
10985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DCHECK_LT(state_, SENDING_CURRENT_LOGS);
10995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Error 400 indicates a problem with the log, not with the server, so
11025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // don't consider that a sign that the server is in trouble.
11035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool server_is_healthy = upload_succeeded || response_code == 400;
11045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Don't notify the scheduler that the upload is finished if we've only sent
11055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the initial stability log, but not yet the initial metrics log (treat the
11065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // two as a single unit of work as far as the scheduler is concerned).
11075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (state_ != SENDING_INITIAL_METRICS_LOG) {
11085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scheduler_->UploadFinished(server_is_healthy,
11095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               log_manager_.has_unsent_logs());
11105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
11115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (server_is_healthy)
1113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    client_->OnLogUploadComplete();
11145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::IncrementPrefValue(const char* path) {
1117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int value = local_state_->GetInteger(path);
1118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  local_state_->SetInteger(path, value + 1);
11195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::IncrementLongPrefsValue(const char* path) {
1122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int64 value = local_state_->GetInt64(path);
1123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  local_state_->SetInt64(path, value + 1);
11245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MetricsService::UmaMetricsProperlyShutdown() {
11275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(clean_shutdown_status_ == CLEANLY_SHUTDOWN ||
11285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        clean_shutdown_status_ == NEED_TO_SHUTDOWN);
11295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return clean_shutdown_status_ == CLEANLY_SHUTDOWN;
11305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1132f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void MetricsService::RegisterSyntheticFieldTrial(
1133f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const SyntheticTrialGroup& trial) {
1134f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) {
1135f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (synthetic_trial_groups_[i].id.name == trial.id.name) {
1136f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      if (synthetic_trial_groups_[i].id.group != trial.id.group) {
1137f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        synthetic_trial_groups_[i].id.group = trial.id.group;
1138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        synthetic_trial_groups_[i].start_time = base::TimeTicks::Now();
1139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      }
1140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      return;
1141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
1142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
1143f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SyntheticTrialGroup trial_group = trial;
1145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  trial_group.start_time = base::TimeTicks::Now();
1146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  synthetic_trial_groups_.push_back(trial_group);
1147f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
1148f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::RegisterMetricsProvider(
1150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    scoped_ptr<metrics::MetricsProvider> provider) {
1151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK_EQ(INITIALIZED, state_);
1152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  metrics_providers_.push_back(provider.release());
1153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
1154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1155cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::CheckForClonedInstall(
1156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
1157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  state_manager_->CheckForClonedInstall(task_runner);
1158effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
1159effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
1160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void MetricsService::GetCurrentSyntheticFieldTrials(
1161cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    std::vector<variations::ActiveGroupId>* synthetic_trials) {
1162f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(synthetic_trials);
1163f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  synthetic_trials->clear();
1164116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const MetricsLog* current_log = log_manager_.current_log();
1165f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  for (size_t i = 0; i < synthetic_trial_groups_.size(); ++i) {
1166f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (synthetic_trial_groups_[i].start_time <= current_log->creation_time())
1167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      synthetic_trials->push_back(synthetic_trial_groups_[i].id);
1168f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
1169f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
1170f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1171cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)scoped_ptr<MetricsLog> MetricsService::CreateLog(MetricsLog::LogType log_type) {
1172cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return make_scoped_ptr(new MetricsLog(state_manager_->client_id(),
1173cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                        session_id_,
1174cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                        log_type,
1175cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                        client_,
1176cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                        local_state_));
1177cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
1178cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1179cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::RecordCurrentHistograms() {
1180cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(log_manager_.current_log());
1181cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  histogram_snapshot_manager_.PrepareDeltas(
1182cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::Histogram::kNoFlags, base::Histogram::kUmaTargetedHistogramFlag);
1183cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
1184cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1185cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void MetricsService::RecordCurrentStabilityHistograms() {
1186cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(log_manager_.current_log());
1187cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  histogram_snapshot_manager_.PrepareDeltas(
1188cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::Histogram::kNoFlags, base::Histogram::kUmaStabilityHistogramFlag);
1189cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
1190cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
11915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::LogCleanShutdown() {
11925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Redundant hack to write pref ASAP.
11931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  MarkAppCleanShutdownAndCommit(&clean_exit_beacon_, local_state_);
11942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Redundant setting to assure that we always reset this value at shutdown
11965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (and that we don't use some alternate path, and not call LogCleanShutdown).
11975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  clean_shutdown_status_ = CLEANLY_SHUTDOWN;
11985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  clean_exit_beacon_.WriteBeaconValue(true);
12001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  RecordCurrentState(local_state_);
1201f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  local_state_->SetInteger(metrics::prefs::kStabilityExecutionPhase,
1202cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                           MetricsService::SHUTDOWN_COMPLETE);
12035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool MetricsService::ShouldLogEvents() {
1206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // We simply don't log events to UMA if there is a single incognito
12075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // session visible. The problem is that we always notify using the orginal
12085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // profile in order to simplify notification processing.
1209cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return !client_->IsOffTheRecordSessionActive();
12105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::RecordBooleanPrefValue(const char* path, bool value) {
12135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(IsSingleThreaded());
1214cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  local_state_->SetBoolean(path, value);
1215cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  RecordCurrentState(local_state_);
12165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
12175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetricsService::RecordCurrentState(PrefService* pref) {
1219f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  pref->SetInt64(metrics::prefs::kStabilityLastTimestampSec,
12201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 base::Time::Now().ToTimeT());
1221effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
12221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
12231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace metrics
1224