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); |
| 43 |
| 44 // Swaps log text with |log_text| and updates the hash. This is more |
| 45 // performant than SetLogText as it avoids doing a large string copy. |
| 46 void SwapLogText(std::string* log_text); |
| 47 |
| 48 // Clears the log. |
| 49 void Clear(); |
| 50 |
| 51 // Swaps log contents with |other|. |
| 52 void Swap(SerializedLog* other); |
| 53 |
| 54 private: |
| 55 // Update |log_hash_| based on |log_text_|. |
| 56 void UpdateHash(); |
| 57 |
| 58 // Non-human readable log text (serialized proto). |
| 59 std::string log_text_; |
| 60 |
| 61 // Non-human readable SHA1 of |log_text| or empty if |log_text| is empty. |
| 62 std::string log_hash_; |
| 63 |
| 64 // Intentionally omits DISALLOW_COPY_AND_ASSIGN() so that it can be used |
| 65 // in std::vector<SerializedLog>. |
| 66 }; |
| 67 |
30 enum StoreType { | 68 enum StoreType { |
31 NORMAL_STORE, // A standard store operation. | 69 NORMAL_STORE, // A standard store operation. |
32 PROVISIONAL_STORE, // A store operation that can be easily reverted later. | 70 PROVISIONAL_STORE, // A store operation that can be easily reverted later. |
33 }; | 71 }; |
34 | 72 |
35 // Takes ownership of |log|, which has type |log_type|, and makes it the | 73 // 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. | 74 // current_log. This should only be called if there is not a current log. |
37 void BeginLoggingWithLog(MetricsLogBase* log, LogType log_type); | 75 void BeginLoggingWithLog(MetricsLogBase* log, LogType log_type); |
38 | 76 |
39 // Returns the in-progress log. | 77 // Returns the in-progress log. |
(...skipping 10 matching lines...) Expand all Loading... |
50 | 88 |
51 // Populates staged_log_text() with the next stored log to send. | 89 // Populates staged_log_text() with the next stored log to send. |
52 // Should only be called if has_unsent_logs() is true. | 90 // Should only be called if has_unsent_logs() is true. |
53 void StageNextLogForUpload(); | 91 void StageNextLogForUpload(); |
54 | 92 |
55 // Returns true if there is a log that needs to be, or is being, uploaded. | 93 // Returns true if there is a log that needs to be, or is being, uploaded. |
56 bool has_staged_log() const; | 94 bool has_staged_log() const; |
57 | 95 |
58 // The text of the staged log, as a serialized protobuf. Empty if there is no | 96 // 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. | 97 // staged log, or if compression of the staged log failed. |
60 const std::string& staged_log_text() const { return staged_log_text_; } | 98 const std::string& staged_log_text() const { return staged_log_.log_text(); } |
| 99 |
| 100 // The SHA1 hash (non-human readable) of the staged log or empty if there is |
| 101 // no staged log. |
| 102 const std::string& staged_log_hash() const { return staged_log_.log_hash(); } |
61 | 103 |
62 // Discards the staged log. | 104 // Discards the staged log. |
63 void DiscardStagedLog(); | 105 void DiscardStagedLog(); |
64 | 106 |
65 // Closes and discards |current_log|. | 107 // Closes and discards |current_log|. |
66 void DiscardCurrentLog(); | 108 void DiscardCurrentLog(); |
67 | 109 |
68 // Sets current_log to NULL, but saves the current log for future use with | 110 // 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. | 111 // ResumePausedLog(). Only one log may be paused at a time. |
70 // TODO(stuartmorgan): Pause/resume support is really a workaround for a | 112 // TODO(stuartmorgan): Pause/resume support is really a workaround for a |
(...skipping 27 matching lines...) Expand all Loading... |
98 } | 140 } |
99 | 141 |
100 // Interface for a utility class to serialize and deserialize logs for | 142 // Interface for a utility class to serialize and deserialize logs for |
101 // persistent storage. | 143 // persistent storage. |
102 class LogSerializer { | 144 class LogSerializer { |
103 public: | 145 public: |
104 virtual ~LogSerializer() {} | 146 virtual ~LogSerializer() {} |
105 | 147 |
106 // Serializes |logs| to persistent storage, replacing any previously | 148 // Serializes |logs| to persistent storage, replacing any previously |
107 // serialized logs of the same type. | 149 // serialized logs of the same type. |
108 virtual void SerializeLogs(const std::vector<std::string>& logs, | 150 virtual void SerializeLogs(const std::vector<SerializedLog>& logs, |
109 LogType log_type) = 0; | 151 LogType log_type) = 0; |
110 | 152 |
111 // Populates |logs| with logs of type |log_type| deserialized from | 153 // Populates |logs| with logs of type |log_type| deserialized from |
112 // persistent storage. | 154 // persistent storage. |
113 virtual void DeserializeLogs(LogType log_type, | 155 virtual void DeserializeLogs(LogType log_type, |
114 std::vector<std::string>* logs) = 0; | 156 std::vector<SerializedLog>* logs) = 0; |
115 }; | 157 }; |
116 | 158 |
117 // Sets the serializer to use for persisting and loading logs; takes ownership | 159 // Sets the serializer to use for persisting and loading logs; takes ownership |
118 // of |serializer|. | 160 // of |serializer|. |
119 void set_log_serializer(LogSerializer* serializer) { | 161 void set_log_serializer(LogSerializer* serializer) { |
120 log_serializer_.reset(serializer); | 162 log_serializer_.reset(serializer); |
121 } | 163 } |
122 | 164 |
123 // Saves any unsent logs to persistent storage using the current log | 165 // Saves any unsent logs to persistent storage using the current log |
124 // serializer. Can only be called after set_log_serializer. | 166 // serializer. Can only be called after set_log_serializer. |
125 void PersistUnsentLogs(); | 167 void PersistUnsentLogs(); |
126 | 168 |
127 // Loads any unsent logs from persistent storage using the current log | 169 // Loads any unsent logs from persistent storage using the current log |
128 // serializer. Can only be called after set_log_serializer. | 170 // serializer. Can only be called after set_log_serializer. |
129 void LoadPersistedUnsentLogs(); | 171 void LoadPersistedUnsentLogs(); |
130 | 172 |
131 private: | 173 private: |
132 // Saves |log_text| as the given type (or discards it in accordance with | 174 // Saves |log| as the given type (or discards it in accordance with |
133 // |max_ongoing_log_store_size_|). | 175 // |max_ongoing_log_store_size_|). |
134 // NOTE: This clears the contents of |log_text| (to avoid an expensive | 176 // NOTE: This clears the contents of |log| (to avoid an expensive copy), |
135 // string copy), so the log should be discarded after this call. | 177 // so the log should be discarded after this call. |
136 void StoreLog(std::string* log_text, | 178 void StoreLog(SerializedLog* log, |
137 LogType log_type, | 179 LogType log_type, |
138 StoreType store_type); | 180 StoreType store_type); |
139 | 181 |
140 // Compresses current_log_ into compressed_log. | 182 // Compresses |current_log_| into |compressed_log|. |
141 void CompressCurrentLog(std::string* compressed_log); | 183 void CompressCurrentLog(SerializedLog* compressed_log); |
142 | 184 |
143 // The log that we are still appending to. | 185 // The log that we are still appending to. |
144 scoped_ptr<MetricsLogBase> current_log_; | 186 scoped_ptr<MetricsLogBase> current_log_; |
145 LogType current_log_type_; | 187 LogType current_log_type_; |
146 | 188 |
147 // A paused, previously-current log. | 189 // A paused, previously-current log. |
148 scoped_ptr<MetricsLogBase> paused_log_; | 190 scoped_ptr<MetricsLogBase> paused_log_; |
149 LogType paused_log_type_; | 191 LogType paused_log_type_; |
150 | 192 |
151 // Helper class to handle serialization/deserialization of logs for persistent | 193 // Helper class to handle serialization/deserialization of logs for persistent |
152 // storage. May be NULL. | 194 // storage. May be NULL. |
153 scoped_ptr<LogSerializer> log_serializer_; | 195 scoped_ptr<LogSerializer> log_serializer_; |
154 | 196 |
155 // The text representations of the staged log, ready for upload to the server. | 197 // The current staged log, ready for upload to the server. |
156 std::string staged_log_text_; | 198 SerializedLog staged_log_; |
157 LogType staged_log_type_; | 199 LogType staged_log_type_; |
158 | 200 |
159 // Logs from a previous session that have not yet been sent. | 201 // 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 | 202 // 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. | 203 // vector), and we'll discard old logs if we have gathered too many logs. |
162 std::vector<std::string> unsent_initial_logs_; | 204 std::vector<SerializedLog> unsent_initial_logs_; |
163 std::vector<std::string> unsent_ongoing_logs_; | 205 std::vector<SerializedLog> unsent_ongoing_logs_; |
164 | 206 |
165 size_t max_ongoing_log_store_size_; | 207 size_t max_ongoing_log_store_size_; |
166 | 208 |
167 // The index and type of the last provisional store. If nothing has been | 209 // The index and type of the last provisional store. If nothing has been |
168 // provisionally stored, or the last provisional store has already been | 210 // provisionally stored, or the last provisional store has already been |
169 // re-staged, the index will be -1; | 211 // re-staged, the index will be -1; |
170 // This is necessary because during an upload there are two logs (staged | 212 // 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 | 213 // 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. | 214 // not necessarily the case that the provisional store is the last store. |
173 int last_provisional_store_index_; | 215 int last_provisional_store_index_; |
174 LogType last_provisional_store_type_; | 216 LogType last_provisional_store_type_; |
175 | 217 |
176 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); | 218 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); |
177 }; | 219 }; |
178 | 220 |
179 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 221 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
OLD | NEW |