1 |
efrain |
1 |
/*
|
|
|
2 |
* Copyright 2018 Google
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORRegistrar.h"
|
|
|
18 |
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
|
|
|
19 |
|
|
|
20 |
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
|
|
|
21 |
|
|
|
22 |
id<GDTCORStorageProtocol> _Nullable GDTCORStorageInstanceForTarget(GDTCORTarget target) {
|
|
|
23 |
return [GDTCORRegistrar sharedInstance].targetToStorage[@(target)];
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
FOUNDATION_EXPORT
|
|
|
27 |
id<GDTCORStoragePromiseProtocol> _Nullable GDTCORStoragePromiseInstanceForTarget(
|
|
|
28 |
GDTCORTarget target) {
|
|
|
29 |
id storage = [GDTCORRegistrar sharedInstance].targetToStorage[@(target)];
|
|
|
30 |
if ([storage conformsToProtocol:@protocol(GDTCORStoragePromiseProtocol)]) {
|
|
|
31 |
return storage;
|
|
|
32 |
} else {
|
|
|
33 |
return nil;
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
@implementation GDTCORRegistrar {
|
|
|
38 |
/** Backing ivar for targetToUploader property. */
|
|
|
39 |
NSMutableDictionary<NSNumber *, id<GDTCORUploader>> *_targetToUploader;
|
|
|
40 |
|
|
|
41 |
/** Backing ivar for targetToStorage property. */
|
|
|
42 |
NSMutableDictionary<NSNumber *, id<GDTCORStorageProtocol>> *_targetToStorage;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
+ (instancetype)sharedInstance {
|
|
|
46 |
static GDTCORRegistrar *sharedInstance;
|
|
|
47 |
static dispatch_once_t onceToken;
|
|
|
48 |
dispatch_once(&onceToken, ^{
|
|
|
49 |
sharedInstance = [[GDTCORRegistrar alloc] init];
|
|
|
50 |
});
|
|
|
51 |
return sharedInstance;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
- (instancetype)init {
|
|
|
55 |
self = [super init];
|
|
|
56 |
if (self) {
|
|
|
57 |
_registrarQueue = dispatch_queue_create("com.google.GDTCORRegistrar", DISPATCH_QUEUE_SERIAL);
|
|
|
58 |
_targetToUploader = [[NSMutableDictionary alloc] init];
|
|
|
59 |
_targetToStorage = [[NSMutableDictionary alloc] init];
|
|
|
60 |
}
|
|
|
61 |
return self;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
- (void)registerUploader:(id<GDTCORUploader>)backend target:(GDTCORTarget)target {
|
|
|
65 |
__weak GDTCORRegistrar *weakSelf = self;
|
|
|
66 |
dispatch_async(_registrarQueue, ^{
|
|
|
67 |
GDTCORRegistrar *strongSelf = weakSelf;
|
|
|
68 |
if (strongSelf) {
|
|
|
69 |
GDTCORLogDebug(@"Registered an uploader: %@ for target:%ld", backend, (long)target);
|
|
|
70 |
strongSelf->_targetToUploader[@(target)] = backend;
|
|
|
71 |
}
|
|
|
72 |
});
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
- (void)registerStorage:(id<GDTCORStorageProtocol>)storage target:(GDTCORTarget)target {
|
|
|
76 |
__weak GDTCORRegistrar *weakSelf = self;
|
|
|
77 |
dispatch_async(_registrarQueue, ^{
|
|
|
78 |
GDTCORRegistrar *strongSelf = weakSelf;
|
|
|
79 |
if (strongSelf) {
|
|
|
80 |
GDTCORLogDebug(@"Registered storage: %@ for target:%ld", storage, (long)target);
|
|
|
81 |
strongSelf->_targetToStorage[@(target)] = storage;
|
|
|
82 |
}
|
|
|
83 |
});
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
- (NSMutableDictionary<NSNumber *, id<GDTCORUploader>> *)targetToUploader {
|
|
|
87 |
__block NSMutableDictionary<NSNumber *, id<GDTCORUploader>> *targetToUploader;
|
|
|
88 |
__weak GDTCORRegistrar *weakSelf = self;
|
|
|
89 |
dispatch_sync(_registrarQueue, ^{
|
|
|
90 |
GDTCORRegistrar *strongSelf = weakSelf;
|
|
|
91 |
if (strongSelf) {
|
|
|
92 |
targetToUploader = strongSelf->_targetToUploader;
|
|
|
93 |
}
|
|
|
94 |
});
|
|
|
95 |
return targetToUploader;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
- (NSMutableDictionary<NSNumber *, id<GDTCORStorageProtocol>> *)targetToStorage {
|
|
|
99 |
__block NSMutableDictionary<NSNumber *, id<GDTCORStorageProtocol>> *targetToStorage;
|
|
|
100 |
__weak GDTCORRegistrar *weakSelf = self;
|
|
|
101 |
dispatch_sync(_registrarQueue, ^{
|
|
|
102 |
GDTCORRegistrar *strongSelf = weakSelf;
|
|
|
103 |
if (strongSelf) {
|
|
|
104 |
targetToStorage = strongSelf->_targetToStorage;
|
|
|
105 |
}
|
|
|
106 |
});
|
|
|
107 |
return targetToStorage;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
#pragma mark - GDTCORLifecycleProtocol
|
|
|
111 |
|
|
|
112 |
- (void)appWillBackground:(nonnull GDTCORApplication *)app {
|
|
|
113 |
NSArray<id<GDTCORUploader>> *uploaders = [self.targetToUploader allValues];
|
|
|
114 |
for (id<GDTCORUploader> uploader in uploaders) {
|
|
|
115 |
if ([uploader respondsToSelector:@selector(appWillBackground:)]) {
|
|
|
116 |
[uploader appWillBackground:app];
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
NSArray<id<GDTCORStorageProtocol>> *storages = [self.targetToStorage allValues];
|
|
|
120 |
for (id<GDTCORStorageProtocol> storage in storages) {
|
|
|
121 |
if ([storage respondsToSelector:@selector(appWillBackground:)]) {
|
|
|
122 |
[storage appWillBackground:app];
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
- (void)appWillForeground:(nonnull GDTCORApplication *)app {
|
|
|
128 |
NSArray<id<GDTCORUploader>> *uploaders = [self.targetToUploader allValues];
|
|
|
129 |
for (id<GDTCORUploader> uploader in uploaders) {
|
|
|
130 |
if ([uploader respondsToSelector:@selector(appWillForeground:)]) {
|
|
|
131 |
[uploader appWillForeground:app];
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
NSArray<id<GDTCORStorageProtocol>> *storages = [self.targetToStorage allValues];
|
|
|
135 |
for (id<GDTCORStorageProtocol> storage in storages) {
|
|
|
136 |
if ([storage respondsToSelector:@selector(appWillForeground:)]) {
|
|
|
137 |
[storage appWillForeground:app];
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
- (void)appWillTerminate:(nonnull GDTCORApplication *)app {
|
|
|
143 |
NSArray<id<GDTCORUploader>> *uploaders = [self.targetToUploader allValues];
|
|
|
144 |
for (id<GDTCORUploader> uploader in uploaders) {
|
|
|
145 |
if ([uploader respondsToSelector:@selector(appWillTerminate:)]) {
|
|
|
146 |
[uploader appWillTerminate:app];
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
NSArray<id<GDTCORStorageProtocol>> *storages = [self.targetToStorage allValues];
|
|
|
150 |
for (id<GDTCORStorageProtocol> storage in storages) {
|
|
|
151 |
if ([storage respondsToSelector:@selector(appWillTerminate:)]) {
|
|
|
152 |
[storage appWillTerminate:app];
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
@end
|