Changes for start_with_ext Telemetry test to work on Windows.
[chromium-blink-merge.git] / chrome / browser / chromeos / external_metrics.h
blobfd4873c601bb40fe8a5a79a303a9061d1d19e3b7
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 <string>
10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
14 namespace metrics {
15 class MetricSample;
16 } // namespace metrics
18 namespace chromeos {
20 // ExternalMetrics is a service that Chrome offers to Chrome OS to upload
21 // metrics to the UMA server on its behalf. Chrome periodically reads the
22 // content of a well-know file, and parses it into name-value pairs, each
23 // representing a Chrome OS metrics event. The events are logged using the
24 // normal UMA mechanism. The file is then truncated to zero size. Chrome uses
25 // flock() to synchronize accesses to the file.
26 class ExternalMetrics : public base::RefCountedThreadSafe<ExternalMetrics> {
27 public:
28 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 // blocking pool but are executed in the UI thread.
33 void Start();
35 // Creates an ExternalMetrics instance reading from |filename| for testing
36 // purpose.
37 static scoped_refptr<ExternalMetrics> CreateForTesting(
38 const std::string& filename);
40 private:
41 friend class base::RefCountedThreadSafe<ExternalMetrics>;
42 friend class ExternalMetricsTest;
44 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, CanReceiveHistogram);
45 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, HandleMissingFile);
46 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest,
47 IncorrectHistogramsAreDiscarded);
49 // The max length of a message (name-value pair, plus header)
50 static const int kMetricsMessageMaxLength = 1024; // be generous
52 ~ExternalMetrics();
54 // Passes an action event to the UMA service on the UI thread.
55 void RecordActionUI(const std::string& action_string);
57 // Passes an action event to the UMA service.
58 void RecordAction(const std::string& action_name);
60 // Records an external crash of the given string description to
61 // UMA service on the UI thread.
62 void RecordCrashUI(const std::string& crash_kind);
64 // Records an external crash of the given string description.
65 void RecordCrash(const std::string& crash_kind);
67 // Records an histogram. |sample| is expected to be an histogram.
68 void RecordHistogram(const metrics::MetricSample& sample);
70 // Records a sparse histogram. |sample| is expected to be a sparse histogram.
71 void RecordSparseHistogram(const metrics::MetricSample& sample);
73 // Records a linear histogram. |sample| is expected to be a linear histogram.
74 void RecordLinearHistogram(const metrics::MetricSample& sample);
76 // Collects external events from metrics log file. This is run at periodic
77 // intervals.
79 // Returns the number of events collected.
80 int CollectEvents();
82 // Calls CollectEvents and reschedules a future collection.
83 void CollectEventsAndReschedule();
85 // Schedules a metrics event collection in the future.
86 void ScheduleCollector();
88 // File used by libmetrics to send metrics to Chrome.
89 std::string uma_events_file_;
91 DISALLOW_COPY_AND_ASSIGN(ExternalMetrics);
94 } // namespace chromeos
96 #endif // CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_