summaryrefslogtreecommitdiff
path: root/chromium/fuchsia/base/legacymetrics_client.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/fuchsia/base/legacymetrics_client.cc')
-rw-r--r--chromium/fuchsia/base/legacymetrics_client.cc85
1 files changed, 62 insertions, 23 deletions
diff --git a/chromium/fuchsia/base/legacymetrics_client.cc b/chromium/fuchsia/base/legacymetrics_client.cc
index a2d45a83ac8..58f384124c1 100644
--- a/chromium/fuchsia/base/legacymetrics_client.cc
+++ b/chromium/fuchsia/base/legacymetrics_client.cc
@@ -11,8 +11,8 @@
#include <utility>
#include <vector>
-#include "base/fuchsia/default_context.h"
#include "base/fuchsia/fuchsia_logging.h"
+#include "base/fuchsia/process_context.h"
#include "base/logging.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
@@ -34,11 +34,13 @@ void LegacyMetricsClient::Start(base::TimeDelta report_interval) {
DCHECK(!metrics_recorder_) << "Start() called more than once.";
report_interval_ = report_interval;
- metrics_recorder_ = base::fuchsia::ComponentContextForCurrentProcess()
+ metrics_recorder_ = base::ComponentContextForProcess()
->svc()
->Connect<fuchsia::legacymetrics::MetricsRecorder>();
metrics_recorder_.set_error_handler(fit::bind_member(
this, &LegacyMetricsClient::OnMetricsRecorderDisconnected));
+ metrics_recorder_.events().OnCloseSoon =
+ fit::bind_member(this, &LegacyMetricsClient::OnCloseSoon);
user_events_recorder_ = std::make_unique<LegacyMetricsUserActionRecorder>();
ScheduleNextReport();
}
@@ -54,7 +56,18 @@ void LegacyMetricsClient::SetReportAdditionalMetricsCallback(
report_additional_callback_ = std::move(callback);
}
+void LegacyMetricsClient::SetNotifyFlushCallback(NotifyFlushCallback callback) {
+ DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
+ DCHECK(callback);
+ DCHECK(!metrics_recorder_)
+ << "SetNotifyFlushCallback() must be called before Start().";
+
+ notify_flush_callback_ = std::move(callback);
+}
+
void LegacyMetricsClient::ScheduleNextReport() {
+ DCHECK(!is_flushing_);
+
DVLOG(1) << "Scheduling next report in " << report_interval_.InSeconds()
<< "seconds.";
timer_.Start(FROM_HERE, report_interval_, this,
@@ -91,44 +104,70 @@ void LegacyMetricsClient::Report(
}
}
- if (events.empty()) {
- ScheduleNextReport();
- return;
- }
+ std::move(events.begin(), events.end(), std::back_inserter(to_send_));
- DrainBuffer(std::move(events));
+ DrainBuffer();
}
-void LegacyMetricsClient::DrainBuffer(
- std::vector<fuchsia::legacymetrics::Event> buffer) {
- if (buffer.empty()) {
+void LegacyMetricsClient::DrainBuffer() {
+ DVLOG(1) << __func__ << " called.";
+
+ if (record_ack_pending_) {
+ // There is a Record() call already inflight. When it is acknowledged,
+ // buffer draining will continue.
+ return;
+ }
+
+ if (to_send_.empty()) {
DVLOG(1) << "Buffer drained.";
- ScheduleNextReport();
+
+ if (is_flushing_) {
+ metrics_recorder_.Unbind();
+ } else {
+ ScheduleNextReport();
+ }
+
return;
}
- // Since ordering doesn't matter, we can efficiently drain |buffer| by
+ // Since ordering doesn't matter, we can efficiently drain |to_send_| by
// repeatedly sending and truncating its tail.
- const size_t batch_size = std::min(buffer.size(), kMaxBatchSize);
- const size_t batch_start_idx = buffer.size() - batch_size;
+ const size_t batch_size = std::min(to_send_.size(), kMaxBatchSize);
+ const size_t batch_start_idx = to_send_.size() - batch_size;
std::vector<fuchsia::legacymetrics::Event> batch;
batch.resize(batch_size);
- std::move(buffer.begin() + batch_start_idx, buffer.end(), batch.begin());
- buffer.resize(buffer.size() - batch_size);
-
- metrics_recorder_->Record(std::move(batch),
- [this, buffer = std::move(buffer)]() mutable {
- DrainBuffer(std::move(buffer));
- });
+ std::move(to_send_.begin() + batch_start_idx, to_send_.end(), batch.begin());
+ to_send_.resize(to_send_.size() - batch_size);
+
+ record_ack_pending_ = true;
+ metrics_recorder_->Record(std::move(batch), [this]() {
+ record_ack_pending_ = false;
+ DrainBuffer();
+ });
}
void LegacyMetricsClient::OnMetricsRecorderDisconnected(zx_status_t status) {
- ZX_LOG_IF(ERROR, status != ZX_ERR_PEER_CLOSED, status)
- << "MetricsRecorder connection lost.";
+ ZX_LOG(ERROR, status) << "MetricsRecorder connection lost.";
// Stop recording & reporting user events.
user_events_recorder_.reset();
timer_.AbandonAndStop();
}
+void LegacyMetricsClient::OnCloseSoon() {
+ DVLOG(1) << __func__ << " called.";
+
+ timer_.AbandonAndStop();
+
+ is_flushing_ = true;
+ if (notify_flush_callback_) {
+ // Defer reporting until the flush operation has finished.
+ std::move(notify_flush_callback_)
+ .Run(base::BindOnce(&LegacyMetricsClient::StartReport,
+ weak_factory_.GetWeakPtr()));
+ } else {
+ StartReport();
+ }
+}
+
} // namespace cr_fuchsia