[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 1 | // 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 | |
[email protected] | 1357c32 | 2010-12-30 22:18:56 | [diff] [blame] | 8 | #ifndef CHROME_FRAME_METRICS_SERVICE_H_ |
| 9 | #define CHROME_FRAME_METRICS_SERVICE_H_ |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 10 | |
| 11 | #include <map> |
| 12 | #include <string> |
| 13 | |
| 14 | #include "base/basictypes.h" |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 15 | #include "base/lazy_instance.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 16 | #include "base/metrics/histogram.h" |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 17 | #include "base/scoped_ptr.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 18 | #include "base/synchronization/lock.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 19 | #include "base/threading/platform_thread.h" |
[email protected] | 1357c32 | 2010-12-30 22:18:56 | [diff] [blame] | 20 | #include "base/threading/thread_local.h" |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 21 | #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. |
| 26 | class 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: |
[email protected] | 606e5bf | 2010-09-16 00:41:19 | [diff] [blame] | 39 | // To enable the default traits object to create an instance of this class. |
| 40 | friend struct base::DefaultLazyInstanceTraits<MetricsService>; |
| 41 | |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 42 | MetricsService(); |
| 43 | virtual ~MetricsService(); |
| 44 | // The MetricsService has a lifecycle that is stored as a state. |
| 45 | // See metrics_service.cc for description of this lifecycle. |
| 46 | enum State { |
| 47 | INITIALIZED, // Constructor was called. |
| 48 | ACTIVE, // Accumalating log data |
| 49 | STOPPED, // Service has stopped |
| 50 | }; |
| 51 | |
| 52 | // Maintain a map of histogram names to the sample stats we've sent. |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 53 | typedef std::map<std::string, base::Histogram::SampleSet> LoggedSampleMap; |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 54 | |
| 55 | // Sets and gets whether metrics recording is active. |
| 56 | // SetRecording(false) also forces a persistent save of logging state (if |
| 57 | // anything has been recorded, or transmitted). |
| 58 | void SetRecording(bool enabled); |
| 59 | |
| 60 | // Enable/disable transmission of accumulated logs and crash reports (dumps). |
| 61 | // Return value "true" indicates setting was definitively set as requested). |
| 62 | // Return value of "false" indicates that the enable state is effectively |
| 63 | // stuck in the other logical setting. |
| 64 | // Google Update maintains the authoritative preference in the registry, so |
| 65 | // the caller *might* not be able to actually change the setting. |
| 66 | // It is always possible to set this to at least one value, which matches the |
| 67 | // current value reported by querying Google Update. |
| 68 | void SetReporting(bool enabled); |
| 69 | |
| 70 | // If in_idle is true, sets idle_since_last_transmission to true. |
| 71 | // If in_idle is false and idle_since_last_transmission_ is true, sets |
| 72 | // idle_since_last_transmission to false and starts the timer (provided |
| 73 | // starting the timer is permitted). |
| 74 | void HandleIdleSinceLastTransmission(bool in_idle); |
| 75 | |
| 76 | // Generates a new client ID to use to identify self to metrics server. |
| 77 | static std::string GenerateClientID(); |
| 78 | |
| 79 | // ChromeFrame UMA data is uploaded when this timer proc gets invoked. |
| 80 | static void CALLBACK TransmissionTimerProc(HWND window, unsigned int message, |
| 81 | unsigned int event_id, |
| 82 | unsigned int time); |
| 83 | |
| 84 | // Called to start recording user experience metrics. |
| 85 | // Constructs a new, empty current_log_. |
| 86 | void StartRecording(); |
| 87 | |
| 88 | // Called to stop recording user experience metrics. |
| 89 | void StopRecording(bool save_log); |
| 90 | |
| 91 | // Takes whatever log should be uploaded next (according to the state_) |
| 92 | // and makes it the pending log. If pending_log_ is not NULL, |
| 93 | // MakePendingLog does nothing and returns. |
| 94 | void MakePendingLog(); |
| 95 | |
| 96 | // Determines from state_ and permissions set out by the server and by |
| 97 | // the user whether the pending_log_ should be sent or discarded. Called by |
| 98 | // TryToStartTransmission. |
| 99 | bool TransmissionPermitted() const; |
| 100 | |
| 101 | bool recording_active() const { |
| 102 | return recording_active_; |
| 103 | } |
| 104 | |
| 105 | bool reporting_active() const { |
| 106 | return reporting_active_; |
| 107 | } |
| 108 | |
| 109 | // Convert pending_log_ to XML in pending_log_text_ for transmission. |
| 110 | std::string PrepareLogSubmissionString(); |
| 111 | |
| 112 | // Upload pending data to the server by converting it to XML and compressing |
| 113 | // it. Returns true on success. |
| 114 | bool UploadData(); |
| 115 | |
| 116 | // Get the current version of the application as a string. |
| 117 | static std::string GetVersionString(); |
| 118 | |
| 119 | // Indicate whether recording and reporting are currently happening. |
| 120 | // These should not be set directly, but by calling SetRecording and |
| 121 | // SetReporting. |
| 122 | bool recording_active_; |
| 123 | bool reporting_active_; |
| 124 | |
| 125 | // Coincides with the check box in options window that lets the user control |
| 126 | // whether to upload. |
| 127 | bool user_permits_upload_; |
| 128 | |
| 129 | // The progession of states made by the browser are recorded in the following |
| 130 | // state. |
| 131 | State state_; |
| 132 | |
| 133 | // The URL for the metrics server. |
| 134 | std::wstring server_url_; |
| 135 | |
| 136 | // The identifier that's sent to the server with the log reports. |
| 137 | std::string client_id_; |
| 138 | |
| 139 | // A number that identifies the how many times the app has been launched. |
| 140 | int session_id_; |
| 141 | |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 142 | base::PlatformThreadId thread_; |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 143 | |
[email protected] | f985b9c | 2010-06-18 17:05:10 | [diff] [blame] | 144 | // 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] | 21a45aa | 2010-12-04 01:00:18 | [diff] [blame] | 150 | // Used to serialize the Start and Stop operations on the metrics service. |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame^] | 151 | static base::Lock metrics_service_lock_; |
[email protected] | 21a45aa | 2010-12-04 01:00:18 | [diff] [blame] | 152 | |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(MetricsService); |
| 154 | }; |
| 155 | |
[email protected] | 1357c32 | 2010-12-30 22:18:56 | [diff] [blame] | 156 | #endif // CHROME_FRAME_METRICS_SERVICE_H_ |