Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(730)

Unified Diff: components/ukm/ukm_service.cc

Issue 2893943004: Refactor UKM interface for mojo-ification (Closed)
Patch Set: Fixed contextualsearch Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/ukm/ukm_service.h ('k') | components/ukm/ukm_service_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/ukm/ukm_service.cc
diff --git a/components/ukm/ukm_service.cc b/components/ukm/ukm_service.cc
index 1981f8a4fb019463622dd306f406e104dfd2f7ff..312ba041dd18ea114b72374cde4e0d4ff8781afe 100644
--- a/components/ukm/ukm_service.cc
+++ b/components/ukm/ukm_service.cc
@@ -8,33 +8,23 @@
#include <string>
#include <utility>
-#include "base/atomic_sequence_num.h"
#include "base/bind.h"
#include "base/feature_list.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_macros.h"
-#include "base/metrics/metrics_hashes.h"
#include "base/rand_util.h"
-#include "base/strings/string_number_conversions.h"
-#include "base/strings/string_split.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "components/metrics/metrics_log.h"
-#include "components/metrics/metrics_log_uploader.h"
#include "components/metrics/metrics_service_client.h"
-#include "components/metrics/proto/ukm/entry.pb.h"
#include "components/metrics/proto/ukm/report.pb.h"
-#include "components/metrics/proto/ukm/source.pb.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/ukm/persisted_logs_metrics_impl.h"
-#include "components/ukm/ukm_entry.h"
-#include "components/ukm/ukm_entry_builder.h"
#include "components/ukm/ukm_pref_names.h"
#include "components/ukm/ukm_rotation_scheduler.h"
-#include "components/ukm/ukm_source.h"
namespace ukm {
@@ -44,35 +34,6 @@ namespace {
// initialization work.
constexpr int kInitializationDelaySeconds = 5;
-// Gets the list of whitelisted Entries as string. Format is a comma seperated
-// list of Entry names (as strings).
-std::string GetWhitelistEntries() {
- return base::GetFieldTrialParamValueByFeature(kUkmFeature,
- "WhitelistEntries");
-}
-
-// Gets the maximum number of Sources we'll keep in memory before discarding any
-// new ones being added.
-size_t GetMaxSources() {
- constexpr size_t kDefaultMaxSources = 500;
- return static_cast<size_t>(base::GetFieldTrialParamByFeatureAsInt(
- kUkmFeature, "MaxSources", kDefaultMaxSources));
-}
-
-// Gets the maximum number of Entries we'll keep in memory before discarding any
-// new ones being added.
-size_t GetMaxEntries() {
- constexpr size_t kDefaultMaxEntries = 5000;
- return static_cast<size_t>(base::GetFieldTrialParamByFeatureAsInt(
- kUkmFeature, "MaxEntries", kDefaultMaxEntries));
-}
-
-// True if we should record the initial_url field of the UKM Source proto.
-bool ShouldRecordInitialUrl() {
- return base::GetFieldTrialParamByFeatureAsBool(kUkmFeature,
- "RecordInitialUrl", false);
-}
-
// True if we should record session ids in the UKM Report proto.
bool ShouldRecordSessionId() {
return base::GetFieldTrialParamByFeatureAsBool(kUkmFeature, "RecordSessionId",
@@ -105,34 +66,11 @@ int32_t LoadSessionId(PrefService* pref_service) {
return session_id;
}
-enum class DroppedDataReason {
- NOT_DROPPED = 0,
- RECORDING_DISABLED = 1,
- MAX_HIT = 2,
- NOT_WHITELISTED = 3,
- NUM_DROPPED_DATA_REASONS
-};
-
-void RecordDroppedSource(DroppedDataReason reason) {
- UMA_HISTOGRAM_ENUMERATION(
- "UKM.Sources.Dropped", static_cast<int>(reason),
- static_cast<int>(DroppedDataReason::NUM_DROPPED_DATA_REASONS));
-}
-
-void RecordDroppedEntry(DroppedDataReason reason) {
- UMA_HISTOGRAM_ENUMERATION(
- "UKM.Entries.Dropped", static_cast<int>(reason),
- static_cast<int>(DroppedDataReason::NUM_DROPPED_DATA_REASONS));
-}
-
} // namespace
-const base::Feature kUkmFeature = {"Ukm", base::FEATURE_DISABLED_BY_DEFAULT};
-
UkmService::UkmService(PrefService* pref_service,
metrics::MetricsServiceClient* client)
: pref_service_(pref_service),
- recording_enabled_(false),
client_id_(0),
session_id_(0),
client_(client),
@@ -178,14 +116,6 @@ void UkmService::Initialize() {
base::TimeDelta::FromSeconds(kInitializationDelaySeconds));
}
-void UkmService::EnableRecording() {
- recording_enabled_ = true;
-}
-
-void UkmService::DisableRecording() {
- recording_enabled_ = false;
-}
-
void UkmService::EnableReporting() {
DCHECK(thread_checker_.CalledOnValidThread());
DVLOG(1) << "UkmService::EnableReporting";
@@ -255,8 +185,7 @@ void UkmService::Purge() {
DCHECK(thread_checker_.CalledOnValidThread());
DVLOG(1) << "UkmService::Purge";
reporting_service_.ukm_log_store()->Purge();
- sources_.clear();
- entries_.clear();
+ UkmRecorderImpl::Purge();
}
// TODO(bmcquade): rename this to something more generic, like
@@ -308,7 +237,7 @@ void UkmService::BuildAndStoreLog() {
// Suppress generating a log if we have no new data to include.
// TODO(zhenw): add a histogram here to debug if this case is hitting a lot.
- if (sources_.empty() && entries_.empty())
+ if (sources().empty() && entries().empty())
return;
Report report;
@@ -316,21 +245,7 @@ void UkmService::BuildAndStoreLog() {
if (ShouldRecordSessionId())
report.set_session_id(session_id_);
- for (const auto& kv : sources_) {
- Source* proto_source = report.add_sources();
- kv.second->PopulateProto(proto_source);
- if (!ShouldRecordInitialUrl())
- proto_source->clear_initial_url();
- }
- for (const auto& entry : entries_) {
- Entry* proto_entry = report.add_entries();
- entry->PopulateProto(proto_entry);
- }
-
- UMA_HISTOGRAM_COUNTS_1000("UKM.Sources.SerializedCount", sources_.size());
- UMA_HISTOGRAM_COUNTS_1000("UKM.Entries.SerializedCount", entries_.size());
- sources_.clear();
- entries_.clear();
+ StoreRecordingsInReport(&report);
metrics::MetricsLog::RecordCoreSystemProfile(client_,
report.mutable_system_profile());
@@ -344,74 +259,4 @@ void UkmService::BuildAndStoreLog() {
reporting_service_.ukm_log_store()->StoreLog(serialized_log);
}
-// static
-int32_t UkmService::GetNewSourceID() {
- static base::StaticAtomicSequenceNumber seq;
- return seq.GetNext();
-}
-
-std::unique_ptr<UkmEntryBuilder> UkmService::GetEntryBuilder(
- int32_t source_id,
- const char* event_name) {
- return std::unique_ptr<UkmEntryBuilder>(new UkmEntryBuilder(
- base::Bind(&UkmService::AddEntry, base::Unretained(this)), source_id,
- event_name));
-}
-
-void UkmService::UpdateSourceURL(int32_t source_id, const GURL& url) {
- DCHECK(thread_checker_.CalledOnValidThread());
-
- if (!recording_enabled_) {
- RecordDroppedSource(DroppedDataReason::RECORDING_DISABLED);
- return;
- }
-
- // Update the pre-existing source if there is any. This happens when the
- // initial URL is different from the committed URL for the same source, e.g.,
- // when there is redirection.
- if (base::ContainsKey(sources_, source_id)) {
- sources_[source_id]->UpdateUrl(url);
- return;
- }
-
- if (sources_.size() >= GetMaxSources()) {
- RecordDroppedSource(DroppedDataReason::MAX_HIT);
- return;
- }
- std::unique_ptr<UkmSource> source = base::MakeUnique<UkmSource>();
- source->set_id(source_id);
- source->set_url(url);
- sources_.insert(std::make_pair(source_id, std::move(source)));
-}
-
-void UkmService::AddEntry(std::unique_ptr<UkmEntry> entry) {
- DCHECK(thread_checker_.CalledOnValidThread());
-
- if (!recording_enabled_) {
- RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED);
- return;
- }
- if (entries_.size() >= GetMaxEntries()) {
- RecordDroppedEntry(DroppedDataReason::MAX_HIT);
- return;
- }
-
- if (!whitelisted_entry_hashes_.empty() &&
- !base::ContainsKey(whitelisted_entry_hashes_, entry->event_hash())) {
- RecordDroppedEntry(DroppedDataReason::NOT_WHITELISTED);
- return;
- }
-
- entries_.push_back(std::move(entry));
-}
-
-void UkmService::StoreWhitelistedEntries() {
- const auto entries =
- base::SplitString(GetWhitelistEntries(), ",", base::TRIM_WHITESPACE,
- base::SPLIT_WANT_NONEMPTY);
- for (const auto& entry_string : entries) {
- whitelisted_entry_hashes_.insert(base::HashMetricName(entry_string));
- }
-}
-
} // namespace ukm
« no previous file with comments | « components/ukm/ukm_service.h ('k') | components/ukm/ukm_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698