OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 5 #ifndef CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
6 #define CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 6 #define CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 #include <utility> | |
13 #include <vector> | 14 #include <vector> |
14 | 15 |
15 class MetricsLogBase; | 16 class MetricsLogBase; |
16 | 17 |
17 // Manages all the log objects used by a MetricsService implementation. Keeps | 18 // Manages all the log objects used by a MetricsService implementation. Keeps |
18 // track of both an in progress log and a log that is staged for uploading as | 19 // track of both an in progress log and a log that is staged for uploading as |
19 // text, as well as saving logs to, and loading logs from, persistent storage. | 20 // text, as well as saving logs to, and loading logs from, persistent storage. |
20 class MetricsLogManager { | 21 class MetricsLogManager { |
21 public: | 22 public: |
22 MetricsLogManager(); | 23 MetricsLogManager(); |
23 ~MetricsLogManager(); | 24 ~MetricsLogManager(); |
24 | 25 |
25 // Takes ownership of |log|, and makes it the current_log. | 26 // Takes ownership of |log|, and makes it the current_log. |
26 // This should only be called if there is not a current log. | 27 // This should only be called if there is not a current log. |
27 void BeginLoggingWithLog(MetricsLogBase* log); | 28 void BeginLoggingWithLog(MetricsLogBase* log); |
28 | 29 |
29 // Returns the in-progress log. | 30 // Returns the in-progress log. |
30 MetricsLogBase* current_log() { return current_log_.get(); } | 31 MetricsLogBase* current_log() { return current_log_.get(); } |
31 | 32 |
32 // Closes |current_log| and stages it for upload, leaving |current_log| NULL. | 33 // Closes |current_log| and stages it for upload, leaving |current_log| NULL. |
33 void StageCurrentLogForUpload(); | 34 void StageCurrentLogForUpload(); |
34 | 35 |
35 // Returns true if there is a log that needs to be, or is being, uploaded. | 36 // Returns true if there is a log that needs to be, or is being, uploaded. |
36 // Note that this returns true even if compressing the log text failed. | 37 // Note that this returns true even if compressing the log text failed. |
37 bool has_staged_log() const; | 38 bool has_staged_log() const; |
38 | 39 |
39 // The compressed text of the staged log. Empty if there is no staged log, | 40 // The text of the staged log, in compressed XML or protobuf format. Empty if |
40 // or if compression of the staged log failed. | 41 // there is no staged log, or if compression of the staged log failed. |
41 const std::string& staged_log_text() { return compressed_staged_log_text_; } | 42 const std::string& staged_log_text_xml() const { |
jar (doing other things)
2012/02/23 01:59:18
This sort of accessor helps some (and keeps us awa
| |
43 return staged_log_text_.first; | |
44 } | |
45 const std::string& staged_log_text_proto() const { | |
46 return staged_log_text_.second; | |
47 } | |
42 | 48 |
43 // Discards the staged log. | 49 // Discards the staged log. |
44 void DiscardStagedLog(); | 50 void DiscardStagedLog(); |
45 | 51 |
46 // Closes and discards |current_log|. | 52 // Closes and discards |current_log|. |
47 void DiscardCurrentLog(); | 53 void DiscardCurrentLog(); |
48 | 54 |
49 // Sets current_log to NULL, but saves the current log for future use with | 55 // Sets current_log to NULL, but saves the current log for future use with |
50 // ResumePausedLog(). Only one log may be paused at a time. | 56 // ResumePausedLog(). Only one log may be paused at a time. |
51 // TODO(stuartmorgan): Pause/resume support is really a workaround for a | 57 // TODO(stuartmorgan): Pause/resume support is really a workaround for a |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 } | 90 } |
85 | 91 |
86 // Interface for a utility class to serialize and deserialize logs for | 92 // Interface for a utility class to serialize and deserialize logs for |
87 // persistent storage. | 93 // persistent storage. |
88 class LogSerializer { | 94 class LogSerializer { |
89 public: | 95 public: |
90 virtual ~LogSerializer() {} | 96 virtual ~LogSerializer() {} |
91 | 97 |
92 // Serializes |logs| to persistent storage, replacing any previously | 98 // Serializes |logs| to persistent storage, replacing any previously |
93 // serialized logs of the same type. | 99 // serialized logs of the same type. |
94 virtual void SerializeLogs(const std::vector<std::string>& logs, | 100 virtual void SerializeLogs( |
95 LogType log_type) = 0; | 101 const std::vector<std::pair<std::string, std::string> >& logs, |
102 LogType log_type) = 0; | |
96 | 103 |
97 // Populates |logs| with logs of type |log_type| deserialized from | 104 // Populates |logs| with logs of type |log_type| deserialized from |
98 // persistent storage. | 105 // persistent storage. |
99 virtual void DeserializeLogs(LogType log_type, | 106 virtual void DeserializeLogs( |
100 std::vector<std::string>* logs) = 0; | 107 LogType log_type, |
108 std::vector<std::pair<std::string, std::string> >* logs) = 0; | |
101 }; | 109 }; |
102 | 110 |
103 // Sets the serializer to use for persisting and loading logs; takes ownership | 111 // Sets the serializer to use for persisting and loading logs; takes ownership |
104 // of |serializer|. | 112 // of |serializer|. |
105 void set_log_serializer(LogSerializer* serializer) { | 113 void set_log_serializer(LogSerializer* serializer) { |
106 log_serializer_.reset(serializer); | 114 log_serializer_.reset(serializer); |
107 } | 115 } |
108 | 116 |
109 // Saves any unsent logs to persistent storage using the current log | 117 // Saves any unsent logs to persistent storage using the current log |
110 // serializer. Can only be called after set_log_serializer. | 118 // serializer. Can only be called after set_log_serializer. |
111 void PersistUnsentLogs(); | 119 void PersistUnsentLogs(); |
112 | 120 |
113 // Loads any unsent logs from persistent storage using the current log | 121 // Loads any unsent logs from persistent storage using the current log |
114 // serializer. Can only be called after set_log_serializer. | 122 // serializer. Can only be called after set_log_serializer. |
115 void LoadPersistedUnsentLogs(); | 123 void LoadPersistedUnsentLogs(); |
116 | 124 |
117 private: | 125 private: |
118 // Compresses staged_log_ and stores the result in | 126 // Compresses |staged_log_| and stores the result in |
119 // compressed_staged_log_text_. | 127 // |compressed_staged_xml_log_text_|. |
120 void CompressStagedLog(); | 128 void CompressStagedLog(); |
121 | 129 |
122 // Compresses the text in |input| using bzip2, store the result in |output|. | 130 // Compresses the text in |input| using bzip2, store the result in |output|. |
123 static bool Bzip2Compress(const std::string& input, std::string* output); | 131 static bool Bzip2Compress(const std::string& input, std::string* output); |
124 | 132 |
125 // The log that we are still appending to. | 133 // The log that we are still appending to. |
126 scoped_ptr<MetricsLogBase> current_log_; | 134 scoped_ptr<MetricsLogBase> current_log_; |
127 | 135 |
128 // A paused, previously-current log. | 136 // A paused, previously-current log. |
129 scoped_ptr<MetricsLogBase> paused_log_; | 137 scoped_ptr<MetricsLogBase> paused_log_; |
130 | 138 |
131 // The log that we are currently transmiting, or about to try to transmit. | 139 // The log that we are currently transmiting, or about to try to transmit. |
132 // Note that when using StageNextStoredLogForUpload, this can be NULL while | 140 // Note that when using StageNextStoredLogForUpload, this can be NULL while |
133 // compressed_staged_log_text_ is non-NULL. | 141 // |compressed_staged_xml_log_text_| is non-NULL. |
134 scoped_ptr<MetricsLogBase> staged_log_; | 142 scoped_ptr<MetricsLogBase> staged_log_; |
135 | 143 |
136 // Helper class to handle serialization/deserialization of logs for persistent | 144 // Helper class to handle serialization/deserialization of logs for persistent |
137 // storage. May be NULL. | 145 // storage. May be NULL. |
138 scoped_ptr<LogSerializer> log_serializer_; | 146 scoped_ptr<LogSerializer> log_serializer_; |
139 | 147 |
140 // The compressed text of the staged log, ready for upload to the server. | 148 // The text representations of the staged log, ready for upload to the server. |
141 std::string compressed_staged_log_text_; | 149 // The first item in the pair is the compressed XML representation; the second |
150 // is the protobuf representation. | |
151 std::pair<std::string, std::string> staged_log_text_; | |
142 | 152 |
143 // Logs from a previous session that have not yet been sent. | 153 // Logs from a previous session that have not yet been sent. |
154 // The first item in each pair is the XML representation; the second item is | |
155 // the protobuf representation. | |
144 // Note that the vector has the oldest logs listed first (early in the | 156 // Note that the vector has the oldest logs listed first (early in the |
145 // vector), and we'll discard old logs if we have gathered too many logs. | 157 // vector), and we'll discard old logs if we have gathered too many logs. |
146 std::vector<std::string> unsent_initial_logs_; | 158 std::vector<std::pair<std::string, std::string> > unsent_initial_logs_; |
jar (doing other things)
2012/02/23 01:59:18
IMO, you should use a tiny class or struct, and no
Ilya Sherman
2012/02/24 02:10:06
Done.
| |
147 std::vector<std::string> unsent_ongoing_logs_; | 159 std::vector<std::pair<std::string, std::string> > unsent_ongoing_logs_; |
148 | 160 |
149 size_t max_ongoing_log_store_size_; | 161 size_t max_ongoing_log_store_size_; |
150 | 162 |
151 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); | 163 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); |
152 }; | 164 }; |
153 | 165 |
154 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 166 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
OLD | NEW |