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