-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert FBSDKDeviceRequestsHelper.h/m to Swift
Reviewed By: samodom Differential Revision: D36334076 fbshipit-source-id: aa522b1642146a66973a9a21c727e334a295d3e8
- Loading branch information
1 parent
8ddea50
commit c82c10f
Showing
10 changed files
with
154 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 0 additions & 45 deletions
45
FBSDKLoginKit/FBSDKLoginKit/Internal/FBSDKDeviceRequestsHelper.h
This file was deleted.
Oops, something went wrong.
127 changes: 0 additions & 127 deletions
127
FBSDKLoginKit/FBSDKLoginKit/Internal/FBSDKDeviceRequestsHelper.m
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
FBSDKLoginKit/FBSDKLoginKit/Internal/_DeviceRequestsHelper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.