Commit bd970cbf authored by Steven Holte's avatar Steven Holte Committed by Commit Bot

Add a method for converting ukm::SourceId from other ID types.

This gathers the code relating to SourceId into a new file, and adds
a conversion method from other ID types.  External ID types aren't
converted when recieving IDs from other processes.

Change-Id: I3c65406457666f9d099d654a75db7e13a53590d7
Reviewed-on: https://chromium-review.googlesource.com/596725
Commit-Queue: Steven Holte <holte@chromium.org>
Reviewed-by: default avatarOystein Eftevaag <oysteine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491602}
parent 334e05fb
......@@ -49,12 +49,14 @@ static_library("ukm_interface") {
public_deps = [
"//base",
"//services/metrics/public/cpp:metrics_cpp",
"//services/metrics/public/interfaces",
]
deps = [
":ukm",
"//mojo/public/cpp/bindings",
"//services/metrics/public/cpp:metrics_cpp",
]
}
......
......@@ -8,24 +8,11 @@
#include "base/memory/ptr_util.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "url/gurl.h"
namespace ukm {
namespace {
// Map source ids from different instances into unique namespaces, so that
// clients can create thier own IDs without having them collide.
// This won't be necessary once we switch to using CoordinationUnitIDs.
int64_t ConvertSourceId(int64_t source_id, int64_t instance_id) {
const int64_t low_bits = (INT64_C(1) << 32) - 1;
// Neither ID should get large enough to cause an issue, but explicitly
// discard down to 32 bits anyway.
return ((instance_id & low_bits) << 32) | (source_id & low_bits);
}
} // namespace
UkmInterface::UkmInterface(UkmRecorder* ukm_recorder, int64_t instance_id)
: ukm_recorder_(ukm_recorder), instance_id_(instance_id) {}
......@@ -42,13 +29,14 @@ void UkmInterface::Create(UkmRecorder* ukm_recorder,
}
void UkmInterface::AddEntry(mojom::UkmEntryPtr ukm_entry) {
ukm_entry->source_id = ConvertSourceId(instance_id_, ukm_entry->source_id);
ukm_entry->source_id =
ConvertSourceIdFromInstance(instance_id_, ukm_entry->source_id);
ukm_recorder_->AddEntry(std::move(ukm_entry));
}
void UkmInterface::UpdateSourceURL(int64_t source_id, const std::string& url) {
ukm_recorder_->UpdateSourceURL(ConvertSourceId(instance_id_, source_id),
GURL(url));
ukm_recorder_->UpdateSourceURL(
ConvertSourceIdFromInstance(instance_id_, source_id), GURL(url));
}
} // namespace ukm
......@@ -5,19 +5,17 @@
#ifndef COMPONENTS_UKM_UKM_SOURCE_H_
#define COMPONENTS_UKM_UKM_SOURCE_H_
#include <stddef.h>
#include <map>
#include "base/macros.h"
#include "base/time/time.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "url/gurl.h"
namespace ukm {
class Source;
typedef int64_t SourceId;
// Contains UKM data for a single navigation entry.
class UkmSource {
public:
......
......@@ -15,6 +15,8 @@ component("metrics_cpp") {
"ukm_entry_builder_base.h",
"ukm_recorder.cc",
"ukm_recorder.h",
"ukm_source_id.cc",
"ukm_source_id.h",
]
defines = [ "METRICS_IMPLEMENTATION" ]
......
......@@ -9,12 +9,11 @@
#include "base/macros.h"
#include "services/metrics/public/cpp/metrics_export.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/metrics/public/interfaces/ukm_interface.mojom.h"
namespace ukm {
typedef int64_t SourceId;
// The builder that builds UkmEntry and adds it to UkmRecorder.
// The example usage is:
//
......
......@@ -4,7 +4,6 @@
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "base/atomic_sequence_num.h"
#include "base/bind.h"
#include "base/feature_list.h"
#include "base/memory/ptr_util.h"
......@@ -33,8 +32,7 @@ UkmRecorder* UkmRecorder::Get() {
// static
ukm::SourceId UkmRecorder::GetNewSourceID() {
static base::AtomicSequenceNumber seq;
return static_cast<ukm::SourceId>(seq.GetNext());
return AssignNewSourceId();
}
std::unique_ptr<UkmEntryBuilder> UkmRecorder::GetEntryBuilder(
......
......@@ -5,8 +5,6 @@
#ifndef SERVICES_METRICS_PUBLIC_CPP_UKM_RECORDER_H_
#define SERVICES_METRICS_PUBLIC_CPP_UKM_RECORDER_H_
#include <stddef.h>
#include <memory>
#include "base/callback.h"
......@@ -15,6 +13,7 @@
#include "base/threading/thread_checker.h"
#include "services/metrics/public/cpp/metrics_export.h"
#include "services/metrics/public/cpp/ukm_entry_builder.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/metrics/public/interfaces/ukm_interface.mojom.h"
#include "url/gurl.h"
......@@ -76,8 +75,6 @@ class UkmEntryBuilderBase;
// This feature controls whether UkmService should be created.
METRICS_EXPORT extern const base::Feature kUkmFeature;
typedef int64_t SourceId;
// Interface for recording UKM
class METRICS_EXPORT UkmRecorder {
public:
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "base/atomic_sequence_num.h"
#include "base/logging.h"
namespace ukm {
namespace {
const int64_t kLowBitsMask = (INT64_C(1) << 32) - 1;
const int64_t kNumTypeBits = 2;
const int64_t kTypeMask = (INT64_C(1) << kNumTypeBits) - 1;
} // namespace
SourceId AssignNewSourceId() {
static base::AtomicSequenceNumber seq;
return ConvertToSourceId(seq.GetNext() + 1, SourceIdType::UKM);
}
SourceId ConvertToSourceId(int64_t other_id, SourceIdType id_type) {
const int64_t type_bits = static_cast<int64_t>(id_type);
DCHECK_EQ(type_bits, type_bits & kTypeMask);
// Stores the the type ID in the low bits of the source id, and shift the rest
// of the ID to make room. This could cause the original ID to overflow, but
// that should be rare enough that it won't matter for UKM's purposes.
return (other_id << kNumTypeBits) | type_bits;
}
SourceId ConvertSourceIdFromInstance(int64_t instance_id, int64_t source_id) {
// Only convert source IDs from Create().
if ((kTypeMask & source_id) != static_cast<int64_t>(SourceIdType::UKM))
return source_id;
// Neither ID should get large enough to cause an issue, but explicitly
// discard down to 32 bits anyway.
return ((instance_id & kLowBitsMask) << 32) | (source_id & kLowBitsMask);
}
} // namespace ukm
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_METRICS_PUBLIC_CPP_UKM_SOURCE_ID_H_
#define SERVICES_METRICS_PUBLIC_CPP_UKM_SOURCE_ID_H_
#include <stdint.h>
#include "services/metrics/public/cpp/metrics_export.h"
namespace ukm {
typedef int64_t SourceId;
enum class SourceIdType : int64_t {
UKM = 0,
NAVIGATION_ID = 1,
};
// Get a new source ID, which is unique for the duration of a browser session.
METRICS_EXPORT SourceId AssignNewSourceId();
// Utility for converting other unique ids to source ids.
METRICS_EXPORT SourceId ConvertToSourceId(int64_t other_id,
SourceIdType id_type);
// Convert source ids from another process.
METRICS_EXPORT SourceId ConvertSourceIdFromInstance(int64_t instance_id,
int64_t source_id);
} // namespace ukm
#endif // SERVICES_METRICS_PUBLIC_CPP_UKM_SOURCE_ID_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment