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 <vector> | 13 #include <vector> |
14 | 14 |
15 class MetricsLogBase; | 15 class MetricsLogBase; |
16 | 16 |
17 // Manages all the log objects used by a MetricsService implementation. Keeps | 17 // 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 | 18 // 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. | 19 // text, as well as saving logs to, and loading logs from, persistent storage. |
20 class MetricsLogManager { | 20 class MetricsLogManager { |
21 public: | 21 public: |
22 MetricsLogManager(); | 22 MetricsLogManager(); |
23 ~MetricsLogManager(); | 23 ~MetricsLogManager(); |
24 | 24 |
| 25 // Stores both XML and protocol buffer serializations for a log. |
| 26 struct SerializedLog { |
| 27 public: |
| 28 // Exposed to reduce code churn as we transition from the XML pipeline to |
| 29 // the protocol buffer pipeline. |
| 30 bool empty() const; |
| 31 |
| 32 std::string xml; |
| 33 std::string proto; |
| 34 }; |
| 35 |
25 // Takes ownership of |log|, and makes it the current_log. | 36 // Takes ownership of |log|, and makes it the current_log. |
26 // This should only be called if there is not a current log. | 37 // This should only be called if there is not a current log. |
27 void BeginLoggingWithLog(MetricsLogBase* log); | 38 void BeginLoggingWithLog(MetricsLogBase* log); |
28 | 39 |
29 // Returns the in-progress log. | 40 // Returns the in-progress log. |
30 MetricsLogBase* current_log() { return current_log_.get(); } | 41 MetricsLogBase* current_log() { return current_log_.get(); } |
31 | 42 |
32 // Closes |current_log| and stages it for upload, leaving |current_log| NULL. | 43 // Closes |current_log| and stages it for upload, leaving |current_log| NULL. |
33 void StageCurrentLogForUpload(); | 44 void StageCurrentLogForUpload(); |
34 | 45 |
35 // Returns true if there is a log that needs to be, or is being, uploaded. | 46 // 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. | 47 // Note that this returns true even if compressing the log text failed. |
37 bool has_staged_log() const; | 48 bool has_staged_log() const; |
38 | 49 |
39 // The compressed text of the staged log. Empty if there is no staged log, | 50 // Returns true if there is a protobuf log that needs to be uploaded. |
40 // or if compression of the staged log failed. | 51 // In the case that an XML upload needs to be re-issued due to a previous |
41 const std::string& staged_log_text() { return compressed_staged_log_text_; } | 52 // failure, |has_staged_log()| will return true while this returns false. |
| 53 bool has_staged_log_proto() const; |
42 | 54 |
43 // Discards the staged log. | 55 // The text of the staged log, in compressed XML or protobuf format. Empty if |
| 56 // there is no staged log, or if compression of the staged log failed. |
| 57 const SerializedLog& staged_log_text() const { |
| 58 return staged_log_text_; |
| 59 } |
| 60 |
| 61 // Discards the staged log (both the XML and the protobuf data). |
44 void DiscardStagedLog(); | 62 void DiscardStagedLog(); |
45 | 63 |
| 64 // Discards the protobuf data in the staged log. |
| 65 // This is useful to prevent needlessly re-issuing successful protobuf uploads |
| 66 // due to XML upload failures. |
| 67 void DiscardStagedLogProto(); |
| 68 |
46 // Closes and discards |current_log|. | 69 // Closes and discards |current_log|. |
47 void DiscardCurrentLog(); | 70 void DiscardCurrentLog(); |
48 | 71 |
49 // Sets current_log to NULL, but saves the current log for future use with | 72 // 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. | 73 // ResumePausedLog(). Only one log may be paused at a time. |
51 // TODO(stuartmorgan): Pause/resume support is really a workaround for a | 74 // TODO(stuartmorgan): Pause/resume support is really a workaround for a |
52 // design issue in initial log writing; that should be fixed, and pause/resume | 75 // design issue in initial log writing; that should be fixed, and pause/resume |
53 // removed. | 76 // removed. |
54 void PauseCurrentLog(); | 77 void PauseCurrentLog(); |
55 | 78 |
(...skipping 28 matching lines...) Expand all Loading... |
84 } | 107 } |
85 | 108 |
86 // Interface for a utility class to serialize and deserialize logs for | 109 // Interface for a utility class to serialize and deserialize logs for |
87 // persistent storage. | 110 // persistent storage. |
88 class LogSerializer { | 111 class LogSerializer { |
89 public: | 112 public: |
90 virtual ~LogSerializer() {} | 113 virtual ~LogSerializer() {} |
91 | 114 |
92 // Serializes |logs| to persistent storage, replacing any previously | 115 // Serializes |logs| to persistent storage, replacing any previously |
93 // serialized logs of the same type. | 116 // serialized logs of the same type. |
94 virtual void SerializeLogs(const std::vector<std::string>& logs, | 117 virtual void SerializeLogs(const std::vector<SerializedLog>& logs, |
95 LogType log_type) = 0; | 118 LogType log_type) = 0; |
96 | 119 |
97 // Populates |logs| with logs of type |log_type| deserialized from | 120 // Populates |logs| with logs of type |log_type| deserialized from |
98 // persistent storage. | 121 // persistent storage. |
99 virtual void DeserializeLogs(LogType log_type, | 122 virtual void DeserializeLogs(LogType log_type, |
100 std::vector<std::string>* logs) = 0; | 123 std::vector<SerializedLog>* logs) = 0; |
101 }; | 124 }; |
102 | 125 |
103 // Sets the serializer to use for persisting and loading logs; takes ownership | 126 // Sets the serializer to use for persisting and loading logs; takes ownership |
104 // of |serializer|. | 127 // of |serializer|. |
105 void set_log_serializer(LogSerializer* serializer) { | 128 void set_log_serializer(LogSerializer* serializer) { |
106 log_serializer_.reset(serializer); | 129 log_serializer_.reset(serializer); |
107 } | 130 } |
108 | 131 |
109 // Saves any unsent logs to persistent storage using the current log | 132 // Saves any unsent logs to persistent storage using the current log |
110 // serializer. Can only be called after set_log_serializer. | 133 // serializer. Can only be called after set_log_serializer. |
111 void PersistUnsentLogs(); | 134 void PersistUnsentLogs(); |
112 | 135 |
113 // Loads any unsent logs from persistent storage using the current log | 136 // Loads any unsent logs from persistent storage using the current log |
114 // serializer. Can only be called after set_log_serializer. | 137 // serializer. Can only be called after set_log_serializer. |
115 void LoadPersistedUnsentLogs(); | 138 void LoadPersistedUnsentLogs(); |
116 | 139 |
117 private: | 140 private: |
118 // Compresses staged_log_ and stores the result in | 141 // Compresses |staged_log_| and stores the result in |
119 // compressed_staged_log_text_. | 142 // |compressed_staged_xml_log_text_|. |
120 void CompressStagedLog(); | 143 void CompressStagedLog(); |
121 | 144 |
122 // Compresses the text in |input| using bzip2, store the result in |output|. | 145 // Compresses the text in |input| using bzip2, store the result in |output|. |
123 static bool Bzip2Compress(const std::string& input, std::string* output); | 146 static bool Bzip2Compress(const std::string& input, std::string* output); |
124 | 147 |
125 // The log that we are still appending to. | 148 // The log that we are still appending to. |
126 scoped_ptr<MetricsLogBase> current_log_; | 149 scoped_ptr<MetricsLogBase> current_log_; |
127 | 150 |
128 // A paused, previously-current log. | 151 // A paused, previously-current log. |
129 scoped_ptr<MetricsLogBase> paused_log_; | 152 scoped_ptr<MetricsLogBase> paused_log_; |
130 | 153 |
131 // The log that we are currently transmiting, or about to try to transmit. | 154 // The log that we are currently transmiting, or about to try to transmit. |
132 // Note that when using StageNextStoredLogForUpload, this can be NULL while | 155 // Note that when using StageNextStoredLogForUpload, this can be NULL while |
133 // compressed_staged_log_text_ is non-NULL. | 156 // |compressed_staged_xml_log_text_| is non-NULL. |
134 scoped_ptr<MetricsLogBase> staged_log_; | 157 scoped_ptr<MetricsLogBase> staged_log_; |
135 | 158 |
136 // Helper class to handle serialization/deserialization of logs for persistent | 159 // Helper class to handle serialization/deserialization of logs for persistent |
137 // storage. May be NULL. | 160 // storage. May be NULL. |
138 scoped_ptr<LogSerializer> log_serializer_; | 161 scoped_ptr<LogSerializer> log_serializer_; |
139 | 162 |
140 // The compressed text of the staged log, ready for upload to the server. | 163 // The text representations of the staged log, ready for upload to the server. |
141 std::string compressed_staged_log_text_; | 164 // The first item in the pair is the compressed XML representation; the second |
| 165 // is the protobuf representation. |
| 166 SerializedLog staged_log_text_; |
142 | 167 |
143 // Logs from a previous session that have not yet been sent. | 168 // Logs from a previous session that have not yet been sent. |
| 169 // The first item in each pair is the XML representation; the second item is |
| 170 // the protobuf representation. |
144 // Note that the vector has the oldest logs listed first (early in the | 171 // 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. | 172 // vector), and we'll discard old logs if we have gathered too many logs. |
146 std::vector<std::string> unsent_initial_logs_; | 173 std::vector<SerializedLog> unsent_initial_logs_; |
147 std::vector<std::string> unsent_ongoing_logs_; | 174 std::vector<SerializedLog> unsent_ongoing_logs_; |
148 | 175 |
149 size_t max_ongoing_log_store_size_; | 176 size_t max_ongoing_log_store_size_; |
150 | 177 |
151 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); | 178 DISALLOW_COPY_AND_ASSIGN(MetricsLogManager); |
152 }; | 179 }; |
153 | 180 |
154 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ | 181 #endif // CHROME_COMMON_METRICS_METRICS_LOG_MANAGER_H_ |
OLD | NEW |