1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file defines a service that collects information about the user
// experience in order to help improve future versions of the app.
#ifndef CHROME_BROWSER_METRICS_METRICS_SERVICE_H_
#define CHROME_BROWSER_METRICS_METRICS_SERVICE_H_
#include <map>
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram_flattener.h"
#include "base/metrics/histogram_snapshot_manager.h"
#include "base/metrics/user_metrics.h"
#include "base/observer_list.h"
#include "base/strings/string16.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "chrome/browser/metrics/metrics_log.h"
#include "chrome/browser/metrics/tracking_synchronizer_observer.h"
#include "components/metrics/metrics_log_manager.h"
#include "components/metrics/metrics_provider.h"
#include "components/metrics/metrics_service_observer.h"
#include "components/variations/active_field_trials.h"
#include "net/url_request/url_fetcher_delegate.h"
class GoogleUpdateMetricsProviderWin;
class MetricsReportingScheduler;
class PrefService;
class PrefRegistrySimple;
class PluginMetricsProvider;
namespace base {
class DictionaryValue;
class HistogramSamples;
class MessageLoopProxy;
class PrefService;
}
namespace variations {
struct ActiveGroupId;
}
namespace content {
}
namespace metrics {
class MetricsServiceClient;
class MetricsStateManager;
}
namespace net {
class URLFetcher;
}
namespace tracked_objects {
struct ProcessDataSnapshot;
}
// A Field Trial and its selected group, which represent a particular
// Chrome configuration state. For example, the trial name could map to
// a preference name, and the group name could map to a preference value.
struct SyntheticTrialGroup {
public:
~SyntheticTrialGroup();
variations::ActiveGroupId id;
base::TimeTicks start_time;
private:
friend class MetricsService;
FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial);
// This constructor is private specifically so as to control which code is
// able to access it. New code that wishes to use it should be added as a
// friend class.
SyntheticTrialGroup(uint32 trial, uint32 group);
};
class MetricsService
: public base::HistogramFlattener,
public chrome_browser_metrics::TrackingSynchronizerObserver,
public net::URLFetcherDelegate {
public:
// The execution phase of the browser.
enum ExecutionPhase {
UNINITIALIZED_PHASE = 0,
START_METRICS_RECORDING = 100,
CREATE_PROFILE = 200,
STARTUP_TIMEBOMB_ARM = 300,
THREAD_WATCHER_START = 400,
MAIN_MESSAGE_LOOP_RUN = 500,
SHUTDOWN_TIMEBOMB_ARM = 600,
SHUTDOWN_COMPLETE = 700,
};
// Creates the MetricsService with the given |state_manager|, |client|, and
// |local_state|. Does not take ownership of the paramaters; instead stores
// a weak pointer to each. Caller should ensure that the parameters are valid
// for the lifetime of this class.
MetricsService(metrics::MetricsStateManager* state_manager,
metrics::MetricsServiceClient* client,
PrefService* local_state);
virtual ~MetricsService();
// Initializes metrics recording state. Updates various bookkeeping values in
// prefs and sets up the scheduler. This is a separate function rather than
// being done by the constructor so that field trials could be created before
// this is run.
void InitializeMetricsRecordingState();
// Starts the metrics system, turning on recording and uploading of metrics.
// Should be called when starting up with metrics enabled, or when metrics
// are turned on.
void Start();
// If metrics reporting is enabled, starts the metrics service. Returns
// whether the metrics service was started.
bool StartIfMetricsReportingEnabled();
// Starts the metrics system in a special test-only mode. Metrics won't ever
// be uploaded or persisted in this mode, but metrics will be recorded in
// memory.
void StartRecordingForTests();
// Shuts down the metrics system. Should be called at shutdown, or if metrics
// are turned off.
void Stop();
// Enable/disable transmission of accumulated logs and crash reports (dumps).
// Calling Start() automatically enables reporting, but sending is
// asyncronous so this can be called immediately after Start() to prevent
// any uploading.
void EnableReporting();
void DisableReporting();
// Returns the client ID for this client, or the empty string if metrics
// recording is not currently running.
std::string GetClientId();
// Returns the preferred entropy provider used to seed persistent activities
// based on whether or not metrics reporting will be permitted on this client.
//
// If metrics reporting is enabled, this method returns an entropy provider
// that has a high source of entropy, partially based on the client ID.
// Otherwise, it returns an entropy provider that is based on a low entropy
// source.
scoped_ptr<const base::FieldTrial::EntropyProvider> CreateEntropyProvider();
// At startup, prefs needs to be called with a list of all the pref names and
// types we'll be using.
static void RegisterPrefs(PrefRegistrySimple* registry);
// HistogramFlattener:
virtual void RecordDelta(const base::HistogramBase& histogram,
const base::HistogramSamples& snapshot) OVERRIDE;
virtual void InconsistencyDetected(
base::HistogramBase::Inconsistency problem) OVERRIDE;
virtual void UniqueInconsistencyDetected(
base::HistogramBase::Inconsistency problem) OVERRIDE;
virtual void InconsistencyDetectedInLoggedCount(int amount) OVERRIDE;
// This should be called when the application is not idle, i.e. the user seems
// to be interacting with the application.
void OnApplicationNotIdle();
// Invoked when we get a WM_SESSIONEND. This places a value in prefs that is
// reset when RecordCompletedSessionEnd is invoked.
void RecordStartOfSessionEnd();
// This should be called when the application is shutting down. It records
// that session end was successful.
void RecordCompletedSessionEnd();
#if defined(OS_ANDROID) || defined(OS_IOS)
// Called when the application is going into background mode.
void OnAppEnterBackground();
// Called when the application is coming out of background mode.
void OnAppEnterForeground();
#else
// Set the dirty flag, which will require a later call to LogCleanShutdown().
static void LogNeedForCleanShutdown(PrefService* local_state);
#endif // defined(OS_ANDROID) || defined(OS_IOS)
static void SetExecutionPhase(ExecutionPhase execution_phase,
PrefService* local_state);
// Saves in the preferences if the crash report registration was successful.
// This count is eventually send via UMA logs.
void RecordBreakpadRegistration(bool success);
// Saves in the preferences if the browser is running under a debugger.
// This count is eventually send via UMA logs.
void RecordBreakpadHasDebugger(bool has_debugger);
bool recording_active() const;
bool reporting_active() const;
void LogPluginLoadingError(const base::FilePath& plugin_path);
// Redundant test to ensure that we are notified of a clean exit.
// This value should be true when process has completed shutdown.
static bool UmaMetricsProperlyShutdown();
// Registers a field trial name and group to be used to annotate a UMA report
// with a particular Chrome configuration state. A UMA report will be
// annotated with this trial group if and only if all events in the report
// were created after the trial is registered. Only one group name may be
// registered at a time for a given trial_name. Only the last group name that
// is registered for a given trial name will be recorded. The values passed
// in must not correspond to any real field trial in the code.
// To use this method, SyntheticTrialGroup should friend your class.
void RegisterSyntheticFieldTrial(const SyntheticTrialGroup& trial_group);
// Register the specified |provider| to provide additional metrics into the
// UMA log. Should be called during MetricsService initialization only.
void RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider> provider);
// Check if this install was cloned or imaged from another machine. If a
// clone is detected, reset the client id and low entropy source. This
// should not be called more than once.
void CheckForClonedInstall(
scoped_refptr<base::SingleThreadTaskRunner> task_runner);
protected:
// Exposed for testing.
metrics::MetricsLogManager* log_manager() { return &log_manager_; }
private:
// The MetricsService has a lifecycle that is stored as a state.
// See metrics_service.cc for description of this lifecycle.
enum State {
INITIALIZED, // Constructor was called.
INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to
// complete.
INIT_TASK_DONE, // Waiting for timer to send initial log.
SENDING_INITIAL_STABILITY_LOG, // Initial stability log being sent.
SENDING_INITIAL_METRICS_LOG, // Initial metrics log being sent.
SENDING_OLD_LOGS, // Sending unsent logs from last session.
SENDING_CURRENT_LOGS, // Sending ongoing logs as they accrue.
};
enum ShutdownCleanliness {
CLEANLY_SHUTDOWN = 0xdeadbeef,
NEED_TO_SHUTDOWN = ~CLEANLY_SHUTDOWN
};
typedef std::vector<SyntheticTrialGroup> SyntheticTrialGroups;
// Calls into the client to start metrics gathering.
void StartGatheringMetrics();
// Callback that continues the init task by loading plugin information.
void OnInitTaskGotHardwareClass();
// Called after the Plugin init task has been completed that continues the
// init task by launching a task to gather Google Update statistics.
void OnInitTaskGotPluginInfo();
// Called after GoogleUpdate init task has been completed that continues the
// init task by loading profiler data.
void OnInitTaskGotGoogleUpdateData();
void OnUserAction(const std::string& action);
// TrackingSynchronizerObserver:
virtual void ReceivedProfilerData(
const tracked_objects::ProcessDataSnapshot& process_data,
int process_type) OVERRIDE;
// Callback that moves the state to INIT_TASK_DONE.
virtual void FinishedReceivingProfilerData() OVERRIDE;
// Get the amount of uptime since this process started and since the last
// call to this function. Also updates the cumulative uptime metric (stored
// as a pref) for uninstall. Uptimes are measured using TimeTicks, which
// guarantees that it is monotonic and does not jump if the user changes
// his/her clock. The TimeTicks implementation also makes the clock not
// count time the computer is suspended.
void GetUptimes(PrefService* pref,
base::TimeDelta* incremental_uptime,
base::TimeDelta* uptime);
// Turns recording on or off.
// DisableRecording() also forces a persistent save of logging state (if
// anything has been recorded, or transmitted).
void EnableRecording();
void DisableRecording();
// If in_idle is true, sets idle_since_last_transmission to true.
// If in_idle is false and idle_since_last_transmission_ is true, sets
// idle_since_last_transmission to false and starts the timer (provided
// starting the timer is permitted).
void HandleIdleSinceLastTransmission(bool in_idle);
// Set up client ID, session ID, etc.
void InitializeMetricsState();
// Registers/unregisters |observer| to receive MetricsLog notifications.
void AddObserver(MetricsServiceObserver* observer);
void RemoveObserver(MetricsServiceObserver* observer);
void NotifyOnDidCreateMetricsLog();
// Schedule the next save of LocalState information. This is called
// automatically by the task that performs each save to schedule the next one.
void ScheduleNextStateSave();
// Save the LocalState information immediately. This should not be called by
// anybody other than the scheduler to avoid doing too many writes. When you
// make a change, call ScheduleNextStateSave() instead.
void SaveLocalState();
// Opens a new log for recording user experience metrics.
void OpenNewLog();
// Closes out the current log after adding any last information.
void CloseCurrentLog();
// Pushes the text of the current and staged logs into persistent storage.
// Called when Chrome shuts down.
void PushPendingLogsToPersistentStorage();
// Ensures that scheduler is running, assuming the current settings are such
// that metrics should be reported. If not, this is a no-op.
void StartSchedulerIfNecessary();
// Starts the process of uploading metrics data.
void StartScheduledUpload();
// Called by the client when final log info collection is complete.
void OnFinalLogInfoCollectionDone();
// Either closes the current log or creates and closes the initial log
// (depending on |state_|), and stages it for upload.
void StageNewLog();
// Prepares the initial stability log, which is only logged when the previous
// run of Chrome crashed. This log contains any stability metrics left over
// from that previous run, and only these stability metrics. It uses the
// system profile from the previous session.
void PrepareInitialStabilityLog();
// Prepares the initial metrics log, which includes startup histograms and
// profiler data, as well as incremental stability-related metrics.
void PrepareInitialMetricsLog();
// Uploads the currently staged log (which must be non-null).
void SendStagedLog();
// Prepared the staged log to be passed to the server. Upon return,
// current_fetch_ should be reset with its upload data set to a compressed
// copy of the staged log.
void PrepareFetchWithStagedLog();
// Implementation of net::URLFetcherDelegate. Called after transmission
// completes (either successfully or with failure).
virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
// Reads, increments and then sets the specified integer preference.
void IncrementPrefValue(const char* path);
// Reads, increments and then sets the specified long preference that is
// stored as a string.
void IncrementLongPrefsValue(const char* path);
// Records that the browser was shut down cleanly.
void LogCleanShutdown();
// Records state that should be periodically saved, like uptime and
// buffered plugin stability statistics.
void RecordCurrentState(PrefService* pref);
// Checks whether events should currently be logged.
bool ShouldLogEvents();
// Sets the value of the specified path in prefs and schedules a save.
void RecordBooleanPrefValue(const char* path, bool value);
// Returns a list of synthetic field trials that were active for the entire
// duration of the current log.
void GetCurrentSyntheticFieldTrials(
std::vector<variations::ActiveGroupId>* synthetic_trials);
// Creates a new MetricsLog instance with the given |log_type|.
scoped_ptr<MetricsLog> CreateLog(MetricsLog::LogType log_type);
// Record complete list of histograms into the current log.
// Called when we close a log.
void RecordCurrentHistograms();
// Record complete list of stability histograms into the current log,
// i.e., histograms with the |kUmaStabilityHistogramFlag| flag set.
void RecordCurrentStabilityHistograms();
// Manager for the various in-flight logs.
metrics::MetricsLogManager log_manager_;
// |histogram_snapshot_manager_| prepares histogram deltas for transmission.
base::HistogramSnapshotManager histogram_snapshot_manager_;
// Used to manage various metrics reporting state prefs, such as client id,
// low entropy source and whether metrics reporting is enabled. Weak pointer.
metrics::MetricsStateManager* const state_manager_;
// Used to interact with the embedder. Weak pointer; must outlive |this|
// instance.
metrics::MetricsServiceClient* const client_;
// Registered metrics providers.
ScopedVector<metrics::MetricsProvider> metrics_providers_;
PrefService* local_state_;
base::ActionCallback action_callback_;
// Indicate whether recording and reporting are currently happening.
// These should not be set directly, but by calling SetRecording and
// SetReporting.
bool recording_active_;
bool reporting_active_;
// Indicate whether test mode is enabled, where the initial log should never
// be cut, and logs are neither persisted nor uploaded.
bool test_mode_active_;
// The progression of states made by the browser are recorded in the following
// state.
State state_;
// Whether the initial stability log has been recorded during startup.
bool has_initial_stability_log_;
#if defined(ENABLE_PLUGINS)
PluginMetricsProvider* plugin_metrics_provider_;
#endif
#if defined(OS_WIN)
GoogleUpdateMetricsProviderWin* google_update_metrics_provider_;
#endif
// The initial metrics log, used to record startup metrics (histograms and
// profiler data). Note that if a crash occurred in the previous session, an
// initial stability log may be sent before this.
scoped_ptr<MetricsLog> initial_metrics_log_;
// The outstanding transmission appears as a URL Fetch operation.
scoped_ptr<net::URLFetcher> current_fetch_;
// Whether the MetricsService object has received any notifications since
// the last time a transmission was sent.
bool idle_since_last_transmission_;
// A number that identifies the how many times the app has been launched.
int session_id_;
// Weak pointers factory used to post task on different threads. All weak
// pointers managed by this factory have the same lifetime as MetricsService.
base::WeakPtrFactory<MetricsService> self_ptr_factory_;
// Weak pointers factory used for saving state. All weak pointers managed by
// this factory are invalidated in ScheduleNextStateSave.
base::WeakPtrFactory<MetricsService> state_saver_factory_;
// The scheduler for determining when uploads should happen.
scoped_ptr<MetricsReportingScheduler> scheduler_;
// Indicates that an asynchronous reporting step is running.
// This is used only for debugging.
bool waiting_for_asynchronous_reporting_step_;
// Stores the time of the first call to |GetUptimes()|.
base::TimeTicks first_updated_time_;
// Stores the time of the last call to |GetUptimes()|.
base::TimeTicks last_updated_time_;
// Execution phase the browser is in.
static ExecutionPhase execution_phase_;
// Reduntant marker to check that we completed our shutdown, and set the
// exited-cleanly bit in the prefs.
static ShutdownCleanliness clean_shutdown_status_;
// Field trial groups that map to Chrome configuration states.
SyntheticTrialGroups synthetic_trial_groups_;
ObserverList<MetricsServiceObserver> observers_;
// Confirms single-threaded access to |observers_| in debug builds.
base::ThreadChecker thread_checker_;
friend class MetricsServiceAccessor;
FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess);
FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, MetricsServiceObserver);
FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest,
PermutedEntropyCacheClearedWhenLowEntropyReset);
FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial);
DISALLOW_COPY_AND_ASSIGN(MetricsService);
};
#endif // CHROME_BROWSER_METRICS_METRICS_SERVICE_H_
|