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.
5 #ifndef CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
6 #define CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/file_path.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/hash_tables.h"
13 #include "base/memory/ref_counted.h"
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.
23 class ExternalMetrics
: public base::RefCountedThreadSafe
<ExternalMetrics
> {
24 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest
, ParseExternalMetricsFile
);
25 friend class base::RefCountedThreadSafe
<ExternalMetrics
>;
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.
36 // There is one function with this type for each action.
37 typedef void (*RecordFunctionType
)();
39 typedef void (*RecorderType
)(const char*, const char*); // For testing only.
41 // The max length of a message (name-value pair, plus header)
42 static const int kMetricsMessageMaxLength
= 1024; // be generous
46 // Passes an action event to the UMA service on the UI thread.
47 void RecordActionUI(std::string action_string
);
49 // Passes an action event to the UMA service.
50 void RecordAction(const char* action_name
);
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
);
56 // Records an external crash of the given string description.
57 void RecordCrash(const std::string
& crash_kind
);
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
);
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
);
67 // Collects external events from metrics log file. This is run at periodic
71 // Calls CollectEvents and reschedules a future collection.
72 void CollectEventsAndReschedule();
74 // Schedules a metrics event collection in the future.
75 void ScheduleCollector();
77 // Maps histogram or action names to recorder structs.
78 base::hash_map
<std::string
, RecordFunctionType
> action_recorders_
;
80 // Set containing known user actions.
81 base::hash_set
<std::string
> valid_user_actions_
;
83 // Used for testing only.
84 RecorderType test_recorder_
;
86 DISALLOW_COPY_AND_ASSIGN(ExternalMetrics
);
89 } // namespace chromeos
91 #endif // CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_