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. 4 5#ifndef COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_ 6#define COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/basictypes.h" 12#include "base/memory/scoped_ptr.h" 13#include "components/metrics/metrics_log.h" 14#include "components/metrics/persisted_logs.h" 15 16namespace metrics { 17 18// Manages all the log objects used by a MetricsService implementation. Keeps 19// track of both an in progress log and a log that is staged for uploading as 20// text, as well as saving logs to, and loading logs from, persistent storage. 21class MetricsLogManager { 22 public: 23 // The metrics log manager will persist it's unsent logs by storing them in 24 // |local_state|, and will not persist ongoing logs over 25 // |max_ongoing_log_size|. 26 MetricsLogManager(PrefService* local_state, size_t max_ongoing_log_size); 27 ~MetricsLogManager(); 28 29 // Makes |log| the current_log. This should only be called if there is not a 30 // current log. 31 void BeginLoggingWithLog(scoped_ptr<MetricsLog> log); 32 33 // Returns the in-progress log. 34 MetricsLog* current_log() { return current_log_.get(); } 35 36 // Closes current_log(), compresses it, and stores the compressed log for 37 // later, leaving current_log() NULL. 38 void FinishCurrentLog(); 39 40 // Returns true if there are any logs waiting to be uploaded. 41 bool has_unsent_logs() const { 42 return initial_log_queue_.size() || ongoing_log_queue_.size(); 43 } 44 45 // Populates staged_log_text() with the next stored log to send. 46 // Should only be called if has_unsent_logs() is true. 47 void StageNextLogForUpload(); 48 49 // Returns true if there is a log that needs to be, or is being, uploaded. 50 bool has_staged_log() const { 51 return initial_log_queue_.has_staged_log() || 52 ongoing_log_queue_.has_staged_log(); 53 } 54 55 // The text of the staged log, as a serialized protobuf. 56 // Will trigger a DCHECK if there is no staged log. 57 const std::string& staged_log() const { 58 return initial_log_queue_.has_staged_log() ? 59 initial_log_queue_.staged_log() : ongoing_log_queue_.staged_log(); 60 } 61 62 // The SHA1 hash of the staged log. 63 // Will trigger a DCHECK if there is no staged log. 64 const std::string& staged_log_hash() const { 65 return initial_log_queue_.has_staged_log() ? 66 initial_log_queue_.staged_log_hash() : 67 ongoing_log_queue_.staged_log_hash(); 68 } 69 70 // Discards the staged log. 71 void DiscardStagedLog(); 72 73 // Closes and discards |current_log|. 74 void DiscardCurrentLog(); 75 76 // Sets current_log to NULL, but saves the current log for future use with 77 // ResumePausedLog(). Only one log may be paused at a time. 78 // TODO(stuartmorgan): Pause/resume support is really a workaround for a 79 // design issue in initial log writing; that should be fixed, and pause/resume 80 // removed. 81 void PauseCurrentLog(); 82 83 // Restores the previously paused log (if any) to current_log(). 84 // This should only be called if there is not a current log. 85 void ResumePausedLog(); 86 87 // Saves the staged log, then clears staged_log(). 88 // If |store_type| is PROVISIONAL_STORE, it can be dropped from storage with 89 // a later call to DiscardLastProvisionalStore (if it hasn't already been 90 // staged again). 91 // This is intended to be used when logs are being saved while an upload is in 92 // progress, in case the upload later succeeds. 93 // This can only be called if has_staged_log() is true. 94 void StoreStagedLogAsUnsent(PersistedLogs::StoreType store_type); 95 96 // Discards the last log stored with StoreStagedLogAsUnsent with |store_type| 97 // set to PROVISIONAL_STORE, as long as it hasn't already been re-staged. If 98 // the log is no longer present, this is a no-op. 99 void DiscardLastProvisionalStore(); 100 101 // Saves any unsent logs to persistent storage. 102 void PersistUnsentLogs(); 103 104 // Loads any unsent logs from persistent storage. 105 void LoadPersistedUnsentLogs(); 106 107 private: 108 // Saves |log_data| as the given type. 109 void StoreLog(const std::string& log_data, MetricsLog::LogType log_type); 110 111 // Tracks whether unsent logs (if any) have been loaded from the serializer. 112 bool unsent_logs_loaded_; 113 114 // The log that we are still appending to. 115 scoped_ptr<MetricsLog> current_log_; 116 117 // A paused, previously-current log. 118 scoped_ptr<MetricsLog> paused_log_; 119 120 // Logs that have not yet been sent. 121 PersistedLogs initial_log_queue_; 122 PersistedLogs ongoing_log_queue_; 123 124 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); 125}; 126 127} // namespace metrics 128 129#endif // COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_ 130