Skip to content

Commit

Permalink
Plumb get OS support call to Android over JNI
Browse files Browse the repository at this point in the history
Get measurement API status from the native API and set the os support
flag accordingly. The flag is populated to all the renderer processes.

Note that the measurement API status can only change when the user
changes the setting on the device (and should be rare), therefore the
global os support flag will be updated for each instance of AttributionOsLevelManagerAndroid to keep track of the latest setting.

The change is gated by `kAttributionReportingCrossAppWeb` which is
currently disabled.

Bug: 1417278
Change-Id: I0505cca9c7e5e5a3f015f06bccca2f902adf514e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4048260
Reviewed-by: Bo Liu <boliu@chromium.org>
Reviewed-by: John Delaney <johnidel@chromium.org>
Reviewed-by: Andrew Paseltiner <apaseltiner@chromium.org>
Commit-Queue: Nan Lin <linnan@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1108746}
  • Loading branch information
linnan-github authored and Chromium LUCI CQ committed Feb 23, 2023
1 parent 0abe97d commit 8e5d554
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 29 deletions.
55 changes: 37 additions & 18 deletions content/browser/attribution_reporting/attribution_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,28 @@ bool IsOperationAllowed(

bool g_run_in_memory = false;

// This flag is per device and can only be changed by the OS.
//
// TODO(linnan): As currently we don't listen to the flag changes on the OS and
// the API is synchronous, consider changing this to be per instance instead of
// global which is set on creation. The renderer would be initialized with the
// instance value without further updates.
attribution_reporting::mojom::OsSupport g_os_support =
attribution_reporting::mojom::OsSupport::kDisabled;

void SetOsSupport(attribution_reporting::mojom::OsSupport os_support) {
if (g_os_support == os_support) {
return;
}

g_os_support = os_support;

for (RenderProcessHost::iterator it = RenderProcessHost::AllHostsIterator();
!it.IsAtEnd(); it.Advance()) {
it.GetCurrentValue()->SetOsSupportForAttributionReporting(g_os_support);
}
}

} // namespace

struct AttributionManagerImpl::SourceOrTriggerRFH {
Expand Down Expand Up @@ -364,18 +386,14 @@ ScopedUseInMemoryStorageForTesting::~ScopedUseInMemoryStorageForTesting() {

ScopedOsSupportForTesting::ScopedOsSupportForTesting(
attribution_reporting::mojom::OsSupport os_support)
: previous_(AttributionManagerImpl::g_os_support_) {
AttributionManagerImpl::SetOsSupportForTesting(os_support);
: previous_(g_os_support) {
SetOsSupport(os_support);
}

ScopedOsSupportForTesting::~ScopedOsSupportForTesting() {
AttributionManagerImpl::SetOsSupportForTesting(previous_);
SetOsSupport(previous_);
}

// static
attribution_reporting::mojom::OsSupport AttributionManagerImpl::g_os_support_ =
attribution_reporting::mojom::OsSupport::kDisabled;

// static
std::unique_ptr<AttributionManagerImpl>
AttributionManagerImpl::CreateWithNewDbForTesting(
Expand All @@ -394,6 +412,11 @@ AttributionManagerImpl::CreateWithNewDbForTesting(
std::move(special_storage_policy));
}

// static
attribution_reporting::mojom::OsSupport AttributionManagerImpl::GetOsSupport() {
return g_os_support;
}

bool AttributionManagerImpl::IsReportAllowed(
const AttributionReport& report) const {
const CommonSourceInfo& common_info =
Expand Down Expand Up @@ -426,17 +449,6 @@ AttributionManagerImpl::CreateForTesting(
std::move(os_level_manager)));
}

// static
void AttributionManagerImpl::SetOsSupportForTesting(
attribution_reporting::mojom::OsSupport os_support) {
g_os_support_ = os_support;

for (RenderProcessHost::iterator it = RenderProcessHost::AllHostsIterator();
!it.IsAtEnd(); it.Advance()) {
it.GetCurrentValue()->SetOsSupportForAttributionReporting(g_os_support_);
}
}

AttributionManagerImpl::AttributionManagerImpl(
StoragePartitionImpl* storage_partition,
const base::FilePath& user_data_directory,
Expand Down Expand Up @@ -504,6 +516,13 @@ AttributionManagerImpl::AttributionManagerImpl(
DCHECK(storage_task_runner_);
DCHECK(cookie_checker_);
DCHECK(report_sender_);

if (attribution_os_level_manager_) {
// The measurement API status can only change when user changes the setting
// on the device, therefore it's fine to update the global variable to keep
// track of the latest setting.
SetOsSupport(attribution_os_level_manager_->GetOsSupport());
}
}

AttributionManagerImpl::~AttributionManagerImpl() {
Expand Down
13 changes: 2 additions & 11 deletions content/browser/attribution_reporting/attribution_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/threading/sequence_bound.h"
#include "components/attribution_reporting/os_support.mojom.h"
#include "components/attribution_reporting/os_support.mojom-forward.h"
#include "components/attribution_reporting/source_registration_error.mojom-forward.h"
#include "content/browser/aggregation_service/aggregation_service.h"
#include "content/browser/aggregation_service/report_scheduler_timer.h"
Expand Down Expand Up @@ -125,9 +125,7 @@ class CONTENT_EXPORT AttributionManagerImpl : public AttributionManager {

// Returns whether OS-level attribution is enabled. `kDisabled` is returned
// before the result is returned from the underlying platform (e.g. Android).
static attribution_reporting::mojom::OsSupport GetOsSupport() {
return g_os_support_;
}
static attribution_reporting::mojom::OsSupport GetOsSupport();

AttributionManagerImpl(
StoragePartitionImpl* storage_partition,
Expand Down Expand Up @@ -178,13 +176,6 @@ class CONTENT_EXPORT AttributionManagerImpl : public AttributionManager {
private:
friend class AttributionManagerImplTest;

static void SetOsSupportForTesting(
attribution_reporting::mojom::OsSupport os_support);

// TODO(crbug.com/1373536): The OS-level support should be derived from the
// underlying platform (e.g. Android).
static attribution_reporting::mojom::OsSupport g_os_support_;

using ReportSentCallback = AttributionReportSender::ReportSentCallback;
using SourceOrTrigger = absl::variant<StorableSource, AttributionTrigger>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ class MockAttributionOsLevelManager : public AttributionOsLevelManager {
bool delete_rate_limit_data,
base::OnceClosure done),
(override));

MOCK_METHOD(attribution_reporting::mojom::OsSupport,
GetOsSupport,
(),
(override));
};

} // namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>

#include "base/functional/callback_forward.h"
#include "components/attribution_reporting/os_support.mojom-forward.h"
#include "content/public/browser/browsing_data_filter_builder.h"

class GURL;
Expand Down Expand Up @@ -43,6 +44,8 @@ class AttributionOsLevelManager {
BrowsingDataFilterBuilder::Mode mode,
bool delete_rate_limit_data,
base::OnceClosure done) = 0;

virtual attribution_reporting::mojom::OsSupport GetOsSupport() = 0;
};

} // namespace content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "base/ranges/algorithm.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "components/attribution_reporting/os_support.mojom-shared.h"
#include "content/public/android/content_jni_headers/AttributionOsLevelManager_jni.h"
#include "content/public/browser/browsing_data_filter_builder.h"
#include "url/android/gurl_android.h"
Expand Down Expand Up @@ -53,6 +54,23 @@ int GetMatchBehavior(BrowsingDataFilterBuilder::Mode mode) {
}
}

attribution_reporting::mojom::OsSupport ConvertToOsSupport(int value) {
// See
// https://developer.android.com/reference/androidx/privacysandbox/ads/adservices/measurement/MeasurementManager
// for constant values.
static constexpr int kMeasurementApiStateDisabled = 0;
static constexpr int kMeasurementApiStateEnabled = 1;

switch (value) {
case kMeasurementApiStateDisabled:
return attribution_reporting::mojom::OsSupport::kDisabled;
case kMeasurementApiStateEnabled:
return attribution_reporting::mojom::OsSupport::kEnabled;
default:
return attribution_reporting::mojom::OsSupport::kDisabled;
}
}

} // namespace

AttributionOsLevelManagerAndroid::AttributionOsLevelManagerAndroid() {
Expand Down Expand Up @@ -110,6 +128,15 @@ void AttributionOsLevelManagerAndroid::ClearData(
GetDeletionMode(delete_rate_limit_data), GetMatchBehavior(mode));
}

attribution_reporting::mojom::OsSupport
AttributionOsLevelManagerAndroid::GetOsSupport() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

return ConvertToOsSupport(
Java_AttributionOsLevelManager_getMeasurementApiStatus(
base::android::AttachCurrentThread(), jobj_));
}

void AttributionOsLevelManagerAndroid::OnDataDeletionCompleted(
JNIEnv* env,
jint request_id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class AttributionOsLevelManagerAndroid : public AttributionOsLevelManager {
bool delete_rate_limit_data,
base::OnceClosure done) override;

attribution_reporting::mojom::OsSupport GetOsSupport() override;

// This is exposed to JNI and therefore has to be public.
void OnDataDeletionCompleted(JNIEnv* env, jint request_id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ private void deleteRegistrations(int requestId, long startMs, long endMs, GURL[]
}
}

/**
* Gets Measurement API status with native, see `getMeasurementApiStatus()`:
* https://developer.android.com/reference/androidx/privacysandbox/ads/adservices/measurement/MeasurementManager#getMeasurementApiStatus
*/
@CalledByNative
private int getMeasurementApiStatus() {
// TODO(linnan): Get from Android API, see
// https://developer.android.com/design-for-safety/privacy-sandbox/guides/attribution.
// This is dependent on support for the Tiramisu Privacy Sandbox SDK.
return 0;
}

@CalledByNative
private void nativeDestroyed() {
mNativePtr = 0;
Expand Down

0 comments on commit 8e5d554

Please sign in to comment.