Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: components/metrics/persisted_logs.h

Issue 2588873002: Delegate PersistedLogs metrics recording (Closed)
Patch Set: Fix tests Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_METRICS_PERSISTED_LOGS_H_ 5 #ifndef COMPONENTS_METRICS_PERSISTED_LOGS_H_
6 #define COMPONENTS_METRICS_PERSISTED_LOGS_H_ 6 #define COMPONENTS_METRICS_PERSISTED_LOGS_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <memory>
10 #include <string> 11 #include <string>
11 #include <vector> 12 #include <vector>
12 13
13 #include "base/logging.h" 14 #include "base/logging.h"
14 #include "base/macros.h" 15 #include "base/macros.h"
15 #include "base/values.h" 16 #include "base/values.h"
16 17
17 class PrefService; 18 class PrefService;
18 19
19 namespace metrics { 20 namespace metrics {
20 21
22 class PersistedLogsMetrics;
23
21 // Maintains a list of unsent logs that are written and restored from disk. 24 // Maintains a list of unsent logs that are written and restored from disk.
22 class PersistedLogs { 25 class PersistedLogs {
23 public: 26 public:
24 // Used to produce a histogram that keeps track of the status of recalling 27 // Used to produce a histogram that keeps track of the status of recalling
25 // persisted per logs. 28 // persisted per logs.
26 enum LogReadStatus { 29 enum LogReadStatus {
27 RECALL_SUCCESS, // We were able to correctly recall a persisted log. 30 RECALL_SUCCESS, // We were able to correctly recall a persisted log.
28 LIST_EMPTY, // Attempting to recall from an empty list. 31 LIST_EMPTY, // Attempting to recall from an empty list.
29 LIST_SIZE_MISSING, // Failed to recover list size using GetAsInteger(). 32 LIST_SIZE_MISSING, // Failed to recover list size using GetAsInteger().
30 LIST_SIZE_TOO_SMALL, // Too few elements in the list (less than 3). 33 LIST_SIZE_TOO_SMALL, // Too few elements in the list (less than 3).
(...skipping 11 matching lines...) Expand all
42 // Constructs a PersistedLogs that stores data in |local_state| under the 45 // Constructs a PersistedLogs that stores data in |local_state| under the
43 // preference |pref_name|. 46 // preference |pref_name|.
44 // Calling code is responsible for ensuring that the lifetime of |local_state| 47 // Calling code is responsible for ensuring that the lifetime of |local_state|
45 // is longer than the lifetime of PersistedLogs. 48 // is longer than the lifetime of PersistedLogs.
46 // 49 //
47 // When saving logs to disk, stores either the first |min_log_count| logs, or 50 // When saving logs to disk, stores either the first |min_log_count| logs, or
48 // at least |min_log_bytes| bytes of logs, whichever is greater. 51 // at least |min_log_bytes| bytes of logs, whichever is greater.
49 // 52 //
50 // If the optional |max_log_size| parameter is non-zero, all logs larger than 53 // If the optional |max_log_size| parameter is non-zero, all logs larger than
51 // that limit will be skipped when writing to disk. 54 // that limit will be skipped when writing to disk.
52 PersistedLogs(PrefService* local_state, 55 PersistedLogs(std::unique_ptr<PersistedLogsMetrics> metrics,
56 PrefService* local_state,
53 const char* pref_name, 57 const char* pref_name,
54 const char* outdated_pref_name, 58 const char* outdated_pref_name,
55 size_t min_log_count, 59 size_t min_log_count,
56 size_t min_log_bytes, 60 size_t min_log_bytes,
57 size_t max_log_size); 61 size_t max_log_size);
58 ~PersistedLogs(); 62 ~PersistedLogs();
59 63
60 // Write list to storage. 64 // Write list to storage.
61 void SerializeLogs() const; 65 void SerializeLogs() const;
62 66
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 private: 107 private:
104 // Writes the list to the ListValue. 108 // Writes the list to the ListValue.
105 void WriteLogsToPrefList(base::ListValue* list) const; 109 void WriteLogsToPrefList(base::ListValue* list) const;
106 110
107 // Reads the list from the ListValue. 111 // Reads the list from the ListValue.
108 LogReadStatus ReadLogsFromPrefList(const base::ListValue& list); 112 LogReadStatus ReadLogsFromPrefList(const base::ListValue& list);
109 113
110 // Reads the list from the ListValue in the old Log-hash pair format. 114 // Reads the list from the ListValue in the old Log-hash pair format.
111 LogReadStatus ReadLogsFromOldFormatPrefList(const base::ListValue& list); 115 LogReadStatus ReadLogsFromOldFormatPrefList(const base::ListValue& list);
112 116
117 // An object for recording UMA metrics.
118 std::unique_ptr<PersistedLogsMetrics> metrics_;
119
113 // A weak pointer to the PrefService object to read and write the preference 120 // A weak pointer to the PrefService object to read and write the preference
114 // from. Calling code should ensure this object continues to exist for the 121 // from. Calling code should ensure this object continues to exist for the
115 // lifetime of the PersistedLogs object. 122 // lifetime of the PersistedLogs object.
116 PrefService* local_state_; 123 PrefService* local_state_;
117 124
118 // The name of the preference to serialize logs to/from. 125 // The name of the preference to serialize logs to/from.
119 const char* pref_name_; 126 const char* pref_name_;
120 127
121 // The name of the preference to serialize logs to/from which may contain log 128 // The name of the preference to serialize logs to/from which may contain log
122 // in the old formatting. 129 // in the old formatting.
123 const char* outdated_pref_name_; 130 const char* outdated_pref_name_;
124 131
125 // We will keep at least this |min_log_count_| logs or |min_log_bytes_| bytes 132 // We will keep at least this |min_log_count_| logs or |min_log_bytes_| bytes
126 // of logs, whichever is greater, when writing to disk. These apply after 133 // of logs, whichever is greater, when writing to disk. These apply after
127 // skipping logs greater than |max_log_size_|. 134 // skipping logs greater than |max_log_size_|.
128 const size_t min_log_count_; 135 const size_t min_log_count_;
129 const size_t min_log_bytes_; 136 const size_t min_log_bytes_;
130 137
131 // Logs greater than this size will not be written to disk. 138 // Logs greater than this size will not be written to disk.
132 const size_t max_log_size_; 139 const size_t max_log_size_;
133 140
134 struct LogInfo { 141 struct LogInfo {
135 // Initializes the members based on uncompressed |log_data| and 142 // Initializes the members based on uncompressed |log_data| and
136 // |log_timestamp|. 143 // |log_timestamp|.
137 void Init(const std::string& log_data, const std::string& log_timestamp); 144 // |metrics| is the parent's metrics_ object, and should not be held.
145 void Init(PersistedLogsMetrics* metrics,
146 const std::string& log_data,
147 const std::string& log_timestamp);
138 148
139 // Compressed log data - a serialized protobuf that's been gzipped. 149 // Compressed log data - a serialized protobuf that's been gzipped.
140 std::string compressed_log_data; 150 std::string compressed_log_data;
141 151
142 // The SHA1 hash of log, stored to catch errors from memory corruption. 152 // The SHA1 hash of log, stored to catch errors from memory corruption.
143 std::string hash; 153 std::string hash;
144 154
145 // The timestamp of when the log was created as a time_t value. 155 // The timestamp of when the log was created as a time_t value.
146 std::string timestamp; 156 std::string timestamp;
147 }; 157 };
148 // A list of all of the stored logs, stored with SHA1 hashes to check for 158 // A list of all of the stored logs, stored with SHA1 hashes to check for
149 // corruption while they are stored in memory. 159 // corruption while they are stored in memory.
150 std::vector<LogInfo> list_; 160 std::vector<LogInfo> list_;
151 161
152 // The index and type of the log staged for upload. If nothing has been 162 // The index and type of the log staged for upload. If nothing has been
153 // staged, the index will be -1. 163 // staged, the index will be -1.
154 int staged_log_index_; 164 int staged_log_index_;
155 165
156 DISALLOW_COPY_AND_ASSIGN(PersistedLogs); 166 DISALLOW_COPY_AND_ASSIGN(PersistedLogs);
157 }; 167 };
158 168
159 } // namespace metrics 169 } // namespace metrics
160 170
161 #endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_ 171 #endif // COMPONENTS_METRICS_PERSISTED_LOGS_H_
OLDNEW
« no previous file with comments | « components/metrics/metrics_log_manager_unittest.cc ('k') | components/metrics/persisted_logs.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698