OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 5 #ifndef CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
6 #define CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 6 #define CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 class MetricsLogBase; | 14 class MetricsLogBase; |
15 | 15 |
16 // Manages all the log objects used by a MetricsService implementation. Keeps | 16 // Manages all the log objects used by a MetricsService implementation. Keeps |
17 // track of both an in progress log and a log that is staged for uploading as | 17 // track of both an in progress log and a log that is staged for uploading as |
18 // text, as well as saving logs to, and loading logs from, persistent storage. | 18 // text, as well as saving logs to, and loading logs from, persistent storage. |
19 class MetricsLogManager { | 19 class MetricsLogManager { |
20 public: | 20 public: |
21 MetricsLogManager(); | 21 MetricsLogManager(); |
22 ~MetricsLogManager(); | 22 ~MetricsLogManager(); |
23 | 23 |
24 enum LogType { | 24 enum LogType { |
25 INITIAL_LOG, // The first log of a session. | 25 INITIAL_LOG, // The first log of a session. |
26 ONGOING_LOG, // Subsequent logs in a session. | 26 ONGOING_LOG, // Subsequent logs in a session. |
27 NO_LOG, // Placeholder value for when there is no log. | 27 NO_LOG, // Placeholder value for when there is no log. |
28 }; | 28 }; |
29 | 29 |
30 class SerializedLog { | |
31 public: | |
32 SerializedLog(); | |
33 ~SerializedLog(); | |
34 | |
35 const std::string& log_text() const { return log_text_; } | |
36 const std::string& log_hash() const { return log_hash_; } | |
37 | |
38 // Returns true if the log is empty. | |
39 bool IsEmpty() const; | |
40 | |
41 // Sets the log text and updates the hash. | |
42 void SetLogText(const std::string& log_text); | |
Ilya Sherman
2013/10/17 01:29:03
Can we pass |log_text| as a pointer, and swap() th
Alexei Svitkine (slow)
2013/10/17 19:47:27
I added a SwapLogText() function that does that no
Ilya Sherman
2013/10/17 22:06:25
Thanks. If SetLogText() is only used in tests, ca
Alexei Svitkine (slow)
2013/10/18 18:51:06
Done.
| |
43 | |
44 // Clears the log. | |
45 void Clear(); | |
46 | |
47 // Swaps log contents with |other|. | |
48 void Swap(SerializedLog* other); | |
49 | |
50 private: | |
51 // Non-human readable log text (serialized proto). | |
52 std::string log_text_; | |
53 | |
54 // Non-human readable SHA1 of |log_text| or empty if |log_text| is empty. | |
55 std::string log_hash_; | |
56 }; | |
Ilya Sherman
2013/10/17 01:29:03
nit: Please either add a DISALLOW_COPY_AND_ASSIGN,
Alexei Svitkine (slow)
2013/10/17 19:47:27
Done.
| |
57 | |
30 enum StoreType { | 58 enum StoreType { |
31 NORMAL_STORE, // A standard store operation. | 59 NORMAL_STORE, // A standard store operation. |
32 PROVISIONAL_STORE, // A store operation that can be easily reverted later. | 60 PROVISIONAL_STORE, // A store operation that can be easily reverted later. |
33 }; | 61 }; |
34 | 62 |
35 // Takes ownership of |log|, which has type |log_type|, and makes it the | 63 // Takes ownership of |log|, which has type |log_type|, and makes it the |
36 // current_log. This should only be called if there is not a current log. | 64 // current_log. This should only be called if there is not a current log. |
37 void BeginLoggingWithLog(MetricsLogBase* log, LogType log_type); | 65 void BeginLoggingWithLog(MetricsLogBase* log, LogType log_type); |
38 | 66 |
39 // Returns the in-progress log. | 67 // Returns the in-progress log. |
(...skipping 10 matching lines...) Expand all Loading... | |
50 | 78 |
51 // Populates staged_log_text() with the next stored log to send. | 79 // Populates staged_log_text() with the next stored log to send. |
52 // Should only be called if has_unsent_logs() is true. | 80 // Should only be called if has_unsent_logs() is true. |
53 void StageNextLogForUpload(); | 81 void StageNextLogForUpload(); |
54 | 82 |
55 // Returns true if there is a log that needs to be, or is being, uploaded. | 83 // Returns true if there is a log that needs to be, or is being, uploaded. |
56 bool has_staged_log() const; | 84 bool has_staged_log() const; |
57 | 85 |
58 // The text of the staged log, as a serialized protobuf. Empty if there is no | 86 // The text of the staged log, as a serialized protobuf. Empty if there is no |
59 // staged log, or if compression of the staged log failed. | 87 // staged log, or if compression of the staged log failed. |
60 const std::string& staged_log_text() const { return staged_log_text_; } | 88 const std::string& staged_log_text() const { return staged_log_.log_text(); } |
89 | |
90 // The SHA1 hash (non-human readable) of the staged log or empty if there is | |
91 // no staged log. | |
92 const std::string& staged_log_hash() const { return staged_log_.log_hash(); } | |
61 | 93 |
62 // Discards the staged log. | 94 // Discards the staged log. |
63 void DiscardStagedLog(); | 95 void DiscardStagedLog(); |
64 | 96 |
65 // Closes and discards |current_log|. | 97 // Closes and discards |current_log|. |
66 void DiscardCurrentLog(); | 98 void DiscardCurrentLog(); |
67 | 99 |
68 // Sets current_log to NULL, but saves the current log for future use with | 100 // Sets current_log to NULL, but saves the current log for future use with |
69 // ResumePausedLog(). Only one log may be paused at a time. | 101 // ResumePausedLog(). Only one log may be paused at a time. |
70 // TODO(stuartmorgan): Pause/resume support is really a workaround for a | 102 // TODO(stuartmorgan): Pause/resume support is really a workaround for a |
(...skipping 27 matching lines...) Expand all Loading... | |
98 } | 130 } |
99 | 131 |
100 // Interface for a utility class to serialize and deserialize logs for | 132 // Interface for a utility class to serialize and deserialize logs for |
101 // persistent storage. | 133 // persistent storage. |
102 class LogSerializer { | 134 class LogSerializer { |
103 public: | 135 public: |
104 virtual ~LogSerializer() {} | 136 virtual ~LogSerializer() {} |
105 | 137 |
106 // Serializes |logs| to persistent storage, replacing any previously | 138 // Serializes |logs| to persistent storage, replacing any previously |
107 // serialized logs of the same type. | 139 // serialized logs of the same type. |
108 virtual void SerializeLogs(const std::vector<std::string>& logs, | 140 virtual void SerializeLogs(const std::vector<SerializedLog>& logs, |
109 LogType log_type) = 0; | 141 LogType log_type) = 0; |
110 | 142 |
111 // Populates |logs| with logs of type |log_type| deserialized from | 143 // Populates |logs| with logs of type |log_type| deserialized from |
112 // persistent storage. | 144 // persistent storage. |
113 virtual void DeserializeLogs(LogType log_type, | 145 virtual void DeserializeLogs(LogType log_type, |
114 std::vector<std::string>* logs) = 0; | 146 std::vector<SerializedLog>* logs) = 0; |
115 }; | 147 }; |
116 | 148 |
117 // Sets the serializer to use for persisting and loading logs; takes ownership | 149 // Sets the serializer to use for persisting and loading logs; takes ownership |
118 // of |serializer|. | 150 // of |serializer|. |
119 void set_log_serializer(LogSerializer* serializer) { | 151 void set_log_serializer(LogSerializer* serializer) { |
120 log_serializer_.reset(serializer); | 152 log_serializer_.reset(serializer); |
121 } | 153 } |
122 | 154 |
123 // Saves any unsent logs to persistent storage using the current log | 155 // Saves any unsent logs to persistent storage using the current log |
124 // serializer. Can only be called after set_log_serializer. | 156 // serializer. Can only be called after set_log_serializer. |
125 void PersistUnsentLogs(); | 157 void PersistUnsentLogs(); |
126 | 158 |
127 // Loads any unsent logs from persistent storage using the current log | 159 // Loads any unsent logs from persistent storage using the current log |
128 // serializer. Can only be called after set_log_serializer. | 160 // serializer. Can only be called after set_log_serializer. |
129 void LoadPersistedUnsentLogs(); | 161 void LoadPersistedUnsentLogs(); |
130 | 162 |
131 private: | 163 private: |
132 // Saves |log_text| as the given type (or discards it in accordance with | 164 // Saves |log| as the given type (or discards it in accordance with |
133 // |max_ongoing_log_store_size_|). | 165 // |max_ongoing_log_store_size_|). |
134 // NOTE: This clears the contents of |log_text| (to avoid an expensive | 166 // NOTE: This clears the contents of |log| (to avoid an expensive copy), |
135 // string copy), so the log should be discarded after this call. | 167 // so the log should be discarded after this call. |
136 void StoreLog(std::string* log_text, | 168 void StoreLog(SerializedLog* log, |
137 LogType log_type, | 169 LogType log_type, |
138 StoreType store_type); | 170 StoreType store_type); |
139 | 171 |
140 // Compresses current_log_ into compressed_log. | 172 // Compresses |current_log_| into |compressed_log|. |
141 void CompressCurrentLog(std::string* compressed_log); | 173 void CompressCurrentLog(SerializedLog* compressed_log); |
142 | 174 |
143 // The log that we are still appending to. | 175 // The log that we are still appending to. |
144 scoped_ptr<MetricsLogBase> current_log_; | 176 scoped_ptr<MetricsLogBase> current_log_; |
145 LogType current_log_type_; | 177 LogType current_log_type_; |
146 | 178 |
147 // A paused, previously-current log. | 179 // A paused, previously-current log. |
148 scoped_ptr<MetricsLogBase> paused_log_; | 180 scoped_ptr<MetricsLogBase> paused_log_; |
149 LogType paused_log_type_; | 181 LogType paused_log_type_; |
150 | 182 |
151 // Helper class to handle serialization/deserialization of logs for persistent | 183 // Helper class to handle serialization/deserialization of logs for persistent |
152 // storage. May be NULL. | 184 // storage. May be NULL. |
153 scoped_ptr<LogSerializer> log_serializer_; | 185 scoped_ptr<LogSerializer> log_serializer_; |
154 | 186 |
155 // The text representations of the staged log, ready for upload to the server. | 187 // The current staged log, ready for upload to the server. |
156 std::string staged_log_text_; | 188 SerializedLog staged_log_; |
157 LogType staged_log_type_; | 189 LogType staged_log_type_; |
158 | 190 |
159 // Logs from a previous session that have not yet been sent. | 191 // Logs from a previous session that have not yet been sent. |
160 // Note that the vector has the oldest logs listed first (early in the | 192 // Note that the vector has the oldest logs listed first (early in the |
161 // vector), and we'll discard old logs if we have gathered too many logs. | 193 // vector), and we'll discard old logs if we have gathered too many logs. |
162 std::vector<std::string> unsent_initial_logs_; | 194 std::vector<SerializedLog> unsent_initial_logs_; |
163 std::vector<std::string> unsent_ongoing_logs_; | 195 std::vector<SerializedLog> unsent_ongoing_logs_; |
164 | 196 |
165 size_t max_ongoing_log_store_size_; | 197 size_t max_ongoing_log_store_size_; |
166 | 198 |
167 // The index and type of the last provisional store. If nothing has been | 199 // The index and type of the last provisional store. If nothing has been |
168 // provisionally stored, or the last provisional store has already been | 200 // provisionally stored, or the last provisional store has already been |
169 // re-staged, the index will be -1; | 201 // re-staged, the index will be -1; |
170 // This is necessary because during an upload there are two logs (staged | 202 // This is necessary because during an upload there are two logs (staged |
171 // and current) and a client might store them in either order, so it's | 203 // and current) and a client might store them in either order, so it's |
172 // not necessarily the case that the provisional store is the last store. | 204 // not necessarily the case that the provisional store is the last store. |
173 int last_provisional_store_index_; | 205 int last_provisional_store_index_; |
174 LogType last_provisional_store_type_; | 206 LogType last_provisional_store_type_; |
175 | 207 |
176 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); | 208 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); |
177 }; | 209 }; |
178 | 210 |
179 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 211 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
OLD | NEW |