[email protected] | 2e4cd1a | 2012-01-12 08:51:03 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 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] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/scoped_ptr.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 17 | #include "base/metrics/histogram.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] | e182be0 | 2012-01-27 02:35:44 | [diff] [blame] | 21 | #include "chrome/common/metrics/metrics_service_base.h" |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 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. |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 31 | static void Start(); |
| 32 | static void Stop(); |
| 33 | // Set up client ID, session ID, etc. |
| 34 | void InitializeMetricsState(); |
| 35 | |
[email protected] | add184df | 2012-03-10 10:08:57 | [diff] [blame] | 36 | // Retrieves a client ID to use to identify self to metrics server. |
| 37 | static const std::string& GetClientID(); |
| 38 | |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 39 | 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] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 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 | |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 71 | // 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] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 101 | // 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] | add184df | 2012-03-10 10:08:57 | [diff] [blame] | 126 | static std::string client_id_; |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 127 | |
| 128 | // A number that identifies the how many times the app has been launched. |
| 129 | int session_id_; |
| 130 | |
[email protected] | 13a8095f | 2011-04-20 18:17:04 | [diff] [blame] | 131 | static base::LazyInstance<base::ThreadLocalPointer<MetricsService> > |
| 132 | g_metrics_instance_; |
| 133 | |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 134 | base::PlatformThreadId thread_; |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 135 | |
[email protected] | f985b9c | 2010-06-18 17:05:10 | [diff] [blame] | 136 | // 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] | 21a45aa | 2010-12-04 01:00:18 | [diff] [blame] | 142 | // Used to serialize the Start and Stop operations on the metrics service. |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 143 | static base::Lock metrics_service_lock_; |
[email protected] | 21a45aa | 2010-12-04 01:00:18 | [diff] [blame] | 144 | |
[email protected] | 897b2627 | 2010-06-11 02:23:44 | [diff] [blame] | 145 | DISALLOW_COPY_AND_ASSIGN(MetricsService); |
| 146 | }; |
| 147 | |
[email protected] | 1357c32 | 2010-12-30 22:18:56 | [diff] [blame] | 148 | #endif // CHROME_FRAME_METRICS_SERVICE_H_ |