Home | History | Annotate | Download | only in chrome_frame
      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.
      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_SERVICE_H_
      9 #define CHROME_FRAME_METRICS_SERVICE_H_
     10 
     11 #include <map>
     12 #include <string>
     13 
     14 #include "base/basictypes.h"
     15 #include "base/lazy_instance.h"
     16 #include "base/memory/scoped_ptr.h"
     17 #include "base/metrics/histogram.h"
     18 #include "base/synchronization/lock.h"
     19 #include "base/threading/platform_thread.h"
     20 #include "base/threading/thread_local.h"
     21 #include "chrome/common/metrics/metrics_service_base.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   static void Start();
     32   static void Stop();
     33   // Set up client ID, session ID, etc.
     34   void InitializeMetricsState();
     35 
     36   // Retrieves a client ID to use to identify self to metrics server.
     37   static const std::string& GetClientID();
     38 
     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 
     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   // 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 
    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.
    126   static std::string client_id_;
    127 
    128   // A number that identifies the how many times the app has been launched.
    129   int session_id_;
    130 
    131   static base::LazyInstance<base::ThreadLocalPointer<MetricsService> >
    132       g_metrics_instance_;
    133 
    134   base::PlatformThreadId thread_;
    135 
    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 
    142   // Used to serialize the Start and Stop operations on the metrics service.
    143   static base::Lock metrics_service_lock_;
    144 
    145   DISALLOW_COPY_AND_ASSIGN(MetricsService);
    146 };
    147 
    148 #endif  // CHROME_FRAME_METRICS_SERVICE_H_
    149