-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathFileProviderValidationServiceSource.m
124 lines (96 loc) · 4.54 KB
/
FileProviderValidationServiceSource.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// FileProviderValidationServiceSource.m
//
// Copyright © 2018 Apple. All rights reserved.
//
#if DEBUG
#import "FileProviderValidationServiceSource.h"
NSFileProviderServiceName NSFileProviderValidationServiceV1Name = @"com.apple.FileProvider.ValidationV1";
typedef NSString * NSFileProviderValidationOption NS_EXTENSIBLE_STRING_ENUM;
// which container to use for validation; defaults to NSFileProviderRootContainerItemIdentifier
NSFileProviderValidationOption NSFileProviderValidationOptionAlternateRootContainer = @"AlternateRootContainer";
// whether the provider is expected to upload/download from a server; defaults to YES
NSFileProviderValidationOption NSFileProviderValidationOptionIsSyncingProvider = @"IsSyncingProvider";
@protocol NSFileProviderValidationServiceV1
// Implement this method to enable validation of the provider. You must call the completion handler with YES for your
// provider to be shown as eligible in the validation app.
- (void)validation_allowValidation:(void (^)(BOOL shouldAllow, NSDictionary <NSFileProviderValidationOption, id> * _Nullable options))completionHandler;
// Implement this method to evict the item from your local cache. The item is expected to be in state uploaded = YES,
// downloaded = NO afterwards. If you do not support non-local items, call the completion with
// [NSError errorWithDomain:NSCocoaErrorDomain code:NSFeatureUnsupportedError userInfo:nil].
- (void)validation_evictDocumentWithCompletionHandler:(void (^)(NSError * _Nullable error))completionHandler;
// Implement this method to share the item to another account. The item is expected to be in state isShared = YES afterwards
// If you do not support shared items, call the completion with
// [NSError errorWithDomain:NSCocoaErrorDomain code:NSFeatureUnsupportedError userInfo:nil].
- (void)validation_shareDocumentWithCompletionHandler:(void (^)(NSError * _Nullable error))completionHandler;
@end
@interface FileProviderValidationServiceSource () <NSXPCListenerDelegate, NSFileProviderValidationServiceV1>
{
NSFileProviderExtension *_fileProviderExtension;
NSFileProviderItemIdentifier _itemIdentifier;
NSOperationQueue *_operationQueue;
NSHashTable *_listeners;
}
@end
@implementation FileProviderValidationServiceSource
- (instancetype)initWithFileProviderExtension:(NSFileProviderExtension *)fileProviderExtension
itemIdentifier:(NSFileProviderItemIdentifier)itemIdentifier
{
if (self = [super init]) {
_itemIdentifier = itemIdentifier;
_fileProviderExtension = fileProviderExtension;
_listeners = [NSHashTable hashTableWithOptions:NSHashTableStrongMemory];
}
return self;
}
- (NSFileProviderServiceName)serviceName
{
return NSFileProviderValidationServiceV1Name;
}
- (nullable NSXPCListenerEndpoint *)makeListenerEndpointAndReturnError:(NSError **)error
{
NSXPCListenerEndpoint *endpoint;
NSXPCListener *listener = [NSXPCListener anonymousListener];
listener.delegate = self;
endpoint = listener.endpoint;
[listener resume];
@synchronized (self) {
[_listeners addObject:listener];
}
return endpoint;
}
#pragma mark NSXPCListenerDelegate
- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection
{
newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(NSFileProviderValidationServiceV1)];
newConnection.exportedObject = self;
@synchronized (self) {
[_listeners removeObject:listener];
}
__weak typeof(newConnection) wConnection = newConnection;
newConnection.invalidationHandler = ^{
//LogDebug("collaboration service source connection invalidated");
[listener invalidate];
};
newConnection.interruptionHandler = ^{
//LogDebug("collaboration service source connection interrupted");
[wConnection invalidate];
};
[newConnection resume];
return YES;
}
#pragma mark NSFileProviderValidationServiceV1
- (void)validation_evictDocumentWithCompletionHandler:(void (^)(NSError * _Nullable))completionHandler
{
completionHandler(nil);
}
- (void)validation_shareDocumentWithCompletionHandler:(void (^)(NSError * _Nullable error))completionHandler
{
completionHandler([NSError errorWithDomain:NSCocoaErrorDomain code:NSFeatureUnsupportedError userInfo:nil]);
}
- (void)validation_allowValidation:(void (^)(BOOL, NSDictionary<NSFileProviderValidationOption,id> * _Nullable))completionHandler
{
return completionHandler(YES, @{ NSFileProviderValidationOptionIsSyncingProvider : @(YES) });
}
@end
#endif