• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string_view>
11 #include <utility>
12 
13 #include "base/base64.h"
14 #include "base/hash/sha1.h"
15 #include "base/metrics/histogram_macros.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/timer/elapsed_timer.h"
19 #include "components/metrics/metrics_features.h"
20 #include "components/metrics/unsent_log_store_metrics.h"
21 #include "components/prefs/pref_service.h"
22 #include "components/prefs/scoped_user_pref_update.h"
23 #include "crypto/hmac.h"
24 #include "third_party/zlib/google/compression_utils.h"
25 
26 namespace metrics {
27 
28 namespace {
29 
30 const char kLogHashKey[] = "hash";
31 const char kLogSignatureKey[] = "signature";
32 const char kLogTimestampKey[] = "timestamp";
33 const char kLogDataKey[] = "data";
34 const char kLogUnsentCountKey[] = "unsent_samples_count";
35 const char kLogSentCountKey[] = "sent_samples_count";
36 const char kLogPersistedSizeInKbKey[] = "unsent_persisted_size_in_kb";
37 const char kLogUserIdKey[] = "user_id";
38 const char kLogSourceType[] = "type";
39 
EncodeToBase64(const std::string & to_convert)40 std::string EncodeToBase64(const std::string& to_convert) {
41   DCHECK(to_convert.data());
42   return base::Base64Encode(to_convert);
43 }
44 
DecodeFromBase64(const std::string & to_convert)45 std::string DecodeFromBase64(const std::string& to_convert) {
46   std::string result;
47   base::Base64Decode(to_convert, &result);
48   return result;
49 }
50 
51 // Used to write unsent logs to prefs.
52 class LogsPrefWriter {
53  public:
54   // Create a writer that will write unsent logs to |list_value|. |list_value|
55   // should be a base::Value::List representing a pref. Clears the contents of
56   // |list_value|.
LogsPrefWriter(base::Value::List * list_value)57   explicit LogsPrefWriter(base::Value::List* list_value)
58       : list_value_(list_value) {
59     DCHECK(list_value);
60     list_value->clear();
61   }
62 
63   LogsPrefWriter(const LogsPrefWriter&) = delete;
64   LogsPrefWriter& operator=(const LogsPrefWriter&) = delete;
65 
~LogsPrefWriter()66   ~LogsPrefWriter() { DCHECK(finished_); }
67 
68   // Persists |log| by appending it to |list_value_|.
WriteLogEntry(UnsentLogStore::LogInfo * log)69   void WriteLogEntry(UnsentLogStore::LogInfo* log) {
70     DCHECK(!finished_);
71 
72     base::Value::Dict dict_value;
73     dict_value.Set(kLogHashKey, EncodeToBase64(log->hash));
74     dict_value.Set(kLogSignatureKey, EncodeToBase64(log->signature));
75     dict_value.Set(kLogDataKey, EncodeToBase64(log->compressed_log_data));
76     dict_value.Set(kLogTimestampKey, log->timestamp);
77     if (log->log_metadata.log_source_type.has_value()) {
78       dict_value.Set(
79           kLogSourceType,
80           static_cast<int>(log->log_metadata.log_source_type.value()));
81     }
82     auto user_id = log->log_metadata.user_id;
83     if (user_id.has_value()) {
84       dict_value.Set(kLogUserIdKey,
85                      EncodeToBase64(base::NumberToString(user_id.value())));
86     }
87     list_value_->Append(std::move(dict_value));
88 
89     auto samples_count = log->log_metadata.samples_count;
90     if (samples_count.has_value()) {
91       unsent_samples_count_ += samples_count.value();
92     }
93     unsent_persisted_size_ += log->compressed_log_data.length();
94     ++unsent_logs_count_;
95   }
96 
97   // Indicates to this writer that it is finished, and that it should not write
98   // any more logs. This also reverses |list_value_| in order to maintain the
99   // original order of the logs that were written.
Finish()100   void Finish() {
101     DCHECK(!finished_);
102     finished_ = true;
103     std::reverse(list_value_->begin(), list_value_->end());
104   }
105 
unsent_samples_count() const106   base::HistogramBase::Count unsent_samples_count() const {
107     return unsent_samples_count_;
108   }
109 
unsent_persisted_size() const110   size_t unsent_persisted_size() const { return unsent_persisted_size_; }
111 
unsent_logs_count() const112   size_t unsent_logs_count() const { return unsent_logs_count_; }
113 
114  private:
115   // The list where the logs will be written to. This should represent a pref.
116   raw_ptr<base::Value::List> list_value_;
117 
118   // Whether or not this writer has finished writing to pref.
119   bool finished_ = false;
120 
121   // The total number of histogram samples written so far.
122   base::HistogramBase::Count unsent_samples_count_ = 0;
123 
124   // The total size of logs written so far.
125   size_t unsent_persisted_size_ = 0;
126 
127   // The total number of logs written so far.
128   size_t unsent_logs_count_ = 0;
129 };
130 
GetString(const base::Value::Dict & dict,std::string_view key,std::string & out)131 bool GetString(const base::Value::Dict& dict,
132                std::string_view key,
133                std::string& out) {
134   const std::string* value = dict.FindString(key);
135   if (!value)
136     return false;
137   out = *value;
138   return true;
139 }
140 
141 }  // namespace
142 
143 UnsentLogStore::LogInfo::LogInfo() = default;
144 UnsentLogStore::LogInfo::~LogInfo() = default;
145 
Init(const std::string & log_data,const std::string & log_timestamp,const std::string & signing_key,const LogMetadata & optional_log_metadata)146 void UnsentLogStore::LogInfo::Init(const std::string& log_data,
147                                    const std::string& log_timestamp,
148                                    const std::string& signing_key,
149                                    const LogMetadata& optional_log_metadata) {
150   DCHECK(!log_data.empty());
151 
152   if (!compression::GzipCompress(log_data, &compressed_log_data)) {
153     DUMP_WILL_BE_NOTREACHED();
154     return;
155   }
156 
157   hash = base::SHA1HashString(log_data);
158 
159   CHECK(ComputeHMACForLog(log_data, signing_key, &signature))
160     << "HMAC signing failed";
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(std::string_view reason)263 void UnsentLogStore::DiscardStagedLog(std::string_view 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(list_).first(static_cast<size_t>(i + 1)),
310                           MetricsLogsEventManager::LogEvent::kLogTrimmed);
311         }
312         break;
313       }
314       // Omit overly large individual logs if the value is non-zero.
315       if (log_store_limits_.max_log_size_bytes != 0 &&
316           log_size > log_store_limits_.max_log_size_bytes) {
317         metrics_->RecordDroppedLogSize(log_size);
318         if (overwrite_in_memory_store) {
319           NotifyLogEvent(MetricsLogsEventManager::LogEvent::kLogTrimmed,
320                          list_[i]->hash, "Log size too large.");
321         }
322         continue;
323       }
324     }
325 
326     bytes_used += log_size;
327 
328     if (staged_log_index_ == i) {
329       staged_index_distance = writer.unsent_logs_count();
330     }
331 
332     // Append log to prefs.
333     writer.WriteLogEntry(list_[i].get());
334     if (overwrite_in_memory_store) {
335       trimmed_list.emplace_back(std::move(list_[i]));
336     }
337   }
338 
339   writer.Finish();
340 
341   if (overwrite_in_memory_store) {
342     // We went in reverse order, but appended entries. So reverse list to
343     // correct.
344     std::reverse(trimmed_list.begin(), trimmed_list.end());
345 
346     size_t dropped_logs_count = list_.size() - trimmed_list.size();
347     if (dropped_logs_count > 0)
348       metrics_->RecordDroppedLogsNum(dropped_logs_count);
349 
350     // Put the trimmed list in the correct place.
351     list_.swap(trimmed_list);
352 
353     // We may need to adjust the staged index since the number of logs may be
354     // reduced.
355     if (staged_index_distance.has_value()) {
356       staged_log_index_ = list_.size() - 1 - staged_index_distance.value();
357     } else {
358       // Set |staged_log_index_| to -1. It might already be -1. E.g., at the
359       // time we are trimming logs, there was no staged log. However, it is also
360       // possible that we trimmed away the staged log, so we need to update the
361       // index to -1.
362       staged_log_index_ = -1;
363     }
364   }
365 
366   WriteToMetricsPref(writer.unsent_samples_count(), total_samples_sent_,
367                      writer.unsent_persisted_size());
368 }
369 
LoadPersistedUnsentLogs()370 void UnsentLogStore::LoadPersistedUnsentLogs() {
371   ReadLogsFromPrefList(local_state_->GetList(log_data_pref_name_));
372   RecordMetaDataMetrics();
373 }
374 
StoreLog(const std::string & log_data,const LogMetadata & log_metadata,MetricsLogsEventManager::CreateReason reason)375 void UnsentLogStore::StoreLog(const std::string& log_data,
376                               const LogMetadata& log_metadata,
377                               MetricsLogsEventManager::CreateReason reason) {
378   std::unique_ptr<LogInfo> info = std::make_unique<LogInfo>();
379   info->Init(log_data, signing_key_, log_metadata);
380   StoreLogInfo(std::move(info), log_data.size(), reason);
381 }
382 
StoreLogInfo(std::unique_ptr<LogInfo> log_info,size_t uncompressed_log_size,MetricsLogsEventManager::CreateReason reason)383 void UnsentLogStore::StoreLogInfo(
384     std::unique_ptr<LogInfo> log_info,
385     size_t uncompressed_log_size,
386     MetricsLogsEventManager::CreateReason reason) {
387   DCHECK(log_info);
388   metrics_->RecordCompressionRatio(log_info->compressed_log_data.size(),
389                                    uncompressed_log_size);
390   NotifyLogCreated(*log_info, reason);
391   list_.emplace_back(std::move(log_info));
392 }
393 
GetLogAtIndex(size_t index)394 const std::string& UnsentLogStore::GetLogAtIndex(size_t index) {
395   DCHECK_GE(index, 0U);
396   DCHECK_LT(index, list_.size());
397   return list_[index]->compressed_log_data;
398 }
399 
ReplaceLogAtIndex(size_t index,const std::string & new_log_data,const LogMetadata & log_metadata)400 std::string UnsentLogStore::ReplaceLogAtIndex(size_t index,
401                                               const std::string& new_log_data,
402                                               const LogMetadata& log_metadata) {
403   DCHECK_GE(index, 0U);
404   DCHECK_LT(index, list_.size());
405 
406   // Avoid copying of long strings.
407   std::string old_log_data;
408   old_log_data.swap(list_[index]->compressed_log_data);
409   std::string old_timestamp;
410   old_timestamp.swap(list_[index]->timestamp);
411   std::string old_hash;
412   old_hash.swap(list_[index]->hash);
413 
414   std::unique_ptr<LogInfo> info = std::make_unique<LogInfo>();
415   info->Init(new_log_data, old_timestamp, signing_key_, log_metadata);
416   // Note that both the compression ratio of the new log and the log that is
417   // being replaced are recorded.
418   metrics_->RecordCompressionRatio(info->compressed_log_data.size(),
419                                    new_log_data.size());
420 
421   // TODO(crbug.com/40238818): Pass a message to make it clear that the new log
422   // is replacing the old log.
423   NotifyLogEvent(MetricsLogsEventManager::LogEvent::kLogDiscarded, old_hash);
424   NotifyLogCreated(*info, MetricsLogsEventManager::CreateReason::kUnknown);
425   list_[index] = std::move(info);
426   return old_log_data;
427 }
428 
Purge()429 void UnsentLogStore::Purge() {
430   NotifyLogsEvent(list_, MetricsLogsEventManager::LogEvent::kLogDiscarded,
431                   "Purged.");
432 
433   if (has_staged_log()) {
434     DiscardStagedLog();
435   }
436   list_.clear();
437   local_state_->ClearPref(log_data_pref_name_);
438   // The |total_samples_sent_| isn't cleared intentionally because it is still
439   // meaningful.
440   if (metadata_pref_name_)
441     local_state_->ClearPref(metadata_pref_name_);
442 }
443 
SetLogsEventManager(MetricsLogsEventManager * logs_event_manager)444 void UnsentLogStore::SetLogsEventManager(
445     MetricsLogsEventManager* logs_event_manager) {
446   logs_event_manager_ = logs_event_manager;
447 }
448 
ReadLogsFromPrefList(const base::Value::List & list_value)449 void UnsentLogStore::ReadLogsFromPrefList(const base::Value::List& list_value) {
450   // The below DCHECK ensures that a log from prefs is not loaded multiple
451   // times, which is important for the semantics of the NotifyLogsCreated() call
452   // below.
453   DCHECK(list_.empty());
454 
455   if (list_value.empty()) {
456     metrics_->RecordLogReadStatus(UnsentLogStoreMetrics::LIST_EMPTY);
457     return;
458   }
459 
460   const size_t log_count = list_value.size();
461 
462   list_.resize(log_count);
463 
464   for (size_t i = 0; i < log_count; ++i) {
465     const base::Value::Dict* dict = list_value[i].GetIfDict();
466     std::unique_ptr<LogInfo> info = std::make_unique<LogInfo>();
467     if (!dict || !GetString(*dict, kLogDataKey, info->compressed_log_data) ||
468         !GetString(*dict, kLogHashKey, info->hash) ||
469         !GetString(*dict, kLogTimestampKey, info->timestamp) ||
470         !GetString(*dict, kLogSignatureKey, info->signature)) {
471       // Something is wrong, so we don't try to get any persisted logs.
472       list_.clear();
473       metrics_->RecordLogReadStatus(
474           UnsentLogStoreMetrics::LOG_STRING_CORRUPTION);
475       return;
476     }
477 
478     info->compressed_log_data = DecodeFromBase64(info->compressed_log_data);
479     info->hash = DecodeFromBase64(info->hash);
480     info->signature = DecodeFromBase64(info->signature);
481     // timestamp doesn't need to be decoded.
482 
483     std::optional<int> log_source_type = dict->FindInt(kLogSourceType);
484     if (log_source_type.has_value()) {
485       info->log_metadata.log_source_type =
486           static_cast<UkmLogSourceType>(log_source_type.value());
487     }
488 
489     // Extract user id of the log if it exists.
490     const std::string* user_id_str = dict->FindString(kLogUserIdKey);
491     if (user_id_str) {
492       uint64_t user_id;
493 
494       // Only initialize the metadata if conversion was successful.
495       if (base::StringToUint64(DecodeFromBase64(*user_id_str), &user_id))
496         info->log_metadata.user_id = user_id;
497     }
498 
499     list_[i] = std::move(info);
500   }
501 
502   // Only notify log observers after loading all logs from pref instead of
503   // notifying as logs are loaded. This is because we may return early and end
504   // up not loading any logs.
505   NotifyLogsCreated(
506       list_, MetricsLogsEventManager::CreateReason::kLoadFromPreviousSession);
507 
508   metrics_->RecordLogReadStatus(UnsentLogStoreMetrics::RECALL_SUCCESS);
509 }
510 
WriteToMetricsPref(base::HistogramBase::Count unsent_samples_count,base::HistogramBase::Count sent_samples_count,size_t unsent_persisted_size) const511 void UnsentLogStore::WriteToMetricsPref(
512     base::HistogramBase::Count unsent_samples_count,
513     base::HistogramBase::Count sent_samples_count,
514     size_t unsent_persisted_size) const {
515   if (metadata_pref_name_ == nullptr)
516     return;
517 
518   ScopedDictPrefUpdate update(local_state_, metadata_pref_name_);
519   base::Value::Dict& pref_data = update.Get();
520   pref_data.Set(kLogUnsentCountKey, unsent_samples_count);
521   pref_data.Set(kLogSentCountKey, sent_samples_count);
522   // Round up to kb.
523   pref_data.Set(kLogPersistedSizeInKbKey,
524                 static_cast<int>(std::ceil(unsent_persisted_size / 1024.0)));
525 }
526 
RecordMetaDataMetrics()527 void UnsentLogStore::RecordMetaDataMetrics() {
528   if (metadata_pref_name_ == nullptr)
529     return;
530 
531   const base::Value::Dict& value = local_state_->GetDict(metadata_pref_name_);
532 
533   auto unsent_samples_count = value.FindInt(kLogUnsentCountKey);
534   auto sent_samples_count = value.FindInt(kLogSentCountKey);
535   auto unsent_persisted_size_in_kb = value.FindInt(kLogPersistedSizeInKbKey);
536 
537   if (unsent_samples_count && sent_samples_count &&
538       unsent_persisted_size_in_kb) {
539     metrics_->RecordLastUnsentLogMetadataMetrics(
540         unsent_samples_count.value(), sent_samples_count.value(),
541         unsent_persisted_size_in_kb.value());
542   }
543 }
544 
NotifyLogCreated(const LogInfo & info,MetricsLogsEventManager::CreateReason reason)545 void UnsentLogStore::NotifyLogCreated(
546     const LogInfo& info,
547     MetricsLogsEventManager::CreateReason reason) {
548   if (!logs_event_manager_)
549     return;
550   logs_event_manager_->NotifyLogCreated(info.hash, info.compressed_log_data,
551                                         info.timestamp, reason);
552 }
553 
NotifyLogsCreated(base::span<std::unique_ptr<LogInfo>> logs,MetricsLogsEventManager::CreateReason reason)554 void UnsentLogStore::NotifyLogsCreated(
555     base::span<std::unique_ptr<LogInfo>> logs,
556     MetricsLogsEventManager::CreateReason reason) {
557   if (!logs_event_manager_)
558     return;
559   for (const std::unique_ptr<LogInfo>& info : logs) {
560     logs_event_manager_->NotifyLogCreated(info->hash, info->compressed_log_data,
561                                           info->timestamp, reason);
562   }
563 }
564 
NotifyLogEvent(MetricsLogsEventManager::LogEvent event,std::string_view log_hash,std::string_view message)565 void UnsentLogStore::NotifyLogEvent(MetricsLogsEventManager::LogEvent event,
566                                     std::string_view log_hash,
567                                     std::string_view message) {
568   if (!logs_event_manager_)
569     return;
570   logs_event_manager_->NotifyLogEvent(event, log_hash, message);
571 }
572 
NotifyLogsEvent(base::span<std::unique_ptr<LogInfo>> logs,MetricsLogsEventManager::LogEvent event,std::string_view message)573 void UnsentLogStore::NotifyLogsEvent(base::span<std::unique_ptr<LogInfo>> logs,
574                                      MetricsLogsEventManager::LogEvent event,
575                                      std::string_view message) {
576   if (!logs_event_manager_)
577     return;
578   for (const std::unique_ptr<LogInfo>& info : logs) {
579     logs_event_manager_->NotifyLogEvent(event, info->hash, message);
580   }
581 }
582 
583 }  // namespace metrics
584