1 // Copyright (c) 2012 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 CHROME_FRAME_METRICS_SERVICE_H_
9 #define CHROME_FRAME_METRICS_SERVICE_H_
14 #include "base/basictypes.h"
15 #include "base/lazy_instance.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/metrics/field_trial.h"
18 #include "base/metrics/histogram.h"
19 #include "base/synchronization/lock.h"
20 #include "base/threading/platform_thread.h"
21 #include "base/threading/thread_local.h"
22 #include "chrome/common/metrics/metrics_service_base.h"
25 // Refactor more common code from chrome/browser/metrics/metrics_service.h into
26 // the MetricsServiceBase class.
27 class MetricsService
: public MetricsServiceBase
{
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.
34 // Set up client ID, session ID, etc.
35 void InitializeMetricsState();
37 // Retrieves a client ID to use to identify self to metrics server.
38 static const std::string
& GetClientID();
42 virtual ~MetricsService();
43 // The MetricsService has a lifecycle that is stored as a state.
44 // See metrics_service.cc for description of this lifecycle.
46 INITIALIZED
, // Constructor was called.
47 ACTIVE
, // Accumalating log data
48 STOPPED
, // Service has stopped
51 // Sets and gets whether metrics recording is active.
52 // SetRecording(false) also forces a persistent save of logging state (if
53 // anything has been recorded, or transmitted).
54 void SetRecording(bool enabled
);
56 // Enable/disable transmission of accumulated logs and crash reports (dumps).
57 // Return value "true" indicates setting was definitively set as requested).
58 // Return value of "false" indicates that the enable state is effectively
59 // stuck in the other logical setting.
60 // Google Update maintains the authoritative preference in the registry, so
61 // the caller *might* not be able to actually change the setting.
62 // It is always possible to set this to at least one value, which matches the
63 // current value reported by querying Google Update.
64 void SetReporting(bool enabled
);
66 // If in_idle is true, sets idle_since_last_transmission to true.
67 // If in_idle is false and idle_since_last_transmission_ is true, sets
68 // idle_since_last_transmission to false and starts the timer (provided
69 // starting the timer is permitted).
70 void HandleIdleSinceLastTransmission(bool in_idle
);
72 // ChromeFrame UMA data is uploaded when this timer proc gets invoked.
73 static void CALLBACK
TransmissionTimerProc(HWND window
, unsigned int message
,
74 unsigned int event_id
,
77 // Called to start recording user experience metrics.
78 // Constructs a new, empty current_log_.
79 void StartRecording();
81 // Called to stop recording user experience metrics.
82 void StopRecording(bool save_log
);
84 // Takes whatever log should be uploaded next (according to the state_)
85 // and makes it the pending log. If pending_log_ is not NULL,
86 // MakePendingLog does nothing and returns.
87 void MakePendingLog();
89 // Determines from state_ and permissions set out by the server and by
90 // the user whether the pending_log_ should be sent or discarded. Called by
91 // TryToStartTransmission.
92 bool TransmissionPermitted() const;
94 bool recording_active() const {
95 return recording_active_
;
98 bool reporting_active() const {
99 return reporting_active_
;
102 // Upload pending data to the server by converting it to XML and compressing
103 // it. Returns true on success.
106 // Get the current version of the application as a string.
107 static std::string
GetVersionString();
109 // Indicate whether recording and reporting are currently happening.
110 // These should not be set directly, but by calling SetRecording and
112 bool recording_active_
;
113 bool reporting_active_
;
115 // Coincides with the check box in options window that lets the user control
116 // whether to upload.
117 bool user_permits_upload_
;
119 // The progession of states made by the browser are recorded in the following
123 // The URL for the metrics server.
124 std::wstring server_url_
;
126 // The identifier that's sent to the server with the log reports.
127 static std::string client_id_
;
129 // A number that identifies the how many times the app has been launched.
132 static base::LazyInstance
<base::ThreadLocalPointer
<MetricsService
> >
135 base::PlatformThreadId thread_
;
137 // Indicates if this is the first uma upload from this instance.
138 bool initial_uma_upload_
;
140 // The transmission timer id returned by SetTimer
141 int transmission_timer_id_
;
143 // Used to serialize the Start and Stop operations on the metrics service.
144 static base::Lock metrics_service_lock_
;
146 DISALLOW_COPY_AND_ASSIGN(MetricsService
);
149 #endif // CHROME_FRAME_METRICS_SERVICE_H_