Skip to content

Commit

Permalink
Uplift of #21767 (squashed) to release
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBarabash committed Jan 31, 2024
1 parent b2bcea9 commit 121ba4f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 1 deletion.
4 changes: 4 additions & 0 deletions browser/android/brave_sync_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ void BraveSyncWorker::ResetSync(JNIEnv* env) {
if (!sync_service)
return;

sync_service->prefs().AddLeaveChainDetail(__FILE__, __LINE__, __func__);

auto* device_info_sync_service =
DeviceInfoSyncServiceFactory::GetForProfile(profile_);
brave_sync::ResetSync(sync_service, device_info_sync_service,
Expand Down Expand Up @@ -258,6 +260,8 @@ void BraveSyncWorker::PermanentlyDeleteAccount(
auto* sync_service = GetSyncService();
CHECK_NE(sync_service, nullptr);

sync_service->prefs().AddLeaveChainDetail(__FILE__, __LINE__, __func__);

base::android::ScopedJavaGlobalRef<jobject> java_callback;
java_callback.Reset(env, callback);

Expand Down
4 changes: 4 additions & 0 deletions chromium_src/components/sync/service/sync_internals_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ base::Value::Dict ConstructAboutInformation(
section_brave_sync.AddBoolStat("OS encryption available");
is_os_encryption_available->Set(OSCrypt::IsEncryptionAvailable());

Stat<std::string>* leave_chain_details =
section_brave_sync.AddStringStat("Leave chain details");
leave_chain_details->Set(brave_sync_service->prefs().GetLeaveChainDetails());

base::Value::List* details = about_info.FindList(kDetailsKey);
DCHECK_NE(details, nullptr);

Expand Down
21 changes: 21 additions & 0 deletions components/brave_sync/brave_sync_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <utility>

#include "base/base64.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "components/os_crypt/sync/os_crypt.h"
#include "components/prefs/pref_registry_simple.h"
Expand All @@ -23,6 +24,7 @@ const char kSyncFailedDecryptSeedNoticeDismissed[] =
"brave_sync_v2.failed_decrypt_seed_notice_dismissed";
const char kSyncAccountDeletedNoticePending[] =
"brave_sync_v2.account_deleted_notice_pending";
const char kSyncLeaveChainDetails[] = "brave_sync_v2.diag.leave_chain_details";

// Deprecated
// ============================================================================
Expand Down Expand Up @@ -69,6 +71,7 @@ void Prefs::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterStringPref(kSyncV2Seed, std::string());
registry->RegisterBooleanPref(kSyncFailedDecryptSeedNoticeDismissed, false);
registry->RegisterBooleanPref(kSyncAccountDeletedNoticePending, false);
registry->RegisterStringPref(kSyncLeaveChainDetails, std::string());
}

// static
Expand Down Expand Up @@ -163,6 +166,24 @@ void Prefs::SetSyncAccountDeletedNoticePending(bool is_pending) {
pref_service_->SetBoolean(kSyncAccountDeletedNoticePending, is_pending);
}

void Prefs::AddLeaveChainDetail(const char* file, int line, const char* func) {
std::string details = pref_service_->GetString(kSyncLeaveChainDetails);

std::ostringstream stream;
stream << base::Time::Now() << " "
<< base::FilePath::FromASCII(file).BaseName() << "(" << line << ") "
<< func << std::endl;
pref_service_->SetString(kSyncLeaveChainDetails, details + stream.str());
}

std::string Prefs::GetLeaveChainDetails() const {
return pref_service_->GetString(kSyncLeaveChainDetails);
}

void Prefs::ClearLeaveChainDetails() {
pref_service_->ClearPref(kSyncLeaveChainDetails);
}

void Prefs::Clear() {
pref_service_->ClearPref(kSyncV2Seed);
pref_service_->ClearPref(kSyncFailedDecryptSeedNoticeDismissed);
Expand Down
4 changes: 4 additions & 0 deletions components/brave_sync/brave_sync_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class Prefs {
bool IsFailedDecryptSeedNoticeDismissed() const;
void DismissFailedDecryptSeedNotice();

void AddLeaveChainDetail(const char* file, int line, const char* func);
std::string GetLeaveChainDetails() const;
void ClearLeaveChainDetails();

void Clear();

private:
Expand Down
1 change: 1 addition & 0 deletions components/brave_sync/sync_service_impl_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace brave_sync {
void ResetSync(syncer::BraveSyncServiceImpl* sync_service_impl,
syncer::DeviceInfoSyncService* device_info_service,
base::OnceClosure on_reset_done) {
sync_service_impl->prefs().AddLeaveChainDetail(__FILE__, __LINE__, __func__);
if (sync_service_impl->GetTransportState() !=
syncer::SyncService::TransportState::ACTIVE) {
sync_service_impl->OnSelfDeviceInfoDeleted(std::move(on_reset_done));
Expand Down
10 changes: 10 additions & 0 deletions components/sync/service/brave_sync_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ bool BraveSyncServiceImpl::IsSetupInProgress() const {
}

void BraveSyncServiceImpl::StopAndClear() {
brave_sync_prefs_.AddLeaveChainDetail(__FILE__, __LINE__, __func__);
// Clear prefs before StopAndClear() to make NotifyObservers() be invoked
brave_sync_prefs_.Clear();
SyncServiceImpl::StopAndClear();
Expand Down Expand Up @@ -107,6 +108,7 @@ bool BraveSyncServiceImpl::SetSyncCode(const std::string& sync_code) {
}

void BraveSyncServiceImpl::OnSelfDeviceInfoDeleted(base::OnceClosure cb) {
brave_sync_prefs_.AddLeaveChainDetail(__FILE__, __LINE__, __func__);
initiated_self_device_info_deleted_ = true;
// This function will follow normal reset process and set SyncRequested to
// false
Expand Down Expand Up @@ -154,8 +156,11 @@ void BraveSyncServiceImpl::OnBraveSyncPrefsChanged(const std::string& path) {
syncer::UserSelectableTypeSet selected_types;
selected_types.Put(UserSelectableType::kBookmarks);
GetUserSettings()->SetSelectedTypes(false, selected_types);

brave_sync_prefs_.ClearLeaveChainDetails();
} else {
VLOG(1) << "Brave sync seed cleared";
brave_sync_prefs_.AddLeaveChainDetail(__FILE__, __LINE__, __func__);
GetBraveSyncAuthManager()->ResetKeys();
// Send updated status here, because OnDeviceInfoChange is not triggered
// when device leaves the chain by `Leave Sync Chain` button
Expand Down Expand Up @@ -215,6 +220,7 @@ void BraveSyncServiceImpl::OnAccountDeleted(
const int current_attempt,
base::OnceCallback<void(const SyncProtocolError&)> callback,
const SyncProtocolError& sync_protocol_error) {
brave_sync_prefs_.AddLeaveChainDetail(__FILE__, __LINE__, __func__);
if (sync_protocol_error.error_type == SYNC_SUCCESS) {
std::move(callback).Run(sync_protocol_error);
// If request succeded - reset and clear all in a forced way
Expand All @@ -240,6 +246,7 @@ void BraveSyncServiceImpl::OnAccountDeleted(
void BraveSyncServiceImpl::PermanentlyDeleteAccountImpl(
const int current_attempt,
base::OnceCallback<void(const SyncProtocolError&)> callback) {
brave_sync_prefs_.AddLeaveChainDetail(__FILE__, __LINE__, __func__);
if (!engine_) {
// We can reach here if two devices almost at the same time will initiate
// the deletion procedure
Expand All @@ -259,6 +266,7 @@ void BraveSyncServiceImpl::PermanentlyDeleteAccountImpl(

void BraveSyncServiceImpl::PermanentlyDeleteAccount(
base::OnceCallback<void(const SyncProtocolError&)> callback) {
brave_sync_prefs_.AddLeaveChainDetail(__FILE__, __LINE__, __func__);
initiated_delete_account_ = true;
PermanentlyDeleteAccountImpl(1, std::move(callback));
}
Expand All @@ -275,12 +283,14 @@ void BraveSyncServiceImpl::ResetEngine(ShutdownReason shutdown_reason,
reset_reason == ResetEngineReason::kDisabledAccount &&
sync_disabled_by_admin_ && !initiated_delete_account_ &&
!initiated_join_chain_) {
brave_sync_prefs_.AddLeaveChainDetail(__FILE__, __LINE__, __func__);
brave_sync_prefs_.SetSyncAccountDeletedNoticePending(true);
// Forcing stop and clear, because sync account was deleted
BraveSyncServiceImpl::StopAndClear();
} else if (shutdown_reason == ShutdownReason::DISABLE_SYNC_AND_CLEAR_DATA &&
reset_reason == ResetEngineReason::kDisabledAccount &&
sync_disabled_by_admin_ && initiated_join_chain_) {
brave_sync_prefs_.AddLeaveChainDetail(__FILE__, __LINE__, __func__);
// Forcing stop and clear, because we are trying to join the sync chain, but
// sync account was deleted
BraveSyncServiceImpl::StopAndClear();
Expand Down
3 changes: 2 additions & 1 deletion components/sync/service/brave_sync_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class BraveSyncServiceImpl : public SyncServiceImpl {

void Initialize() override;

const brave_sync::Prefs& prefs() { return brave_sync_prefs_; }
const brave_sync::Prefs& prefs() const { return brave_sync_prefs_; }
brave_sync::Prefs& prefs() { return brave_sync_prefs_; }

void PermanentlyDeleteAccount(
base::OnceCallback<void(const SyncProtocolError&)> callback);
Expand Down
4 changes: 4 additions & 0 deletions ios/browser/api/sync/brave_sync_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ void BraveSyncWorker::ResetSync() {
return;
}

sync_service->prefs().AddLeaveChainDetail(__FILE__, __LINE__, __func__);

auto* device_info_service =
DeviceInfoSyncServiceFactory::GetForBrowserState(browser_state_);
DCHECK(device_info_service);
Expand Down Expand Up @@ -358,6 +360,8 @@ void BraveSyncWorker::PermanentlyDeleteAccount(
return;
}

sync_service->prefs().AddLeaveChainDetail(__FILE__, __LINE__, __func__);

sync_service->PermanentlyDeleteAccount(std::move(callback));
}

Expand Down

0 comments on commit 121ba4f

Please sign in to comment.