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_
10 #include "base/compiler_specific.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
17 } // namespace metrics
21 // ExternalMetrics is a service that Chrome offers to Chrome OS to upload
22 // metrics to the UMA server on its behalf. Chrome periodically reads the
23 // content of a well-know file, and parses it into name-value pairs, each
24 // representing a Chrome OS metrics event. The events are logged using the
25 // normal UMA mechanism. The file is then truncated to zero size. Chrome uses
26 // flock() to synchronize accesses to the file.
27 class ExternalMetrics
: public base::RefCountedThreadSafe
<ExternalMetrics
> {
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.
36 // Creates an ExternalMetrics instance reading from |filename| for testing
38 static scoped_refptr
<ExternalMetrics
> CreateForTesting(
39 const std::string
& filename
);
42 friend class base::RefCountedThreadSafe
<ExternalMetrics
>;
43 friend class ExternalMetricsTest
;
45 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest
, CanReceiveHistogram
);
46 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest
, HandleMissingFile
);
47 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest
,
48 IncorrectHistogramsAreDiscarded
);
50 // The max length of a message (name-value pair, plus header)
51 static const int kMetricsMessageMaxLength
= 1024; // be generous
55 // Passes an action event to the UMA service on the UI thread.
56 void RecordActionUI(std::string action_string
);
58 // Passes an action event to the UMA service.
59 void RecordAction(const std::string
& action_name
);
61 // Records an external crash of the given string description to
62 // UMA service on the UI thread.
63 void RecordCrashUI(const std::string
& crash_kind
);
65 // Records an external crash of the given string description.
66 void RecordCrash(const std::string
& crash_kind
);
68 // Records an histogram. |sample| is expected to be an histogram.
69 void RecordHistogram(const metrics::MetricSample
& sample
);
71 // Records a sparse histogram. |sample| is expected to be a sparse histogram.
72 void RecordSparseHistogram(const metrics::MetricSample
& sample
);
74 // Records a linear histogram. |sample| is expected to be a linear histogram.
75 void RecordLinearHistogram(const metrics::MetricSample
& sample
);
77 // Collects external events from metrics log file. This is run at periodic
80 // Returns the number of events collected.
83 // Calls CollectEvents and reschedules a future collection.
84 void CollectEventsAndReschedule();
86 // Schedules a metrics event collection in the future.
87 void ScheduleCollector();
89 // Calls setup methods for Chrome OS field trials that need to be initialized
90 // based on data from the file system. They are setup here so that we can
91 // make absolutely sure that they are setup before we gather UMA statistics
93 void SetupFieldTrialsOnFileThread();
95 // Set containing known user actions.
96 base::hash_set
<std::string
> valid_user_actions_
;
98 // File used by libmetrics to send metrics to Chrome.
99 std::string uma_events_file_
;
101 DISALLOW_COPY_AND_ASSIGN(ExternalMetrics
);
104 } // namespace chromeos
106 #endif // CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_