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