blob: 7b5f84fc00b2fd343487084779aad8b3b949720a [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 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]835d7c82010-10-14 04:38:3817#include "base/metrics/histogram.h"
[email protected]20305ec2011-01-21 04:55:5218#include "base/synchronization/lock.h"
[email protected]f214f8792011-01-01 02:17:0819#include "base/threading/platform_thread.h"
[email protected]1357c322010-12-30 22:18:5620#include "base/threading/thread_local.h"
[email protected]897b26272010-06-11 02:23:4421#include "chrome/common/metrics_helpers.h"
22
23// TODO(ananta)
24// Refactor more common code from chrome/browser/metrics/metrics_service.h into
25// the MetricsServiceBase class.
26class MetricsService : public MetricsServiceBase {
27 public:
28 static MetricsService* GetInstance();
29 // Start/stop the metrics recording and uploading machine. These should be
30 // used on startup and when the user clicks the checkbox in the prefs.
31 // StartRecordingOnly starts the metrics recording but not reporting, for use
32 // in tests only.
33 static void Start();
34 static void Stop();
35 // Set up client ID, session ID, etc.
36 void InitializeMetricsState();
37
38 private:
39 MetricsService();
40 virtual ~MetricsService();
41 // The MetricsService has a lifecycle that is stored as a state.
42 // See metrics_service.cc for description of this lifecycle.
43 enum State {
44 INITIALIZED, // Constructor was called.
45 ACTIVE, // Accumalating log data
46 STOPPED, // Service has stopped
47 };
48
49 // Maintain a map of histogram names to the sample stats we've sent.
[email protected]835d7c82010-10-14 04:38:3850 typedef std::map<std::string, base::Histogram::SampleSet> LoggedSampleMap;
[email protected]897b26272010-06-11 02:23:4451
52 // Sets and gets whether metrics recording is active.
53 // SetRecording(false) also forces a persistent save of logging state (if
54 // anything has been recorded, or transmitted).
55 void SetRecording(bool enabled);
56
57 // Enable/disable transmission of accumulated logs and crash reports (dumps).
58 // Return value "true" indicates setting was definitively set as requested).
59 // Return value of "false" indicates that the enable state is effectively
60 // stuck in the other logical setting.
61 // Google Update maintains the authoritative preference in the registry, so
62 // the caller *might* not be able to actually change the setting.
63 // It is always possible to set this to at least one value, which matches the
64 // current value reported by querying Google Update.
65 void SetReporting(bool enabled);
66
67 // If in_idle is true, sets idle_since_last_transmission to true.
68 // If in_idle is false and idle_since_last_transmission_ is true, sets
69 // idle_since_last_transmission to false and starts the timer (provided
70 // starting the timer is permitted).
71 void HandleIdleSinceLastTransmission(bool in_idle);
72
73 // Generates a new client ID to use to identify self to metrics server.
74 static std::string GenerateClientID();
75
76 // ChromeFrame UMA data is uploaded when this timer proc gets invoked.
77 static void CALLBACK TransmissionTimerProc(HWND window, unsigned int message,
78 unsigned int event_id,
79 unsigned int time);
80
81 // Called to start recording user experience metrics.
82 // Constructs a new, empty current_log_.
83 void StartRecording();
84
85 // Called to stop recording user experience metrics.
86 void StopRecording(bool save_log);
87
88 // Takes whatever log should be uploaded next (according to the state_)
89 // and makes it the pending log. If pending_log_ is not NULL,
90 // MakePendingLog does nothing and returns.
91 void MakePendingLog();
92
93 // Determines from state_ and permissions set out by the server and by
94 // the user whether the pending_log_ should be sent or discarded. Called by
95 // TryToStartTransmission.
96 bool TransmissionPermitted() const;
97
98 bool recording_active() const {
99 return recording_active_;
100 }
101
102 bool reporting_active() const {
103 return reporting_active_;
104 }
105
106 // Convert pending_log_ to XML in pending_log_text_ for transmission.
107 std::string PrepareLogSubmissionString();
108
109 // Upload pending data to the server by converting it to XML and compressing
110 // it. Returns true on success.
111 bool UploadData();
112
113 // Get the current version of the application as a string.
114 static std::string GetVersionString();
115
116 // Indicate whether recording and reporting are currently happening.
117 // These should not be set directly, but by calling SetRecording and
118 // SetReporting.
119 bool recording_active_;
120 bool reporting_active_;
121
122 // Coincides with the check box in options window that lets the user control
123 // whether to upload.
124 bool user_permits_upload_;
125
126 // The progession of states made by the browser are recorded in the following
127 // state.
128 State state_;
129
130 // The URL for the metrics server.
131 std::wstring server_url_;
132
133 // The identifier that's sent to the server with the log reports.
134 std::string client_id_;
135
136 // A number that identifies the how many times the app has been launched.
137 int session_id_;
138
[email protected]13a8095f2011-04-20 18:17:04139 static base::LazyInstance<base::ThreadLocalPointer<MetricsService> >
140 g_metrics_instance_;
141
[email protected]f214f8792011-01-01 02:17:08142 base::PlatformThreadId thread_;
[email protected]897b26272010-06-11 02:23:44143
[email protected]f985b9c2010-06-18 17:05:10144 // Indicates if this is the first uma upload from this instance.
145 bool initial_uma_upload_;
146
147 // The transmission timer id returned by SetTimer
148 int transmission_timer_id_;
149
[email protected]21a45aa2010-12-04 01:00:18150 // Used to serialize the Start and Stop operations on the metrics service.
[email protected]20305ec2011-01-21 04:55:52151 static base::Lock metrics_service_lock_;
[email protected]21a45aa2010-12-04 01:00:18152
[email protected]897b26272010-06-11 02:23:44153 DISALLOW_COPY_AND_ASSIGN(MetricsService);
154};
155
[email protected]1357c322010-12-30 22:18:56156#endif // CHROME_FRAME_METRICS_SERVICE_H_