Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / metrics / metrics_log_manager.h
blob006d4ed04262f4917ed56c3e220ea237491e0b81
1 // Copyright 2014 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 COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_
6 #define COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "components/metrics/metrics_log_base.h"
15 namespace metrics {
17 // Manages all the log objects used by a MetricsService implementation. Keeps
18 // track of both an in progress log and a log that is staged for uploading as
19 // text, as well as saving logs to, and loading logs from, persistent storage.
20 class MetricsLogManager {
21 public:
22 typedef MetricsLogBase::LogType LogType;
24 MetricsLogManager();
25 ~MetricsLogManager();
27 class SerializedLog {
28 public:
29 SerializedLog();
30 ~SerializedLog();
32 const std::string& log_text() const { return log_text_; }
33 const std::string& log_hash() const { return log_hash_; }
35 // Returns true if the log is empty.
36 bool IsEmpty() const;
38 // Swaps log text with |log_text| and updates the hash. This is more
39 // performant than a regular setter as it avoids doing a large string copy.
40 void SwapLogText(std::string* log_text);
42 // Clears the log.
43 void Clear();
45 // Swaps log contents with |other|.
46 void Swap(SerializedLog* other);
48 private:
49 // Non-human readable log text (serialized proto).
50 std::string log_text_;
52 // Non-human readable SHA1 of |log_text| or empty if |log_text| is empty.
53 std::string log_hash_;
55 // Intentionally omits DISALLOW_COPY_AND_ASSIGN() so that it can be used
56 // in std::vector<SerializedLog>.
59 enum StoreType {
60 NORMAL_STORE, // A standard store operation.
61 PROVISIONAL_STORE, // A store operation that can be easily reverted later.
64 // Takes ownership of |log| and makes it the current_log. This should only be
65 // called if there is not a current log.
66 void BeginLoggingWithLog(MetricsLogBase* log);
68 // Returns the in-progress log.
69 MetricsLogBase* current_log() { return current_log_.get(); }
71 // Closes current_log(), compresses it, and stores the compressed log for
72 // later, leaving current_log() NULL.
73 void FinishCurrentLog();
75 // Returns true if there are any logs waiting to be uploaded.
76 bool has_unsent_logs() const {
77 return !unsent_initial_logs_.empty() || !unsent_ongoing_logs_.empty();
80 // Populates staged_log_text() with the next stored log to send.
81 // Should only be called if has_unsent_logs() is true.
82 void StageNextLogForUpload();
84 // Returns true if there is a log that needs to be, or is being, uploaded.
85 bool has_staged_log() const;
87 // The text of the staged log, as a serialized protobuf. Empty if there is no
88 // staged log, or if compression of the staged log failed.
89 const std::string& staged_log_text() const { return staged_log_.log_text(); }
91 // The SHA1 hash (non-human readable) of the staged log or empty if there is
92 // no staged log.
93 const std::string& staged_log_hash() const { return staged_log_.log_hash(); }
95 // Discards the staged log.
96 void DiscardStagedLog();
98 // Closes and discards |current_log|.
99 void DiscardCurrentLog();
101 // Sets current_log to NULL, but saves the current log for future use with
102 // ResumePausedLog(). Only one log may be paused at a time.
103 // TODO(stuartmorgan): Pause/resume support is really a workaround for a
104 // design issue in initial log writing; that should be fixed, and pause/resume
105 // removed.
106 void PauseCurrentLog();
108 // Restores the previously paused log (if any) to current_log().
109 // This should only be called if there is not a current log.
110 void ResumePausedLog();
112 // Saves the staged log, then clears staged_log().
113 // If |store_type| is PROVISIONAL_STORE, it can be dropped from storage with
114 // a later call to DiscardLastProvisionalStore (if it hasn't already been
115 // staged again).
116 // This is intended to be used when logs are being saved while an upload is in
117 // progress, in case the upload later succeeds.
118 // This can only be called if has_staged_log() is true.
119 void StoreStagedLogAsUnsent(StoreType store_type);
121 // Discards the last log stored with StoreStagedLogAsUnsent with |store_type|
122 // set to PROVISIONAL_STORE, as long as it hasn't already been re-staged. If
123 // the log is no longer present, this is a no-op.
124 void DiscardLastProvisionalStore();
126 // Sets the threshold for how large an onging log can be and still be written
127 // to persistant storage. Ongoing logs larger than this will be discarded
128 // before persisting. 0 is interpreted as no limit.
129 void set_max_ongoing_log_store_size(size_t max_size) {
130 max_ongoing_log_store_size_ = max_size;
133 // Interface for a utility class to serialize and deserialize logs for
134 // persistent storage.
135 class LogSerializer {
136 public:
137 virtual ~LogSerializer() {}
139 // Serializes |logs| to persistent storage, replacing any previously
140 // serialized logs of the same type.
141 virtual void SerializeLogs(const std::vector<SerializedLog>& logs,
142 LogType log_type) = 0;
144 // Populates |logs| with logs of type |log_type| deserialized from
145 // persistent storage.
146 virtual void DeserializeLogs(LogType log_type,
147 std::vector<SerializedLog>* logs) = 0;
150 // Sets the serializer to use for persisting and loading logs; takes ownership
151 // of |serializer|.
152 void set_log_serializer(LogSerializer* serializer) {
153 log_serializer_.reset(serializer);
156 // Saves any unsent logs to persistent storage using the current log
157 // serializer. Can only be called after set_log_serializer.
158 void PersistUnsentLogs();
160 // Loads any unsent logs from persistent storage using the current log
161 // serializer. Can only be called after set_log_serializer.
162 void LoadPersistedUnsentLogs();
164 private:
165 // Saves |log| as the given type (or discards it in accordance with
166 // |max_ongoing_log_store_size_|).
167 // NOTE: This clears the contents of |log| (to avoid an expensive copy),
168 // so the log should be discarded after this call.
169 void StoreLog(SerializedLog* log, LogType log_type, StoreType store_type);
171 // Compresses |current_log_| into |compressed_log|.
172 void CompressCurrentLog(SerializedLog* compressed_log);
174 // Tracks whether unsent logs (if any) have been loaded from the serializer.
175 bool unsent_logs_loaded_;
177 // The log that we are still appending to.
178 scoped_ptr<MetricsLogBase> current_log_;
180 // A paused, previously-current log.
181 scoped_ptr<MetricsLogBase> paused_log_;
183 // Helper class to handle serialization/deserialization of logs for persistent
184 // storage. May be NULL.
185 scoped_ptr<LogSerializer> log_serializer_;
187 // The current staged log, ready for upload to the server.
188 SerializedLog staged_log_;
189 LogType staged_log_type_;
191 // Logs from a previous session that have not yet been sent.
192 // Note that the vector has the oldest logs listed first (early in the
193 // vector), and we'll discard old logs if we have gathered too many logs.
194 std::vector<SerializedLog> unsent_initial_logs_;
195 std::vector<SerializedLog> unsent_ongoing_logs_;
197 size_t max_ongoing_log_store_size_;
199 // The index and type of the last provisional store. If nothing has been
200 // provisionally stored, or the last provisional store has already been
201 // re-staged, the index will be -1;
202 // This is necessary because during an upload there are two logs (staged
203 // and current) and a client might store them in either order, so it's
204 // not necessarily the case that the provisional store is the last store.
205 int last_provisional_store_index_;
206 LogType last_provisional_store_type_;
208 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager);
211 } // namespace metrics
213 #endif // COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_