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
3 // found in the LICENSE file.
5 #ifndef CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_
6 #define CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
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
18 // text, as well as saving logs to, and loading logs from, persistent storage.
19 class MetricsLogManager {
25 INITIAL_LOG, // The first log of a session.
26 ONGOING_LOG, // Subsequent logs in a session.
27 NO_LOG, // Placeholder value for when there is no log.
35 const std::string& log_text() const { return log_text_; }
36 const std::string& log_hash() const { return log_hash_; }
38 // Returns true if the log is empty.
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);
48 // Swaps log contents with |other|.
49 void Swap(SerializedLog* other);
52 // Non-human readable log text (serialized proto).
53 std::string log_text_;
55 // Non-human readable SHA1 of |log_text| or empty if |log_text| is empty.
56 std::string log_hash_;
58 // Intentionally omits DISALLOW_COPY_AND_ASSIGN() so that it can be used
59 // in std::vector<SerializedLog>.
63 NORMAL_STORE, // A standard store operation.
64 PROVISIONAL_STORE, // A store operation that can be easily reverted later.
67 // Takes ownership of |log|, which has type |log_type|, and makes it the
68 // current_log. This should only be called if there is not a current log.
69 void BeginLoggingWithLog(MetricsLogBase* log, LogType log_type);
71 // Returns the in-progress log.
72 MetricsLogBase* current_log() { return current_log_.get(); }
74 // Closes current_log(), compresses it, and stores the compressed log for
75 // later, leaving current_log() NULL.
76 void FinishCurrentLog();
78 // Returns true if there are any logs waiting to be uploaded.
79 bool has_unsent_logs() const {
80 return !unsent_initial_logs_.empty() || !unsent_ongoing_logs_.empty();
83 // Populates staged_log_text() with the next stored log to send.
84 // Should only be called if has_unsent_logs() is true.
85 void StageNextLogForUpload();
87 // Returns true if there is a log that needs to be, or is being, uploaded.
88 bool has_staged_log() const;
90 // The text of the staged log, as a serialized protobuf. Empty if there is no
91 // staged log, or if compression of the staged log failed.
92 const std::string& staged_log_text() const { return staged_log_.log_text(); }
94 // The SHA1 hash (non-human readable) of the staged log or empty if there is
96 const std::string& staged_log_hash() const { return staged_log_.log_hash(); }
98 // Discards the staged log.
99 void DiscardStagedLog();
101 // Closes and discards |current_log|.
102 void DiscardCurrentLog();
104 // Sets current_log to NULL, but saves the current log for future use with
105 // ResumePausedLog(). Only one log may be paused at a time.
106 // TODO(stuartmorgan): Pause/resume support is really a workaround for a
107 // design issue in initial log writing; that should be fixed, and pause/resume
109 void PauseCurrentLog();
111 // Restores the previously paused log (if any) to current_log().
112 // This should only be called if there is not a current log.
113 void ResumePausedLog();
115 // Saves the staged log, then clears staged_log().
116 // If |store_type| is PROVISIONAL_STORE, it can be dropped from storage with
117 // a later call to DiscardLastProvisionalStore (if it hasn't already been
119 // This is intended to be used when logs are being saved while an upload is in
120 // progress, in case the upload later succeeds.
121 // This can only be called if has_staged_log() is true.
122 void StoreStagedLogAsUnsent(StoreType store_type);
124 // Discards the last log stored with StoreStagedLogAsUnsent with |store_type|
125 // set to PROVISIONAL_STORE, as long as it hasn't already been re-staged. If
126 // the log is no longer present, this is a no-op.
127 void DiscardLastProvisionalStore();
129 // Sets the threshold for how large an onging log can be and still be written
130 // to persistant storage. Ongoing logs larger than this will be discarded
131 // before persisting. 0 is interpreted as no limit.
132 void set_max_ongoing_log_store_size(size_t max_size) {
133 max_ongoing_log_store_size_ = max_size;
136 // Interface for a utility class to serialize and deserialize logs for
137 // persistent storage.
138 class LogSerializer {
140 virtual ~LogSerializer() {}
142 // Serializes |logs| to persistent storage, replacing any previously
143 // serialized logs of the same type.
144 virtual void SerializeLogs(const std::vector<SerializedLog>& logs,
145 LogType log_type) = 0;
147 // Populates |logs| with logs of type |log_type| deserialized from
148 // persistent storage.
149 virtual void DeserializeLogs(LogType log_type,
150 std::vector<SerializedLog>* logs) = 0;
153 // Sets the serializer to use for persisting and loading logs; takes ownership
155 void set_log_serializer(LogSerializer* serializer) {
156 log_serializer_.reset(serializer);
159 // Saves any unsent logs to persistent storage using the current log
160 // serializer. Can only be called after set_log_serializer.
161 void PersistUnsentLogs();
163 // Loads any unsent logs from persistent storage using the current log
164 // serializer. Can only be called after set_log_serializer.
165 void LoadPersistedUnsentLogs();
168 // Saves |log| as the given type (or discards it in accordance with
169 // |max_ongoing_log_store_size_|).
170 // NOTE: This clears the contents of |log| (to avoid an expensive copy),
171 // so the log should be discarded after this call.
172 void StoreLog(SerializedLog* log,
174 StoreType store_type);
176 // Compresses |current_log_| into |compressed_log|.
177 void CompressCurrentLog(SerializedLog* compressed_log);
179 // The log that we are still appending to.
180 scoped_ptr<MetricsLogBase> current_log_;
181 LogType current_log_type_;
183 // A paused, previously-current log.
184 scoped_ptr<MetricsLogBase> paused_log_;
185 LogType paused_log_type_;
187 // Helper class to handle serialization/deserialization of logs for persistent
188 // storage. May be NULL.
189 scoped_ptr<LogSerializer> log_serializer_;
191 // The current staged log, ready for upload to the server.
192 SerializedLog staged_log_;
193 LogType staged_log_type_;
195 // Logs from a previous session that have not yet been sent.
196 // Note that the vector has the oldest logs listed first (early in the
197 // vector), and we'll discard old logs if we have gathered too many logs.
198 std::vector<SerializedLog> unsent_initial_logs_;
199 std::vector<SerializedLog> unsent_ongoing_logs_;
201 size_t max_ongoing_log_store_size_;
203 // The index and type of the last provisional store. If nothing has been
204 // provisionally stored, or the last provisional store has already been
205 // re-staged, the index will be -1;
206 // This is necessary because during an upload there are two logs (staged
207 // and current) and a client might store them in either order, so it's
208 // not necessarily the case that the provisional store is the last store.
209 int last_provisional_store_index_;
210 LogType last_provisional_store_type_;
212 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager);
215 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_