Skip to content

Commit 96cb769

Browse files
authored
Update ShareExtensionService and NotificationSupportService usage (#24389)
* Update ShareExtensionService and NotificationSupportService usageg * Add assertions * Update to non-blocking call * Update
1 parent 3856427 commit 96cb769

File tree

10 files changed

+110
-120
lines changed

10 files changed

+110
-120
lines changed

Modules/Sources/ShareExtensionCore/ShareExtensionService.swift

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import Foundation
22
import BuildSettingsKit
33
import SFHFKeychainUtils
44

5-
@objc
6-
open class ShareExtensionService: NSObject {
5+
public final class ShareExtensionService {
76
private let appGroupName: String
87
private let appKeychainAccessGroup: String
98
private let configuration: ShareExtensionConfiguration
109

11-
@objc public convenience override init() {
10+
public convenience init() {
1211
let settings = BuildSettings.current
1312
self.init(
1413
appGroupName: settings.appGroupName,
@@ -31,7 +30,7 @@ open class ShareExtensionService: NSObject {
3130
///
3231
/// - Parameter oauth2Token: WordPress.com OAuth Token
3332
///
34-
@objc public func configureShareExtensionToken(_ oauth2Token: String) {
33+
public func storeToken(_ oauth2Token: String) {
3534
do {
3635
try SFHFKeychainUtils.storeUsername(
3736
configuration.keychainTokenKey,
@@ -49,7 +48,7 @@ open class ShareExtensionService: NSObject {
4948
///
5049
/// - Parameter oauth2Token: WordPress.com OAuth Token
5150
///
52-
@objc public func configureShareExtensionUsername(_ username: String) {
51+
public func storeUsername(_ username: String) {
5352
do {
5453
try SFHFKeychainUtils.storeUsername(
5554
configuration.keychainUsernameKey,
@@ -70,7 +69,7 @@ open class ShareExtensionService: NSObject {
7069
/// - defaultSiteID: The ID of the Primary Site.
7170
/// - defaultSiteName: The Primary Site's Name
7271
///
73-
@objc public func configureShareExtensionDefaultSiteID(_ defaultSiteID: Int, defaultSiteName: String) {
72+
public func storeDefaultSiteID(_ defaultSiteID: Int, defaultSiteName: String) {
7473
guard let userDefaults = UserDefaults(suiteName: appGroupName) else {
7574
return
7675
}
@@ -85,7 +84,7 @@ open class ShareExtensionService: NSObject {
8584
/// - lastUsedSiteID: The ID of the Last Used Site.
8685
/// - lastUsedSiteName: The Last Used Site's Name
8786
///
88-
@objc public func configureShareExtensionLastUsedSiteID(_ lastUsedSiteID: Int, lastUsedSiteName: String) {
87+
public func storeLastUsedSiteID(_ lastUsedSiteID: Int, lastUsedSiteName: String) {
8988
guard let userDefaults = UserDefaults(suiteName: appGroupName) else {
9089
return
9190
}
@@ -98,7 +97,7 @@ open class ShareExtensionService: NSObject {
9897
///
9998
/// - Parameter maximumMediaSize: The maximum size a media attachment might occupy.
10099
///
101-
@objc public func configureShareExtensionMaximumMediaDimension(_ maximumMediaDimension: Int) {
100+
public func storeMaximumMediaDimension(_ maximumMediaDimension: Int) {
102101
guard let userDefaults = UserDefaults(suiteName: appGroupName) else {
103102
return
104103
}
@@ -110,7 +109,7 @@ open class ShareExtensionService: NSObject {
110109
///
111110
/// - Parameter recentSites: An array of URL's representing the recently used sites.
112111
///
113-
@objc public func configureShareExtensionRecentSites(_ recentSites: [String]) {
112+
public func storeRecentSites(_ recentSites: [String]) {
114113
guard let userDefaults = UserDefaults(suiteName: appGroupName) else {
115114
return
116115
}
@@ -120,7 +119,7 @@ open class ShareExtensionService: NSObject {
120119

121120
/// Nukes all of the Share Extension Configuration
122121
///
123-
@objc public func removeShareExtensionConfiguration() {
122+
public func removeShareExtensionConfiguration() {
124123
do {
125124
try SFHFKeychainUtils.deleteItem(
126125
forUsername: configuration.keychainTokenKey,
@@ -153,7 +152,7 @@ open class ShareExtensionService: NSObject {
153152

154153
/// Retrieves the WordPress.com OAuth Token, meant for Extension usage.
155154
///
156-
@objc public func retrieveShareExtensionToken() -> String? {
155+
public func retrieveShareExtensionToken() -> String? {
157156
guard let oauth2Token = try? SFHFKeychainUtils.getPasswordForUsername(
158157
configuration.keychainTokenKey,
159158
andServiceName: configuration.keychainServiceName,
@@ -167,7 +166,7 @@ open class ShareExtensionService: NSObject {
167166

168167
/// Retrieves the WordPress.com Username, meant for Extension usage.
169168
///
170-
@objc public func retrieveShareExtensionUsername() -> String? {
169+
public func retrieveShareExtensionUsername() -> String? {
171170
guard let oauth2Token = try? SFHFKeychainUtils.getPasswordForUsername(
172171
configuration.keychainUsernameKey,
173172
andServiceName: configuration.keychainServiceName,

Sources/WordPressAuthenticator/Helpers/UnifiedAuth/ViewRelated/SiteAddress/SiteCredentialsViewController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private extension SiteCredentialsViewController {
278278
case let cell as TextLabelTableViewCell where row == .instructions:
279279
configureInstructionLabel(cell)
280280
case let cell as TextFieldTableViewCell where row == .username:
281-
configureUsernameTextField(cell)
281+
storeUsernameTextField(cell)
282282
case let cell as TextFieldTableViewCell where row == .password:
283283
configurePasswordTextField(cell)
284284
case let cell as TextLinkButtonTableViewCell:
@@ -300,7 +300,7 @@ private extension SiteCredentialsViewController {
300300

301301
/// Configure the username textfield cell.
302302
///
303-
func configureUsernameTextField(_ cell: TextFieldTableViewCell) {
303+
func storeUsernameTextField(_ cell: TextFieldTableViewCell) {
304304
cell.configure(withStyle: .username,
305305
placeholder: WordPressAuthenticator.shared.displayStrings.usernamePlaceholder,
306306
text: loginFields.username)

WordPress/Classes/Services/AccountService+Cookies.swift

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import Foundation
2+
import WordPressShared
3+
import ShareExtensionCore
4+
5+
extension AccountService {
6+
func setupAppExtensions() {
7+
let context = coreDataStack.mainContext
8+
context.performAndWait {
9+
guard let account = try? WPAccount.lookupDefaultWordPressComAccount(in: context) else {
10+
return
11+
}
12+
self.setupAppExtensions(defaultAccount: account)
13+
}
14+
}
15+
16+
@objc func setupAppExtensions(defaultAccount: WPAccount) {
17+
let shareExtensionService = ShareExtensionService()
18+
let notificationSupportService = NotificationSupportService()
19+
20+
guard let defaultBlog = defaultAccount.defaultBlog, !defaultBlog.isDeleted else {
21+
DispatchQueue.main.async {
22+
shareExtensionService.removeShareExtensionConfiguration()
23+
notificationSupportService.deleteServiceExtensionToken()
24+
}
25+
return
26+
}
27+
28+
let defaultAccountObjectID = TaggedManagedObjectID(defaultAccount)
29+
let siteID = defaultBlog.dotComID?.intValue
30+
let siteName = defaultBlog.settings?.name
31+
32+
DispatchQueue.main.async {
33+
do {
34+
let defaultAccount = try self.coreDataStack.mainContext.existingObject(with: defaultAccountObjectID)
35+
36+
if let siteID, let siteName {
37+
shareExtensionService.storeDefaultSiteID(siteID, defaultSiteName: siteName)
38+
} else {
39+
wpAssertionFailure("siteID and/or siteName missing")
40+
}
41+
42+
if let authToken = defaultAccount.authToken {
43+
shareExtensionService.storeToken(authToken)
44+
notificationSupportService.storeToken(authToken)
45+
} else {
46+
wpAssertionFailure("authToken missing")
47+
}
48+
49+
shareExtensionService.storeUsername(defaultAccount.username)
50+
notificationSupportService.storeUsername(defaultAccount.username)
51+
52+
if let userID = defaultAccount.userID?.stringValue {
53+
notificationSupportService.storeUserID(userID)
54+
} else {
55+
wpAssertionFailure("userID missing")
56+
}
57+
} catch {
58+
wpAssertionFailure("failed to fetch the default account")
59+
}
60+
}
61+
}
62+
63+
/// Loads the default WordPress account's cookies into shared cookie storage.
64+
///
65+
static func loadDefaultAccountCookies() {
66+
guard
67+
let account = try? WPAccount.lookupDefaultWordPressComAccount(in: ContextManager.shared.mainContext),
68+
let auth = RequestAuthenticator(account: account),
69+
let url = URL(string: WPComDomain)
70+
else {
71+
return
72+
}
73+
auth.request(url: url, cookieJar: HTTPCookieStorage.shared) { _ in
74+
// no op
75+
}
76+
}
77+
78+
}

WordPress/Classes/Services/AccountService.h

-6
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,6 @@ extern NSNotificationName const WPAccountEmailAndDefaultBlogUpdatedNotification;
117117

118118
- (NSManagedObjectID *)createOrUpdateAccountWithUserDetails:(RemoteUser *)remoteUser authToken:(NSString *)authToken;
119119

120-
/**
121-
Initializes the WordPress iOS Extensions with the WordPress.com Default Account.
122-
*/
123-
- (void)setupAppExtensionsWithDefaultAccount;
124-
125-
126120
/**
127121
Removes an account if it's not the default account and there are no associated blogs
128122
*/

WordPress/Classes/Services/AccountService.m

+3-48
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#import "AccountService.h"
22
#import "WPAccount.h"
3-
@import WordPressDataObjC;
43
#import "Blog.h"
54
#import "BlogService.h"
65

6+
@import WordPressDataObjC;
77
@import WordPressKit;
88
@import WordPressShared;
9-
@import ShareExtensionCore;
9+
1010
#ifdef KEYSTONE
1111
#import "Keystone-Swift.h"
1212
#else
@@ -393,52 +393,7 @@ - (void)updateDefaultBlogIfNeeded:(WPAccount *)account inContext:(NSManagedObjec
393393

394394
// Update app extensions if needed.
395395
if ([account isDefaultWordPressComAccount]) {
396-
[self setupAppExtensionsWithDefaultAccount:account inContext:context];
397-
}
398-
}
399-
400-
- (void)setupAppExtensionsWithDefaultAccount
401-
{
402-
NSManagedObjectContext *context = self.coreDataStack.mainContext;
403-
[context performBlockAndWait:^{
404-
WPAccount *account = [WPAccount lookupDefaultWordPressComAccountInContext:context];
405-
if (account == nil) {
406-
return;
407-
}
408-
[self setupAppExtensionsWithDefaultAccount:account inContext:context];
409-
}];
410-
}
411-
412-
- (void)setupAppExtensionsWithDefaultAccount:(WPAccount *)defaultAccount inContext:(NSManagedObjectContext *)context
413-
{
414-
NSParameterAssert(defaultAccount.managedObjectContext == context);
415-
416-
NSManagedObjectID *defaultAccountObjectID = defaultAccount.objectID;
417-
Blog *defaultBlog = [defaultAccount defaultBlog];
418-
NSNumber *siteId = defaultBlog.dotComID;
419-
NSString *blogName = defaultBlog.settings.name;
420-
421-
ShareExtensionService *shareExtensionService = [ShareExtensionService new];
422-
NotificationSupportService *notificationSupportService = [NotificationSupportService new];
423-
424-
if (defaultBlog == nil || defaultBlog.isDeleted) {
425-
dispatch_async(dispatch_get_main_queue(), ^{
426-
[shareExtensionService removeShareExtensionConfiguration];
427-
428-
[notificationSupportService deleteServiceExtensionToken];
429-
});
430-
} else {
431-
dispatch_async(dispatch_get_main_queue(), ^{
432-
WPAccount *defaultAccount = [self.coreDataStack.mainContext existingObjectWithID:defaultAccountObjectID error:nil];
433-
434-
[shareExtensionService configureShareExtensionDefaultSiteID:siteId.integerValue defaultSiteName:blogName];
435-
[shareExtensionService configureShareExtensionToken:defaultAccount.authToken];
436-
[shareExtensionService configureShareExtensionUsername:defaultAccount.username];
437-
438-
[notificationSupportService insertServiceExtensionToken:defaultAccount.authToken];
439-
[notificationSupportService insertServiceExtensionUsername:defaultAccount.username];
440-
[notificationSupportService insertServiceExtensionUserID:defaultAccount.userID.stringValue];
441-
});
396+
[self setupAppExtensionsWithDefaultAccount:account];
442397
}
443398
}
444399

WordPress/Classes/Services/NotificationSupportService.swift

+8-15
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import BuildSettingsKit
33
import SFHFKeychainUtils
44
import NotificationServiceExtensionCore
55

6-
@objc
7-
open class NotificationSupportService: NSObject {
6+
final class NotificationSupportService {
87
private let appKeychainAccessGroup: String
98
private let configuration: NotificationServiceExtensionConfiguration
109

11-
@objc convenience override init() {
10+
convenience init() {
1211
let settings = BuildSettings.current
1312
self.init(
1413
appKeychainAccessGroup: settings.appKeychainAccessGroup,
@@ -24,14 +23,13 @@ open class NotificationSupportService: NSObject {
2423

2524
/// Sets the OAuth Token that should be used by the Notification Service Extension to access WPCOM.
2625
///
27-
/// - Parameter oauth2Token: WordPress.com OAuth Token
26+
/// - Parameter authToken: WordPress.com OAuth Token
2827
///
29-
@objc
30-
func insertServiceExtensionToken(_ oauthToken: String) {
28+
func storeToken(_ authToken: String) {
3129
do {
3230
try SFHFKeychainUtils.storeUsername(
3331
configuration.keychainTokenKey,
34-
andPassword: oauthToken,
32+
andPassword: authToken,
3533
forServiceName: configuration.keychainServiceName,
3634
accessGroup: appKeychainAccessGroup,
3735
updateExisting: true
@@ -45,8 +43,7 @@ open class NotificationSupportService: NSObject {
4543
///
4644
/// - Parameter username: WordPress.com username
4745
///
48-
@objc
49-
func insertServiceExtensionUsername(_ username: String) {
46+
func storeUsername(_ username: String) {
5047
do {
5148
try SFHFKeychainUtils.storeUsername(
5249
configuration.keychainUsernameKey,
@@ -64,8 +61,7 @@ open class NotificationSupportService: NSObject {
6461
///
6562
/// - Parameter userID: WordPress.com userID
6663
///
67-
@objc
68-
func insertServiceExtensionUserID(_ userID: String) {
64+
func storeUserID(_ userID: String) {
6965
do {
7066
try SFHFKeychainUtils.storeUsername(
7167
configuration.keychainUserIDKey,
@@ -81,7 +77,6 @@ open class NotificationSupportService: NSObject {
8177

8278
/// Attempts to delete the current WPCOM OAuth Token used by the Notification Service Extension.
8379
///
84-
@objc
8580
func deleteServiceExtensionToken() {
8681
do {
8782
try SFHFKeychainUtils.deleteItem(
@@ -96,7 +91,6 @@ open class NotificationSupportService: NSObject {
9691

9792
/// Attempts to delete the current WPCOM Username used by the Notification Service Extension.
9893
///
99-
@objc
10094
func deleteServiceExtensionUsername() {
10195
do {
10296
try SFHFKeychainUtils.deleteItem(
@@ -111,8 +105,7 @@ open class NotificationSupportService: NSObject {
111105

112106
/// Attempts to delete the current WPCOM Username used by the Notification Service Extension.
113107
///
114-
@objc
115-
public func deleteServiceExtensionUserID() {
108+
func deleteServiceExtensionUserID() {
116109
do {
117110
try SFHFKeychainUtils.deleteItem(
118111
forUsername: configuration.keychainUserIDKey,

0 commit comments

Comments
 (0)