• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 namespace 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.
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();
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.
current_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.
has_unsent_logs()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.
has_staged_log()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.
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.
staged_log_hash()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 any unsent logs to persistent storage.
88   void PersistUnsentLogs();
89 
90   // Loads any unsent logs from persistent storage.
91   void LoadPersistedUnsentLogs();
92 
93  private:
94   // Saves |log_data| as the given type.
95   void StoreLog(const std::string& log_data, MetricsLog::LogType log_type);
96 
97   // Tracks whether unsent logs (if any) have been loaded from the serializer.
98   bool unsent_logs_loaded_;
99 
100   // The log that we are still appending to.
101   scoped_ptr<MetricsLog> current_log_;
102 
103   // A paused, previously-current log.
104   scoped_ptr<MetricsLog> paused_log_;
105 
106   // Logs that have not yet been sent.
107   PersistedLogs initial_log_queue_;
108   PersistedLogs ongoing_log_queue_;
109 
110   DISALLOW_COPY_AND_ASSIGN(MetricsLogManager);
111 };
112 
113 }  // namespace metrics
114 
115 #endif  // COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_
116