xref: /aosp_15_r20/external/cronet/components/metrics/unsent_log_store.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/metrics/unsent_log_store.h"
6 
7 #include <cmath>
8 #include <memory>
9 #include <string>
10 #include <utility>
11 
12 #include "base/base64.h"
13 #include "base/hash/sha1.h"
14 #include "base/metrics/histogram_macros.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h"
17 #include "base/timer/elapsed_timer.h"
18 #include "components/metrics/metrics_features.h"
19 #include "components/metrics/unsent_log_store_metrics.h"
20 #include "components/prefs/pref_service.h"
21 #include "components/prefs/scoped_user_pref_update.h"
22 #include "crypto/hmac.h"
23 #include "third_party/zlib/google/compression_utils.h"
24 
25 namespace metrics {
26 
27 namespace {
28 
29 const char kLogHashKey[] = "hash";
30 const char kLogSignatureKey[] = "signature";
31 const char kLogTimestampKey[] = "timestamp";
32 const char kLogDataKey[] = "data";
33 const char kLogUnsentCountKey[] = "unsent_samples_count";
34 const char kLogSentCountKey[] = "sent_samples_count";
35 const char kLogPersistedSizeInKbKey[] = "unsent_persisted_size_in_kb";
36 const char kLogUserIdKey[] = "user_id";
37 const char kLogSourceType[] = "type";
38 
EncodeToBase64(const std::string & to_convert)39 std::string EncodeToBase64(const std::string& to_convert) {
40   DCHECK(to_convert.data());
41   return base::Base64Encode(to_convert);
42 }
43 
DecodeFromBase64(const std::string & to_convert)44 std::string DecodeFromBase64(const std::string& to_convert) {
45   std::string result;
46   base::Base64Decode(to_convert, &result);
47   return result;
48 }
49 
50 // Used to write unsent logs to prefs.
51 class LogsPrefWriter {
52  public:
53   // Create a writer that will write unsent logs to |list_value|. |list_value|
54   // should be a base::Value::List representing a pref. Clears the contents of
55   // |list_value|.
LogsPrefWriter(base::Value::List * list_value)56   explicit LogsPrefWriter(base::Value::List* list_value)
57       : list_value_(list_value) {
58     DCHECK(list_value);
59     list_value->clear();
60   }
61 
62   LogsPrefWriter(const LogsPrefWriter&) = delete;
63   LogsPrefWriter& operator=(const LogsPrefWriter&) = delete;
64 
~LogsPrefWriter()65   ~LogsPrefWriter() { DCHECK(finished_); }
66 
67   // Persists |log| by appending it to |list_value_|.
WriteLogEntry(UnsentLogStore::LogInfo * log)68   void WriteLogEntry(UnsentLogStore::LogInfo* log) {
69     DCHECK(!finished_);
70 
71     base::Value::Dict dict_value;
72     dict_value.Set(kLogHashKey, EncodeToBase64(log->hash));
73     dict_value.Set(kLogSignatureKey, EncodeToBase64(log->signature));
74     dict_value.Set(kLogDataKey, EncodeToBase64(log->compressed_log_data));
75     dict_value.Set(kLogTimestampKey, log->timestamp);
76     if (log->log_metadata.log_source_type.has_value()) {
77       dict_value.Set(
78           kLogSourceType,
79           static_cast<int>(log->log_metadata.log_source_type.value()));
80     }
81     auto user_id = log->log_metadata.user_id;
82     if (user_id.has_value()) {
83       dict_value.Set(kLogUserIdKey,
84                      EncodeToBase64(base::NumberToString(user_id.value())));
85     }
86     list_value_->Append(std::move(dict_value));
87 
88     auto samples_count = log->log_metadata.samples_count;
89     if (samples_count.has_value()) {
90       unsent_samples_count_ += samples_count.value();
91     }
92     unsent_persisted_size_ += log->compressed_log_data.length();
93     ++unsent_logs_count_;
94   }
95 
96   // Indicates to this writer that it is finished, and that it should not write
97   // any more logs. This also reverses |list_value_| in order to maintain the
98   // original order of the logs that were written.
Finish()99   void Finish() {
100     DCHECK(!finished_);
101     finished_ = true;
102     std::reverse(list_value_->begin(), list_value_->end());
103   }
104 
unsent_samples_count() const105   base::HistogramBase::Count unsent_samples_count() const {
106     return unsent_samples_count_;
107   }
108 
unsent_persisted_size() const109   size_t unsent_persisted_size() const { return unsent_persisted_size_; }
110 
unsent_logs_count() const111   size_t unsent_logs_count() const { return unsent_logs_count_; }
112 
113  private:
114   // The list where the logs will be written to. This should represent a pref.
115   raw_ptr<base::Value::List> list_value_;
116 
117   // Whether or not this writer has finished writing to pref.
118   bool finished_ = false;
119 
120   // The total number of histogram samples written so far.
121   base::HistogramBase::Count unsent_samples_count_ = 0;
122 
123   // The total size of logs written so far.
124   size_t unsent_persisted_size_ = 0;
125 
126   // The total number of logs written so far.
127   size_t unsent_logs_count_ = 0;
128 };
129 
GetString(const base::Value::Dict & dict,base::StringPiece key,std::string & out)130 bool GetString(const base::Value::Dict& dict,
131                base::StringPiece key,
132                std::string& out) {
133   const std::string* value = dict.FindString(key);
134   if (!value)
135     return false;
136   out = *value;
137   return true;
138 }
139 
140 }  // namespace
141 
142 UnsentLogStore::LogInfo::LogInfo() = default;
143 UnsentLogStore::LogInfo::~LogInfo() = default;
144 
Init(const std::string & log_data,const std::string & log_timestamp,const std::string & signing_key,const LogMetadata & optional_log_metadata)145 void UnsentLogStore::LogInfo::Init(const std::string& log_data,
146                                    const std::string& log_timestamp,
147                                    const std::string& signing_key,
148                                    const LogMetadata& optional_log_metadata) {
149   DCHECK(!log_data.empty());
150 
151   if (!compression::GzipCompress(log_data, &compressed_log_data)) {
152     DUMP_WILL_BE_NOTREACHED_NORETURN();
153     return;
154   }
155 
156   hash = base::SHA1HashString(log_data);
157 
158   if (!ComputeHMACForLog(log_data, signing_key, &signature)) {
159     NOTREACHED() << "HMAC signing failed";
160   }
161 
162   timestamp = log_timestamp;
163   this->log_metadata = optional_log_metadata;
164 }
165 
Init(const std::string & log_data,const std::string & signing_key,const LogMetadata & optional_log_metadata)166 void UnsentLogStore::LogInfo::Init(const std::string& log_data,
167                                    const std::string& signing_key,
168                                    const LogMetadata& optional_log_metadata) {
169   Init(log_data, base::NumberToString(base::Time::Now().ToTimeT()), signing_key,
170        optional_log_metadata);
171 }
172 
UnsentLogStore(std::unique_ptr<UnsentLogStoreMetrics> metrics,PrefService * local_state,const char * log_data_pref_name,const char * metadata_pref_name,UnsentLogStoreLimits log_store_limits,const std::string & signing_key,MetricsLogsEventManager * logs_event_manager)173 UnsentLogStore::UnsentLogStore(std::unique_ptr<UnsentLogStoreMetrics> metrics,
174                                PrefService* local_state,
175                                const char* log_data_pref_name,
176                                const char* metadata_pref_name,
177                                UnsentLogStoreLimits log_store_limits,
178                                const std::string& signing_key,
179                                MetricsLogsEventManager* logs_event_manager)
180     : metrics_(std::move(metrics)),
181       local_state_(local_state),
182       log_data_pref_name_(log_data_pref_name),
183       metadata_pref_name_(metadata_pref_name),
184       log_store_limits_(log_store_limits),
185       signing_key_(signing_key),
186       logs_event_manager_(logs_event_manager),
187       staged_log_index_(-1) {
188   DCHECK(local_state_);
189   // One of the limit arguments must be non-zero.
190   DCHECK(log_store_limits_.min_log_count > 0 ||
191          log_store_limits_.min_queue_size_bytes > 0);
192 }
193 
194 UnsentLogStore::~UnsentLogStore() = default;
195 
has_unsent_logs() const196 bool UnsentLogStore::has_unsent_logs() const {
197   return !!size();
198 }
199 
200 // True if a log has been staged.
has_staged_log() const201 bool UnsentLogStore::has_staged_log() const {
202   return staged_log_index_ != -1;
203 }
204 
205 // Returns the compressed data of the element in the front of the list.
staged_log() const206 const std::string& UnsentLogStore::staged_log() const {
207   DCHECK(has_staged_log());
208   return list_[staged_log_index_]->compressed_log_data;
209 }
210 
211 // Returns the hash of element in the front of the list.
staged_log_hash() const212 const std::string& UnsentLogStore::staged_log_hash() const {
213   DCHECK(has_staged_log());
214   return list_[staged_log_index_]->hash;
215 }
216 
217 // Returns the signature of element in the front of the list.
staged_log_signature() const218 const std::string& UnsentLogStore::staged_log_signature() const {
219   DCHECK(has_staged_log());
220   return list_[staged_log_index_]->signature;
221 }
222 
223 // Returns the timestamp of the element in the front of the list.
staged_log_timestamp() const224 const std::string& UnsentLogStore::staged_log_timestamp() const {
225   DCHECK(has_staged_log());
226   return list_[staged_log_index_]->timestamp;
227 }
228 
229 // Returns the user id of the current staged log.
staged_log_user_id() const230 std::optional<uint64_t> UnsentLogStore::staged_log_user_id() const {
231   DCHECK(has_staged_log());
232   return list_[staged_log_index_]->log_metadata.user_id;
233 }
234 
staged_log_metadata() const235 const LogMetadata UnsentLogStore::staged_log_metadata() const {
236   DCHECK(has_staged_log());
237   return std::move(list_[staged_log_index_]->log_metadata);
238 }
239 
240 // static
ComputeHMACForLog(const std::string & log_data,const std::string & signing_key,std::string * signature)241 bool UnsentLogStore::ComputeHMACForLog(const std::string& log_data,
242                                        const std::string& signing_key,
243                                        std::string* signature) {
244   crypto::HMAC hmac(crypto::HMAC::SHA256);
245   const size_t digest_length = hmac.DigestLength();
246   unsigned char* hmac_data = reinterpret_cast<unsigned char*>(
247       base::WriteInto(signature, digest_length + 1));
248   return hmac.Init(signing_key) &&
249          hmac.Sign(log_data, hmac_data, digest_length);
250 }
251 
StageNextLog()252 void UnsentLogStore::StageNextLog() {
253   // CHECK, rather than DCHECK, because swap()ing with an empty list causes
254   // hard-to-identify crashes much later.
255   CHECK(!list_.empty());
256   DCHECK(!has_staged_log());
257   staged_log_index_ = list_.size() - 1;
258   NotifyLogEvent(MetricsLogsEventManager::LogEvent::kLogStaged,
259                  list_[staged_log_index_]->hash);
260   DCHECK(has_staged_log());
261 }
262 
DiscardStagedLog(base::StringPiece reason)263 void UnsentLogStore::DiscardStagedLog(base::StringPiece reason) {
264   DCHECK(has_staged_log());
265   DCHECK_LT(static_cast<size_t>(staged_log_index_), list_.size());
266   NotifyLogEvent(MetricsLogsEventManager::LogEvent::kLogDiscarded,
267                  list_[staged_log_index_]->hash, reason);
268   list_.erase(list_.begin() + staged_log_index_);
269   staged_log_index_ = -1;
270 }
271 
MarkStagedLogAsSent()272 void UnsentLogStore::MarkStagedLogAsSent() {
273   DCHECK(has_staged_log());
274   DCHECK_LT(static_cast<size_t>(staged_log_index_), list_.size());
275   auto samples_count = list_[staged_log_index_]->log_metadata.samples_count;
276   if (samples_count.has_value())
277     total_samples_sent_ += samples_count.value();
278   NotifyLogEvent(MetricsLogsEventManager::LogEvent::kLogUploaded,
279                  list_[staged_log_index_]->hash);
280 }
281 
TrimAndPersistUnsentLogs(bool overwrite_in_memory_store)282 void UnsentLogStore::TrimAndPersistUnsentLogs(bool overwrite_in_memory_store) {
283   ScopedListPrefUpdate update(local_state_, log_data_pref_name_);
284   LogsPrefWriter writer(&update.Get());
285 
286   std::vector<std::unique_ptr<LogInfo>> trimmed_list;
287   size_t bytes_used = 0;
288 
289   // The distance of the staged log from the end of the list of logs, which is
290   // usually 0 (end of list). This is used in case there is currently a staged
291   // log, which may or may not get trimmed. We want to keep track of the new
292   // position of the staged log after trimming so that we can update
293   // |staged_log_index_|.
294   std::optional<size_t> staged_index_distance;
295 
296   const bool trimming_enabled =
297       base::FeatureList::IsEnabled(features::kMetricsLogTrimming);
298 
299   // Reverse order, so newest ones are prioritized.
300   for (int i = list_.size() - 1; i >= 0; --i) {
301     size_t log_size = list_[i]->compressed_log_data.length();
302 
303     if (trimming_enabled) {
304       // Hit the caps, we can stop moving the logs.
305       if (bytes_used >= log_store_limits_.min_queue_size_bytes &&
306           writer.unsent_logs_count() >= log_store_limits_.min_log_count) {
307         // The rest of the logs (including the current one) are trimmed.
308         if (overwrite_in_memory_store) {
309           NotifyLogsEvent(base::span<std::unique_ptr<LogInfo>>(
310                               list_.begin(), list_.begin() + i + 1),
311                           MetricsLogsEventManager::LogEvent::kLogTrimmed);
312         }
313         break;
314       }
315       // Omit overly large individual logs if the value is non-zero.
316       if (log_store_limits_.max_log_size_bytes != 0 &&
317           log_size > log_store_limits_.max_log_size_bytes) {
318         metrics_->RecordDroppedLogSize(log_size);
319         if (overwrite_in_memory_store) {
320           NotifyLogEvent(MetricsLogsEventManager::LogEvent::kLogTrimmed,
321                          list_[i]->hash, "Log size too large.");
322         }
323         continue;
324       }
325     }
326 
327     bytes_used += log_size;
328 
329     if (staged_log_index_ == i) {
330       staged_index_distance = writer.unsent_logs_count();
331     }
332 
333     // Append log to prefs.
334     writer.WriteLogEntry(list_[i].get());
335     if (overwrite_in_memory_store) {
336       trimmed_list.emplace_back(std::move(list_[i]));
337     }
338   }
339 
340   writer.Finish();
341 
342   if (overwrite_in_memory_store) {
343     // We went in reverse order, but appended entries. So reverse list to
344     // correct.
345     std::reverse(trimmed_list.begin(), trimmed_list.end());
346 
347     size_t dropped_logs_count = list_.size() - trimmed_list.size();
348     if (dropped_logs_count > 0)
349       metrics_->RecordDroppedLogsNum(dropped_logs_count);
350 
351     // Put the trimmed list in the correct place.
352     list_.swap(trimmed_list);
353 
354     // We may need to adjust the staged index since the number of logs may be
355     // reduced.
356     if (staged_index_distance.has_value()) {
357       staged_log_index_ = list_.size() - 1 - staged_index_distance.value();
358     } else {
359       // Set |staged_log_index_| to -1. It might already be -1. E.g., at the
360       // time we are trimming logs, there was no staged log. However, it is also
361       // possible that we trimmed away the staged log, so we need to update the
362       // index to -1.
363       staged_log_index_ = -1;
364     }
365   }
366 
367   WriteToMetricsPref(writer.unsent_samples_count(), total_samples_sent_,
368                      writer.unsent_persisted_size());
369 }
370 
LoadPersistedUnsentLogs()371 void UnsentLogStore::LoadPersistedUnsentLogs() {
372   ReadLogsFromPrefList(local_state_->GetList(log_data_pref_name_));
373   RecordMetaDataMetrics();
374 }
375 
StoreLog(const std::string & log_data,const LogMetadata & log_metadata,MetricsLogsEventManager::CreateReason reason)376 void UnsentLogStore::StoreLog(const std::string& log_data,
377                               const LogMetadata& log_metadata,
378                               MetricsLogsEventManager::CreateReason reason) {
379   std::unique_ptr<LogInfo> info = std::make_unique<LogInfo>();
380   info->Init(log_data, signing_key_, log_metadata);
381   StoreLogInfo(std::move(info), log_data.size(), reason);
382 }
383 
StoreLogInfo(std::unique_ptr<LogInfo> log_info,size_t uncompressed_log_size,MetricsLogsEventManager::CreateReason reason)384 void UnsentLogStore::StoreLogInfo(
385     std::unique_ptr<LogInfo> log_info,
386     size_t uncompressed_log_size,
387     MetricsLogsEventManager::CreateReason reason) {
388   DCHECK(log_info);
389   metrics_->RecordCompressionRatio(log_info->compressed_log_data.size(),
390                                    uncompressed_log_size);
391   NotifyLogCreated(*log_info, reason);
392   list_.emplace_back(std::move(log_info));
393 }
394 
GetLogAtIndex(size_t index)395 const std::string& UnsentLogStore::GetLogAtIndex(size_t index) {
396   DCHECK_GE(index, 0U);
397   DCHECK_LT(index, list_.size());
398   return list_[index]->compressed_log_data;
399 }
400 
ReplaceLogAtIndex(size_t index,const std::string & new_log_data,const LogMetadata & log_metadata)401 std::string UnsentLogStore::ReplaceLogAtIndex(size_t index,
402                                               const std::string& new_log_data,
403                                               const LogMetadata& log_metadata) {
404   DCHECK_GE(index, 0U);
405   DCHECK_LT(index, list_.size());
406 
407   // Avoid copying of long strings.
408   std::string old_log_data;
409   old_log_data.swap(list_[index]->compressed_log_data);
410   std::string old_timestamp;
411   old_timestamp.swap(list_[index]->timestamp);
412   std::string old_hash;
413   old_hash.swap(list_[index]->hash);
414 
415   std::unique_ptr<LogInfo> info = std::make_unique<LogInfo>();
416   info->Init(new_log_data, old_timestamp, signing_key_, log_metadata);
417   // Note that both the compression ratio of the new log and the log that is
418   // being replaced are recorded.
419   metrics_->RecordCompressionRatio(info->compressed_log_data.size(),
420                                    new_log_data.size());
421 
422   // TODO(crbug/1363747): Pass a message to make it clear that the new log is
423   // replacing the old log.
424   NotifyLogEvent(MetricsLogsEventManager::LogEvent::kLogDiscarded, old_hash);
425   NotifyLogCreated(*info, MetricsLogsEventManager::CreateReason::kUnknown);
426   list_[index] = std::move(info);
427   return old_log_data;
428 }
429 
Purge()430 void UnsentLogStore::Purge() {
431   NotifyLogsEvent(list_, MetricsLogsEventManager::LogEvent::kLogDiscarded,
432                   "Purged.");
433 
434   if (has_staged_log()) {
435     DiscardStagedLog();
436   }
437   list_.clear();
438   local_state_->ClearPref(log_data_pref_name_);
439   // The |total_samples_sent_| isn't cleared intentionally because it is still
440   // meaningful.
441   if (metadata_pref_name_)
442     local_state_->ClearPref(metadata_pref_name_);
443 }
444 
SetLogsEventManager(MetricsLogsEventManager * logs_event_manager)445 void UnsentLogStore::SetLogsEventManager(
446     MetricsLogsEventManager* logs_event_manager) {
447   logs_event_manager_ = logs_event_manager;
448 }
449 
ReadLogsFromPrefList(const base::Value::List & list_value)450 void UnsentLogStore::ReadLogsFromPrefList(const base::Value::List& list_value) {
451   // The below DCHECK ensures that a log from prefs is not loaded multiple
452   // times, which is important for the semantics of the NotifyLogsCreated() call
453   // below.
454   DCHECK(list_.empty());
455 
456   if (list_value.empty()) {
457     metrics_->RecordLogReadStatus(UnsentLogStoreMetrics::LIST_EMPTY);
458     return;
459   }
460 
461   const size_t log_count = list_value.size();
462 
463   list_.resize(log_count);
464 
465   for (size_t i = 0; i < log_count; ++i) {
466     const base::Value::Dict* dict = list_value[i].GetIfDict();
467     std::unique_ptr<LogInfo> info = std::make_unique<LogInfo>();
468     if (!dict || !GetString(*dict, kLogDataKey, info->compressed_log_data) ||
469         !GetString(*dict, kLogHashKey, info->hash) ||
470         !GetString(*dict, kLogTimestampKey, info->timestamp) ||
471         !GetString(*dict, kLogSignatureKey, info->signature)) {
472       // Something is wrong, so we don't try to get any persisted logs.
473       list_.clear();
474       metrics_->RecordLogReadStatus(
475           UnsentLogStoreMetrics::LOG_STRING_CORRUPTION);
476       return;
477     }
478 
479     info->compressed_log_data = DecodeFromBase64(info->compressed_log_data);
480     info->hash = DecodeFromBase64(info->hash);
481     info->signature = DecodeFromBase64(info->signature);
482     // timestamp doesn't need to be decoded.
483 
484     std::optional<int> log_source_type = dict->FindInt(kLogSourceType);
485     if (log_source_type.has_value()) {
486       info->log_metadata.log_source_type =
487           static_cast<UkmLogSourceType>(log_source_type.value());
488     }
489 
490     // Extract user id of the log if it exists.
491     const std::string* user_id_str = dict->FindString(kLogUserIdKey);
492     if (user_id_str) {
493       uint64_t user_id;
494 
495       // Only initialize the metadata if conversion was successful.
496       if (base::StringToUint64(DecodeFromBase64(*user_id_str), &user_id))
497         info->log_metadata.user_id = user_id;
498     }
499 
500     list_[i] = std::move(info);
501   }
502 
503   // Only notify log observers after loading all logs from pref instead of
504   // notifying as logs are loaded. This is because we may return early and end
505   // up not loading any logs.
506   NotifyLogsCreated(
507       list_, MetricsLogsEventManager::CreateReason::kLoadFromPreviousSession);
508 
509   metrics_->RecordLogReadStatus(UnsentLogStoreMetrics::RECALL_SUCCESS);
510 }
511 
WriteToMetricsPref(base::HistogramBase::Count unsent_samples_count,base::HistogramBase::Count sent_samples_count,size_t unsent_persisted_size) const512 void UnsentLogStore::WriteToMetricsPref(
513     base::HistogramBase::Count unsent_samples_count,
514     base::HistogramBase::Count sent_samples_count,
515     size_t unsent_persisted_size) const {
516   if (metadata_pref_name_ == nullptr)
517     return;
518 
519   ScopedDictPrefUpdate update(local_state_, metadata_pref_name_);
520   base::Value::Dict& pref_data = update.Get();
521   pref_data.Set(kLogUnsentCountKey, unsent_samples_count);
522   pref_data.Set(kLogSentCountKey, sent_samples_count);
523   // Round up to kb.
524   pref_data.Set(kLogPersistedSizeInKbKey,
525                 static_cast<int>(std::ceil(unsent_persisted_size / 1024.0)));
526 }
527 
RecordMetaDataMetrics()528 void UnsentLogStore::RecordMetaDataMetrics() {
529   if (metadata_pref_name_ == nullptr)
530     return;
531 
532   const base::Value::Dict& value = local_state_->GetDict(metadata_pref_name_);
533 
534   auto unsent_samples_count = value.FindInt(kLogUnsentCountKey);
535   auto sent_samples_count = value.FindInt(kLogSentCountKey);
536   auto unsent_persisted_size_in_kb = value.FindInt(kLogPersistedSizeInKbKey);
537 
538   if (unsent_samples_count && sent_samples_count &&
539       unsent_persisted_size_in_kb) {
540     metrics_->RecordLastUnsentLogMetadataMetrics(
541         unsent_samples_count.value(), sent_samples_count.value(),
542         unsent_persisted_size_in_kb.value());
543   }
544 }
545 
NotifyLogCreated(const LogInfo & info,MetricsLogsEventManager::CreateReason reason)546 void UnsentLogStore::NotifyLogCreated(
547     const LogInfo& info,
548     MetricsLogsEventManager::CreateReason reason) {
549   if (!logs_event_manager_)
550     return;
551   logs_event_manager_->NotifyLogCreated(info.hash, info.compressed_log_data,
552                                         info.timestamp, reason);
553 }
554 
NotifyLogsCreated(base::span<std::unique_ptr<LogInfo>> logs,MetricsLogsEventManager::CreateReason reason)555 void UnsentLogStore::NotifyLogsCreated(
556     base::span<std::unique_ptr<LogInfo>> logs,
557     MetricsLogsEventManager::CreateReason reason) {
558   if (!logs_event_manager_)
559     return;
560   for (const std::unique_ptr<LogInfo>& info : logs) {
561     logs_event_manager_->NotifyLogCreated(info->hash, info->compressed_log_data,
562                                           info->timestamp, reason);
563   }
564 }
565 
NotifyLogEvent(MetricsLogsEventManager::LogEvent event,base::StringPiece log_hash,base::StringPiece message)566 void UnsentLogStore::NotifyLogEvent(MetricsLogsEventManager::LogEvent event,
567                                     base::StringPiece log_hash,
568                                     base::StringPiece message) {
569   if (!logs_event_manager_)
570     return;
571   logs_event_manager_->NotifyLogEvent(event, log_hash, message);
572 }
573 
NotifyLogsEvent(base::span<std::unique_ptr<LogInfo>> logs,MetricsLogsEventManager::LogEvent event,base::StringPiece message)574 void UnsentLogStore::NotifyLogsEvent(base::span<std::unique_ptr<LogInfo>> logs,
575                                      MetricsLogsEventManager::LogEvent event,
576                                      base::StringPiece message) {
577   if (!logs_event_manager_)
578     return;
579   for (const std::unique_ptr<LogInfo>& info : logs) {
580     logs_event_manager_->NotifyLogEvent(event, info->hash, message);
581   }
582 }
583 
584 }  // namespace metrics
585