• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_
6 #define CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_
7 
8 
9 #include <string>
10 #include <vector>
11 
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "chrome/common/metrics/metrics_log_base.h"
15 
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;
22 
23   MetricsLogManager();
24   ~MetricsLogManager();
25 
26   class SerializedLog {
27    public:
28     SerializedLog();
29     ~SerializedLog();
30 
log_text()31     const std::string& log_text() const { return log_text_; }
log_hash()32     const std::string& log_hash() const { return log_hash_; }
33 
34     // Returns true if the log is empty.
35     bool IsEmpty() const;
36 
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);
40 
41     // Clears the log.
42     void Clear();
43 
44     // Swaps log contents with |other|.
45     void Swap(SerializedLog* other);
46 
47    private:
48     // Non-human readable log text (serialized proto).
49     std::string log_text_;
50 
51     // Non-human readable SHA1 of |log_text| or empty if |log_text| is empty.
52     std::string log_hash_;
53 
54     // Intentionally omits DISALLOW_COPY_AND_ASSIGN() so that it can be used
55     // in std::vector<SerializedLog>.
56   };
57 
58   enum StoreType {
59     NORMAL_STORE,       // A standard store operation.
60     PROVISIONAL_STORE,  // A store operation that can be easily reverted later.
61   };
62 
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);
66 
67   // Returns the in-progress log.
current_log()68   MetricsLogBase* current_log() { return current_log_.get(); }
69 
70   // Closes current_log(), compresses it, and stores the compressed log for
71   // later, leaving current_log() NULL.
72   void FinishCurrentLog();
73 
74   // Returns true if there are any logs waiting to be uploaded.
has_unsent_logs()75   bool has_unsent_logs() const {
76     return !unsent_initial_logs_.empty() || !unsent_ongoing_logs_.empty();
77   }
78 
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();
82 
83   // Returns true if there is a log that needs to be, or is being, uploaded.
84   bool has_staged_log() const;
85 
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.
staged_log_text()88   const std::string& staged_log_text() const { return staged_log_.log_text(); }
89 
90   // The SHA1 hash (non-human readable) of the staged log or empty if there is
91   // no staged log.
staged_log_hash()92   const std::string& staged_log_hash() const { return staged_log_.log_hash(); }
93 
94   // Discards the staged log.
95   void DiscardStagedLog();
96 
97   // Closes and discards |current_log|.
98   void DiscardCurrentLog();
99 
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();
106 
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();
110 
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);
119 
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();
124 
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.
set_max_ongoing_log_store_size(size_t max_size)128   void set_max_ongoing_log_store_size(size_t max_size) {
129     max_ongoing_log_store_size_ = max_size;
130   }
131 
132   // Interface for a utility class to serialize and deserialize logs for
133   // persistent storage.
134   class LogSerializer {
135    public:
~LogSerializer()136     virtual ~LogSerializer() {}
137 
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;
142 
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;
147   };
148 
149   // Sets the serializer to use for persisting and loading logs; takes ownership
150   // of |serializer|.
set_log_serializer(LogSerializer * serializer)151   void set_log_serializer(LogSerializer* serializer) {
152     log_serializer_.reset(serializer);
153   }
154 
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();
158 
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();
162 
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);
171 
172   // Compresses |current_log_| into |compressed_log|.
173   void CompressCurrentLog(SerializedLog* compressed_log);
174 
175   // Tracks whether unsent logs (if any) have been loaded from the serializer.
176   bool unsent_logs_loaded_;
177 
178   // The log that we are still appending to.
179   scoped_ptr<MetricsLogBase> current_log_;
180   LogType current_log_type_;
181 
182   // A paused, previously-current log.
183   scoped_ptr<MetricsLogBase> paused_log_;
184   LogType paused_log_type_;
185 
186   // Helper class to handle serialization/deserialization of logs for persistent
187   // storage. May be NULL.
188   scoped_ptr<LogSerializer> log_serializer_;
189 
190   // The current staged log, ready for upload to the server.
191   SerializedLog staged_log_;
192   LogType staged_log_type_;
193 
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_;
199 
200   size_t max_ongoing_log_store_size_;
201 
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_;
210 
211   DISALLOW_COPY_AND_ASSIGN(MetricsLogManager);
212 };
213 
214 #endif  // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_
215