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 #include "base/basictypes.h" 9 #include "base/memory/scoped_ptr.h" 10 11 #include <string> 12 #include <vector> 13 14 class MetricsLogBase; 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 MetricsLogManager(); 22 ~MetricsLogManager(); 23 24 enum LogType { 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. 28 }; 29 30 enum StoreType { 31 NORMAL_STORE, // A standard store operation. 32 PROVISIONAL_STORE, // A store operation that can be easily reverted later. 33 }; 34 35 // 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. 37 void BeginLoggingWithLog(MetricsLogBase* log, LogType log_type); 38 39 // Returns the in-progress log. 40 MetricsLogBase* current_log() { return current_log_.get(); } 41 42 // Closes current_log(), compresses it, and stores the compressed log for 43 // later, leaving current_log() NULL. 44 void FinishCurrentLog(); 45 46 // Returns true if there are any logs waiting to be uploaded. 47 bool has_unsent_logs() const { 48 return !unsent_initial_logs_.empty() || !unsent_ongoing_logs_.empty(); 49 } 50 51 // Populates staged_log_text() with the next stored log to send. 52 // Should only be called if has_unsent_logs() is true. 53 void StageNextLogForUpload(); 54 55 // Returns true if there is a log that needs to be, or is being, uploaded. 56 bool has_staged_log() const; 57 58 // 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. 60 const std::string& staged_log_text() const { return staged_log_text_; } 61 62 // Discards the staged log. 63 void DiscardStagedLog(); 64 65 // Closes and discards |current_log|. 66 void DiscardCurrentLog(); 67 68 // 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. 70 // TODO(stuartmorgan): Pause/resume support is really a workaround for a 71 // design issue in initial log writing; that should be fixed, and pause/resume 72 // removed. 73 void PauseCurrentLog(); 74 75 // Restores the previously paused log (if any) to current_log(). 76 // This should only be called if there is not a current log. 77 void ResumePausedLog(); 78 79 // Saves the staged log, then clears staged_log(). 80 // If |store_type| is PROVISIONAL_STORE, it can be dropped from storage with 81 // a later call to DiscardLastProvisionalStore (if it hasn't already been 82 // staged again). 83 // This is intended to be used when logs are being saved while an upload is in 84 // progress, in case the upload later succeeds. 85 // This can only be called if has_staged_log() is true. 86 void StoreStagedLogAsUnsent(StoreType store_type); 87 88 // Discards the last log stored with StoreStagedLogAsUnsent with |store_type| 89 // set to PROVISIONAL_STORE, as long as it hasn't already been re-staged. If 90 // the log is no longer present, this is a no-op. 91 void DiscardLastProvisionalStore(); 92 93 // Sets the threshold for how large an onging log can be and still be written 94 // to persistant storage. Ongoing logs larger than this will be discarded 95 // before persisting. 0 is interpreted as no limit. 96 void set_max_ongoing_log_store_size(size_t max_size) { 97 max_ongoing_log_store_size_ = max_size; 98 } 99 100 // Interface for a utility class to serialize and deserialize logs for 101 // persistent storage. 102 class LogSerializer { 103 public: 104 virtual ~LogSerializer() {} 105 106 // Serializes |logs| to persistent storage, replacing any previously 107 // serialized logs of the same type. 108 virtual void SerializeLogs(const std::vector<std::string>& logs, 109 LogType log_type) = 0; 110 111 // Populates |logs| with logs of type |log_type| deserialized from 112 // persistent storage. 113 virtual void DeserializeLogs(LogType log_type, 114 std::vector<std::string>* logs) = 0; 115 }; 116 117 // Sets the serializer to use for persisting and loading logs; takes ownership 118 // of |serializer|. 119 void set_log_serializer(LogSerializer* serializer) { 120 log_serializer_.reset(serializer); 121 } 122 123 // Saves any unsent logs to persistent storage using the current log 124 // serializer. Can only be called after set_log_serializer. 125 void PersistUnsentLogs(); 126 127 // Loads any unsent logs from persistent storage using the current log 128 // serializer. Can only be called after set_log_serializer. 129 void LoadPersistedUnsentLogs(); 130 131 private: 132 // Saves |log_text| as the given type (or discards it in accordance with 133 // |max_ongoing_log_store_size_|). 134 // NOTE: This clears the contents of |log_text| (to avoid an expensive 135 // string copy), so the log should be discarded after this call. 136 void StoreLog(std::string* log_text, 137 LogType log_type, 138 StoreType store_type); 139 140 // Compresses current_log_ into compressed_log. 141 void CompressCurrentLog(std::string* compressed_log); 142 143 // The log that we are still appending to. 144 scoped_ptr<MetricsLogBase> current_log_; 145 LogType current_log_type_; 146 147 // A paused, previously-current log. 148 scoped_ptr<MetricsLogBase> paused_log_; 149 LogType paused_log_type_; 150 151 // Helper class to handle serialization/deserialization of logs for persistent 152 // storage. May be NULL. 153 scoped_ptr<LogSerializer> log_serializer_; 154 155 // The text representations of the staged log, ready for upload to the server. 156 std::string staged_log_text_; 157 LogType staged_log_type_; 158 159 // 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 161 // vector), and we'll discard old logs if we have gathered too many logs. 162 std::vector<std::string> unsent_initial_logs_; 163 std::vector<std::string> unsent_ongoing_logs_; 164 165 size_t max_ongoing_log_store_size_; 166 167 // The index and type of the last provisional store. If nothing has been 168 // provisionally stored, or the last provisional store has already been 169 // re-staged, the index will be -1; 170 // 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 172 // not necessarily the case that the provisional store is the last store. 173 int last_provisional_store_index_; 174 LogType last_provisional_store_type_; 175 176 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); 177 }; 178 179 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ 180