Content settings: remove some plugin-related code/resources when... there are no...
[chromium-blink-merge.git] / components / metrics / metrics_service.h
blob5a63352092993d40899195299874790609845cd9
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file defines a service that collects information about the user
6 // experience in order to help improve future versions of the app.
8 #ifndef COMPONENTS_METRICS_METRICS_SERVICE_H_
9 #define COMPONENTS_METRICS_METRICS_SERVICE_H_
11 #include <map>
12 #include <string>
13 #include <vector>
15 #include "base/basictypes.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/scoped_vector.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/metrics/field_trial.h"
21 #include "base/metrics/histogram_flattener.h"
22 #include "base/metrics/histogram_snapshot_manager.h"
23 #include "base/metrics/user_metrics.h"
24 #include "base/observer_list.h"
25 #include "base/time/time.h"
26 #include "components/metrics/clean_exit_beacon.h"
27 #include "components/metrics/metrics_log.h"
28 #include "components/metrics/metrics_log_manager.h"
29 #include "components/metrics/metrics_provider.h"
30 #include "components/metrics/net/network_metrics_provider.h"
31 #include "components/variations/active_field_trials.h"
33 class PrefService;
34 class PrefRegistrySimple;
36 namespace base {
37 class DictionaryValue;
38 class HistogramSamples;
39 class PrefService;
42 namespace variations {
43 struct ActiveGroupId;
46 namespace net {
47 class URLFetcher;
50 namespace metrics {
52 class MetricsLogUploader;
53 class MetricsReportingScheduler;
54 class MetricsServiceAccessor;
55 class MetricsServiceClient;
56 class MetricsStateManager;
58 // A Field Trial and its selected group, which represent a particular
59 // Chrome configuration state. For example, the trial name could map to
60 // a preference name, and the group name could map to a preference value.
61 struct SyntheticTrialGroup {
62 public:
63 ~SyntheticTrialGroup();
65 variations::ActiveGroupId id;
66 base::TimeTicks start_time;
68 private:
69 // Synthetic field trial users:
70 friend class MetricsServiceAccessor;
71 friend class MetricsService;
72 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial);
74 // This constructor is private specifically so as to control which code is
75 // able to access it. New code that wishes to use it should be added as a
76 // friend class.
77 SyntheticTrialGroup(uint32 trial, uint32 group);
80 // Interface class to observe changes to synthetic trials in MetricsService.
81 class SyntheticTrialObserver {
82 public:
83 // Called when the list of synthetic field trial groups has changed.
84 virtual void OnSyntheticTrialsChanged(
85 const std::vector<SyntheticTrialGroup>& groups) = 0;
87 protected:
88 virtual ~SyntheticTrialObserver() {}
91 // See metrics_service.cc for a detailed description.
92 class MetricsService : public base::HistogramFlattener {
93 public:
94 // The execution phase of the browser.
95 enum ExecutionPhase {
96 UNINITIALIZED_PHASE = 0,
97 START_METRICS_RECORDING = 100,
98 CREATE_PROFILE = 200,
99 STARTUP_TIMEBOMB_ARM = 300,
100 THREAD_WATCHER_START = 400,
101 MAIN_MESSAGE_LOOP_RUN = 500,
102 SHUTDOWN_TIMEBOMB_ARM = 600,
103 SHUTDOWN_COMPLETE = 700,
106 // Creates the MetricsService with the given |state_manager|, |client|, and
107 // |local_state|. Does not take ownership of the paramaters; instead stores
108 // a weak pointer to each. Caller should ensure that the parameters are valid
109 // for the lifetime of this class.
110 MetricsService(MetricsStateManager* state_manager,
111 MetricsServiceClient* client,
112 PrefService* local_state);
113 ~MetricsService() override;
115 // Initializes metrics recording state. Updates various bookkeeping values in
116 // prefs and sets up the scheduler. This is a separate function rather than
117 // being done by the constructor so that field trials could be created before
118 // this is run.
119 void InitializeMetricsRecordingState();
121 // Starts the metrics system, turning on recording and uploading of metrics.
122 // Should be called when starting up with metrics enabled, or when metrics
123 // are turned on.
124 void Start();
126 // Starts the metrics system in a special test-only mode. Metrics won't ever
127 // be uploaded or persisted in this mode, but metrics will be recorded in
128 // memory.
129 void StartRecordingForTests();
131 // Shuts down the metrics system. Should be called at shutdown, or if metrics
132 // are turned off.
133 void Stop();
135 // Enable/disable transmission of accumulated logs and crash reports (dumps).
136 // Calling Start() automatically enables reporting, but sending is
137 // asyncronous so this can be called immediately after Start() to prevent
138 // any uploading.
139 void EnableReporting();
140 void DisableReporting();
142 // Returns the client ID for this client, or the empty string if metrics
143 // recording is not currently running.
144 std::string GetClientId();
146 // Returns the install date of the application, in seconds since the epoch.
147 int64 GetInstallDate();
149 // Returns the date at which the current metrics client ID was created as
150 // an int64 containing seconds since the epoch.
151 int64 GetMetricsReportingEnabledDate();
153 // Returns true if the last session exited cleanly.
154 bool WasLastShutdownClean() const;
156 // Returns the preferred entropy provider used to seed persistent activities
157 // based on whether or not metrics reporting will be permitted on this client.
159 // If metrics reporting is enabled, this method returns an entropy provider
160 // that has a high source of entropy, partially based on the client ID.
161 // Otherwise, it returns an entropy provider that is based on a low entropy
162 // source.
163 scoped_ptr<const base::FieldTrial::EntropyProvider> CreateEntropyProvider();
165 // At startup, prefs needs to be called with a list of all the pref names and
166 // types we'll be using.
167 static void RegisterPrefs(PrefRegistrySimple* registry);
169 // HistogramFlattener:
170 void RecordDelta(const base::HistogramBase& histogram,
171 const base::HistogramSamples& snapshot) override;
172 void InconsistencyDetected(
173 base::HistogramBase::Inconsistency problem) override;
174 void UniqueInconsistencyDetected(
175 base::HistogramBase::Inconsistency problem) override;
176 void InconsistencyDetectedInLoggedCount(int amount) override;
178 // This should be called when the application is not idle, i.e. the user seems
179 // to be interacting with the application.
180 void OnApplicationNotIdle();
182 // Invoked when we get a WM_SESSIONEND. This places a value in prefs that is
183 // reset when RecordCompletedSessionEnd is invoked.
184 void RecordStartOfSessionEnd();
186 // This should be called when the application is shutting down. It records
187 // that session end was successful.
188 void RecordCompletedSessionEnd();
190 #if defined(OS_ANDROID) || defined(OS_IOS)
191 // Called when the application is going into background mode.
192 void OnAppEnterBackground();
194 // Called when the application is coming out of background mode.
195 void OnAppEnterForeground();
196 #else
197 // Set the dirty flag, which will require a later call to LogCleanShutdown().
198 void LogNeedForCleanShutdown();
199 #endif // defined(OS_ANDROID) || defined(OS_IOS)
201 static void SetExecutionPhase(ExecutionPhase execution_phase,
202 PrefService* local_state);
204 // Saves in the preferences if the crash report registration was successful.
205 // This count is eventually send via UMA logs.
206 void RecordBreakpadRegistration(bool success);
208 // Saves in the preferences if the browser is running under a debugger.
209 // This count is eventually send via UMA logs.
210 void RecordBreakpadHasDebugger(bool has_debugger);
212 bool recording_active() const;
213 bool reporting_active() const;
215 // Redundant test to ensure that we are notified of a clean exit.
216 // This value should be true when process has completed shutdown.
217 static bool UmaMetricsProperlyShutdown();
219 // Registers a field trial name and group to be used to annotate a UMA report
220 // with a particular Chrome configuration state. A UMA report will be
221 // annotated with this trial group if and only if all events in the report
222 // were created after the trial is registered. Only one group name may be
223 // registered at a time for a given trial_name. Only the last group name that
224 // is registered for a given trial name will be recorded. The values passed
225 // in must not correspond to any real field trial in the code.
226 // To use this method, SyntheticTrialGroup should friend your class.
227 void RegisterSyntheticFieldTrial(const SyntheticTrialGroup& trial_group);
229 // Adds an observer to be notified when the synthetic trials list changes.
230 void AddSyntheticTrialObserver(SyntheticTrialObserver* observer);
232 // Removes an existing observer of synthetic trials list changes.
233 void RemoveSyntheticTrialObserver(SyntheticTrialObserver* observer);
235 // Register the specified |provider| to provide additional metrics into the
236 // UMA log. Should be called during MetricsService initialization only.
237 void RegisterMetricsProvider(scoped_ptr<MetricsProvider> provider);
239 // Check if this install was cloned or imaged from another machine. If a
240 // clone is detected, reset the client id and low entropy source. This
241 // should not be called more than once.
242 void CheckForClonedInstall(
243 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
245 // Clears the stability metrics that are saved in local state.
246 void ClearSavedStabilityMetrics();
248 // Pushes a log that has been generated by an external component.
249 void PushExternalLog(const std::string& log);
251 protected:
252 // Exposed for testing.
253 MetricsLogManager* log_manager() { return &log_manager_; }
255 private:
256 // The MetricsService has a lifecycle that is stored as a state.
257 // See metrics_service.cc for description of this lifecycle.
258 enum State {
259 INITIALIZED, // Constructor was called.
260 INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish.
261 INIT_TASK_DONE, // Waiting for timer to send initial log.
262 SENDING_LOGS, // Sending logs an creating new ones when we run out.
265 enum ShutdownCleanliness {
266 CLEANLY_SHUTDOWN = 0xdeadbeef,
267 NEED_TO_SHUTDOWN = ~CLEANLY_SHUTDOWN
270 // The current state of recording for the MetricsService. The state is UNSET
271 // until set to something else, at which point it remains INACTIVE or ACTIVE
272 // for the lifetime of the object.
273 enum RecordingState {
274 INACTIVE,
275 ACTIVE,
276 UNSET
279 typedef std::vector<SyntheticTrialGroup> SyntheticTrialGroups;
281 // Calls into the client to initialize some system profile metrics.
282 void StartInitTask();
284 // Callback that moves the state to INIT_TASK_DONE. When this is called, the
285 // state should be INIT_TASK_SCHEDULED.
286 void FinishedInitTask();
288 void OnUserAction(const std::string& action);
290 // Get the amount of uptime since this process started and since the last
291 // call to this function. Also updates the cumulative uptime metric (stored
292 // as a pref) for uninstall. Uptimes are measured using TimeTicks, which
293 // guarantees that it is monotonic and does not jump if the user changes
294 // his/her clock. The TimeTicks implementation also makes the clock not
295 // count time the computer is suspended.
296 void GetUptimes(PrefService* pref,
297 base::TimeDelta* incremental_uptime,
298 base::TimeDelta* uptime);
300 // Turns recording on or off.
301 // DisableRecording() also forces a persistent save of logging state (if
302 // anything has been recorded, or transmitted).
303 void EnableRecording();
304 void DisableRecording();
306 // If in_idle is true, sets idle_since_last_transmission to true.
307 // If in_idle is false and idle_since_last_transmission_ is true, sets
308 // idle_since_last_transmission to false and starts the timer (provided
309 // starting the timer is permitted).
310 void HandleIdleSinceLastTransmission(bool in_idle);
312 // Set up client ID, session ID, etc.
313 void InitializeMetricsState();
315 // Notifies providers when a new metrics log is created.
316 void NotifyOnDidCreateMetricsLog();
318 // Schedule the next save of LocalState information. This is called
319 // automatically by the task that performs each save to schedule the next one.
320 void ScheduleNextStateSave();
322 // Save the LocalState information immediately. This should not be called by
323 // anybody other than the scheduler to avoid doing too many writes. When you
324 // make a change, call ScheduleNextStateSave() instead.
325 void SaveLocalState();
327 // Opens a new log for recording user experience metrics.
328 void OpenNewLog();
330 // Closes out the current log after adding any last information.
331 void CloseCurrentLog();
333 // Pushes the text of the current and staged logs into persistent storage.
334 // Called when Chrome shuts down.
335 void PushPendingLogsToPersistentStorage();
337 // Ensures that scheduler is running, assuming the current settings are such
338 // that metrics should be reported. If not, this is a no-op.
339 void StartSchedulerIfNecessary();
341 // Starts the process of uploading metrics data.
342 void StartScheduledUpload();
344 // Called by the client via a callback when final log info collection is
345 // complete.
346 void OnFinalLogInfoCollectionDone();
348 // If recording is enabled, begins uploading the next completed log from
349 // the log manager, staging it if necessary.
350 void SendNextLog();
352 // Returns true if any of the registered metrics providers have critical
353 // stability metrics to report in an initial stability log.
354 bool ProvidersHaveInitialStabilityMetrics();
356 // Prepares the initial stability log, which is only logged when the previous
357 // run of Chrome crashed. This log contains any stability metrics left over
358 // from that previous run, and only these stability metrics. It uses the
359 // system profile from the previous session. Returns true if a log was
360 // created.
361 bool PrepareInitialStabilityLog();
363 // Prepares the initial metrics log, which includes startup histograms and
364 // profiler data, as well as incremental stability-related metrics.
365 void PrepareInitialMetricsLog();
367 // Uploads the currently staged log (which must be non-null).
368 void SendStagedLog();
370 // Called after transmission completes (either successfully or with failure).
371 void OnLogUploadComplete(int response_code);
373 // Reads, increments and then sets the specified integer preference.
374 void IncrementPrefValue(const char* path);
376 // Reads, increments and then sets the specified long preference that is
377 // stored as a string.
378 void IncrementLongPrefsValue(const char* path);
380 // Records that the browser was shut down cleanly.
381 void LogCleanShutdown();
383 // Records state that should be periodically saved, like uptime and
384 // buffered plugin stability statistics.
385 void RecordCurrentState(PrefService* pref);
387 // Checks whether events should currently be logged.
388 bool ShouldLogEvents();
390 // Sets the value of the specified path in prefs and schedules a save.
391 void RecordBooleanPrefValue(const char* path, bool value);
393 // Notifies observers on a synthetic trial list change.
394 void NotifySyntheticTrialObservers();
396 // Returns a list of synthetic field trials that were active for the entire
397 // duration of the current log.
398 void GetCurrentSyntheticFieldTrials(
399 std::vector<variations::ActiveGroupId>* synthetic_trials);
401 // Creates a new MetricsLog instance with the given |log_type|.
402 scoped_ptr<MetricsLog> CreateLog(MetricsLog::LogType log_type);
404 // Records the current environment (system profile) in |log|.
405 void RecordCurrentEnvironment(MetricsLog* log);
407 // Record complete list of histograms into the current log.
408 // Called when we close a log.
409 void RecordCurrentHistograms();
411 // Record complete list of stability histograms into the current log,
412 // i.e., histograms with the |kUmaStabilityHistogramFlag| flag set.
413 void RecordCurrentStabilityHistograms();
415 // Skips staged upload and discards the log. Used in case of unsuccessful
416 // upload or intentional sampling of logs.
417 void SkipAndDiscardUpload();
419 // Manager for the various in-flight logs.
420 MetricsLogManager log_manager_;
422 // |histogram_snapshot_manager_| prepares histogram deltas for transmission.
423 base::HistogramSnapshotManager histogram_snapshot_manager_;
425 // Used to manage various metrics reporting state prefs, such as client id,
426 // low entropy source and whether metrics reporting is enabled. Weak pointer.
427 MetricsStateManager* const state_manager_;
429 // Used to interact with the embedder. Weak pointer; must outlive |this|
430 // instance.
431 MetricsServiceClient* const client_;
433 // Registered metrics providers.
434 ScopedVector<MetricsProvider> metrics_providers_;
436 PrefService* local_state_;
438 CleanExitBeacon clean_exit_beacon_;
440 base::ActionCallback action_callback_;
442 // Indicate whether recording and reporting are currently happening.
443 // These should not be set directly, but by calling SetRecording and
444 // SetReporting.
445 RecordingState recording_state_;
446 bool reporting_active_;
448 // Indicate whether test mode is enabled, where the initial log should never
449 // be cut, and logs are neither persisted nor uploaded.
450 bool test_mode_active_;
452 // The progression of states made by the browser are recorded in the following
453 // state.
454 State state_;
456 // The initial metrics log, used to record startup metrics (histograms and
457 // profiler data). Note that if a crash occurred in the previous session, an
458 // initial stability log may be sent before this.
459 scoped_ptr<MetricsLog> initial_metrics_log_;
461 // Instance of the helper class for uploading logs.
462 scoped_ptr<MetricsLogUploader> log_uploader_;
464 // Whether there is a current log upload in progress.
465 bool log_upload_in_progress_;
467 // Whether the MetricsService object has received any notifications since
468 // the last time a transmission was sent.
469 bool idle_since_last_transmission_;
471 // A number that identifies the how many times the app has been launched.
472 int session_id_;
474 // The scheduler for determining when uploads should happen.
475 scoped_ptr<MetricsReportingScheduler> scheduler_;
477 // Stores the time of the first call to |GetUptimes()|.
478 base::TimeTicks first_updated_time_;
480 // Stores the time of the last call to |GetUptimes()|.
481 base::TimeTicks last_updated_time_;
483 // Field trial groups that map to Chrome configuration states.
484 SyntheticTrialGroups synthetic_trial_groups_;
486 // List of observers of |synthetic_trial_groups_| changes.
487 base::ObserverList<SyntheticTrialObserver> synthetic_trial_observer_list_;
489 // Execution phase the browser is in.
490 static ExecutionPhase execution_phase_;
492 // Reduntant marker to check that we completed our shutdown, and set the
493 // exited-cleanly bit in the prefs.
494 static ShutdownCleanliness clean_shutdown_status_;
496 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess);
497 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest,
498 PermutedEntropyCacheClearedWhenLowEntropyReset);
499 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial);
501 // Weak pointers factory used to post task on different threads. All weak
502 // pointers managed by this factory have the same lifetime as MetricsService.
503 base::WeakPtrFactory<MetricsService> self_ptr_factory_;
505 // Weak pointers factory used for saving state. All weak pointers managed by
506 // this factory are invalidated in ScheduleNextStateSave.
507 base::WeakPtrFactory<MetricsService> state_saver_factory_;
509 DISALLOW_COPY_AND_ASSIGN(MetricsService);
512 } // namespace metrics
514 #endif // COMPONENTS_METRICS_METRICS_SERVICE_H_