| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /*
- * Copyright 2018 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
- #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORAssert.h"
- #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORClock.h"
- #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
- #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORReachability.h"
- #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
- @implementation GDTCORUploadCoordinator
- + (instancetype)sharedInstance {
- static GDTCORUploadCoordinator *sharedUploader;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedUploader = [[GDTCORUploadCoordinator alloc] init];
- [sharedUploader startTimer];
- });
- return sharedUploader;
- }
- - (instancetype)init {
- self = [super init];
- if (self) {
- _coordinationQueue =
- dispatch_queue_create("com.google.GDTCORUploadCoordinator", DISPATCH_QUEUE_SERIAL);
- _registrar = [GDTCORRegistrar sharedInstance];
- _timerInterval = 30 * NSEC_PER_SEC;
- _timerLeeway = 5 * NSEC_PER_SEC;
- }
- return self;
- }
- - (void)forceUploadForTarget:(GDTCORTarget)target {
- dispatch_async(_coordinationQueue, ^{
- GDTCORLogDebug(@"Forcing an upload of target %ld", (long)target);
- GDTCORUploadConditions conditions = [self uploadConditions];
- conditions |= GDTCORUploadConditionHighPriority;
- [self uploadTargets:@[ @(target) ] conditions:conditions];
- });
- }
- #pragma mark - Private helper methods
- /** Starts a timer that checks whether or not events can be uploaded at regular intervals. It will
- * check the next-upload clocks of all targets to determine if an upload attempt can be made.
- */
- - (void)startTimer {
- dispatch_async(_coordinationQueue, ^{
- if (self->_timer) {
- // The timer has been already started.
- return;
- }
- self->_timer =
- dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self->_coordinationQueue);
- dispatch_source_set_timer(self->_timer, DISPATCH_TIME_NOW, self->_timerInterval,
- self->_timerLeeway);
- dispatch_source_set_event_handler(self->_timer, ^{
- if (![[GDTCORApplication sharedApplication] isRunningInBackground]) {
- GDTCORUploadConditions conditions = [self uploadConditions];
- GDTCORLogDebug(@"%@", @"Upload timer fired");
- [self uploadTargets:[self.registrar.targetToUploader allKeys] conditions:conditions];
- }
- });
- GDTCORLogDebug(@"%@", @"Upload timer started");
- dispatch_resume(self->_timer);
- });
- }
- /** Stops the currently running timer. */
- - (void)stopTimer {
- if (_timer) {
- dispatch_source_cancel(_timer);
- _timer = nil;
- }
- }
- /** Triggers the uploader implementations for the given targets to upload.
- *
- * @param targets An array of targets to trigger.
- * @param conditions The set of upload conditions.
- */
- - (void)uploadTargets:(NSArray<NSNumber *> *)targets conditions:(GDTCORUploadConditions)conditions {
- dispatch_async(_coordinationQueue, ^{
- // TODO: The reachability signal may be not reliable enough to prevent an upload attempt.
- // See https://developer.apple.com/videos/play/wwdc2019/712/ (49:40) for more details.
- if ((conditions & GDTCORUploadConditionNoNetwork) == GDTCORUploadConditionNoNetwork) {
- return;
- }
- for (NSNumber *target in targets) {
- id<GDTCORUploader> uploader = self->_registrar.targetToUploader[target];
- [uploader uploadTarget:target.intValue withConditions:conditions];
- }
- });
- }
- - (void)signalToStoragesToCheckExpirations {
- for (id<GDTCORStorageProtocol> storage in [_registrar.targetToStorage allValues]) {
- [storage checkForExpirations];
- }
- }
- /** Returns the registered storage for the given NSNumber wrapped GDTCORTarget.
- *
- * @param target The NSNumber wrapping of a GDTCORTarget to find the storage instance of.
- * @return The storage instance for the given target.
- */
- - (nullable id<GDTCORStorageProtocol>)storageForTarget:(NSNumber *)target {
- id<GDTCORStorageProtocol> storage = [GDTCORRegistrar sharedInstance].targetToStorage[target];
- GDTCORAssert(storage, @"A storage must be registered for target %@", target);
- return storage;
- }
- /** Returns the current upload conditions after making determinations about the network connection.
- *
- * @return The current upload conditions.
- */
- - (GDTCORUploadConditions)uploadConditions {
- GDTCORNetworkReachabilityFlags currentFlags = [GDTCORReachability currentFlags];
- BOOL networkConnected = GDTCORReachabilityFlagsReachable(currentFlags);
- if (!networkConnected) {
- return GDTCORUploadConditionNoNetwork;
- }
- BOOL isWWAN = GDTCORReachabilityFlagsContainWWAN(currentFlags);
- if (isWWAN) {
- return GDTCORUploadConditionMobileData;
- } else {
- return GDTCORUploadConditionWifiData;
- }
- }
- #pragma mark - GDTCORLifecycleProtocol
- - (void)appWillForeground:(GDTCORApplication *)app {
- // -startTimer is thread-safe.
- [self startTimer];
- [self signalToStoragesToCheckExpirations];
- }
- - (void)appWillBackground:(GDTCORApplication *)app {
- dispatch_async(_coordinationQueue, ^{
- [self stopTimer];
- });
- }
- - (void)appWillTerminate:(GDTCORApplication *)application {
- dispatch_sync(_coordinationQueue, ^{
- [self stopTimer];
- });
- }
- @end
|