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