diff --git a/components/sync/service/brave_sync_service_impl.cc b/components/sync/service/brave_sync_service_impl.cc index e151adb8d8f4..64a98995373d 100644 --- a/components/sync/service/brave_sync_service_impl.cc +++ b/components/sync/service/brave_sync_service_impl.cc @@ -420,4 +420,13 @@ void BraveSyncServiceImpl::StopAndClearWithResetLocalDataReason() { StopAndClear(ResetEngineReason::kResetLocalData); } +void BraveSyncServiceImpl::OnAccountsCookieDeletedByUserAction() {} + +void BraveSyncServiceImpl::OnAccountsInCookieUpdated( + const signin::AccountsInCookieJarInfo& accounts_in_cookie_jar_info, + const GoogleServiceAuthError& error) {} + +void BraveSyncServiceImpl::OnPrimaryAccountChanged( + const signin::PrimaryAccountChangeEvent& event_details) {} + } // namespace syncer diff --git a/components/sync/service/brave_sync_service_impl.h b/components/sync/service/brave_sync_service_impl.h index db358f7b0cc8..dbd889b36f3f 100644 --- a/components/sync/service/brave_sync_service_impl.h +++ b/components/sync/service/brave_sync_service_impl.h @@ -74,6 +74,7 @@ class BraveSyncServiceImpl : public SyncServiceImpl { void StopAndClearWithResetLocalDataReason(); private: + friend class BraveSyncServiceImplGACookiesTest; friend class BraveSyncServiceImplTest; FRIEND_TEST_ALL_PREFIXES(BraveSyncServiceImplTest, ForcedSetDecryptionPassphrase); @@ -116,6 +117,17 @@ class BraveSyncServiceImpl : public SyncServiceImpl { void UpdateP3AObjectsNumber(); void OnGetTypeEntitiesCount(const TypeEntitiesCount& count); + // IdentityManager::Observer implementation. + // Override with an empty implementation. + // We need this to avoid device cache guid regeneration once any Google + // Account cookie gets deleted, for example when user signs out from GMail + void OnAccountsCookieDeletedByUserAction() override; + void OnAccountsInCookieUpdated( + const signin::AccountsInCookieJarInfo& accounts_in_cookie_jar_info, + const GoogleServiceAuthError& error) override; + void OnPrimaryAccountChanged( + const signin::PrimaryAccountChangeEvent& event_details) override; + brave_sync::Prefs brave_sync_prefs_; PrefChangeRegistrar brave_sync_prefs_change_registrar_; diff --git a/components/sync/service/brave_sync_service_impl_unittest.cc b/components/sync/service/brave_sync_service_impl_unittest.cc index c0819c5c1726..f0b0b2e87af6 100644 --- a/components/sync/service/brave_sync_service_impl_unittest.cc +++ b/components/sync/service/brave_sync_service_impl_unittest.cc @@ -22,10 +22,12 @@ #include "components/os_crypt/sync/os_crypt.h" #include "components/os_crypt/sync/os_crypt_mocker.h" #include "components/prefs/pref_change_registrar.h" +#include "components/signin/public/identity_manager/accounts_in_cookie_jar_info.h" #include "components/sync/engine/nigori/key_derivation_params.h" #include "components/sync/engine/nigori/nigori.h" #include "components/sync/model/type_entities_count.h" #include "components/sync/service/data_type_manager_impl.h" +#include "components/sync/service/glue/sync_transport_data_prefs.h" #include "components/sync/test/data_type_manager_mock.h" #include "components/sync/test/fake_data_type_controller.h" #include "components/sync/test/fake_sync_engine.h" @@ -46,6 +48,8 @@ namespace syncer { namespace { +constexpr char kCacheGuid[] = "cache_guid"; + constexpr char kValidSyncCode[] = "fringe digital begin feed equal output proof cheap " "exotic ill sure question trial squirrel glove celery " @@ -708,4 +712,93 @@ TEST_F(BraveSyncServiceImplTest, NoLeaveDetailsWhenInitializeIOS) { EXPECT_FALSE(brave_sync_prefs()->GetLeaveChainDetails().empty()); } +namespace { +enum class GACookiesMethodType { + kOnAccountsCookieDeletedByUserAction, + kOnAccountsInCookieUpdated, + kOnPrimaryAccountChanged +}; +} + +class BraveSyncServiceImplGACookiesTest + : public BraveSyncServiceImplTest, + public testing::WithParamInterface { + public: + base::OnceCallback GetGACookiesMethod() { + switch (GetParam()) { + case GACookiesMethodType::kOnAccountsCookieDeletedByUserAction: + return base::BindOnce( + [](BraveSyncServiceImplGACookiesTest* p_this) { + p_this->brave_sync_service_impl() + ->OnAccountsCookieDeletedByUserAction(); + }, + this); + case GACookiesMethodType::kOnAccountsInCookieUpdated: + return base::BindOnce( + [](BraveSyncServiceImplGACookiesTest* p_this) { + p_this->brave_sync_service_impl()->OnAccountsInCookieUpdated( + signin::AccountsInCookieJarInfo(), GoogleServiceAuthError()); + }, + this); + case GACookiesMethodType::kOnPrimaryAccountChanged: + return base::BindOnce( + [](BraveSyncServiceImplGACookiesTest* p_this) { + CoreAccountInfo prev_account_info; + prev_account_info.email = "usertest@gmail.com"; + prev_account_info.gaia = "gaia"; + prev_account_info.account_id = + CoreAccountId::FromGaiaId(prev_account_info.gaia); + + signin::PrimaryAccountChangeEvent primary_accountchange_event( + signin::PrimaryAccountChangeEvent::State( + prev_account_info, signin::ConsentLevel::kSignin), + signin::PrimaryAccountChangeEvent::State(), + signin_metrics::ProfileSignout::kTest); + + p_this->brave_sync_service_impl()->OnPrimaryAccountChanged( + primary_accountchange_event); + }, + this); + default: + NOTREACHED_NORETURN(); + } + } +}; + +TEST_P(BraveSyncServiceImplGACookiesTest, CacheGuidIsNotWiped) { + SyncTransportDataPrefs::RegisterProfilePrefs(pref_service()->registry()); + + SyncTransportDataPrefs sync_transport_data_prefs( + pref_service(), signin::GaiaIdHash::FromGaiaId("user_gaia_id")); + + sync_transport_data_prefs.SetCacheGuid(kCacheGuid); + + CreateSyncService(); + + std::move(GetGACookiesMethod()).Run(); + + EXPECT_EQ(sync_transport_data_prefs.GetCacheGuid(), kCacheGuid); +} + +INSTANTIATE_TEST_SUITE_P( + All, + BraveSyncServiceImplGACookiesTest, + testing::ValuesIn( + {GACookiesMethodType::kOnAccountsCookieDeletedByUserAction, + GACookiesMethodType::kOnAccountsInCookieUpdated, + GACookiesMethodType::kOnPrimaryAccountChanged}), + [](const testing::TestParamInfo< + BraveSyncServiceImplGACookiesTest::ParamType>& info) { + switch (info.param) { + case GACookiesMethodType::kOnAccountsCookieDeletedByUserAction: + return "OnAccountsCookieDeletedByUserAction"; + case GACookiesMethodType::kOnAccountsInCookieUpdated: + return "OnAccountsInCookieUpdated"; + case GACookiesMethodType::kOnPrimaryAccountChanged: + return "OnPrimaryAccountChanged"; + default: + NOTREACHED_NORETURN(); + } + }); + } // namespace syncer