Commit f44c69ed authored by Mihai Sardarescu's avatar Mihai Sardarescu Committed by Commit Bot

[unity] URL consent helper for personalized URL-keyed data collection.

This CL adds the consent API that allows the various browser services to
check whether the user has consented for personalized URL-keyed data
collection (data collection that is keyed by URL and by Google account id).
As defined in the PRD, this decision is based on the sync state:
* If the Unified consent feature is disabled, then we consider that the
user has granted consent for personalized URL-keyed data collection iff sync
history is enabled.
* If the Unified consent feature is enabled, then we consider that the
user has granted consent for personalized URL-keyed data collection iff sync
user events is enabled (aka event logger is enabled).

BUG: 823809
Change-Id: Idf619b96010064020f36efbd90d54b720b45deec
Reviewed-on: https://chromium-review.googlesource.com/1123827
Commit-Queue: Mihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarThomas Tangl <tangltom@chromium.org>
Cr-Commit-Position: refs/heads/master@{#572564}
parent 73c19013
......@@ -9,8 +9,8 @@ static_library("unified_consent") {
"unified_consent_service.cc",
"unified_consent_service.h",
"unified_consent_service_client.h",
"url_keyed_anonymized_data_collection_consent_helper.cc",
"url_keyed_anonymized_data_collection_consent_helper.h",
"url_keyed_data_collection_consent_helper.cc",
"url_keyed_data_collection_consent_helper.h",
]
deps = [
"//base",
......@@ -27,7 +27,7 @@ source_set("unit_tests") {
testonly = true
sources = [
"unified_consent_service_unittest.cc",
"url_keyed_anonymized_data_collection_consent_helper_unittest.cc",
"url_keyed_data_collection_consent_helper_unittest.cc",
]
deps = [
":unified_consent",
......
// Copyright 2018 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 "components/unified_consent/url_keyed_anonymized_data_collection_consent_helper.h"
#include <vector>
#include "components/sync/driver/fake_sync_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/unified_consent/pref_names.h"
#include "components/unified_consent/unified_consent_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace unified_consent {
namespace {
class UrlKeyedDataCollectionConsentHelperTest
: public testing::Test,
public UrlKeyedAnonymizedDataCollectionConsentHelper::Observer {
public:
// testing::Test:
void SetUp() override {
UnifiedConsentService::RegisterPrefs(pref_service_.registry());
}
void OnUrlKeyedDataCollectionConsentStateChanged(bool enabled) override {
state_changed_notifications.push_back(enabled);
}
protected:
sync_preferences::TestingPrefServiceSyncable pref_service_;
std::vector<bool> state_changed_notifications;
syncer::FakeSyncService sync_service_;
};
TEST_F(UrlKeyedDataCollectionConsentHelperTest, UnifiedConsentEnabled) {
std::unique_ptr<UrlKeyedAnonymizedDataCollectionConsentHelper> helper =
UrlKeyedAnonymizedDataCollectionConsentHelper::NewInstance(
true, &pref_service_, &sync_service_);
helper->AddObserver(this);
EXPECT_FALSE(helper->IsEnabled());
EXPECT_TRUE(state_changed_notifications.empty());
pref_service_.SetBoolean(prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
true);
EXPECT_TRUE(helper->IsEnabled());
EXPECT_EQ(1U, state_changed_notifications.size());
EXPECT_TRUE(state_changed_notifications[0]);
state_changed_notifications.clear();
pref_service_.SetBoolean(prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
false);
EXPECT_FALSE(helper->IsEnabled());
EXPECT_EQ(1U, state_changed_notifications.size());
EXPECT_FALSE(state_changed_notifications[0]);
helper->RemoveObserver(this);
}
TEST_F(UrlKeyedDataCollectionConsentHelperTest, UnifiedConsentDisabled) {
std::unique_ptr<UrlKeyedAnonymizedDataCollectionConsentHelper> helper =
UrlKeyedAnonymizedDataCollectionConsentHelper::NewInstance(
false, &pref_service_, &sync_service_);
EXPECT_FALSE(helper->IsEnabled());
}
} // namespace
} // namespace unified_consent
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/unified_consent/url_keyed_anonymized_data_collection_consent_helper.h"
#include "components/unified_consent/url_keyed_data_collection_consent_helper.h"
#include "base/bind.h"
#include "components/prefs/pref_change_registrar.h"
......@@ -18,13 +18,13 @@ namespace unified_consent {
namespace {
class PrefBasedUrlKeyedDataCollectionConsentHelper
: public UrlKeyedAnonymizedDataCollectionConsentHelper {
: public UrlKeyedDataCollectionConsentHelper {
public:
explicit PrefBasedUrlKeyedDataCollectionConsentHelper(
PrefService* pref_service);
~PrefBasedUrlKeyedDataCollectionConsentHelper() override = default;
// UrlKeyedAnonymizedDataCollectionConsentHelper:
// UrlKeyedDataCollectionConsentHelper:
bool IsEnabled() override;
private:
......@@ -36,14 +36,15 @@ class PrefBasedUrlKeyedDataCollectionConsentHelper
};
class SyncBasedUrlKeyedDataCollectionConsentHelper
: public UrlKeyedAnonymizedDataCollectionConsentHelper,
: public UrlKeyedDataCollectionConsentHelper,
syncer::SyncServiceObserver {
public:
explicit SyncBasedUrlKeyedDataCollectionConsentHelper(
syncer::SyncService* sync_service);
SyncBasedUrlKeyedDataCollectionConsentHelper(
syncer::SyncService* sync_service,
syncer::ModelType sync_data_type);
~SyncBasedUrlKeyedDataCollectionConsentHelper() override;
// UrlKeyedAnonymizedDataCollectionConsentHelper:
// UrlKeyedDataCollectionConsentHelper:
bool IsEnabled() override;
// syncer::SyncServiceObserver:
......@@ -52,7 +53,8 @@ class SyncBasedUrlKeyedDataCollectionConsentHelper
private:
syncer::SyncService* sync_service_;
syncer::UploadState sync_history_upload_state_;
syncer::ModelType sync_data_type_;
syncer::UploadState sync_data_type_upload_state_;
DISALLOW_COPY_AND_ASSIGN(SyncBasedUrlKeyedDataCollectionConsentHelper);
};
......@@ -79,11 +81,12 @@ void PrefBasedUrlKeyedDataCollectionConsentHelper::OnPrefChanged() {
SyncBasedUrlKeyedDataCollectionConsentHelper::
SyncBasedUrlKeyedDataCollectionConsentHelper(
syncer::SyncService* sync_service)
syncer::SyncService* sync_service,
syncer::ModelType sync_data_type)
: sync_service_(sync_service),
sync_history_upload_state_(syncer::GetUploadToGoogleState(
sync_service_,
syncer::ModelType::HISTORY_DELETE_DIRECTIVES)) {
sync_data_type_(sync_data_type),
sync_data_type_upload_state_(
syncer::GetUploadToGoogleState(sync_service_, sync_data_type_)) {
DCHECK(sync_service_);
sync_service_->AddObserver(this);
}
......@@ -95,15 +98,15 @@ SyncBasedUrlKeyedDataCollectionConsentHelper::
}
bool SyncBasedUrlKeyedDataCollectionConsentHelper::IsEnabled() {
return sync_history_upload_state_ == syncer::UploadState::ACTIVE;
return sync_data_type_upload_state_ == syncer::UploadState::ACTIVE;
}
void SyncBasedUrlKeyedDataCollectionConsentHelper::OnStateChanged(
syncer::SyncService* sync_service) {
DCHECK_EQ(sync_service_, sync_service);
bool enabled_before_state_updated = IsEnabled();
sync_history_upload_state_ = syncer::GetUploadToGoogleState(
sync_service_, syncer::ModelType::HISTORY_DELETE_DIRECTIVES);
sync_data_type_upload_state_ =
syncer::GetUploadToGoogleState(sync_service_, sync_data_type_);
if (enabled_before_state_updated != IsEnabled())
FireOnStateChanged();
......@@ -118,13 +121,14 @@ void SyncBasedUrlKeyedDataCollectionConsentHelper::OnSyncShutdown(
} // namespace
UrlKeyedAnonymizedDataCollectionConsentHelper::
UrlKeyedAnonymizedDataCollectionConsentHelper() = default;
UrlKeyedAnonymizedDataCollectionConsentHelper::
~UrlKeyedAnonymizedDataCollectionConsentHelper() = default;
UrlKeyedDataCollectionConsentHelper::UrlKeyedDataCollectionConsentHelper() =
default;
UrlKeyedDataCollectionConsentHelper::~UrlKeyedDataCollectionConsentHelper() =
default;
std::unique_ptr<UrlKeyedAnonymizedDataCollectionConsentHelper>
UrlKeyedAnonymizedDataCollectionConsentHelper::NewInstance(
// static
std::unique_ptr<UrlKeyedDataCollectionConsentHelper>
UrlKeyedDataCollectionConsentHelper::NewAnonymizedDataCollectionConsentHelper(
bool is_unified_consent_enabled,
PrefService* pref_service,
syncer::SyncService* sync_service) {
......@@ -134,22 +138,31 @@ UrlKeyedAnonymizedDataCollectionConsentHelper::NewInstance(
}
return std::make_unique<SyncBasedUrlKeyedDataCollectionConsentHelper>(
sync_service);
sync_service, syncer::ModelType::HISTORY_DELETE_DIRECTIVES);
}
// static
std::unique_ptr<UrlKeyedDataCollectionConsentHelper>
UrlKeyedDataCollectionConsentHelper::NewPersonalizedDataCollectionConsentHelper(
bool is_unified_consent_enabled,
syncer::SyncService* sync_service) {
syncer::ModelType sync_type =
is_unified_consent_enabled ? syncer::ModelType::USER_EVENTS
: syncer::ModelType::HISTORY_DELETE_DIRECTIVES;
return std::make_unique<SyncBasedUrlKeyedDataCollectionConsentHelper>(
sync_service, sync_type);
}
void UrlKeyedAnonymizedDataCollectionConsentHelper::AddObserver(
Observer* observer) {
void UrlKeyedDataCollectionConsentHelper::AddObserver(Observer* observer) {
observer_list_.AddObserver(observer);
}
void UrlKeyedAnonymizedDataCollectionConsentHelper::RemoveObserver(
Observer* observer) {
void UrlKeyedDataCollectionConsentHelper::RemoveObserver(Observer* observer) {
observer_list_.RemoveObserver(observer);
}
void UrlKeyedAnonymizedDataCollectionConsentHelper::FireOnStateChanged() {
bool is_enabled = IsEnabled();
void UrlKeyedDataCollectionConsentHelper::FireOnStateChanged() {
for (auto& observer : observer_list_)
observer.OnUrlKeyedDataCollectionConsentStateChanged(is_enabled);
observer.OnUrlKeyedDataCollectionConsentStateChanged(this);
}
} // namespace unified_consent
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_UNIFIED_CONSENT_URL_KEYED_ANONYMIZED_DATA_COLLECTION_CONSENT_HELPER_H_
#define COMPONENTS_UNIFIED_CONSENT_URL_KEYED_ANONYMIZED_DATA_COLLECTION_CONSENT_HELPER_H_
#ifndef COMPONENTS_UNIFIED_CONSENT_URL_KEYED_DATA_COLLECTION_CONSENT_HELPER_H_
#define COMPONENTS_UNIFIED_CONSENT_URL_KEYED_DATA_COLLECTION_CONSENT_HELPER_H_
#include <memory>
......@@ -17,18 +17,22 @@ class SyncService;
namespace unified_consent {
// Helper class that allows clients to check whether the user has consented
// for URL-keyed anonymized data collection.
class UrlKeyedAnonymizedDataCollectionConsentHelper {
// for URL-keyed data collection.
class UrlKeyedDataCollectionConsentHelper {
public:
class Observer {
public:
// Called when the state of the URL-keyed anonymized data collection
// changes.
virtual void OnUrlKeyedDataCollectionConsentStateChanged(bool enabled) = 0;
// Called when the state of the URL-keyed data collection changes.
virtual void OnUrlKeyedDataCollectionConsentStateChanged(
UrlKeyedDataCollectionConsentHelper* consent_helper) = 0;
};
// Creates a new |UrlKeyedAnonymizedDataCollectionConsentHelper| instance. We
// distinguish the following cases:
// Creates a new |UrlKeyedDataCollectionConsentHelper| instance that checks
// whether *anonymized* data collection is enabled. This should be used when
// the client needs to check whether the user has granted consent for
// *anonymized* URL-keyed data collection.
//
// Implementation-wise we distinguish the following cases:
// 1. If |is_unified_consent_enabled| true, then the instance is backed by
// |pref_service|. Url-keyed data collection is enabled if the preference
// |prefs::kUrlKeyedAnonymizedDataCollectionEnabled| is set to true.
......@@ -38,12 +42,26 @@ class UrlKeyedAnonymizedDataCollectionConsentHelper {
// and if sync history is enabled.
//
// Note: |pref_service| must outlive the retuned instance.
static std::unique_ptr<UrlKeyedAnonymizedDataCollectionConsentHelper>
NewInstance(bool is_unified_consent_enabled,
static std::unique_ptr<UrlKeyedDataCollectionConsentHelper>
NewAnonymizedDataCollectionConsentHelper(bool is_unified_consent_enabled,
PrefService* pref_service,
syncer::SyncService* sync_service);
virtual ~UrlKeyedAnonymizedDataCollectionConsentHelper();
// Creates a new |UrlKeyedDataCollectionConsentHelper| instance that checks
// whether *personalized* data collection is enabled. This should be used when
// the client needs to check whether the user has granted consent for
// URL-keyed data collection keyed by their Google account.
//
// Implementation-wise we distinguish the following cases:
// 1. If |is_unified_consent_enabled| is true then URL-keyed data collection
// is enabled if sync is active and if sync event logger is enabled.
// 2. If |is_unified_consent_enabled| is false then URL-keyed data collection
// is enabled if sync is active and if sync history is enabled.
static std::unique_ptr<UrlKeyedDataCollectionConsentHelper>
NewPersonalizedDataCollectionConsentHelper(bool is_unified_consent_enabled,
syncer::SyncService* sync_service);
virtual ~UrlKeyedDataCollectionConsentHelper();
// Returns true if the user has consented for URL keyed anonymized data
// collection.
......@@ -54,7 +72,7 @@ class UrlKeyedAnonymizedDataCollectionConsentHelper {
void RemoveObserver(Observer* observer);
protected:
UrlKeyedAnonymizedDataCollectionConsentHelper();
UrlKeyedDataCollectionConsentHelper();
// Fires |OnUrlKeyedDataCollectionConsentStateChanged| on all the observers.
void FireOnStateChanged();
......@@ -62,9 +80,9 @@ class UrlKeyedAnonymizedDataCollectionConsentHelper {
private:
base::ObserverList<Observer, true> observer_list_;
DISALLOW_COPY_AND_ASSIGN(UrlKeyedAnonymizedDataCollectionConsentHelper);
DISALLOW_COPY_AND_ASSIGN(UrlKeyedDataCollectionConsentHelper);
};
} // namespace unified_consent
#endif // COMPONENTS_UNIFIED_CONSENT_URL_KEYED_ANONYMIZED_DATA_COLLECTION_CONSENT_HELPER_H_
#endif // COMPONENTS_UNIFIED_CONSENT_URL_KEYED_DATA_COLLECTION_CONSENT_HELPER_H_
// Copyright 2018 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 "components/unified_consent/url_keyed_data_collection_consent_helper.h"
#include <vector>
#include "components/sync/driver/fake_sync_service.h"
#include "components/sync/engine/cycle/sync_cycle_snapshot.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/unified_consent/pref_names.h"
#include "components/unified_consent/unified_consent_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace unified_consent {
namespace {
class TestSyncService : public syncer::FakeSyncService {
public:
void set_sync_initialized(bool sync_initialized) {
sync_initialized_ = sync_initialized;
}
void set_sync_active_data_type(syncer::ModelType type) {
sync_active_data_type_ = type;
}
void FireOnStateChangeOnAllObservers() {
for (auto& observer : observers_)
observer.OnStateChanged(this);
}
// syncer::FakeSyncService:
bool IsSyncAllowed() const override { return true; }
bool CanSyncStart() const override { return true; }
syncer::ModelTypeSet GetPreferredDataTypes() const override {
return syncer::ModelTypeSet(syncer::ModelType::HISTORY_DELETE_DIRECTIVES,
syncer::ModelType::USER_EVENTS);
}
bool IsSyncActive() const override { return true; }
bool ConfigurationDone() const override { return true; }
syncer::SyncCycleSnapshot GetLastCycleSnapshot() const override {
if (!sync_initialized_)
return syncer::SyncCycleSnapshot();
return syncer::SyncCycleSnapshot(
syncer::ModelNeutralState(), syncer::ProgressMarkerMap(), false, 5, 2,
7, false, 0, base::Time::Now(), base::Time::Now(),
std::vector<int>(syncer::MODEL_TYPE_COUNT, 0),
std::vector<int>(syncer::MODEL_TYPE_COUNT, 0),
sync_pb::SyncEnums::UNKNOWN_ORIGIN,
/*short_poll_interval=*/base::TimeDelta::FromMinutes(30),
/*long_poll_interval=*/base::TimeDelta::FromMinutes(180),
/*has_remaining_local_changes=*/false);
}
syncer::ModelTypeSet GetActiveDataTypes() const override {
if (sync_active_data_type_ != syncer::ModelType::UNSPECIFIED) {
return syncer::ModelTypeSet(sync_active_data_type_);
}
return syncer::ModelTypeSet();
}
void AddObserver(syncer::SyncServiceObserver* observer) override {
observers_.AddObserver(observer);
}
void RemoveObserver(syncer::SyncServiceObserver* observer) override {
observers_.RemoveObserver(observer);
}
private:
bool sync_initialized_ = false;
syncer::ModelType sync_active_data_type_ = syncer::ModelType::UNSPECIFIED;
base::ObserverList<syncer::SyncServiceObserver> observers_;
};
class UrlKeyedDataCollectionConsentHelperTest
: public testing::Test,
public UrlKeyedDataCollectionConsentHelper::Observer {
public:
// testing::Test:
void SetUp() override {
UnifiedConsentService::RegisterPrefs(pref_service_.registry());
}
void OnUrlKeyedDataCollectionConsentStateChanged(
UrlKeyedDataCollectionConsentHelper* consent_helper) override {
state_changed_notifications.push_back(consent_helper->IsEnabled());
}
protected:
sync_preferences::TestingPrefServiceSyncable pref_service_;
std::vector<bool> state_changed_notifications;
TestSyncService sync_service_;
};
TEST_F(UrlKeyedDataCollectionConsentHelperTest,
AnonymizedDataCollection_UnifiedConsentEnabled) {
std::unique_ptr<UrlKeyedDataCollectionConsentHelper> helper =
UrlKeyedDataCollectionConsentHelper::
NewAnonymizedDataCollectionConsentHelper(true, &pref_service_,
&sync_service_);
helper->AddObserver(this);
EXPECT_FALSE(helper->IsEnabled());
EXPECT_TRUE(state_changed_notifications.empty());
pref_service_.SetBoolean(prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
true);
EXPECT_TRUE(helper->IsEnabled());
ASSERT_EQ(1U, state_changed_notifications.size());
EXPECT_TRUE(state_changed_notifications[0]);
state_changed_notifications.clear();
pref_service_.SetBoolean(prefs::kUrlKeyedAnonymizedDataCollectionEnabled,
false);
EXPECT_FALSE(helper->IsEnabled());
ASSERT_EQ(1U, state_changed_notifications.size());
EXPECT_FALSE(state_changed_notifications[0]);
helper->RemoveObserver(this);
}
TEST_F(UrlKeyedDataCollectionConsentHelperTest,
AnonymizedDataCollection_UnifiedConsentDisabled) {
std::unique_ptr<UrlKeyedDataCollectionConsentHelper> helper =
UrlKeyedDataCollectionConsentHelper::
NewAnonymizedDataCollectionConsentHelper(false, &pref_service_,
&sync_service_);
helper->AddObserver(this);
EXPECT_FALSE(helper->IsEnabled());
EXPECT_TRUE(state_changed_notifications.empty());
sync_service_.set_sync_initialized(true);
sync_service_.set_sync_active_data_type(
syncer::ModelType::HISTORY_DELETE_DIRECTIVES);
sync_service_.FireOnStateChangeOnAllObservers();
EXPECT_TRUE(helper->IsEnabled());
ASSERT_EQ(1U, state_changed_notifications.size());
helper->RemoveObserver(this);
}
TEST_F(UrlKeyedDataCollectionConsentHelperTest,
PersonalizeddDataCollection_UnifiedConsentEnabled) {
std::unique_ptr<UrlKeyedDataCollectionConsentHelper> helper =
UrlKeyedDataCollectionConsentHelper::
NewPersonalizedDataCollectionConsentHelper(true, &sync_service_);
helper->AddObserver(this);
EXPECT_FALSE(helper->IsEnabled());
EXPECT_TRUE(state_changed_notifications.empty());
sync_service_.set_sync_initialized(true);
sync_service_.set_sync_active_data_type(syncer::ModelType::USER_EVENTS);
sync_service_.FireOnStateChangeOnAllObservers();
EXPECT_TRUE(helper->IsEnabled());
ASSERT_EQ(1U, state_changed_notifications.size());
helper->RemoveObserver(this);
}
TEST_F(UrlKeyedDataCollectionConsentHelperTest,
PersonalizedDataCollection_UnifiedConsentDisabled) {
std::unique_ptr<UrlKeyedDataCollectionConsentHelper> helper =
UrlKeyedDataCollectionConsentHelper::
NewPersonalizedDataCollectionConsentHelper(false, &sync_service_);
helper->AddObserver(this);
EXPECT_FALSE(helper->IsEnabled());
EXPECT_TRUE(state_changed_notifications.empty());
sync_service_.set_sync_initialized(true);
sync_service_.set_sync_active_data_type(
syncer::ModelType::HISTORY_DELETE_DIRECTIVES);
sync_service_.FireOnStateChangeOnAllObservers();
EXPECT_TRUE(helper->IsEnabled());
ASSERT_EQ(1U, state_changed_notifications.size());
helper->RemoveObserver(this);
}
} // namespace
} // namespace unified_consent
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