Home | History | Annotate | Download | only in chromeos
      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 #ifndef CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
      6 #define CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/containers/hash_tables.h"
     13 #include "base/files/file_path.h"
     14 #include "base/gtest_prod_util.h"
     15 #include "base/memory/ref_counted.h"
     16 
     17 namespace chromeos {
     18 
     19 // ExternalMetrics is a service that Chrome offers to Chrome OS to upload
     20 // metrics to the UMA server on its behalf.  Chrome periodically reads the
     21 // content of a well-know file, and parses it into name-value pairs, each
     22 // representing a Chrome OS metrics event. The events are logged using the
     23 // normal UMA mechanism. The file is then truncated to zero size. Chrome uses
     24 // flock() to synchronize accesses to the file.
     25 class ExternalMetrics : public base::RefCountedThreadSafe<ExternalMetrics> {
     26  public:
     27   ExternalMetrics();
     28 
     29   // Begins the external data collection.  This service is started and stopped
     30   // by the chrome metrics service.  Calls to RecordAction originate in the
     31   // File thread but are executed in the UI thread.
     32   void Start();
     33 
     34  private:
     35   friend class base::RefCountedThreadSafe<ExternalMetrics>;
     36   FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, ParseExternalMetricsFile);
     37 
     38   // There is one function with this type for each action.
     39   typedef void (*RecordFunctionType)();
     40 
     41   typedef void (*RecorderType)(const char*, const char*);  // For testing only.
     42 
     43   // The max length of a message (name-value pair, plus header)
     44   static const int kMetricsMessageMaxLength = 1024;  // be generous
     45 
     46   ~ExternalMetrics();
     47 
     48   // Passes an action event to the UMA service on the UI thread.
     49   void RecordActionUI(std::string action_string);
     50 
     51   // Passes an action event to the UMA service.
     52   void RecordAction(const char* action_name);
     53 
     54   // Records an external crash of the given string description to
     55   // UMA service on the UI thread.
     56   void RecordCrashUI(const std::string& crash_kind);
     57 
     58   // Records an external crash of the given string description.
     59   void RecordCrash(const std::string& crash_kind);
     60 
     61   // Passes an histogram event to the UMA service.  |histogram_data| is in the
     62   // form <histogram-name> <sample> <min> <max> <buckets_count>.
     63   void RecordHistogram(const char* histogram_data);
     64 
     65   // Passes a linear histogram event to the UMA service.  |histogram_data| is
     66   // in the form <histogram-name> <sample> <max>.
     67   void RecordLinearHistogram(const char* histogram_data);
     68 
     69   // Passes a sparse histogram event to the UMA service.  |histogram_data| is
     70   // in the form <histogram-name> <sample>.
     71   void RecordSparseHistogram(const char* histogram_data);
     72 
     73   // Collects external events from metrics log file.  This is run at periodic
     74   // intervals.
     75   void CollectEvents();
     76 
     77   // Calls CollectEvents and reschedules a future collection.
     78   void CollectEventsAndReschedule();
     79 
     80   // Schedules a metrics event collection in the future.
     81   void ScheduleCollector();
     82 
     83   // Calls setup methods for Chrome OS field trials that need to be initialized
     84   // based on data from the file system.  They are setup here so that we can
     85   // make absolutely sure that they are setup before we gather UMA statistics
     86   // from ChromeOS.
     87   void SetupFieldTrialsOnFileThread();
     88 
     89   // Maps histogram or action names to recorder structs.
     90   base::hash_map<std::string, RecordFunctionType> action_recorders_;
     91 
     92   // Set containing known user actions.
     93   base::hash_set<std::string> valid_user_actions_;
     94 
     95   // Used for testing only.
     96   RecorderType test_recorder_;
     97   base::FilePath test_path_;
     98 
     99   DISALLOW_COPY_AND_ASSIGN(ExternalMetrics);
    100 };
    101 
    102 }  // namespace chromeos
    103 
    104 #endif  // CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
    105