linux_aura: Disable the plugin install infobar.
[chromium-blink-merge.git] / components / metrics / metrics_log_manager.h
blobe494743cfacbec8f1b1d99452cb1f7026db21149
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"
14 #include "components/metrics/persisted_logs.h"
16 namespace metrics {
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.
21 class MetricsLogManager {
22 public:
23 typedef MetricsLogBase::LogType LogType;
25 // The metrics log manager will persist it's unsent logs by storing them in
26 // |local_state|, and will not persist ongoing logs over
27 // |max_ongoing_log_size|.
28 MetricsLogManager(PrefService* local_state, size_t max_ongoing_log_size);
29 ~MetricsLogManager();
31 // Makes |log| the current_log. This should only be called if there is not a
32 // current log.
33 void BeginLoggingWithLog(scoped_ptr<MetricsLogBase> log);
35 // Returns the in-progress log.
36 MetricsLogBase* current_log() { return current_log_.get(); }
38 // Closes current_log(), compresses it, and stores the compressed log for
39 // later, leaving current_log() NULL.
40 void FinishCurrentLog();
42 // Returns true if there are any logs waiting to be uploaded.
43 bool has_unsent_logs() const {
44 return initial_log_queue_.size() || ongoing_log_queue_.size();
47 // Populates staged_log_text() with the next stored log to send.
48 // Should only be called if has_unsent_logs() is true.
49 void StageNextLogForUpload();
51 // Returns true if there is a log that needs to be, or is being, uploaded.
52 bool has_staged_log() const {
53 return initial_log_queue_.has_staged_log() ||
54 ongoing_log_queue_.has_staged_log();
57 // The text of the staged log, as a serialized protobuf.
58 // Will trigger a DCHECK if there is no staged log.
59 const std::string& staged_log() const {
60 return initial_log_queue_.has_staged_log() ?
61 initial_log_queue_.staged_log() : ongoing_log_queue_.staged_log();
64 // The SHA1 hash of the staged log.
65 // Will trigger a DCHECK if there is no staged log.
66 const std::string& staged_log_hash() const {
67 return initial_log_queue_.has_staged_log() ?
68 initial_log_queue_.staged_log_hash() :
69 ongoing_log_queue_.staged_log_hash();
72 // Discards the staged log.
73 void DiscardStagedLog();
75 // Closes and discards |current_log|.
76 void DiscardCurrentLog();
78 // Sets current_log to NULL, but saves the current log for future use with
79 // ResumePausedLog(). Only one log may be paused at a time.
80 // TODO(stuartmorgan): Pause/resume support is really a workaround for a
81 // design issue in initial log writing; that should be fixed, and pause/resume
82 // removed.
83 void PauseCurrentLog();
85 // Restores the previously paused log (if any) to current_log().
86 // This should only be called if there is not a current log.
87 void ResumePausedLog();
89 // Saves the staged log, then clears staged_log().
90 // If |store_type| is PROVISIONAL_STORE, it can be dropped from storage with
91 // a later call to DiscardLastProvisionalStore (if it hasn't already been
92 // staged again).
93 // This is intended to be used when logs are being saved while an upload is in
94 // progress, in case the upload later succeeds.
95 // This can only be called if has_staged_log() is true.
96 void StoreStagedLogAsUnsent(metrics::PersistedLogs::StoreType store_type);
98 // Discards the last log stored with StoreStagedLogAsUnsent with |store_type|
99 // set to PROVISIONAL_STORE, as long as it hasn't already been re-staged. If
100 // the log is no longer present, this is a no-op.
101 void DiscardLastProvisionalStore();
103 // Saves any unsent logs to persistent storage.
104 void PersistUnsentLogs();
106 // Loads any unsent logs from persistent storage.
107 void LoadPersistedUnsentLogs();
109 private:
110 // Saves |log| as the given type.
111 // NOTE: This clears the contents of |log| (to avoid an expensive copy),
112 // so the log should be discarded after this call.
113 void StoreLog(std::string* log, LogType log_type);
115 // Tracks whether unsent logs (if any) have been loaded from the serializer.
116 bool unsent_logs_loaded_;
118 // The log that we are still appending to.
119 scoped_ptr<MetricsLogBase> current_log_;
121 // A paused, previously-current log.
122 scoped_ptr<MetricsLogBase> paused_log_;
124 // Logs that have not yet been sent.
125 metrics::PersistedLogs initial_log_queue_;
126 metrics::PersistedLogs ongoing_log_queue_;
128 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager);
131 } // namespace metrics
133 #endif // COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_