@@ -97,10 +97,16 @@ final class ShellController: NSObject, WebControllerDelegate {
9797 #if DEBUG
9898 if config . resetDataStore {
9999 steps . append { [ weak self ] next in
100- let store = WKWebsiteDataStore . default ( )
100+ guard let self = self else { return }
101+ let store = self . web . webView . configuration . websiteDataStore
102+ let current = self . config . dataStoreID
101103 store . removeData ( ofTypes : WKWebsiteDataStore . allWebsiteDataTypes ( ) , modifiedSince : . distantPast ) {
102- self ? . diagnostics . log ( " test-hook " , [ " hook " : " reset-data-store " ] )
103- DispatchQueue . main . async { next ( ) }
104+ // Other servers' stores from earlier tests go too.
105+ WKWebsiteDataStore . fetchAllDataStoreIdentifiers { ids in
106+ for id in ids where id != current { WKWebsiteDataStore . remove ( forIdentifier : id ) { _ in } }
107+ self . diagnostics . log ( " test-hook " , [ " hook " : " reset-data-store " , " otherStores " : ids . count ] )
108+ DispatchQueue . main . async { next ( ) }
109+ }
104110 }
105111 }
106112 }
@@ -404,14 +410,21 @@ final class ShellController: NSObject, WebControllerDelegate {
404410 web . stop ( )
405411 model . signIn = . idle
406412 model . addressChecking = true
407- // Everything the web view stored, for every site: nothing of one server carries to the other.
408- WKWebsiteDataStore . default ( ) . removeData ( ofTypes : WKWebsiteDataStore . allWebsiteDataTypes ( ) , modifiedSince : . distantPast ) { [ weak self ] in
409- DispatchQueue . main . async {
410- guard let self = self else { return }
411- self . diagnostics . log ( " site-data-cleared " , [ " reason " : " server-switch " , " all " : true ] )
412- ShellConfig . saveSelfHosted ( origin )
413- self . model . addressChecking = false
414- self . onRestart ? ( )
413+ // Each server has a data store of its own: the new one is simply another store, nothing to
414+ // wait for. The old one is removed once its web view is gone.
415+ let oldStore = config . dataStoreID
416+ ShellConfig . saveSelfHosted ( origin )
417+ model . addressChecking = false
418+ onRestart ? ( )
419+ ShellController . removeStore ( oldStore , attempt : 1 )
420+ }
421+
422+ /// Removes a server's data store in the background (it fails while a web view still uses it:
423+ /// tried again a few times).
424+ static func removeStore ( _ id : UUID , attempt : Int ) {
425+ DispatchQueue . main . asyncAfter ( deadline : . now ( ) + Double ( attempt ) ) {
426+ WKWebsiteDataStore . remove ( forIdentifier : id ) { error in
427+ if error != nil , attempt < 5 { removeStore ( id , attempt : attempt + 1 ) }
415428 }
416429 }
417430 }
@@ -516,9 +529,14 @@ final class ShellController: NSObject, WebControllerDelegate {
516529 // at a cold launch, see decideLaunchState). The server refuses `enter` for a web view
517530 // holding another person's session, so the site's data goes first, and `enter` loads
518531 // only once that has finished.
519- clearSiteData ( reason : " before-enter " ) { [ weak self ] in
532+ clearSiteData ( reason : " before-enter " ) { [ weak self ] finished in
520533 guard let self = self , self . model . signIn == . entering else { return }
521- load ( )
534+ if finished {
535+ load ( )
536+ } else {
537+ // Never sign in on unknown state: the leftover session may still be there.
538+ self . failSignIn ( " Sign-in didn't finish. Try again. " )
539+ }
522540 }
523541 }
524542 }
@@ -582,20 +600,21 @@ final class ShellController: NSObject, WebControllerDelegate {
582600 /// that is still running is waited for, so a sign-in that starts during sign-out's clear loads
583601 /// `enter` only after it.
584602 private var siteDataClearWaiters : [ ( ) -> Void ] ?
585- private func clearSiteData ( reason : String , completion : ( ( ) -> Void ) ? = nil ) {
586- // Never left waiting: WebKit once did not call back at all (run 46164083101, right after the
587- // app was built again for another server). After 4 s the caller goes ahead.
603+ /// Calls back with true once the clear has finished, or false after 4 s without an answer from
604+ /// WebKit (it did not answer once, right after a change of server in run 46164083101, when all
605+ /// servers shared 1 store; each now has its own). The caller then fails rather than going on.
606+ private func clearSiteData ( reason : String , completion : ( ( Bool ) -> Void ) ? = nil ) {
588607 var completion = completion
589608 if let inner = completion {
590609 var called = false
591- let once = { [ weak self ] ( timedOut : Bool ) in
610+ let once = { [ weak self ] ( finished : Bool ) in
592611 guard ! called else { return }
593612 called = true
594- if timedOut { self ? . diagnostics . log ( " site-data-clear-timeout " , [ " reason " : reason ] ) }
595- inner ( )
613+ if ! finished { self ? . diagnostics . log ( " site-data-clear-timeout " , [ " reason " : reason ] ) }
614+ inner ( finished )
596615 }
597- completion = { once ( false ) }
598- DispatchQueue . main . asyncAfter ( deadline : . now ( ) + 4 ) { once ( true ) }
616+ completion = { _ in once ( true ) }
617+ DispatchQueue . main . asyncAfter ( deadline : . now ( ) + 4 ) { once ( false ) }
599618 }
600619 if siteDataClearWaiters != nil {
601620 // One clear at a time; a later caller waits for this one and then clears again.
@@ -627,7 +646,7 @@ final class ShellController: NSObject, WebControllerDelegate {
627646 self . model . siteDataClears += 1
628647 let waiters = self . siteDataClearWaiters ?? [ ]
629648 self . siteDataClearWaiters = nil
630- completion ? ( )
649+ completion ? ( true )
631650 waiters . forEach { $0 ( ) }
632651 }
633652 }
0 commit comments