Skip to content

Commit

Permalink
Define AttributionSourceType and AttributionReport::Type enums in Mojo
Browse files Browse the repository at this point in the history
Instead of defining a redundant enum mapping between C++ and Mojo.

https://chromium.googlesource.com/chromium/src/+/main/docs/security/mojo.md#using-mojo-enums-directly-when-possible

Change-Id: I541f0a2d87b00c6f8afd82d5376714329ccb9093
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3933377
Quick-Run: Andrew Paseltiner <apaseltiner@chromium.org>
Reviewed-by: Arthur Sonzogni <arthursonzogni@chromium.org>
Commit-Queue: Andrew Paseltiner <apaseltiner@chromium.org>
Reviewed-by: Robert Sesek <rsesek@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1055262}
  • Loading branch information
Andrew Paseltiner authored and Chromium LUCI CQ committed Oct 5, 2022
1 parent 7890d24 commit ca2a82d
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 112 deletions.
1 change: 1 addition & 0 deletions content/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ grit("dev_ui_content_resources") {
]
deps = [
"//content/browser/aggregation_service:mojo_bindings_webui_js",
"//content/browser/attribution_reporting:internals_mojo_bindings_webui_js",
"//content/browser/attribution_reporting:mojo_bindings_webui_js",
"//content/browser/preloading/prerender:mojo_bindings_webui_js",
"//content/browser/resources/aggregation_service:build_ts",
Expand Down
1 change: 1 addition & 0 deletions content/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ source_set("browser") {
"//content/browser/aggregation_service:mojo_bindings",
"//content/browser/aggregation_service/proto:aggregatable_report_proto",
"//content/browser/attribution_reporting:attribution_reporting_proto",
"//content/browser/attribution_reporting:internals_mojo_bindings",
"//content/browser/attribution_reporting:mojo_bindings",
"//content/browser/background_fetch:background_fetch_proto",
"//content/browser/background_sync:background_sync_proto",
Expand Down
19 changes: 9 additions & 10 deletions content/browser/attribution_reporting/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ proto_library("attribution_reporting_proto") {
}

mojom("mojo_bindings") {
sources = [ "attribution_reporting.mojom" ]
webui_module_path = "/"
}

mojom("internals_mojo_bindings") {
sources = [ "attribution_internals.mojom" ]
public_deps = [ "//url/mojom:url_mojom_origin" ]
public_deps = [
":mojo_bindings",
"//url/mojom:url_mojom_origin",
]
webui_module_path = "/"

cpp_typemaps = [
Expand All @@ -29,19 +37,10 @@ mojom("mojo_bindings") {
mojom = "attribution_internals.mojom.ReportID"
cpp = "::content::AttributionReport::Id"
},
{
mojom = "attribution_internals.mojom.SourceType"
cpp = "::content::AttributionSourceType"
},
{
mojom = "attribution_internals.mojom.ReportType"
cpp = "::content::AttributionReport::Type"
},
]
traits_headers = [
"attribution_internals_mojom_traits.h",
"attribution_report.h",
"attribution_source_type.h",
]
traits_sources = [ "attribution_internals_mojom_traits.cc" ]
traits_deps = [
Expand Down
19 changes: 4 additions & 15 deletions content/browser/attribution_reporting/attribution_internals.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@

module attribution_internals.mojom;

import "content/browser/attribution_reporting/attribution_reporting.mojom";
import "url/mojom/origin.mojom";
import "url/mojom/url.mojom";

// Represents AttributionSourceType.
enum SourceType {
kNavigation,
kEvent,
};

// Represents `AttributionReport::EventLevelData::Id`.
// Will be used to issue commands for individual reports.
struct EventLevelReportID {
Expand Down Expand Up @@ -94,7 +89,7 @@ struct WebUISource {
url.mojom.Origin reporting_origin;
double source_time;
double expiry_time;
SourceType source_type;
attribution_reporting.mojom.SourceType source_type;
int64 priority;
DebugKey? debug_key;
array<uint64> dedup_keys;
Expand Down Expand Up @@ -178,12 +173,6 @@ struct WebUITrigger {
Status aggregatable_status;
};

// Represents `AttributionReport::ReportType`.
enum ReportType {
kEventLevel,
kAggregatableAttribution,
};

// Struct containing reason for a source registration failure to be displayed
// as a log by WebUI
struct FailedSourceRegistration {
Expand All @@ -200,7 +189,7 @@ interface Observer {

// Called when the reports in storage changed, indicating that the observer
// should call `Handler::GetReports()`.
OnReportsChanged(ReportType report_type);
OnReportsChanged(attribution_reporting.mojom.ReportType report_type);

// Called when a source-registration attempt fails.
OnSourceRejected(WebUISource source);
Expand Down Expand Up @@ -233,7 +222,7 @@ interface Handler {

// Returns all reports of the specified type contained in storage, including
// those that are actively being sent.
GetReports(ReportType report_type) => (array<WebUIReport> reports);
GetReports(attribution_reporting.mojom.ReportType report_type) => (array<WebUIReport> reports);

// Sends the given reports, ignoring delay, returning when the operation has
// been completed and the reports have been cleared from storage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,11 @@

#include "content/browser/attribution_reporting/attribution_internals.mojom.h"
#include "content/browser/attribution_reporting/attribution_report.h"
#include "content/browser/attribution_reporting/attribution_source_type.h"
#include "mojo/public/cpp/bindings/enum_traits.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
#include "mojo/public/cpp/bindings/union_traits.h"

namespace mojo {

template <>
class EnumTraits<attribution_internals::mojom::SourceType,
content::AttributionSourceType> {
public:
static attribution_internals::mojom::SourceType ToMojom(
content::AttributionSourceType input) {
switch (input) {
case content::AttributionSourceType::kNavigation:
return attribution_internals::mojom::SourceType::kNavigation;
case content::AttributionSourceType::kEvent:
return attribution_internals::mojom::SourceType::kEvent;
}
}

static bool FromMojom(attribution_internals::mojom::SourceType input,
content::AttributionSourceType* out) {
switch (input) {
case attribution_internals::mojom::SourceType::kNavigation:
*out = content::AttributionSourceType::kNavigation;
break;
case attribution_internals::mojom::SourceType::kEvent:
*out = content::AttributionSourceType::kEvent;
break;
}

return true;
}
};

template <>
class EnumTraits<attribution_internals::mojom::ReportType,
content::AttributionReport::Type> {
public:
static attribution_internals::mojom::ReportType ToMojom(
content::AttributionReport::Type input) {
switch (input) {
case content::AttributionReport::Type::kEventLevel:
return attribution_internals::mojom::ReportType::kEventLevel;
case content::AttributionReport::Type::kAggregatableAttribution:
return attribution_internals::mojom::ReportType::
kAggregatableAttribution;
}
}

static bool FromMojom(attribution_internals::mojom::ReportType input,
content::AttributionReport::Type* out) {
switch (input) {
case attribution_internals::mojom::ReportType::kEventLevel:
*out = content::AttributionReport::Type::kEventLevel;
break;
case attribution_internals::mojom::ReportType::kAggregatableAttribution:
*out = content::AttributionReport::Type::kAggregatableAttribution;
break;
}

return true;
}
};

template <>
class StructTraits<attribution_internals::mojom::EventLevelReportIDDataView,
content::AttributionReport::EventLevelData::Id> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ AttributionInternalsUI::AttributionInternalsUI(WebUI* web_ui)
IDR_ATTRIBUTION_INTERNALS_TABLE_JS);
source->AddResourcePath("attribution_internals_table.html.js",
IDR_ATTRIBUTION_INTERNALS_TABLE_HTML_JS);
source->AddResourcePath("attribution_reporting.mojom-webui.js",
IDR_ATTRIBUTION_REPORTING_MOJOM_JS);
source->AddResourcePath("table_model.js",
IDR_ATTRIBUTION_INTERNALS_TABLE_MODEL_JS);
source->AddResourcePath("attribution_internals.css",
Expand Down
8 changes: 2 additions & 6 deletions content/browser/attribution_reporting/attribution_report.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "content/browser/aggregation_service/aggregatable_report.h"
#include "content/browser/attribution_reporting/aggregatable_histogram_contribution.h"
#include "content/browser/attribution_reporting/attribution_info.h"
#include "content/browser/attribution_reporting/attribution_reporting.mojom.h"
#include "content/common/content_export.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
Expand All @@ -30,12 +31,7 @@ namespace content {
// report. This class can represent multiple different types of reports.
class CONTENT_EXPORT AttributionReport {
public:
enum class Type {
kEventLevel = 0,
kAggregatableAttribution = 1,
kMinValue = kEventLevel,
kMaxValue = kAggregatableAttribution,
};
using Type = ::attribution_reporting::mojom::ReportType;

using Types = base::EnumSet<Type, Type::kMinValue, Type::kMaxValue>;

Expand Down
21 changes: 21 additions & 0 deletions content/browser/attribution_reporting/attribution_reporting.mojom
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

module attribution_reporting.mojom;

enum ReportType {
// An event-level report.
// https://github.com/WICG/attribution-reporting-api/blob/main/EVENT.md
kEventLevel = 0,
// An aggregatable report.
// https://github.com/WICG/attribution-reporting-api/blob/main/AGGREGATE.md
kAggregatableAttribution = 1,
};

enum SourceType {
// A source that was associated with a top-level navigation.
kNavigation = 0,
// A source that was not associated with a top-level navigation.
kEvent = 1,
};
15 changes: 3 additions & 12 deletions content/browser/attribution_reporting/attribution_source_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,14 @@
#ifndef CONTENT_BROWSER_ATTRIBUTION_REPORTING_ATTRIBUTION_SOURCE_TYPE_H_
#define CONTENT_BROWSER_ATTRIBUTION_REPORTING_ATTRIBUTION_SOURCE_TYPE_H_

#include "content/common/content_export.h"
#include "content/browser/attribution_reporting/attribution_reporting.mojom.h"

namespace content {

// Denotes the type of source for this impression. This allows different types
// of impressions to be processed differently by storage and attribution
// logic.
enum class AttributionSourceType {
// An impression which was associated with a top-level navigation.
kNavigation = 0,
// An impression which was not associated with a navigation.
kEvent = 1,
kMaxValue = kEvent,
};
using AttributionSourceType = ::attribution_reporting::mojom::SourceType;

// Returns "navigation" or "event".
CONTENT_EXPORT const char* AttributionSourceTypeToString(AttributionSourceType);
const char* AttributionSourceTypeToString(AttributionSourceType);

} // namespace content

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/test/bind.h"
#include "content/browser/attribution_reporting/attribution_observer.h"
#include "content/browser/attribution_reporting/attribution_source_type.h"
#include "content/browser/attribution_reporting/rate_limit_result.h"
#include "content/public/browser/attribution_reporting.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
Expand Down Expand Up @@ -1035,10 +1036,6 @@ std::ostream& operator<<(std::ostream& out, RateLimitResult result) {
return out;
}

std::ostream& operator<<(std::ostream& out, AttributionSourceType source_type) {
return out << AttributionSourceTypeToString(source_type);
}

std::ostream& operator<<(std::ostream& out,
StoredSource::AttributionLogic attribution_logic) {
switch (attribution_logic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,6 @@ std::ostream& operator<<(std::ostream& out,

std::ostream& operator<<(std::ostream& out, RateLimitResult result);

std::ostream& operator<<(std::ostream& out, AttributionSourceType source_type);

std::ostream& operator<<(
std::ostream& out,
const AttributionTrigger::EventTriggerData& event_trigger);
Expand Down
7 changes: 6 additions & 1 deletion content/browser/resources/attribution_reporting/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ html_to_wrapper("html_wrapper_files") {
# Copy (via creating sym links) all the other files into the same folder for
# ts_library.
copy("copy_files") {
deps = [ "//content/browser/attribution_reporting:mojo_bindings_webui_js" ]
deps = [
"//content/browser/attribution_reporting:internals_mojo_bindings_webui_js",
"//content/browser/attribution_reporting:mojo_bindings_webui_js",
]
sources = [
"$root_gen_dir/mojom-webui/content/browser/attribution_reporting/attribution_internals.mojom-webui.js",
"$root_gen_dir/mojom-webui/content/browser/attribution_reporting/attribution_reporting.mojom-webui.js",
"attribution_internals.ts",
"attribution_internals_table.ts",
"table_model.ts",
Expand All @@ -33,6 +37,7 @@ ts_library("build_ts") {
"attribution_internals_table.html.ts",
"table_model.ts",
"attribution_internals.mojom-webui.js",
"attribution_reporting.mojom-webui.js",
]
deps = [
"//ui/webui/resources:library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {assert} from 'chrome://resources/js/assert_ts.js';
import {getTrustedHTML} from 'chrome://resources/js/static_types.js';
import {Origin} from 'chrome://resources/mojo/url/mojom/origin.mojom-webui.js';

import {FailedSourceRegistration, Handler as AttributionInternalsHandler, HandlerRemote as AttributionInternalsHandlerRemote, ObserverInterface, ObserverReceiver, ReportID, ReportType, SourceType, WebUIReport, WebUISource, WebUISource_Attributability, WebUITrigger, WebUITrigger_Status} from './attribution_internals.mojom-webui.js';
import {FailedSourceRegistration, Handler as AttributionInternalsHandler, HandlerRemote as AttributionInternalsHandlerRemote, ObserverInterface, ObserverReceiver, ReportID, WebUIReport, WebUISource, WebUISource_Attributability, WebUITrigger, WebUITrigger_Status} from './attribution_internals.mojom-webui.js';
import {AttributionInternalsTableElement} from './attribution_internals_table.js';
import {ReportType, SourceType} from './attribution_reporting.mojom-webui.js';
import {Column, TableModel} from './table_model.js';

// If kAttributionAggregatableBudgetPerSource changes, update this value
Expand Down
1 change: 1 addition & 0 deletions content/dev_ui_content_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ files that belong there instead.
<include name="IDR_ATTRIBUTION_INTERNALS_TABLE_HTML_JS" file="${root_gen_dir}/content/browser/resources/attribution_reporting/tsc/attribution_internals_table.html.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_ATTRIBUTION_INTERNALS_CSS" file="browser/resources/attribution_reporting/attribution_internals.css" type="BINDATA" />
<include name="IDR_ATTRIBUTION_INTERNALS_MOJOM_JS" file="${root_gen_dir}/mojom-webui/content/browser/attribution_reporting/attribution_internals.mojom-webui.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_ATTRIBUTION_REPORTING_MOJOM_JS" file="${root_gen_dir}/mojom-webui/content/browser/attribution_reporting/attribution_reporting.mojom-webui.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_GPU_BROWSER_BRIDGE_JS" file="browser/resources/gpu/browser_bridge.js" type="BINDATA" />
<include name="IDR_GPU_INTERNALS_HTML" file="browser/resources/gpu/gpu_internals.html" type="BINDATA" />
<include name="IDR_GPU_INTERNALS_JS" file="browser/resources/gpu/gpu_internals.js" type="BINDATA" />
Expand Down
1 change: 1 addition & 0 deletions content/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ static_library("test_support") {
"//content/app:for_content_tests",
"//content/browser:for_content_tests",
"//content/browser/attribution_reporting:attribution_reporting_proto",
"//content/browser/attribution_reporting:mojo_bindings",
"//content/child:for_content_tests",
"//content/common:for_content_tests",
"//content/gpu",
Expand Down

0 comments on commit ca2a82d

Please sign in to comment.