Skip to content

Commit

Permalink
Convert FBSDKDeviceRequestsHelper.h/m to Swift
Browse files Browse the repository at this point in the history
Reviewed By: samodom

Differential Revision: D36334076

fbshipit-source-id: aa522b1642146a66973a9a21c727e334a295d3e8
  • Loading branch information
Pablo Brizuela Guardado authored and facebook-github-bot committed May 18, 2022
1 parent 8ddea50 commit c82c10f
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 221 deletions.
15 changes: 6 additions & 9 deletions FBSDKLoginKit/FBSDKLoginKit/FBSDKDeviceLoginManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#import "FBSDKDeviceLoginManagerDelegate.h"
#import "FBSDKDevicePolling.h"
#import "FBSDKDeviceRequestsHelper.h"
#import "FBSDKLoginConstants.h"

static NSMutableArray<FBSDKDeviceLoginManager *> *g_loginManagerInstances;
Expand Down Expand Up @@ -81,7 +80,7 @@ - (void)start
NSDictionary<NSString *, id> *parameters = @{
@"scope" : [self.permissions componentsJoinedByString:@","] ?: @"",
@"redirect_uri" : self.redirectURL.absoluteString ?: @"",
FBSDK_DEVICE_INFO_PARAM : [FBSDKDeviceRequestsHelper getDeviceInfo],
@"device_info" : [FBSDKDeviceRequestsHelper getDeviceInfo],
};
id<FBSDKGraphRequest> request = [self.graphRequestFactory createGraphRequestWithGraphPath:@"device/login"
parameters:parameters
Expand Down Expand Up @@ -117,9 +116,7 @@ - (void)start
pollingInterval:interval];

if (self.isSmartLoginEnabled) {
[FBSDKDeviceRequestsHelper startAdvertisementService:self.codeInfo.loginCode
withDelegate:self
];
[FBSDKDeviceRequestsHelper startAdvertisementServiceWithLoginCode:self.codeInfo.loginCode delegate:self];
}

[self.delegate deviceLoginManager:self startedWithCodeInfo:self.codeInfo];
Expand All @@ -138,7 +135,7 @@ - (void)start

- (void)cancel
{
[FBSDKDeviceRequestsHelper cleanUpAdvertisementService:self];
[FBSDKDeviceRequestsHelper cleanUpAdvertisementServiceFor:self];
self.isCancelled = YES;
[g_loginManagerInstances removeObject:self];
}
Expand All @@ -147,7 +144,7 @@ - (void)cancel

- (void)_notifyError:(NSError *)error
{
[FBSDKDeviceRequestsHelper cleanUpAdvertisementService:self];
[FBSDKDeviceRequestsHelper cleanUpAdvertisementServiceFor:self];
[self.delegate deviceLoginManager:self
completedWithResult:nil
error:error];
Expand All @@ -156,7 +153,7 @@ - (void)_notifyError:(NSError *)error

- (void)_notifyToken:(NSString *)tokenString withExpirationDate:(NSDate *)expirationDate withDataAccessExpirationDate:(NSDate *)dataAccessExpirationDate
{
[FBSDKDeviceRequestsHelper cleanUpAdvertisementService:self];
[FBSDKDeviceRequestsHelper cleanUpAdvertisementServiceFor:self];
void (^completeWithResult)(FBSDKDeviceLoginManagerResult *) = ^(FBSDKDeviceLoginManagerResult *result) {
[self.delegate deviceLoginManager:self completedWithResult:result error:nil];
[g_loginManagerInstances removeObject:self];
Expand Down Expand Up @@ -289,7 +286,7 @@ - (void)netService:(NSNetService *)sender
{
// Only cleanup if the publish error is from our advertising service
if ([FBSDKDeviceRequestsHelper isDelegate:self forAdvertisementService:sender]) {
[FBSDKDeviceRequestsHelper cleanUpAdvertisementService:self];
[FBSDKDeviceRequestsHelper cleanUpAdvertisementServiceFor:self];
}
}

Expand Down
45 changes: 0 additions & 45 deletions FBSDKLoginKit/FBSDKLoginKit/Internal/FBSDKDeviceRequestsHelper.h

This file was deleted.

127 changes: 0 additions & 127 deletions FBSDKLoginKit/FBSDKLoginKit/Internal/FBSDKDeviceRequestsHelper.m

This file was deleted.

4 changes: 0 additions & 4 deletions FBSDKLoginKit/FBSDKLoginKit/Internal/FBSDKLoginAppEventName.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

#import <FBSDKCoreKit/FBSDKCoreKit.h>

// MARK: - Device Requests

FOUNDATION_EXPORT FBSDKAppEventName const FBSDKAppEventNameFBSDKSmartLoginService;

// MARK: - Login Manager

/// Use to log the result of the App Switch OS AlertView. Only available on OS >= iOS10
Expand Down
4 changes: 0 additions & 4 deletions FBSDKLoginKit/FBSDKLoginKit/Internal/FBSDKLoginAppEventName.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

#import "FBSDKLoginAppEventName.h"

// MARK: - Device Requests

FBSDKAppEventName const FBSDKAppEventNameFBSDKSmartLoginService = @"fb_smart_login_service";

// MARK: - Login Manager

/// Use to log the result of the App Switch OS AlertView. Only available on OS >= iOS10
Expand Down
131 changes: 131 additions & 0 deletions FBSDKLoginKit/FBSDKLoginKit/Internal/_DeviceRequestsHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

import FBSDKCoreKit
import Foundation

/**
Internal Type exposed to facilitate transition to Swift.
API Subject to change or removal without warning. Do not use.

@warning INTERNAL - DO NOT USE
*/

/// Helper class for device requests mDNS broadcasts. Note this is only intended for internal consumption.
@objcMembers
@objc(FBSDKDeviceRequestsHelper)
public final class _DeviceRequestsHelper: NSObject {
private(set) static var mdnsAdvertisementServices = NSMapTable<NetServiceDelegate, AnyObject>.weakToStrongObjects()

private enum DeviceInfoKeys {
static let deviceInfo = "device"
static let deviceModel = "model"
}

private enum NetServiceValues {

static let header = "fbsdk"

#if !os(tvOS)
static let flavor = "ios"
#else
static let flavor = "tvos"
#endif

static let sdkVersion: String = {
var sdkVersion = Settings.shared.sdkVersion.replacingOccurrences(of: ".", with: "|")

guard
sdkVersion.count > 10,
let firstCharacter = sdkVersion.first,
firstCharacter.isASCII,
firstCharacter.isNumber
else {
return "dev"
}

return sdkVersion
}()

static let netServiceType = "_fb._tcp."
}

/// Get device info to include with the GraphRequest
public static func getDeviceInfo() -> String {
var systemInfo = utsname()
uname(&systemInfo)

let data = Data(bytes: &systemInfo.machine, count: Int(_SYS_NAMELEN))

guard
let device = String(bytes: data, encoding: .ascii)?.trimmingCharacters(in: .controlCharacters)
else { return "" }

let model = UIDevice.current.model

return """
{"\(DeviceInfoKeys.deviceModel)":"\(model)","\(DeviceInfoKeys.deviceInfo)":"\(device)"}
"""
}

/**
Start the mDNS advertisement service for a device request
@param loginCode The login code associated with the action for the device request.
@return True if the service broadcast was successfully started.
*/
@discardableResult
public static func startAdvertisementService(loginCode: String, delegate: NetServiceDelegate) -> Bool {
let serviceName = """
\(NetServiceValues.header)_\(NetServiceValues.flavor)-\(NetServiceValues.sdkVersion)_\(loginCode)
"""

guard serviceName.count <= 60 else { return false }

let mdnsAdvertisementService = NetService(
domain: "local.",
type: NetServiceValues.netServiceType,
name: serviceName,
port: 0
)
mdnsAdvertisementService.delegate = delegate
mdnsAdvertisementService.publish(options: [.noAutoRename, .listenForConnections])
AppEvents.shared.logInternalEvent(.smartLoginService, parameters: [:], isImplicitlyLogged: true)
mdnsAdvertisementServices.setObject(mdnsAdvertisementService, forKey: delegate)
return true
}

/**
Check if a service delegate is registered with particular advertisement service
@param delegate The delegate to check if registered.
@param service The advertisement service to check for.
@return True if the service is the one the delegate registered with.
*/
public static func isDelegate(_ delegate: NetServiceDelegate, forAdvertisementService service: NetService) -> Bool {
guard
let mdnsAdvertisementService = mdnsAdvertisementServices.object(forKey: delegate) as? NetService
else {
return false
}
return mdnsAdvertisementService === service
}

/**
Stop the mDNS advertisement service for a device request
@param delegate The delegate registered with the service.
*/
public static func cleanUpAdvertisementService(for delegate: NetServiceDelegate) {
guard
let mdnsAdvertisementService = mdnsAdvertisementServices.object(forKey: delegate) as? NetService
else {
return
}
mdnsAdvertisementService.delegate = nil
mdnsAdvertisementService.stop()
mdnsAdvertisementServices.removeObject(forKey: delegate)
}
}
Loading

0 comments on commit c82c10f

Please sign in to comment.