Files.app: make default-action as a command.
[chromium-blink-merge.git] / components / metrics / metrics_log_manager.h
blob38815b5b94e36008960a423bfc62d0f853cc796a
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.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 // 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();
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);
33 // Returns the in-progress log.
34 MetricsLog* current_log() { return current_log_.get(); }
36 // Closes current_log(), compresses it, and stores the compressed log for
37 // later, leaving current_log() NULL.
38 void FinishCurrentLog();
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();
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();
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();
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();
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();
70 // Discards the staged log.
71 void DiscardStagedLog();
73 // Closes and discards |current_log|.
74 void DiscardCurrentLog();
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();
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();
87 // Saves any unsent logs to persistent storage.
88 void PersistUnsentLogs();
90 // Loads any unsent logs from persistent storage.
91 void LoadPersistedUnsentLogs();
93 // Saves |log_data| as the given type. Public to allow to push log created by
94 // external components.
95 void StoreLog(const std::string& log_data, MetricsLog::LogType log_type);
97 private:
98 // Tracks whether unsent logs (if any) have been loaded from the serializer.
99 bool unsent_logs_loaded_;
101 // The log that we are still appending to.
102 scoped_ptr<MetricsLog> current_log_;
104 // A paused, previously-current log.
105 scoped_ptr<MetricsLog> paused_log_;
107 // Logs that have not yet been sent.
108 PersistedLogs initial_log_queue_;
109 PersistedLogs ongoing_log_queue_;
111 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager);
114 } // namespace metrics
116 #endif // COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_