Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_ | 5 #ifndef COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_ |
| 6 #define COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_ | 6 #define COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "components/metrics/metrics_log.h" | 15 #include "components/metrics/metrics_log.h" |
| 16 #include "components/metrics/persisted_logs.h" | |
| 17 | 16 |
| 18 namespace metrics { | 17 namespace metrics { |
| 19 | 18 |
| 19 class MetricsLogStore; | |
| 20 | |
| 20 // Manages all the log objects used by a MetricsService implementation. Keeps | 21 // Manages all the log objects used by a MetricsService implementation. Keeps |
| 21 // track of both an in progress log and a log that is staged for uploading as | 22 // track of an progress log and a paused log. |
|
bcwhite
2017/02/22 16:25:08
an in-progress log?
Steven Holte
2017/02/22 20:35:50
Done.
| |
| 22 // text, as well as saving logs to, and loading logs from, persistent storage. | |
| 23 class MetricsLogManager { | 23 class MetricsLogManager { |
| 24 public: | 24 public: |
| 25 // The metrics log manager will persist it's unsent logs by storing them in | 25 MetricsLogManager(); |
| 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(); | 26 ~MetricsLogManager(); |
| 30 | 27 |
| 31 // Makes |log| the current_log. This should only be called if there is not a | 28 // Makes |log| the current_log. This should only be called if there is not a |
| 32 // current log. | 29 // current log. |
| 33 void BeginLoggingWithLog(std::unique_ptr<MetricsLog> log); | 30 void BeginLoggingWithLog(std::unique_ptr<MetricsLog> log); |
| 34 | 31 |
| 35 // Returns the in-progress log. | 32 // Returns the in-progress log. |
| 36 MetricsLog* current_log() { return current_log_.get(); } | 33 MetricsLog* current_log() { return current_log_.get(); } |
| 37 | 34 |
| 38 // Closes current_log(), compresses it, and stores the compressed log for | 35 // Closes current_log(), compresses it, and stores it in the log_store for |
|
bcwhite
2017/02/22 16:25:09
|current_log_|
|log_store|
Steven Holte
2017/02/22 20:35:50
Done.
| |
| 39 // later, leaving current_log() NULL. | 36 // later, leaving current_log() NULL. |
| 40 void FinishCurrentLog(); | 37 void FinishCurrentLog(MetricsLogStore* log_store); |
| 41 | |
| 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(); | |
| 45 } | |
| 46 | |
| 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(); | |
| 50 | |
| 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(); | |
| 55 } | |
| 56 | |
| 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(); | |
| 62 } | |
| 63 | |
| 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(); | |
| 70 } | |
| 71 | |
| 72 // Discards the staged log. | |
| 73 void DiscardStagedLog(); | |
| 74 | 38 |
| 75 // Closes and discards |current_log|. | 39 // Closes and discards |current_log|. |
| 76 void DiscardCurrentLog(); | 40 void DiscardCurrentLog(); |
| 77 | 41 |
| 78 // Sets current_log to NULL, but saves the current log for future use with | 42 // 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. | 43 // ResumePausedLog(). Only one log may be paused at a time. |
| 80 // TODO(stuartmorgan): Pause/resume support is really a workaround for a | 44 // 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 | 45 // design issue in initial log writing; that should be fixed, and pause/resume |
| 82 // removed. | 46 // removed. |
| 83 void PauseCurrentLog(); | 47 void PauseCurrentLog(); |
| 84 | 48 |
| 85 // Restores the previously paused log (if any) to current_log(). | 49 // Restores the previously paused log (if any) to current_log(). |
| 86 // This should only be called if there is not a current log. | 50 // This should only be called if there is not a current log. |
| 87 void ResumePausedLog(); | 51 void ResumePausedLog(); |
| 88 | 52 |
| 89 // Saves any unsent logs to persistent storage. | |
| 90 void PersistUnsentLogs(); | |
| 91 | |
| 92 // Loads any unsent logs from persistent storage. | |
| 93 void LoadPersistedUnsentLogs(); | |
| 94 | |
| 95 // Saves |log_data| as the given type. Public to allow to push log created by | |
| 96 // external components. | |
| 97 void StoreLog(const std::string& log_data, MetricsLog::LogType log_type); | |
| 98 | |
| 99 private: | 53 private: |
| 100 // Tracks whether unsent logs (if any) have been loaded from the serializer. | |
| 101 bool unsent_logs_loaded_; | |
| 102 | |
| 103 // The log that we are still appending to. | 54 // The log that we are still appending to. |
| 104 std::unique_ptr<MetricsLog> current_log_; | 55 std::unique_ptr<MetricsLog> current_log_; |
| 105 | 56 |
| 106 // A paused, previously-current log. | 57 // A paused, previously-current log. |
| 107 std::unique_ptr<MetricsLog> paused_log_; | 58 std::unique_ptr<MetricsLog> paused_log_; |
| 108 | 59 |
| 109 // Logs that have not yet been sent. | |
| 110 PersistedLogs initial_log_queue_; | |
| 111 PersistedLogs ongoing_log_queue_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); | 60 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); |
| 114 }; | 61 }; |
| 115 | 62 |
| 116 } // namespace metrics | 63 } // namespace metrics |
| 117 | 64 |
| 118 #endif // COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_ | 65 #endif // COMPONENTS_METRICS_METRICS_LOG_MANAGER_H_ |
| OLD | NEW |