blob: 821a53751817037dee48e51d842f123a88eac234 [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]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]e182be02012-01-27 02:35:4421#include "chrome/common/metrics/metrics_service_base.h"
[email protected]897b26272010-06-11 02:23:4422
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.
[email protected]897b26272010-06-11 02:23:4431 static void Start();
32 static void Stop();
33 // Set up client ID, session ID, etc.
34 void InitializeMetricsState();
35
[email protected]add184df2012-03-10 10:08:5736 // Retrieves a client ID to use to identify self to metrics server.
37 static const std::string& GetClientID();
38
[email protected]897b26272010-06-11 02:23:4439 private:
40 MetricsService();
41 virtual ~MetricsService();
42 // The MetricsService has a lifecycle that is stored as a state.
43 // See metrics_service.cc for description of this lifecycle.
44 enum State {
45 INITIALIZED, // Constructor was called.
46 ACTIVE, // Accumalating log data
47 STOPPED, // Service has stopped
48 };
49
[email protected]897b26272010-06-11 02:23:4450 // 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
[email protected]897b26272010-06-11 02:23:4471 // ChromeFrame UMA data is uploaded when this timer proc gets invoked.
72 static void CALLBACK TransmissionTimerProc(HWND window, unsigned int message,
73 unsigned int event_id,
74 unsigned int time);
75
76 // Called to start recording user experience metrics.
77 // Constructs a new, empty current_log_.
78 void StartRecording();
79
80 // Called to stop recording user experience metrics.
81 void StopRecording(bool save_log);
82
83 // Takes whatever log should be uploaded next (according to the state_)
84 // and makes it the pending log. If pending_log_ is not NULL,
85 // MakePendingLog does nothing and returns.
86 void MakePendingLog();
87
88 // Determines from state_ and permissions set out by the server and by
89 // the user whether the pending_log_ should be sent or discarded. Called by
90 // TryToStartTransmission.
91 bool TransmissionPermitted() const;
92
93 bool recording_active() const {
94 return recording_active_;
95 }
96
97 bool reporting_active() const {
98 return reporting_active_;
99 }
100
[email protected]897b26272010-06-11 02:23:44101 // Upload pending data to the server by converting it to XML and compressing
102 // it. Returns true on success.
103 bool UploadData();
104
105 // Get the current version of the application as a string.
106 static std::string GetVersionString();
107
108 // Indicate whether recording and reporting are currently happening.
109 // These should not be set directly, but by calling SetRecording and
110 // SetReporting.
111 bool recording_active_;
112 bool reporting_active_;
113
114 // Coincides with the check box in options window that lets the user control
115 // whether to upload.
116 bool user_permits_upload_;
117
118 // The progession of states made by the browser are recorded in the following
119 // state.
120 State state_;
121
122 // The URL for the metrics server.
123 std::wstring server_url_;
124
125 // The identifier that's sent to the server with the log reports.
[email protected]add184df2012-03-10 10:08:57126 static std::string client_id_;
[email protected]897b26272010-06-11 02:23:44127
128 // A number that identifies the how many times the app has been launched.
129 int session_id_;
130
[email protected]13a8095f2011-04-20 18:17:04131 static base::LazyInstance<base::ThreadLocalPointer<MetricsService> >
132 g_metrics_instance_;
133
[email protected]f214f8792011-01-01 02:17:08134 base::PlatformThreadId thread_;
[email protected]897b26272010-06-11 02:23:44135
[email protected]f985b9c2010-06-18 17:05:10136 // Indicates if this is the first uma upload from this instance.
137 bool initial_uma_upload_;
138
139 // The transmission timer id returned by SetTimer
140 int transmission_timer_id_;
141
[email protected]21a45aa2010-12-04 01:00:18142 // Used to serialize the Start and Stop operations on the metrics service.
[email protected]20305ec2011-01-21 04:55:52143 static base::Lock metrics_service_lock_;
[email protected]21a45aa2010-12-04 01:00:18144
[email protected]897b26272010-06-11 02:23:44145 DISALLOW_COPY_AND_ASSIGN(MetricsService);
146};
147
[email protected]1357c322010-12-30 22:18:56148#endif // CHROME_FRAME_METRICS_SERVICE_H_