mirror of https://github.com/oxen-io/session-ios
Resolve more errors
parent
b7c6dba0f3
commit
53a8c9fd57
@ -0,0 +1,53 @@
|
||||
//
|
||||
// Copyright (c) 2020 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class DarwinNotificationName;
|
||||
|
||||
extern const int DarwinNotificationInvalidObserver;
|
||||
|
||||
@interface DarwinNotificationCenter : NSObject
|
||||
|
||||
/// Determines if an observer token is valid for a current registration.
|
||||
/// Negative integers are never valid. A positive or zero value is valid
|
||||
/// if the current process has a registration associated with the given value.
|
||||
/// @param observerToken The token returned by `addObserverForName:`
|
||||
+ (BOOL)isValidObserver:(int)observerToken;
|
||||
|
||||
/// Post a darwin notification that can be listened for from other processes.
|
||||
/// @param name The name of the notification to post.
|
||||
+ (void)postNotificationName:(DarwinNotificationName *)name;
|
||||
|
||||
/// Add an observer for a darwin notification of the given name.
|
||||
/// @param name The name of the notification to listen for.
|
||||
/// @param queue The queue to callback on.
|
||||
/// @param block The block to callback. Includes the observer token as an input parameter to allow
|
||||
/// removing the observer after receipt.
|
||||
/// @return An `int` observer token that can be used to remove this observer.
|
||||
+ (int)addObserverForName:(DarwinNotificationName *)name queue:(dispatch_queue_t)queue usingBlock:(void (^)(int))block;
|
||||
|
||||
/// Stops listening for notifications registered by the given observer token.
|
||||
/// @param observerToken The token returned by `addObserverForName:` for the notification you want to stop listening
|
||||
/// for.
|
||||
+ (void)removeObserver:(int)observerToken;
|
||||
|
||||
/// Sets the state value for a given observer. This value can be set and read from
|
||||
/// any process listening for this notification. Note: `setState:` and `getState`
|
||||
/// are vulnerable to races.
|
||||
/// @param state The `uint64_t` state you wish to share with another process.
|
||||
/// @param observerToken The token returned by `addObserverForName:` for the notification you want to set state for.
|
||||
+ (void)setState:(uint64_t)state forObserver:(int)observerToken;
|
||||
|
||||
/// Retrieves the state for a given observer. This value can be set and read from
|
||||
/// any process listening for this notification. Note: `setState:` and `getState`
|
||||
/// are vulnerable to races.
|
||||
/// @param observerToken The token returned by `addObserverForName:` for the notification you want to get state for.
|
||||
+ (uint64_t)getStateForObserver:(int)observerToken;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,61 @@
|
||||
//
|
||||
// Copyright (c) 2021 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
#import <SessionUtilitiesKit/DarwinNotificationCenter.h>
|
||||
#import <SessionUtilitiesKit/SessionUtilitiesKit-Swift.h>
|
||||
#import <notify.h>
|
||||
|
||||
const int DarwinNotificationInvalidObserver = NOTIFY_TOKEN_INVALID;
|
||||
|
||||
@implementation DarwinNotificationCenter
|
||||
|
||||
+ (BOOL)isValidObserver:(int)observerToken
|
||||
{
|
||||
return notify_is_valid_token(observerToken);
|
||||
}
|
||||
|
||||
+ (void)postNotificationName:(DarwinNotificationName *)name
|
||||
{
|
||||
notify_post((const char *)name.cString);
|
||||
}
|
||||
|
||||
+ (int)addObserverForName:(DarwinNotificationName *)name
|
||||
queue:(dispatch_queue_t)queue
|
||||
usingBlock:(notify_handler_t)block
|
||||
{
|
||||
int observerToken;
|
||||
notify_register_dispatch((const char *)name.cString, &observerToken, queue, block);
|
||||
return observerToken;
|
||||
}
|
||||
|
||||
+ (void)removeObserver:(int)observerToken
|
||||
{
|
||||
if (![self isValidObserver:observerToken]) {
|
||||
return;
|
||||
}
|
||||
|
||||
notify_cancel(observerToken);
|
||||
}
|
||||
|
||||
+ (void)setState:(uint64_t)state forObserver:(int)observerToken
|
||||
{
|
||||
if (![self isValidObserver:observerToken]) {
|
||||
return;
|
||||
}
|
||||
|
||||
notify_set_state(observerToken, state);
|
||||
}
|
||||
|
||||
+ (uint64_t)getStateForObserver:(int)observerToken
|
||||
{
|
||||
if (![self isValidObserver:observerToken]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t state;
|
||||
notify_get_state(observerToken, &state);
|
||||
return state;
|
||||
}
|
||||
|
||||
@end
|
@ -0,0 +1,45 @@
|
||||
//
|
||||
// Copyright (c) 2021 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
@objc
|
||||
public class DarwinNotificationName: NSObject, ExpressibleByStringLiteral {
|
||||
@objc public static let sdsCrossProcess: DarwinNotificationName = "org.signal.sdscrossprocess"
|
||||
@objc public static let nseDidReceiveNotification: DarwinNotificationName = "org.signal.nseDidReceiveNotification"
|
||||
@objc public static let mainAppHandledNotification: DarwinNotificationName = "org.signal.mainAppHandledNotification"
|
||||
@objc public static let mainAppLaunched: DarwinNotificationName = "org.signal.mainAppLaunched"
|
||||
|
||||
public typealias StringLiteralType = String
|
||||
|
||||
private let stringValue: String
|
||||
|
||||
@objc
|
||||
public var cString: UnsafePointer<Int8> {
|
||||
return stringValue.withCString { $0 }
|
||||
}
|
||||
|
||||
@objc
|
||||
public var isValid: Bool {
|
||||
return stringValue.isEmpty == false
|
||||
}
|
||||
|
||||
public required init(stringLiteral value: String) {
|
||||
stringValue = value
|
||||
}
|
||||
|
||||
@objc
|
||||
public init(_ name: String) {
|
||||
stringValue = name
|
||||
}
|
||||
|
||||
public override func isEqual(_ object: Any?) -> Bool {
|
||||
guard let otherName = object as? DarwinNotificationName else { return false }
|
||||
return otherName.stringValue == stringValue
|
||||
}
|
||||
|
||||
public override var hash: Int {
|
||||
return stringValue.hashValue
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue