Skip to content

Commit

Permalink
Use async API to get measurement state and populate to renderers
Browse files Browse the repository at this point in the history
Per crrev.com/c/4313359, MeasurementManagerFutures will be used and
therefore the Android API to get measurement state is asynchronous.

The measurement state is per device and can only change when the user
changes the setting on the device. The flag is cached as a global
variable and initialized once on startup. The value returned from JNI
will be populated to all renderer processes created earlier.

Bug: 1417278
Change-Id: Ideb3582849d9415acc3b7265a88d54f07a517b4d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4319466
Reviewed-by: Andrew Paseltiner <apaseltiner@chromium.org>
Reviewed-by: Ken Buchanan <kenrb@chromium.org>
Auto-Submit: Nan Lin <linnan@chromium.org>
Commit-Queue: Ken Buchanan <kenrb@chromium.org>
Reviewed-by: John Delaney <johnidel@chromium.org>
Reviewed-by: Bo Liu <boliu@chromium.org>
Reviewed-by: Alex Moshchuk <alexmos@chromium.org>
Reviewed-by: Garrett Tanzer <gtanzer@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1115853}
  • Loading branch information
linnan-github authored and Chromium LUCI CQ committed Mar 10, 2023
1 parent a47c900 commit 555c9f6
Show file tree
Hide file tree
Showing 21 changed files with 230 additions and 87 deletions.
6 changes: 6 additions & 0 deletions content/browser/attribution_reporting/attribution_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "content/browser/attribution_reporting/attribution_manager.h"

#include "base/check.h"
#include "content/browser/attribution_reporting/attribution_manager_impl.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
Expand All @@ -28,4 +29,9 @@ AttributionManager* AttributionManager::FromBrowserContext(
->GetAttributionManager();
}

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

} // namespace content
4 changes: 2 additions & 2 deletions content/browser/attribution_reporting/attribution_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class CONTENT_EXPORT AttributionManager : public AttributionDataModel {

static AttributionManager* FromBrowserContext(BrowserContext*);

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

~AttributionManager() override = default;

virtual void AddObserver(AttributionObserver* observer) = 0;
Expand Down Expand Up @@ -138,8 +140,6 @@ class CONTENT_EXPORT AttributionManager : public AttributionDataModel {
BrowsingDataFilterBuilder* filter_builder,
bool delete_rate_limit_data,
base::OnceClosure done) = 0;

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

} // namespace content
Expand Down
19 changes: 9 additions & 10 deletions content/browser/attribution_reporting/attribution_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,15 @@ AttributionManagerImpl::CreateForTesting(
/*data_host_manager=*/nullptr, std::move(storage_task_runner)));
}

// static
attribution_reporting::mojom::OsSupport AttributionManagerImpl::GetOsSupport() {
#if BUILDFLAG(IS_ANDROID)
return AttributionOsLevelManagerAndroid::GetOsSupport();
#else
return attribution_reporting::mojom::OsSupport::kDisabled;
#endif
}

AttributionManagerImpl::AttributionManagerImpl(
StoragePartitionImpl* storage_partition,
const base::FilePath& user_data_directory,
Expand Down Expand Up @@ -1333,14 +1342,4 @@ void AttributionManagerImpl::ProcessNextOsEvent() {

#endif // BUILDFLAG(IS_ANDROID)

attribution_reporting::mojom::OsSupport AttributionManagerImpl::GetOsSupport() {
#if BUILDFLAG(IS_ANDROID)
if (attribution_os_level_manager_) {
return attribution_os_level_manager_->GetOsSupport();
}
#endif

return attribution_reporting::mojom::OsSupport::kDisabled;
}

} // namespace content
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class CONTENT_EXPORT AttributionManagerImpl : public AttributionManager {
const base::FilePath& user_data_directory,
scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy);

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

AttributionManagerImpl(
StoragePartitionImpl* storage_partition,
const base::FilePath& user_data_directory,
Expand Down Expand Up @@ -164,8 +166,6 @@ class CONTENT_EXPORT AttributionManagerImpl : public AttributionManager {
void RemoveAttributionDataByDataKey(const DataKey& data_key,
base::OnceClosure callback) override;

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

#if BUILDFLAG(IS_ANDROID)

void HandleOsSource(const GURL& registration_url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,6 @@ class MockAttributionOsLevelManager : public AttributionOsLevelManager {
bool delete_rate_limit_data,
base::OnceClosure done),
(override));

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#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 @@ -51,8 +50,6 @@ 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 @@ -4,6 +4,8 @@

#include "content/browser/attribution_reporting/attribution_os_level_manager_android.h"

#include <jni.h>

#include <iterator>
#include <set>
#include <string>
Expand All @@ -13,14 +15,17 @@
#include "base/android/jni_array.h"
#include "base/android/scoped_java_ref.h"
#include "base/atomic_sequence_num.h"
#include "base/dcheck_is_on.h"
#include "base/functional/callback.h"
#include "base/no_destructor.h"
#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/browser/attribution_reporting/attribution_input_event.h"
#include "content/public/android/content_jni_headers/AttributionOsLevelManager_jni.h"
#include "content/public/browser/browsing_data_filter_builder.h"
#include "content/public/browser/render_process_host.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "url/android/gurl_android.h"
#include "url/gurl.h"
Expand All @@ -30,6 +35,40 @@ namespace content {

namespace {

using ScopedOsSupportForTesting =
::content::AttributionOsLevelManagerAndroid::ScopedOsSupportForTesting;

using attribution_reporting::mojom::OsSupport;

#if DCHECK_IS_ON()
const base::SequenceChecker& GetSequenceChecker() {
static base::NoDestructor<base::SequenceChecker> checker;
return *checker;
}
#endif

// This flag is per device and can only be changed by the OS. Currently we don't
// observe setting changes on the device and the flag is only initialized once
// on startup. The value may vary in tests.
absl::optional<OsSupport> g_os_support GUARDED_BY_CONTEXT(GetSequenceChecker());

void SetOsSupport(OsSupport os_support) {
DCHECK_CALLED_ON_VALID_SEQUENCE(GetSequenceChecker());

OsSupport previous = AttributionOsLevelManagerAndroid::GetOsSupport();

g_os_support = os_support;

if (previous == os_support) {
return;
}

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

int GetDeletionMode(bool delete_rate_limit_data) {
// See
// https://developer.android.com/reference/androidx/privacysandbox/ads/adservices/measurement/DeletionRequest#constants
Expand All @@ -56,7 +95,7 @@ int GetMatchBehavior(BrowsingDataFilterBuilder::Mode mode) {
}
}

attribution_reporting::mojom::OsSupport ConvertToOsSupport(int value) {
OsSupport ConvertToOsSupport(int value) {
// See
// https://developer.android.com/reference/androidx/privacysandbox/ads/adservices/measurement/MeasurementManager
// for constant values.
Expand All @@ -65,19 +104,42 @@ attribution_reporting::mojom::OsSupport ConvertToOsSupport(int value) {

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

} // namespace

static void JNI_AttributionOsLevelManager_OnMeasurementStateReturned(
JNIEnv* env,
jint state) {
SetOsSupport(ConvertToOsSupport(state));
}

ScopedOsSupportForTesting::ScopedOsSupportForTesting(OsSupport os_support)
: previous_(GetOsSupport()) {
SetOsSupport(os_support);
}

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

// static
OsSupport AttributionOsLevelManagerAndroid::GetOsSupport() {
DCHECK_CALLED_ON_VALID_SEQUENCE(GetSequenceChecker());
return g_os_support.value_or(OsSupport::kDisabled);
}

AttributionOsLevelManagerAndroid::AttributionOsLevelManagerAndroid() {
jobj_ = Java_AttributionOsLevelManager_Constructor(
base::android::AttachCurrentThread(), reinterpret_cast<intptr_t>(this));

InitializeOsSupport();
}

AttributionOsLevelManagerAndroid::~AttributionOsLevelManagerAndroid() {
Expand Down Expand Up @@ -144,16 +206,18 @@ void AttributionOsLevelManagerAndroid::ClearData(
GetDeletionMode(delete_rate_limit_data), GetMatchBehavior(mode));
}

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

if (!os_support_) {
os_support_ = ConvertToOsSupport(
Java_AttributionOsLevelManager_getMeasurementApiStatus(
base::android::AttachCurrentThread(), jobj_));
if (g_os_support.has_value()) {
return;
}
return *os_support_;

// Only make the async call once.
g_os_support.emplace(OsSupport::kDisabled);

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

void AttributionOsLevelManagerAndroid::OnDataDeletionCompleted(
Expand All @@ -170,10 +234,4 @@ void AttributionOsLevelManagerAndroid::OnDataDeletionCompleted(
pending_data_deletion_callbacks_.erase(it);
}

void AttributionOsLevelManagerAndroid::SetOsSupportForTesting(
attribution_reporting::mojom::OsSupport os_support) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
os_support_ = os_support;
}

} // namespace content
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "components/attribution_reporting/os_support.mojom-forward.h"
#include "content/browser/attribution_reporting/attribution_os_level_manager.h"
#include "content/common/content_export.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace content {

Expand All @@ -25,6 +24,26 @@ namespace content {
class CONTENT_EXPORT AttributionOsLevelManagerAndroid
: public AttributionOsLevelManager {
public:
class CONTENT_EXPORT ScopedOsSupportForTesting {
public:
explicit ScopedOsSupportForTesting(attribution_reporting::mojom::OsSupport);
~ScopedOsSupportForTesting();

ScopedOsSupportForTesting(const ScopedOsSupportForTesting&) = delete;
ScopedOsSupportForTesting& operator=(const ScopedOsSupportForTesting&) =
delete;

ScopedOsSupportForTesting(ScopedOsSupportForTesting&&) = delete;
ScopedOsSupportForTesting& operator=(ScopedOsSupportForTesting&&) = delete;

private:
const attribution_reporting::mojom::OsSupport previous_;
};

// Returns whether OS-level attribution is enabled. `kDisabled` is returned
// before the result is returned from JNI.
static attribution_reporting::mojom::OsSupport GetOsSupport();

AttributionOsLevelManagerAndroid();
~AttributionOsLevelManagerAndroid() override;

Expand Down Expand Up @@ -54,18 +73,13 @@ class CONTENT_EXPORT AttributionOsLevelManagerAndroid
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);

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

private:
base::flat_map<int, base::OnceClosure> pending_data_deletion_callbacks_
GUARDED_BY_CONTEXT(sequence_checker_);
void InitializeOsSupport() VALID_CONTEXT_REQUIRED(sequence_checker_);

absl::optional<attribution_reporting::mojom::OsSupport> os_support_
base::flat_map<int, base::OnceClosure> pending_data_deletion_callbacks_
GUARDED_BY_CONTEXT(sequence_checker_);

base::android::ScopedJavaGlobalRef<jobject> jobj_
Expand Down

0 comments on commit 555c9f6

Please sign in to comment.