1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/content_index/content_index_metrics.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/metrics/ukm_background_recorder_service.h"
#include "content/public/browser/web_contents.h"
#include "net/base/network_change_notifier.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
namespace {
void MaybeRecordUkmContentAdded(blink::mojom::ContentCategory category,
std::optional<ukm::SourceId> source_id) {
if (!source_id)
return;
ukm::builders::ContentIndex_Added(*source_id)
.SetCategory(static_cast<int>(category))
.Record(ukm::UkmRecorder::Get());
}
void MaybeRecordUkmContentDeletedByUser(
std::optional<ukm::SourceId> source_id) {
if (!source_id)
return;
ukm::builders::ContentIndex_DeletedByUser(*source_id)
.SetDeleted(true)
.Record(ukm::UkmRecorder::Get());
}
} // namespace
ContentIndexMetrics::ContentIndexMetrics(
ukm::UkmBackgroundRecorderService* ukm_background_service)
: ukm_background_service_(ukm_background_service) {
DCHECK(ukm_background_service_);
}
ContentIndexMetrics::~ContentIndexMetrics() = default;
void ContentIndexMetrics::RecordContentAdded(
const url::Origin& origin,
blink::mojom::ContentCategory category) {
ukm_background_service_->GetBackgroundSourceIdIfAllowed(
origin, base::BindOnce(&MaybeRecordUkmContentAdded, category));
}
void ContentIndexMetrics::RecordContentOpened(
content::WebContents* web_contents,
blink::mojom::ContentCategory category) {
ukm::builders::ContentIndex_Opened(
web_contents->GetPrimaryMainFrame()->GetPageUkmSourceId())
.SetIsOffline(net::NetworkChangeNotifier::IsOffline())
.Record(ukm::UkmRecorder::Get());
}
void ContentIndexMetrics::RecordContentDeletedByUser(
const url::Origin& origin) {
ukm_background_service_->GetBackgroundSourceIdIfAllowed(
origin, base::BindOnce(&MaybeRecordUkmContentDeletedByUser));
}
|