Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / metrics / metrics_log_manager.h
blobcee8335552b4f20509e1ad3f6f84cd2d16efc11d
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_
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "chrome/common/metrics/metrics_log_base.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 {
20 public:
21 typedef MetricsLogBase::LogType LogType;
23 MetricsLogManager();
24 ~MetricsLogManager();
26 class SerializedLog {
27 public:
28 SerializedLog();
29 ~SerializedLog();
31 const std::string& log_text() const { return log_text_; }
32 const std::string& log_hash() const { return log_hash_; }
34 // Returns true if the log is empty.
35 bool IsEmpty() const;
37 // Swaps log text with |log_text| and updates the hash. This is more
38 // performant than a regular setter as it avoids doing a large string copy.
39 void SwapLogText(std::string* log_text);
41 // Clears the log.
42 void Clear();
44 // Swaps log contents with |other|.
45 void Swap(SerializedLog* other);
47 private:
48 // Non-human readable log text (serialized proto).
49 std::string log_text_;
51 // Non-human readable SHA1 of |log_text| or empty if |log_text| is empty.
52 std::string log_hash_;
54 // Intentionally omits DISALLOW_COPY_AND_ASSIGN() so that it can be used
55 // in std::vector<SerializedLog>.
58 enum StoreType {
59 NORMAL_STORE, // A standard store operation.
60 PROVISIONAL_STORE, // A store operation that can be easily reverted later.
63 // Takes ownership of |log|, which has type |log_type|, and makes it the
64 // current_log. This should only be called if there is not a current log.
65 void BeginLoggingWithLog(MetricsLogBase* log, LogType log_type);
67 // Returns the in-progress log.
68 MetricsLogBase* current_log() { return current_log_.get(); }
70 // Closes current_log(), compresses it, and stores the compressed log for
71 // later, leaving current_log() NULL.
72 void FinishCurrentLog();
74 // Returns true if there are any logs waiting to be uploaded.
75 bool has_unsent_logs() const {
76 return !unsent_initial_logs_.empty() || !unsent_ongoing_logs_.empty();
79 // Populates staged_log_text() with the next stored log to send.
80 // Should only be called if has_unsent_logs() is true.
81 void StageNextLogForUpload();
83 // Returns true if there is a log that needs to be, or is being, uploaded.
84 bool has_staged_log() const;
86 // The text of the staged log, as a serialized protobuf. Empty if there is no
87 // staged log, or if compression of the staged log failed.
88 const std::string& staged_log_text() const { return staged_log_.log_text(); }
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(); }
94 // Discards the staged log.
95 void DiscardStagedLog();
97 // Closes and discards |current_log|.
98 void DiscardCurrentLog();
100 // Sets current_log to NULL, but saves the current log for future use with
101 // ResumePausedLog(). Only one log may be paused at a time.
102 // TODO(stuartmorgan): Pause/resume support is really a workaround for a
103 // design issue in initial log writing; that should be fixed, and pause/resume
104 // removed.
105 void PauseCurrentLog();
107 // Restores the previously paused log (if any) to current_log().
108 // This should only be called if there is not a current log.
109 void ResumePausedLog();
111 // Saves the staged log, then clears staged_log().
112 // If |store_type| is PROVISIONAL_STORE, it can be dropped from storage with
113 // a later call to DiscardLastProvisionalStore (if it hasn't already been
114 // staged again).
115 // This is intended to be used when logs are being saved while an upload is in
116 // progress, in case the upload later succeeds.
117 // This can only be called if has_staged_log() is true.
118 void StoreStagedLogAsUnsent(StoreType store_type);
120 // Discards the last log stored with StoreStagedLogAsUnsent with |store_type|
121 // set to PROVISIONAL_STORE, as long as it hasn't already been re-staged. If
122 // the log is no longer present, this is a no-op.
123 void DiscardLastProvisionalStore();
125 // Sets the threshold for how large an onging log can be and still be written
126 // to persistant storage. Ongoing logs larger than this will be discarded
127 // before persisting. 0 is interpreted as no limit.
128 void set_max_ongoing_log_store_size(size_t max_size) {
129 max_ongoing_log_store_size_ = max_size;
132 // Interface for a utility class to serialize and deserialize logs for
133 // persistent storage.
134 class LogSerializer {
135 public:
136 virtual ~LogSerializer() {}
138 // Serializes |logs| to persistent storage, replacing any previously
139 // serialized logs of the same type.
140 virtual void SerializeLogs(const std::vector<SerializedLog>& logs,
141 LogType log_type) = 0;
143 // Populates |logs| with logs of type |log_type| deserialized from
144 // persistent storage.
145 virtual void DeserializeLogs(LogType log_type,
146 std::vector<SerializedLog>* logs) = 0;
149 // Sets the serializer to use for persisting and loading logs; takes ownership
150 // of |serializer|.
151 void set_log_serializer(LogSerializer* serializer) {
152 log_serializer_.reset(serializer);
155 // Saves any unsent logs to persistent storage using the current log
156 // serializer. Can only be called after set_log_serializer.
157 void PersistUnsentLogs();
159 // Loads any unsent logs from persistent storage using the current log
160 // serializer. Can only be called after set_log_serializer.
161 void LoadPersistedUnsentLogs();
163 private:
164 // Saves |log| as the given type (or discards it in accordance with
165 // |max_ongoing_log_store_size_|).
166 // NOTE: This clears the contents of |log| (to avoid an expensive copy),
167 // so the log should be discarded after this call.
168 void StoreLog(SerializedLog* log,
169 LogType log_type,
170 StoreType store_type);
172 // Compresses |current_log_| into |compressed_log|.
173 void CompressCurrentLog(SerializedLog* compressed_log);
175 // Tracks whether unsent logs (if any) have been loaded from the serializer.
176 bool unsent_logs_loaded_;
178 // The log that we are still appending to.
179 scoped_ptr<MetricsLogBase> current_log_;
180 LogType current_log_type_;
182 // A paused, previously-current log.
183 scoped_ptr<MetricsLogBase> paused_log_;
184 LogType paused_log_type_;
186 // Helper class to handle serialization/deserialization of logs for persistent
187 // storage. May be NULL.
188 scoped_ptr<LogSerializer> log_serializer_;
190 // The current staged log, ready for upload to the server.
191 SerializedLog staged_log_;
192 LogType staged_log_type_;
194 // Logs from a previous session that have not yet been sent.
195 // Note that the vector has the oldest logs listed first (early in the
196 // vector), and we'll discard old logs if we have gathered too many logs.
197 std::vector<SerializedLog> unsent_initial_logs_;
198 std::vector<SerializedLog> unsent_ongoing_logs_;
200 size_t max_ongoing_log_store_size_;
202 // The index and type of the last provisional store. If nothing has been
203 // provisionally stored, or the last provisional store has already been
204 // re-staged, the index will be -1;
205 // This is necessary because during an upload there are two logs (staged
206 // and current) and a client might store them in either order, so it's
207 // not necessarily the case that the provisional store is the last store.
208 int last_provisional_store_index_;
209 LogType last_provisional_store_type_;
211 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager);
214 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_