Skip to content

Commit

Permalink
Fixes local builds not delivering send tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarik Eshaq committed Nov 24, 2022
1 parent e7755ba commit 83478a6
Showing 1 changed file with 71 additions and 66 deletions.
137 changes: 71 additions & 66 deletions Account/FxAPushMessageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,82 +61,87 @@ extension FxAPushMessageHandler {

// return handle(plaintext: string)
let deferred = PushMessageResults()
RustFirefoxAccounts.reconfig(prefs: profile.prefs).uponQueue(.main) { accountManager in
accountManager.deviceConstellation()?.processRawIncomingAccountEvent(pushPayload: string) {
result in
guard case .success(let events) = result, !events.isEmpty else {
let err: PushMessageError
if case .failure(let error) = result {
SentryIntegration.shared.send(message: "Failed to get any events from FxA", tag: .fxaClient, severity: .error, description: error.localizedDescription)
err = PushMessageError.messageIncomplete(error.localizedDescription)
} else {
SentryIntegration.shared.send(message: "Got zero events from FxA", tag: .fxaClient, severity: .error, description: "no events retrieved from fxa")
err = PushMessageError.messageIncomplete("empty message")
// Reconfig has to happen on the main thread, since it calls `startup`
// and `startup` asserts that we are on the main thread. Otherwise the notification
// service will crash.
DispatchQueue.main.async {
RustFirefoxAccounts.reconfig(prefs: self.profile.prefs).uponQueue(.main) { accountManager in
accountManager.deviceConstellation()?.processRawIncomingAccountEvent(pushPayload: string) {
result in
guard case .success(let events) = result, !events.isEmpty else {
let err: PushMessageError
if case .failure(let error) = result {
SentryIntegration.shared.send(message: "Failed to get any events from FxA", tag: .fxaClient, severity: .error, description: error.localizedDescription)
err = PushMessageError.messageIncomplete(error.localizedDescription)
} else {
SentryIntegration.shared.send(message: "Got zero events from FxA", tag: .fxaClient, severity: .error, description: "no events retrieved from fxa")
err = PushMessageError.messageIncomplete("empty message")
}
deferred.fill(Maybe(failure: err))
return
}
deferred.fill(Maybe(failure: err))
return
}
var messages: [PushMessage] = []

// It's possible one of the messages is a device disconnection
// in that case, we have an async call to get the name of the device
// we should make sure not to resolve our own value before that name retrieval
// is done
var waitForClient: Deferred<Maybe<String>>?
for event in events {
switch event {
case .commandReceived(let deviceCommand):
switch deviceCommand {
case .tabReceived(_, let tabData):
let title = tabData.entries.last?.title ?? ""
let url = tabData.entries.last?.url ?? ""
messages.append(PushMessage.commandReceived(tab: ["title": title, "url": url]))
if let json = try? accountManager.gatherTelemetry() {
let events = FxATelemetry.parseTelemetry(fromJSONString: json)
events.forEach { $0.record(intoPrefs: self.profile.prefs) }
var messages: [PushMessage] = []

// It's possible one of the messages is a device disconnection
// in that case, we have an async call to get the name of the device
// we should make sure not to resolve our own value before that name retrieval
// is done
var waitForClient: Deferred<Maybe<String>>?
for event in events {
switch event {
case .commandReceived(let deviceCommand):
switch deviceCommand {
case .tabReceived(_, let tabData):
let title = tabData.entries.last?.title ?? ""
let url = tabData.entries.last?.url ?? ""
messages.append(PushMessage.commandReceived(tab: ["title": title, "url": url]))
if let json = try? accountManager.gatherTelemetry() {
let events = FxATelemetry.parseTelemetry(fromJSONString: json)
events.forEach { $0.record(intoPrefs: self.profile.prefs) }
}
}
case .deviceConnected(let deviceName):
messages.append(PushMessage.deviceConnected(deviceName))
case let .deviceDisconnected(deviceId, isLocalDevice):
if isLocalDevice {
// We can't disconnect the device from the account until we have access to the application, so we'll handle this properly in the AppDelegate (as this code in an extension),
// by calling the FxALoginHelper.applicationDidDisonnect(application).
self.profile.prefs.setBool(true, forKey: PendingAccountDisconnectedKey)
messages.append(PushMessage.thisDeviceDisconnected)
}
}
case .deviceConnected(let deviceName):
messages.append(PushMessage.deviceConnected(deviceName))
case let .deviceDisconnected(deviceId, isLocalDevice):
if isLocalDevice {
// We can't disconnect the device from the account until we have access to the application, so we'll handle this properly in the AppDelegate (as this code in an extension),
// by calling the FxALoginHelper.applicationDidDisonnect(application).
self.profile.prefs.setBool(true, forKey: PendingAccountDisconnectedKey)
messages.append(PushMessage.thisDeviceDisconnected)
}

guard let profile = self.profile as? BrowserProfile else {
// We can't look up a name in testing, so this is the same as not knowing about it.
messages.append(PushMessage.deviceDisconnected(nil))
break
}

waitForClient = Deferred<Maybe<String>>()
profile.remoteClientsAndTabs.getClient(fxaDeviceId: deviceId).uponQueue(.main) { result in
guard let device = result.successValue else {
waitForClient?.fill(Maybe(failure: result.failureValue ?? "Unknown Error"))
return
guard let profile = self.profile as? BrowserProfile else {
// We can't look up a name in testing, so this is the same as not knowing about it.
messages.append(PushMessage.deviceDisconnected(nil))
break
}
messages.append(PushMessage.deviceDisconnected(device?.name))
waitForClient?.fill(Maybe(success: device?.name ?? "Unknown Device"))
if let id = device?.guid {
profile.remoteClientsAndTabs.deleteClient(guid: id).uponQueue(.main) { _ in
print("deleted client")

waitForClient = Deferred<Maybe<String>>()
profile.remoteClientsAndTabs.getClient(fxaDeviceId: deviceId).uponQueue(.main) { result in
guard let device = result.successValue else {
waitForClient?.fill(Maybe(failure: result.failureValue ?? "Unknown Error"))
return
}
messages.append(PushMessage.deviceDisconnected(device?.name))
waitForClient?.fill(Maybe(success: device?.name ?? "Unknown Device"))
if let id = device?.guid {
profile.remoteClientsAndTabs.deleteClient(guid: id).uponQueue(.main) { _ in
print("deleted client")
}
}
}
default:
// There are other events, but we ignore them at this level.
do {}
}
default:
// There are other events, but we ignore them at this level.
do {}
}
}
if let waitForClient = waitForClient {
waitForClient.upon { _ in
if let waitForClient = waitForClient {
waitForClient.upon { _ in
deferred.fill(Maybe(success: messages))
}
} else {
deferred.fill(Maybe(success: messages))
}
} else {
deferred.fill(Maybe(success: messages))
}
}
}
Expand Down

0 comments on commit 83478a6

Please sign in to comment.