Skip to content

Commit

Permalink
Fix #6279: Rust fxa Staging server support (#6289)
Browse files Browse the repository at this point in the history
* Fix #6279: Rust fxa Staging server support

* RustFirefoxAccounts now takes prefs as a param

* static prefs
  • Loading branch information
garvankeeley committed Mar 25, 2020
1 parent 7fc5868 commit f042433
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Account/FxAPushMessageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extension FxAPushMessageHandler {

// return handle(plaintext: string)
let deferred = PushMessageResult()
RustFirefoxAccounts.startup() { fxa in
RustFirefoxAccounts.startup(prefs: profile.prefs) { fxa in
fxa.accountManager.deviceConstellation()?.processRawIncomingAccountEvent(pushPayload: string) {
result in
guard case .success(let events) = result else {
Expand Down
13 changes: 9 additions & 4 deletions Client/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati

pushNotificationSetup()

RustFirefoxAccounts.startup() { shared in
guard shared.accountManager.hasAccount() else { return }
NotificationCenter.default.post(name: .RegisterForPushNotifications, object: nil)
if let profile = profile as? BrowserProfile {
RustFirefoxAccounts.startup(prefs: profile.prefs) { shared in
if !shared.accountManager.hasAccount() {
// Migrate bookmarks from old browser.db to new Rust places.db only
// if this user is NOT signed into Sync (only migrates once if needed).
profile.places.migrateBookmarksIfNeeded(fromBrowserDB: profile.db)
}
}
}

if let profile = self.profile, LeanPlumClient.shouldEnable(profile: profile) {
LeanPlumClient.shared.setup(profile: profile)
LeanPlumClient.shared.set(enabled: true)
Expand Down
6 changes: 4 additions & 2 deletions Client/Frontend/Extensions/DevicePickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class DevicePickerViewController: UITableViewController {
self?.refreshControl?.endRefreshing()
}

RustFirefoxAccounts.startup() { shared in
let profile = ensureOpenProfile()
RustFirefoxAccounts.startup(prefs: profile.prefs) { shared in
shared.accountManager.deviceConstellation()?.refreshState()
}

Expand All @@ -84,7 +85,8 @@ class DevicePickerViewController: UITableViewController {
}

private func loadList() {
RustFirefoxAccounts.startup() { [weak self] shared in
let profile = ensureOpenProfile()
RustFirefoxAccounts.startup(prefs: profile.prefs) { [weak self] shared in
guard let state = shared.accountManager.deviceConstellation()?.state() else {
self?.loadingState = .loaded
return
Expand Down
1 change: 0 additions & 1 deletion Extensions/ShareTo/SendToDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class SendToDevice: DevicePickerViewControllerDelegate, InstructionsViewControll
instructionsViewController.delegate = self
return instructionsViewController
}

let devicePickerViewController = DevicePickerViewController()
devicePickerViewController.pickerDelegate = self
devicePickerViewController.profile = nil // This means the picker will open and close the default profile
Expand Down
3 changes: 2 additions & 1 deletion Extensions/ShareTo/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ class ShareViewController: UIViewController {
self.pageInfoRowTitleLabel?.text = text.quoted
}

RustFirefoxAccounts.startup() { _ in }
let profile = BrowserProfile(localName: "profile")
RustFirefoxAccounts.startup(prefs: profile.prefs) { _ in }
}

private func setupRows() {
Expand Down
6 changes: 0 additions & 6 deletions Providers/Profile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,6 @@ open class BrowserProfile: Profile {
prefs.clearAll()
}

// Migrate bookmarks from old browser.db to new Rust places.db only
// if this user is NOT signed into Sync (only migrates once if needed).
if !self.hasAccount() {
self.places.migrateBookmarksIfNeeded(fromBrowserDB: self.db)
}

// Log SQLite compile_options.
// db.sqliteCompileOptions() >>== { compileOptions in
// log.debug("SQLite compile_options:\n\(compileOptions.joined(separator: "\n"))")
Expand Down
35 changes: 22 additions & 13 deletions RustFxA/RustFirefoxAccounts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import Shared
import MozillaAppServices
import SwiftKeychainWrapper

fileprivate let prefs = NSUserDefaultsPrefs(prefix: "profile")

/**
A singleton that wraps the Rust FxA library.
The singleton design is poor for testability through dependency injection and may need to be changed in future.
Expand All @@ -23,7 +21,7 @@ open class RustFirefoxAccounts {
public var avatar: Avatar?
private static var startupCalled = false
public let syncAuthState: SyncAuthState

fileprivate static var prefs: Prefs?
public let pushNotifications = PushNotificationSetup()

// This is used so that if a migration failed, show a UI indicator for the user to manually log in to their account.
Expand All @@ -45,7 +43,8 @@ open class RustFirefoxAccounts {
The alternative implemention would be to have `shared` as a Deferred<RustFirefoxAccounts>. However that
would require a significant rewrite of existing code, for minimal added benefit.
*/
public static func startup(completion: ((RustFirefoxAccounts) -> Void)? = nil) {
public static func startup(prefs _prefs: Prefs, completion: ((RustFirefoxAccounts) -> Void)? = nil) {
prefs = _prefs
if startupCalled {
completion?(shared)
return
Expand All @@ -65,25 +64,32 @@ open class RustFirefoxAccounts {
shared.accountManager.authenticateViaMigration(sessionToken: tokens.session, kSync: tokens.ksync, kXCS: tokens.kxcs) { _ in }
}

if shared.accountManager.hasAccount() {
NotificationCenter.default.post(name: .RegisterForPushNotifications, object: nil)
}

completion?(shared)
}
}

private static let prefKeySyncAuthStateUniqueID = "PrefKeySyncAuthStateUniqueID"
private static var syncAuthStateUniqueId: String {
private static func syncAuthStateUniqueId(prefs: Prefs?) -> String {
let id: String
let key = RustFirefoxAccounts.prefKeySyncAuthStateUniqueID
if let _id = prefs.stringForKey(key) {
if let _id = prefs?.stringForKey(key) {
id = _id
} else {
id = UUID().uuidString
prefs.setString(id, forKey: key)
prefs?.setString(id, forKey: key)
}
return id
}

private init() {
let server = prefs.boolForKey("useChinaSyncService") ?? AppInfo.isChinaEdition ? FxAConfig.Server.china : FxAConfig.Server.release
let prefs = RustFirefoxAccounts.prefs
assert(prefs != nil)
let server = prefs?.intForKey(PrefsKeys.UseStageServer) == 1 ? FxAConfig.Server.dev :
(prefs?.boolForKey("useChinaSyncService") ?? AppInfo.isChinaEdition ? FxAConfig.Server.china : FxAConfig.Server.release)

let config = FxAConfig(server: server, clientId: clientID, redirectUri: redirectURL)
let type = UIDevice.current.userInterfaceIdiom == .pad ? DeviceType.tablet : DeviceType.mobile
Expand All @@ -95,7 +101,7 @@ open class RustFirefoxAccounts {

syncAuthState = FirefoxAccountSyncAuthState(
cache: KeychainCache.fromBranch("rustAccounts.syncAuthState",
withLabel: RustFirefoxAccounts.syncAuthStateUniqueId,
withLabel: RustFirefoxAccounts.syncAuthStateUniqueId(prefs: prefs),
factory: syncAuthStateCachefromJSON))

// Called when account is logged in for the first time, on every app start when the account is found (even if offline), and when migration of an account is completed.
Expand Down Expand Up @@ -187,17 +193,19 @@ open class RustFirefoxAccounts {
private var cachedUserProfile: FxAUserProfile?
public var userProfile: FxAUserProfile? {
get {
let prefs = RustFirefoxAccounts.prefs

if let profile = accountManager.accountProfile() {
if let p = cachedUserProfile, FxAUserProfile(profile: profile) == p {
return cachedUserProfile
}

cachedUserProfile = FxAUserProfile(profile: profile)
if let data = try? JSONEncoder().encode(cachedUserProfile!) {
prefs.setObject(data, forKey: prefKeyCachedUserProfile)
prefs?.setObject(data, forKey: prefKeyCachedUserProfile)
}
} else if cachedUserProfile == nil {
if let data: Data = prefs.objectForKey(prefKeyCachedUserProfile) {
if let data: Data = prefs?.objectForKey(prefKeyCachedUserProfile) {
cachedUserProfile = try? JSONDecoder().decode(FxAUserProfile.self, from: data)
}
}
Expand All @@ -208,8 +216,9 @@ open class RustFirefoxAccounts {

public func disconnect() {
accountManager.logout() { _ in }
prefs.removeObjectForKey(RustFirefoxAccounts.prefKeySyncAuthStateUniqueID)
prefs.removeObjectForKey(prefKeyCachedUserProfile)
let prefs = RustFirefoxAccounts.prefs
prefs?.removeObjectForKey(RustFirefoxAccounts.prefKeySyncAuthStateUniqueID)
prefs?.removeObjectForKey(prefKeyCachedUserProfile)
cachedUserProfile = nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion XCUITests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class IntegrationTests: BaseTestCase {
private func signInFxAccounts() {
navigator.goto(FxASigninScreen)
sleep(5)
waitForExistence(app.navigationBars["Client.FxAContentView"], timeout: 20)
waitForExistence(app.navigationBars["Client.FxAWebView"], timeout: 20)
userState.fxaUsername = ProcessInfo.processInfo.environment["FXA_EMAIL"]!
userState.fxaPassword = ProcessInfo.processInfo.environment["FXA_PASSWORD"]!
navigator.performAction(Action.FxATypeEmail)
Expand Down

0 comments on commit f042433

Please sign in to comment.