blob: af97b1c84638b7ab42f7b0ed620bae0c6e565f72 [file] [log] [blame]
[email protected]897b26272010-06-11 02:23:441// Copyright (c) 2010 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.
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
8#ifndef CHROME_FRAME_METRICS_METRICS_SERVICE_H_
9#define CHROME_FRAME_METRICS_METRICS_SERVICE_H_
10
11#include <map>
12#include <string>
13
14#include "base/basictypes.h"
15#include "base/histogram.h"
16#include "base/lazy_instance.h"
17#include "base/scoped_ptr.h"
18#include "base/thread_local.h"
19#include "chrome/common/metrics_helpers.h"
20
21// TODO(ananta)
22// Refactor more common code from chrome/browser/metrics/metrics_service.h into
23// the MetricsServiceBase class.
24class MetricsService : public MetricsServiceBase {
25 public:
26 static MetricsService* GetInstance();
27 // Start/stop the metrics recording and uploading machine. These should be
28 // used on startup and when the user clicks the checkbox in the prefs.
29 // StartRecordingOnly starts the metrics recording but not reporting, for use
30 // in tests only.
31 static void Start();
32 static void Stop();
33 // Set up client ID, session ID, etc.
34 void InitializeMetricsState();
35
36 private:
37 MetricsService();
38 virtual ~MetricsService();
39 // The MetricsService has a lifecycle that is stored as a state.
40 // See metrics_service.cc for description of this lifecycle.
41 enum State {
42 INITIALIZED, // Constructor was called.
43 ACTIVE, // Accumalating log data
44 STOPPED, // Service has stopped
45 };
46
47 // Maintain a map of histogram names to the sample stats we've sent.
48 typedef std::map<std::string, Histogram::SampleSet> LoggedSampleMap;
49
50 // Sets and gets whether metrics recording is active.
51 // SetRecording(false) also forces a persistent save of logging state (if
52 // anything has been recorded, or transmitted).
53 void SetRecording(bool enabled);
54
55 // Enable/disable transmission of accumulated logs and crash reports (dumps).
56 // Return value "true" indicates setting was definitively set as requested).
57 // Return value of "false" indicates that the enable state is effectively
58 // stuck in the other logical setting.
59 // Google Update maintains the authoritative preference in the registry, so
60 // the caller *might* not be able to actually change the setting.
61 // It is always possible to set this to at least one value, which matches the
62 // current value reported by querying Google Update.
63 void SetReporting(bool enabled);
64
65 // If in_idle is true, sets idle_since_last_transmission to true.
66 // If in_idle is false and idle_since_last_transmission_ is true, sets
67 // idle_since_last_transmission to false and starts the timer (provided
68 // starting the timer is permitted).
69 void HandleIdleSinceLastTransmission(bool in_idle);
70
71 // Generates a new client ID to use to identify self to metrics server.
72 static std::string GenerateClientID();
73
74 // ChromeFrame UMA data is uploaded when this timer proc gets invoked.
75 static void CALLBACK TransmissionTimerProc(HWND window, unsigned int message,
76 unsigned int event_id,
77 unsigned int time);
78
79 // Called to start recording user experience metrics.
80 // Constructs a new, empty current_log_.
81 void StartRecording();
82
83 // Called to stop recording user experience metrics.
84 void StopRecording(bool save_log);
85
86 // Takes whatever log should be uploaded next (according to the state_)
87 // and makes it the pending log. If pending_log_ is not NULL,
88 // MakePendingLog does nothing and returns.
89 void MakePendingLog();
90
91 // Determines from state_ and permissions set out by the server and by
92 // the user whether the pending_log_ should be sent or discarded. Called by
93 // TryToStartTransmission.
94 bool TransmissionPermitted() const;
95
96 bool recording_active() const {
97 return recording_active_;
98 }
99
100 bool reporting_active() const {
101 return reporting_active_;
102 }
103
104 // Convert pending_log_ to XML in pending_log_text_ for transmission.
105 std::string PrepareLogSubmissionString();
106
107 // 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.
132 std::string client_id_;
133
134 // A number that identifies the how many times the app has been launched.
135 int session_id_;
136
137 static base::LazyInstance<base::ThreadLocalPointer<MetricsService> >
138 g_metrics_instance_;
139
140 PlatformThreadId thread_;
141
[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]897b26272010-06-11 02:23:44148 DISALLOW_COPY_AND_ASSIGN(MetricsService);
149};
150
151#endif // CHROME_FRAME_METRICS_METRICS_SERVICE_H_
152