Skip to content

Commit dae882f

Browse files
Emily ToopStephan Leroux
Emily Toop
authored and
Stephan Leroux
committedFeb 13, 2017
Migrates codebase to Swift 3.0
1 parent 51abcc7 commit dae882f

File tree

1,961 files changed

+22175
-714015
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,961 files changed

+22175
-714015
lines changed
 

‎.swiftlint.yml

+12
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,24 @@ disabled_rules: # rule identifiers to exclude from running
2222
- operator_whitespace
2323
- file_length
2424
- mark
25+
- unused_closure_parameter
26+
- empty_parentheses_with_trailing_closure
27+
- redundant_string_enum_value
28+
- large_tuple
29+
- vertical_parameter_alignment
30+
- class_delegate_protocol
31+
- syntactic_sugar
32+
- implicit_getter
33+
- weak_delegate
34+
- shorthand_operator
35+
- trailing_comma
2536
opt_in_rules: # some rules are only opt-in
2637
- closing_brace
2738
- opening_brace
2839
- return_arrow_whitespace
2940
- trailing_semicolon
3041
- statement_position
42+
- explicit_init
3143
# Find all the available rules by running:
3244
# swiftlint rules
3345
included: # paths to include during linting. `--path` is ignored if present.

‎Account/FirefoxAccount.swift

+51-51
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Foundation
66
import Shared
77
import XCGLogger
88
import Deferred
9+
import SwiftyJSON
910

1011
private let log = Logger.syncLogger
1112

@@ -20,31 +21,31 @@ let AccountSchemaVersion = 1
2021
///
2122
/// Non-sensitive but persistent data should be maintained outside of
2223
/// the account itself.
23-
public class FirefoxAccount {
24+
open class FirefoxAccount {
2425
/// The email address identifying the account. A Firefox Account is uniquely identified on a particular server
2526
/// (auth endpoint) by its email address.
26-
public let email: String
27+
open let email: String
2728

2829
/// The auth endpoint user identifier identifying the account. A Firefox Account is uniquely identified on a
2930
/// particular server (auth endpoint) by its assigned uid.
30-
public let uid: String
31+
open let uid: String
3132

32-
public var deviceRegistration: FxADeviceRegistration?
33+
open var deviceRegistration: FxADeviceRegistration?
3334

34-
public let configuration: FirefoxAccountConfiguration
35+
open let configuration: FirefoxAccountConfiguration
3536

36-
private let stateCache: KeychainCache<FxAState>
37-
public var syncAuthState: SyncAuthState! // We can't give a reference to self if this is a let.
37+
fileprivate let stateCache: KeychainCache<FxAState>
38+
open var syncAuthState: SyncAuthState! // We can't give a reference to self if this is a let.
3839

3940
// To prevent advance() consumers racing, we maintain a shared advance() deferred (`advanceDeferred`). If an
4041
// advance() is in progress, the shared deferred will be returned. (Multiple consumers can chain off a single
4142
// deferred safely.) If no advance() is in progress, a new shared deferred will be scheduled and returned. To
4243
// prevent data races against the shared deferred, advance() locks accesses to `advanceDeferred` using
4344
// `advanceLock`.
44-
private var advanceLock = OSSpinLock()
45-
private var advanceDeferred: Deferred<FxAState>? = nil
45+
fileprivate var advanceLock = OSSpinLock()
46+
fileprivate var advanceDeferred: Deferred<FxAState>?
4647

47-
public var actionNeeded: FxAActionNeeded {
48+
open var actionNeeded: FxAActionNeeded {
4849
return stateCache.value!.actionNeeded
4950
}
5051

@@ -63,34 +64,34 @@ public class FirefoxAccount {
6364
cache: KeychainCache.fromBranch("account.syncAuthState", withLabel: self.stateCache.label, factory: syncAuthStateCachefromJSON))
6465
}
6566

66-
public class func fromConfigurationAndJSON(configuration: FirefoxAccountConfiguration, data: JSON) -> FirefoxAccount? {
67-
guard let email = data["email"].asString ,
68-
let uid = data["uid"].asString,
69-
let sessionToken = data["sessionToken"].asString?.hexDecodedData,
70-
let keyFetchToken = data["keyFetchToken"].asString?.hexDecodedData,
71-
let unwrapkB = data["unwrapBKey"].asString?.hexDecodedData else {
67+
open class func from(_ configuration: FirefoxAccountConfiguration, andJSON data: JSON) -> FirefoxAccount? {
68+
guard let email = data["email"].string ,
69+
let uid = data["uid"].string,
70+
let sessionToken = data["sessionToken"].string?.hexDecodedData,
71+
let keyFetchToken = data["keyFetchToken"].string?.hexDecodedData,
72+
let unwrapkB = data["unwrapBKey"].string?.hexDecodedData else {
7273
return nil
7374
}
7475

75-
let verified = data["verified"].asBool ?? false
76-
return FirefoxAccount.fromConfigurationAndParameters(configuration,
77-
email: email, uid: uid, deviceRegistration: nil, verified: verified,
76+
let verified = data["verified"].bool ?? false
77+
return FirefoxAccount.from(configuration: configuration,
78+
andParametersWithEmail: email, uid: uid, deviceRegistration: nil, verified: verified,
7879
sessionToken: sessionToken, keyFetchToken: keyFetchToken, unwrapkB: unwrapkB)
7980
}
8081

81-
public class func fromConfigurationAndLoginResponse(configuration: FirefoxAccountConfiguration,
82-
response: FxALoginResponse, unwrapkB: NSData) -> FirefoxAccount {
83-
return FirefoxAccount.fromConfigurationAndParameters(configuration,
84-
email: response.remoteEmail, uid: response.uid, deviceRegistration: nil, verified: response.verified,
85-
sessionToken: response.sessionToken, keyFetchToken: response.keyFetchToken, unwrapkB: unwrapkB)
82+
open class func from(_ configuration: FirefoxAccountConfiguration,
83+
andLoginResponse response: FxALoginResponse, unwrapkB: Data) -> FirefoxAccount {
84+
return FirefoxAccount.from(configuration: configuration,
85+
andParametersWithEmail: response.remoteEmail, uid: response.uid, deviceRegistration: nil, verified: response.verified,
86+
sessionToken: response.sessionToken as Data, keyFetchToken: response.keyFetchToken as Data, unwrapkB: unwrapkB)
8687
}
8788

88-
private class func fromConfigurationAndParameters(configuration: FirefoxAccountConfiguration,
89-
email: String, uid: String, deviceRegistration: FxADeviceRegistration?, verified: Bool,
90-
sessionToken: NSData, keyFetchToken: NSData, unwrapkB: NSData) -> FirefoxAccount {
89+
fileprivate class func from(configuration: FirefoxAccountConfiguration,
90+
andParametersWithEmail email: String, uid: String, deviceRegistration: FxADeviceRegistration?, verified: Bool,
91+
sessionToken: Data, keyFetchToken: Data, unwrapkB: Data) -> FirefoxAccount {
9192
var state: FxAState! = nil
9293
if !verified {
93-
let now = NSDate.now()
94+
let now = Date.now()
9495
state = EngagedBeforeVerifiedState(knownUnverifiedAt: now,
9596
lastNotifiedUserAt: now,
9697
sessionToken: sessionToken,
@@ -116,8 +117,8 @@ public class FirefoxAccount {
116117
return account
117118
}
118119

119-
public func asDictionary() -> [String: AnyObject] {
120-
var dict: [String: AnyObject] = [:]
120+
open func dictionary() -> [String: Any] {
121+
var dict: [String: Any] = [:]
121122
dict["version"] = AccountSchemaVersion
122123
dict["email"] = email
123124
dict["uid"] = uid
@@ -127,7 +128,7 @@ public class FirefoxAccount {
127128
return dict
128129
}
129130

130-
public class func fromDictionary(dictionary: [String: AnyObject]) -> FirefoxAccount? {
131+
open class func fromDictionary(_ dictionary: [String: Any]) -> FirefoxAccount? {
131132
if let version = dictionary["version"] as? Int {
132133
if version == AccountSchemaVersion {
133134
return FirefoxAccount.fromDictionaryV1(dictionary)
@@ -136,17 +137,17 @@ public class FirefoxAccount {
136137
return nil
137138
}
138139

139-
private class func fromDictionaryV1(dictionary: [String: AnyObject]) -> FirefoxAccount? {
140+
fileprivate class func fromDictionaryV1(_ dictionary: [String: Any]) -> FirefoxAccount? {
140141
var configurationLabel: FirefoxAccountConfigurationLabel? = nil
141142
if let rawValue = dictionary["configurationLabel"] as? String {
142143
configurationLabel = FirefoxAccountConfigurationLabel(rawValue: rawValue)
143144
}
144145
if let
145146
configurationLabel = configurationLabel,
146-
email = dictionary["email"] as? String,
147-
uid = dictionary["uid"] as? String {
147+
let email = dictionary["email"] as? String,
148+
let uid = dictionary["uid"] as? String {
148149
let deviceRegistration = dictionary["deviceRegistration"] as? FxADeviceRegistration
149-
let stateCache = KeychainCache.fromBranch("account.state", withLabel: dictionary["stateKeyLabel"] as? String, withDefault: SeparatedState(), factory: stateFromJSON)
150+
let stateCache = KeychainCache.fromBranch("account.state", withLabel: dictionary["stateKeyLabel"] as? String, withDefault: SeparatedState(), factory: state)
150151
return FirefoxAccount(
151152
configuration: configurationLabel.toConfiguration(),
152153
email: email, uid: uid,
@@ -157,16 +158,16 @@ public class FirefoxAccount {
157158
}
158159

159160
public enum AccountError: MaybeErrorType {
160-
case NotMarried
161+
case notMarried
161162

162163
public var description: String {
163164
switch self {
164-
case NotMarried: return "Not married."
165+
case .notMarried: return "Not married."
165166
}
166167
}
167168
}
168169

169-
public func advance() -> Deferred<FxAState> {
170+
@discardableResult open func advance() -> Deferred<FxAState> {
170171
OSSpinLockLock(&advanceLock)
171172
if let deferred = advanceDeferred {
172173
// We already have an advance() in progress. This consumer can chain from it.
@@ -179,10 +180,9 @@ public class FirefoxAccount {
179180
let cachedState = stateCache.value!
180181
var registration = succeed()
181182
if let session = cachedState as? TokenState {
182-
registration = FxADeviceRegistrator.registerOrUpdateDevice(self, sessionToken: session.sessionToken).bind { result in
183-
if result.successValue != FxADeviceRegistrationResult.AlreadyRegistered {
184-
let notification = NSNotification(name: NotificationFirefoxAccountDeviceRegistrationUpdated, object: nil)
185-
NSNotificationCenter.defaultCenter().postNotification(notification)
183+
registration = FxADeviceRegistrator.registerOrUpdateDevice(self, sessionToken: session.sessionToken as NSData).bind { result in
184+
if result.successValue != FxADeviceRegistrationResult.alreadyRegistered {
185+
NotificationCenter.default.post(name: NotificationFirefoxAccountDeviceRegistrationUpdated, object: nil)
186186
}
187187
return succeed()
188188
}
@@ -191,8 +191,8 @@ public class FirefoxAccount {
191191
let deferred: Deferred<FxAState> = registration.bind { _ in
192192
let client = FxAClient10(endpoint: self.configuration.authEndpointURL)
193193
let stateMachine = FxALoginStateMachine(client: client)
194-
let now = NSDate.now()
195-
return stateMachine.advanceFromState(cachedState, now: now).map { newState in
194+
let now = Date.now()
195+
return stateMachine.advance(fromState: cachedState, now: now).map { newState in
196196
self.stateCache.value = newState
197197
return newState
198198
}
@@ -205,7 +205,7 @@ public class FirefoxAccount {
205205
deferred.upon { _ in
206206
// This advance() is complete. Clear the shared deferred.
207207
OSSpinLockLock(&self.advanceLock)
208-
if let existingDeferred = self.advanceDeferred where existingDeferred === deferred {
208+
if let existingDeferred = self.advanceDeferred, existingDeferred === deferred {
209209
// The guard should not be needed, but should prevent trampling racing consumers.
210210
self.advanceDeferred = nil
211211
log.debug("advance() completed and shared deferred is existing deferred; clearing shared deferred.")
@@ -217,30 +217,30 @@ public class FirefoxAccount {
217217
return deferred
218218
}
219219

220-
public func marriedState() -> Deferred<Maybe<MarriedState>> {
220+
open func marriedState() -> Deferred<Maybe<MarriedState>> {
221221
return advance().map { newState in
222-
if newState.label == FxAStateLabel.Married {
222+
if newState.label == FxAStateLabel.married {
223223
if let married = newState as? MarriedState {
224224
return Maybe(success: married)
225225
}
226226
}
227-
return Maybe(failure: AccountError.NotMarried)
227+
return Maybe(failure: AccountError.notMarried)
228228
}
229229
}
230230

231-
public func makeSeparated() -> Bool {
231+
@discardableResult open func makeSeparated() -> Bool {
232232
log.info("Making Account State be Separated.")
233233
self.stateCache.value = SeparatedState()
234234
return true
235235
}
236236

237-
public func makeDoghouse() -> Bool {
237+
@discardableResult open func makeDoghouse() -> Bool {
238238
log.info("Making Account State be Doghouse.")
239239
self.stateCache.value = DoghouseState()
240240
return true
241241
}
242242

243-
public func makeCohabitingWithoutKeyPair() -> Bool {
243+
open func makeCohabitingWithoutKeyPair() -> Bool {
244244
if let married = self.stateCache.value as? MarriedState {
245245
log.info("Making Account State be CohabitingWithoutKeyPair.")
246246
self.stateCache.value = married.withoutKeyPair()

0 commit comments

Comments
 (0)
Please sign in to comment.