Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #6820: Allow to permanently delete Sync accounts (#7031)
Browse files Browse the repository at this point in the history
  • Loading branch information
soner-yuksel authored Mar 3, 2023
1 parent 9bb1b34 commit 0891c01
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 228 deletions.
11 changes: 11 additions & 0 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public class BrowserViewController: UIViewController {
var pageZoomBar: UIHostingController<PageZoomView>?
private var pageZoomListener: NSObjectProtocol?
private var openTabsModelStateListener: SendTabToSelfModelStateListener?
private var syncServiceStateListener: AnyObject?
let collapsedURLBarView = CollapsedURLBarView()

// Single data source used for all favorites vcs
Expand Down Expand Up @@ -361,6 +362,16 @@ public class BrowserViewController: UIViewController {
}
}
})

// Observer watching state change in sync chain
syncServiceStateListener = braveCore.syncAPI.addServiceStateObserver { [weak self] in
guard let self = self else { return }
// Observe Sync State in order to determine if the sync chain is deleted
// from another device - Clean local sync chain
if self.braveCore.syncAPI.shouldLeaveSyncGroup {
self.braveCore.syncAPI.leaveSyncGroup()
}
}
}

deinit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,11 +783,11 @@ extension BrowserViewController {
titleWeight: .semibold,
titleSize: 21
)
popup.addButton(title: Strings.openExternalAppURLDontAllow, fontSize: 16) { () -> PopupViewDismissType in
popup.addButton(title: Strings.openExternalAppURLDontAllow) { () -> PopupViewDismissType in
removeTabIfEmpty()
return .flyDown
}
popup.addButton(title: Strings.openExternalAppURLAllow, type: .primary, fontSize: 16) { () -> PopupViewDismissType in
popup.addButton(title: Strings.openExternalAppURLAllow, type: .primary) { () -> PopupViewDismissType in
UIApplication.shared.open(url, options: [:], completionHandler: openedURLCompletionHandler)
removeTabIfEmpty()
return .flyDown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class TabTrayController: LoadingViewController {
let tabManager: TabManager
let braveCore: BraveCoreMain
private var openTabsSessionServiceListener: OpenTabsSessionStateListener?

private var syncServicStateListener: AnyObject?

weak var delegate: TabTrayDelegate?
weak var toolbarUrlActionsDelegate: ToolbarUrlActionsDelegate?

Expand Down Expand Up @@ -200,6 +201,17 @@ class TabTrayController: LoadingViewController {
}
}
})

// Adding State Sync Observer to watch the states change in sync chain
syncServicStateListener = braveCore.syncAPI.addServiceStateObserver { [weak self] in
guard let self = self else { return }

if self.braveCore.syncAPI.shouldLeaveSyncGroup {
self.tabSyncView.do {
$0.updateSyncStatusPanel(for: self.emptyPanelState)
}
}
}
}

@available(*, unavailable)
Expand Down
39 changes: 31 additions & 8 deletions Sources/Brave/Frontend/Sync/BraveCore/BraveSyncAPIExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ extension BraveSyncAPI {
var isInSyncGroup: Bool {
return Preferences.Chromium.syncEnabled.value
}

/// Property that determines if the local sync chain should be resetted
var shouldLeaveSyncGroup: Bool {
guard isInSyncGroup else {
return false
}

return !(isSyncFeatureActive && isFirstSetupComplete) || isSyncAccountDeletedNoticePending
}

var isSendTabToSelfVisible: Bool {
guard let json = getDeviceListJSON(), let data = json.data(using: .utf8) else {
Expand All @@ -47,9 +56,11 @@ extension BraveSyncAPI {

@discardableResult
func joinSyncGroup(codeWords: String, syncProfileService: BraveSyncProfileServiceIOS) -> Bool {
if self.setSyncCode(codeWords) {
Preferences.Chromium.syncEnabled.value = true
if setSyncCode(codeWords) {
enableSyncTypes(syncProfileService: syncProfileService)
requestSync()
setSetupComplete()
Preferences.Chromium.syncEnabled.value = true

return true
}
Expand All @@ -60,10 +71,15 @@ extension BraveSyncAPI {
deleteDevice(deviceGuid)
}

func leaveSyncGroup() {
// Remove all observers before leaving the sync chain
removeAllObservers()

/// Method for leaving sync chain
/// Removing Observers, clearing local preferences and calling reset chain on brave-core side
/// - Parameter preservingObservers: Parameter that decides if observers should be preserved or removed
func leaveSyncGroup(preservingObservers: Bool = false) {
if !preservingObservers {
// Remove all observers before leaving the sync chain
removeAllObservers()
}

resetSyncChain()
Preferences.Chromium.syncEnabled.value = false
}
Expand Down Expand Up @@ -96,11 +112,18 @@ extension BraveSyncAPI {
}
}

func addServiceStateObserver(_ observer: @escaping () -> Void) -> AnyObject {
/// Method to add observer for SyncService for onStateChanged and onSyncShutdown
/// OnStateChanged can be called in various situations like successful initialization - services unavaiable
/// sync shutdown - sync errors - sync chain deleted
/// - Parameters:
/// - onStateChanged: Callback for sync service state changes
/// - onServiceShutdown: Callback for sync service shutdown
/// - Returns: Listener for service
func addServiceStateObserver(_ onStateChanged: @escaping () -> Void, onServiceShutdown: @escaping () -> Void = {}) -> AnyObject {
let serviceStateListener = BraveSyncServiceListener(onRemoved: { [weak self] observer in
self?.serviceObservers.remove(observer)
})
serviceStateListener.observer = createSyncServiceObserver(observer, onSyncServiceShutdown: {})
serviceStateListener.observer = createSyncServiceObserver(onStateChanged, onSyncServiceShutdown: onServiceShutdown)

serviceObservers.add(serviceStateListener)
return serviceStateListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class SyncAddDeviceViewController: SyncViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

if !syncAPI.isInSyncGroup {
if !syncAPI.isInSyncGroup && !syncAPI.isSyncFeatureActive && !syncAPI.isFirstSetupComplete {
showInitializationError()
}
}
Expand Down
Loading

0 comments on commit 0891c01

Please sign in to comment.