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