blob: dbd6afcb20eafa8ca949b7c1870491c96750b216 [file] [log] [blame]
[email protected]2e4cd1a2012-01-12 08:51:031// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]897b26272010-06-11 02:23:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file defines a service that collects information about the user
6// experience in order to help improve future versions of the app.
7
[email protected]1357c322010-12-30 22:18:568#ifndef CHROME_FRAME_METRICS_SERVICE_H_
9#define CHROME_FRAME_METRICS_SERVICE_H_
[email protected]897b26272010-06-11 02:23:4410
11#include <map>
12#include <string>
13
14#include "base/basictypes.h"
[email protected]897b26272010-06-11 02:23:4415#include "base/lazy_instance.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/scoped_ptr.h"
[email protected]add184df2012-03-10 10:08:5717#include "base/metrics/field_trial.h"
[email protected]835d7c82010-10-14 04:38:3818#include "base/metrics/histogram.h"
[email protected]20305ec2011-01-21 04:55:5219#include "base/synchronization/lock.h"
[email protected]f214f8792011-01-01 02:17:0820#include "base/threading/platform_thread.h"
[email protected]1357c322010-12-30 22:18:5621#include "base/threading/thread_local.h"
[email protected]e182be02012-01-27 02:35:4422#include "chrome/common/metrics/metrics_service_base.h"
[email protected]897b26272010-06-11 02:23:4423
24// TODO(ananta)
25// Refactor more common code from chrome/browser/metrics/metrics_service.h into
26// the MetricsServiceBase class.
27class MetricsService : public MetricsServiceBase {
28 public:
29 static MetricsService* GetInstance();
30 // Start/stop the metrics recording and uploading machine. These should be
31 // used on startup and when the user clicks the checkbox in the prefs.
32 // StartRecordingOnly starts the metrics recording but not reporting, for use
33 // in tests only.
34 static void Start();
35 static void Stop();
36 // Set up client ID, session ID, etc.
37 void InitializeMetricsState();
38
[email protected]add184df2012-03-10 10:08:5739 // Retrieves a client ID to use to identify self to metrics server.
40 static const std::string& GetClientID();
41
[email protected]897b26272010-06-11 02:23:4442 private:
43 MetricsService();
44 virtual ~MetricsService();
45 // The MetricsService has a lifecycle that is stored as a state.
46 // See metrics_service.cc for description of this lifecycle.
47 enum State {
48 INITIALIZED, // Constructor was called.
49 ACTIVE, // Accumalating log data
50 STOPPED, // Service has stopped
51 };
52
53 // Maintain a map of histogram names to the sample stats we've sent.
[email protected]835d7c82010-10-14 04:38:3854 typedef std::map<std::string, base::Histogram::SampleSet> LoggedSampleMap;
[email protected]897b26272010-06-11 02:23:4455
56 // Sets and gets whether metrics recording is active.
57 // SetRecording(false) also forces a persistent save of logging state (if
58 // anything has been recorded, or transmitted).
59 void SetRecording(bool enabled);
60
61 // Enable/disable transmission of accumulated logs and crash reports (dumps).
62 // Return value "true" indicates setting was definitively set as requested).
63 // Return value of "false" indicates that the enable state is effectively
64 // stuck in the other logical setting.
65 // Google Update maintains the authoritative preference in the registry, so
66 // the caller *might* not be able to actually change the setting.
67 // It is always possible to set this to at least one value, which matches the
68 // current value reported by querying Google Update.
69 void SetReporting(bool enabled);
70
71 // If in_idle is true, sets idle_since_last_transmission to true.
72 // If in_idle is false and idle_since_last_transmission_ is true, sets
73 // idle_since_last_transmission to false and starts the timer (provided
74 // starting the timer is permitted).
75 void HandleIdleSinceLastTransmission(bool in_idle);
76
[email protected]897b26272010-06-11 02:23:4477 // ChromeFrame UMA data is uploaded when this timer proc gets invoked.
78 static void CALLBACK TransmissionTimerProc(HWND window, unsigned int message,
79 unsigned int event_id,
80 unsigned int time);
81
82 // Called to start recording user experience metrics.
83 // Constructs a new, empty current_log_.
84 void StartRecording();
85
86 // Called to stop recording user experience metrics.
87 void StopRecording(bool save_log);
88
89 // Takes whatever log should be uploaded next (according to the state_)
90 // and makes it the pending log. If pending_log_ is not NULL,
91 // MakePendingLog does nothing and returns.
92 void MakePendingLog();
93
94 // Determines from state_ and permissions set out by the server and by
95 // the user whether the pending_log_ should be sent or discarded. Called by
96 // TryToStartTransmission.
97 bool TransmissionPermitted() const;
98
99 bool recording_active() const {
100 return recording_active_;
101 }
102
103 bool reporting_active() const {
104 return reporting_active_;
105 }
106
[email protected]897b26272010-06-11 02:23:44107 // Upload pending data to the server by converting it to XML and compressing
108 // it. Returns true on success.
109 bool UploadData();
110
111 // Get the current version of the application as a string.
112 static std::string GetVersionString();
113
114 // Indicate whether recording and reporting are currently happening.
115 // These should not be set directly, but by calling SetRecording and
116 // SetReporting.
117 bool recording_active_;
118 bool reporting_active_;
119
120 // Coincides with the check box in options window that lets the user control
121 // whether to upload.
122 bool user_permits_upload_;
123
124 // The progession of states made by the browser are recorded in the following
125 // state.
126 State state_;
127
128 // The URL for the metrics server.
129 std::wstring server_url_;
130
131 // The identifier that's sent to the server with the log reports.
[email protected]add184df2012-03-10 10:08:57132 static std::string client_id_;
[email protected]897b26272010-06-11 02:23:44133
134 // A number that identifies the how many times the app has been launched.
135 int session_id_;
136
[email protected]13a8095f2011-04-20 18:17:04137 static base::LazyInstance<base::ThreadLocalPointer<MetricsService> >
138 g_metrics_instance_;
139
[email protected]f214f8792011-01-01 02:17:08140 base::PlatformThreadId thread_;
[email protected]897b26272010-06-11 02:23:44141
[email protected]f985b9c2010-06-18 17:05:10142 // Indicates if this is the first uma upload from this instance.
143 bool initial_uma_upload_;
144
145 // The transmission timer id returned by SetTimer
146 int transmission_timer_id_;
147
[email protected]21a45aa2010-12-04 01:00:18148 // Used to serialize the Start and Stop operations on the metrics service.
[email protected]20305ec2011-01-21 04:55:52149 static base::Lock metrics_service_lock_;
[email protected]21a45aa2010-12-04 01:00:18150
[email protected]897b26272010-06-11 02:23:44151 DISALLOW_COPY_AND_ASSIGN(MetricsService);
152};
153
[email protected]1357c322010-12-30 22:18:56154#endif // CHROME_FRAME_METRICS_SERVICE_H_