Prefix types defined in RustFxAFFI.h

This commit is contained in:
Edouard Oger 2019-02-25 17:28:55 -05:00
Родитель 462ba74071
Коммит 23f4c2c976
5 изменённых файлов: 62 добавлений и 60 удалений

Просмотреть файл

@ -4,6 +4,12 @@
[Full Changelog](https://github.com/mozilla/application-services/compare/v0.17.0...master)
## FxA
### Breaking Changes
- Swift: `FxAError` has been renamed to `FirefoxAccountError` ([#713](https://github.com/mozilla/application-services/pull/713))
## Places
### What's Fixed

Просмотреть файл

@ -4,7 +4,7 @@
import Foundation
public enum FxAError: Error {
public enum FirefoxAccountError: Error {
case Unauthorized(message: String)
case Network(message: String)
case Unspecified(message: String)
@ -13,18 +13,18 @@ public enum FxAError: Error {
// The name is attempting to indicate that we free fxaError.message if it
// existed, and that it's a very bad idea to touch it after you call this
// function
static func fromConsuming(_ fxaError: FxAErrorC) -> FxAError? {
let message = fxaError.message
switch Int(fxaError.code) {
case NoError:
static func fromConsuming(_ rustError: FxAError) -> FirefoxAccountError? {
let message = rustError.message
switch rustError.code {
case FxA_NoError:
return nil
case NetworkError:
case FxA_NetworkError:
return .Network(message: String(freeingFxaString: message!))
case AuthenticationError:
case FxA_AuthenticationError:
return .Unauthorized(message: String(freeingFxaString: message!))
case Other:
case FxA_Other:
return .Unspecified(message: String(freeingFxaString: message!))
case InternalPanic:
case FxA_InternalPanic:
return .Panic(message: String(freeingFxaString: message!))
default:
return .Unspecified(message: String(freeingFxaString: message!))
@ -32,10 +32,10 @@ public enum FxAError: Error {
}
@discardableResult
public static func unwrap<T>(_ callback: (UnsafeMutablePointer<FxAErrorC>) throws -> T?) throws -> T {
var err = FxAErrorC(code: Int32(NoError), message: nil)
public static func unwrap<T>(_ callback: (UnsafeMutablePointer<FxAError>) throws -> T?) throws -> T {
var err = FxAError(code: FxA_NoError, message: nil)
let returnedVal = try callback(&err)
if let fxaErr = FxAError.fromConsuming(err) {
if let fxaErr = FirefoxAccountError.fromConsuming(err) {
throw fxaErr
}
guard let result = returnedVal else {
@ -45,10 +45,10 @@ public enum FxAError: Error {
}
@discardableResult
public static func tryUnwrap<T>(_ callback: (UnsafeMutablePointer<FxAErrorC>) throws -> T?) throws -> T? {
var err = FxAErrorC(code: Int32(NoError), message: nil)
public static func tryUnwrap<T>(_ callback: (UnsafeMutablePointer<FxAError>) throws -> T?) throws -> T? {
var err = FxAError(code: FxA_NoError, message: nil)
let returnedVal = try callback(&err)
if let fxaErr = FxAError.fromConsuming(err) {
if let fxaErr = FirefoxAccountError.fromConsuming(err) {
throw fxaErr
}
return returnedVal

Просмотреть файл

@ -5,7 +5,7 @@
import Foundation
extension Data {
init(rustBuffer: RustBuffer) {
init(rustBuffer: FxARustBuffer) {
self.init(bytes: rustBuffer.data!, count: Int(rustBuffer.len))
}
}

Просмотреть файл

@ -62,7 +62,7 @@ open class FirefoxAccount {
/// should not be re-used.
public convenience init(config: FxAConfig) throws {
let pointer = try queue.sync(execute: {
return try FxAError.unwrap({err in
return try FirefoxAccountError.unwrap({err in
fxa_new(config.contentUrl, config.clientId, config.redirectUri, err)
})
})
@ -72,7 +72,7 @@ open class FirefoxAccount {
/// Restore a previous instance of `FirefoxAccount` from a serialized state (obtained with `toJSON(...)`).
open class func fromJSON(state: String) throws -> FirefoxAccount {
return try queue.sync(execute: {
let handle = try FxAError.unwrap({ err in fxa_from_json(state, err) })
let handle = try FirefoxAccountError.unwrap({ err in fxa_from_json(state, err) })
return FirefoxAccount(raw: handle)
})
}
@ -80,7 +80,7 @@ open class FirefoxAccount {
deinit {
if self.raw != 0 {
queue.sync(execute: {
try! FxAError.unwrap({err in
try! FirefoxAccountError.unwrap({err in
// Is `try!` the right thing to do? We should only hit an error here
// for panics and handle misuse, both inidicate bugs in our code
// (the first in the rust code, the 2nd in this swift wrapper).
@ -99,7 +99,7 @@ open class FirefoxAccount {
}
private func toJSONInternal() throws -> String {
return String(freeingFxaString: try FxAError.unwrap({err in
return String(freeingFxaString: try FirefoxAccountError.unwrap({err in
fxa_to_json(self.raw, err)
}))
}
@ -135,13 +135,13 @@ open class FirefoxAccount {
}
/// Gets the logged-in user profile.
/// Throws `FxAError.Unauthorized` if we couldn't find any suitable access token
/// Throws `FirefoxAccountError.Unauthorized` if we couldn't find any suitable access token
/// to make that call. The caller should then start the OAuth Flow again with
/// the "profile" scope.
open func getProfile(completionHandler: @escaping (Profile?, Error?) -> Void) {
queue.async {
do {
let profileBuffer = try FxAError.unwrap({err in
let profileBuffer = try FirefoxAccountError.unwrap({err in
fxa_profile(self.raw, false, err)
})
let msg = try! MsgTypes_Profile(serializedData: Data(rustBuffer: profileBuffer))
@ -156,7 +156,7 @@ open class FirefoxAccount {
open func getTokenServerEndpointURL() throws -> URL {
return try queue.sync(execute: {
return URL(string: String(freeingFxaString: try FxAError.unwrap({err in
return URL(string: String(freeingFxaString: try FirefoxAccountError.unwrap({err in
fxa_get_token_server_endpoint_url(self.raw, err)
})))!
})
@ -164,7 +164,7 @@ open class FirefoxAccount {
open func getConnectionSuccessURL() throws -> URL {
return try queue.sync(execute: {
return URL(string: String(freeingFxaString: try FxAError.unwrap({err in
return URL(string: String(freeingFxaString: try FirefoxAccountError.unwrap({err in
fxa_get_connection_success_url(self.raw, err)
})))!
})
@ -183,7 +183,7 @@ open class FirefoxAccount {
queue.async {
do {
let scope = scopes.joined(separator: " ")
let url = URL(string: String(freeingFxaString: try FxAError.unwrap({err in
let url = URL(string: String(freeingFxaString: try FirefoxAccountError.unwrap({err in
fxa_begin_oauth_flow(self.raw, scope, wantsKeys, err)
})))!
DispatchQueue.main.async { completionHandler(url, nil) }
@ -200,7 +200,7 @@ open class FirefoxAccount {
open func completeOAuthFlow(code: String, state: String, completionHandler: @escaping (Void, Error?) -> Void) {
queue.async {
do {
try FxAError.unwrap({err in
try FirefoxAccountError.unwrap({err in
fxa_complete_oauth_flow(self.raw, code, state, err)
})
DispatchQueue.main.async { completionHandler((), nil) }
@ -213,13 +213,13 @@ open class FirefoxAccount {
/// Try to get an OAuth access token.
///
/// Throws `FxAError.Unauthorized` if we couldn't provide an access token
/// Throws `FirefoxAccountError.Unauthorized` if we couldn't provide an access token
/// for this scope. The caller should then start the OAuth Flow again with
/// the desired scope.
open func getAccessToken(scope: String, completionHandler: @escaping (AccessTokenInfo?, Error?) -> Void) {
queue.async {
do {
let infoBuffer = try FxAError.unwrap({err in
let infoBuffer = try FirefoxAccountError.unwrap({err in
fxa_get_access_token(self.raw, scope, err)
})
let msg = try! MsgTypes_AccessTokenInfo(serializedData: Data(rustBuffer: infoBuffer))

Просмотреть файл

@ -2,9 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef fxa_h
#define fxa_h
#pragma once
#include <stdint.h>
#include <Foundation/NSObjCRuntime.h>
@ -19,69 +17,67 @@
/*
Error codes reported by the fxa-client library, from fxa-client/src/ffi.rs
*/
enum {
InternalPanic = -1,
NoError = 0,
Other = 1,
AuthenticationError = 2,
NetworkError = 3,
};
typedef enum FxAErrorCode {
FxA_InternalPanic = -1,
FxA_NoError = 0,
FxA_Other = 1,
FxA_AuthenticationError = 2,
FxA_NetworkError = 3,
} FxAErrorCode;
/*
A mapping of the ExternError repr(C) Rust struct, from components/support/ffi/src/error.rs.
*/
typedef struct FxAErrorC {
int32_t code;
typedef struct FxAError {
FxAErrorCode code;
char *_Nullable message;
} FxAErrorC;
} FxAError;
/*
A mapping of the ByteBuffer repr(C) Rust struct, from components/support/ffi/src/lib.rs.
*/
typedef struct RustBuffer {
typedef struct FxARustBuffer {
int64_t len;
uint8_t *_Nullable data;
} RustBuffer;
} FxARustBuffer;
typedef uint64_t FirefoxAccountHandle;
char *_Nonnull fxa_begin_oauth_flow(FirefoxAccountHandle handle,
const char *_Nonnull scopes,
bool wants_keys,
FxAErrorC *_Nonnull out);
FxAError *_Nonnull out);
void fxa_complete_oauth_flow(FirefoxAccountHandle handle,
const char *_Nonnull code,
const char *_Nonnull state,
FxAErrorC *_Nonnull out);
FxAError *_Nonnull out);
RustBuffer fxa_get_access_token(FirefoxAccountHandle handle,
const char *_Nonnull scope,
FxAErrorC *_Nonnull out);
FxARustBuffer fxa_get_access_token(FirefoxAccountHandle handle,
const char *_Nonnull scope,
FxAError *_Nonnull out);
FirefoxAccountHandle fxa_from_json(const char *_Nonnull json,
FxAErrorC *_Nonnull out);
FxAError *_Nonnull out);
char *_Nullable fxa_to_json(FirefoxAccountHandle handle,
FxAErrorC *_Nonnull out);
FxAError *_Nonnull out);
FirefoxAccountHandle fxa_new(const char *_Nonnull content_base,
const char *_Nonnull client_id,
const char *_Nonnull redirect_uri,
FxAErrorC *_Nonnull out);
FxAError *_Nonnull out);
RustBuffer fxa_profile(FirefoxAccountHandle handle,
bool ignore_cache,
FxAErrorC *_Nonnull out);
FxARustBuffer fxa_profile(FirefoxAccountHandle handle,
bool ignore_cache,
FxAError *_Nonnull out);
char *_Nullable fxa_get_token_server_endpoint_url(FirefoxAccountHandle handle,
FxAErrorC *_Nonnull out);
FxAError *_Nonnull out);
char *_Nullable fxa_get_connection_success_url(FirefoxAccountHandle handle,
FxAErrorC *_Nonnull out);
FxAError *_Nonnull out);
void fxa_str_free(char *_Nullable ptr);
void fxa_free(FirefoxAccountHandle h, FxAErrorC *_Nonnull out);
void fxa_bytebuffer_free(RustBuffer buffer);
#endif /* fxa_h */
void fxa_free(FirefoxAccountHandle h, FxAError *_Nonnull out);
void fxa_bytebuffer_free(FxARustBuffer buffer);