Files
platform-external-webrtc/sdk/objc/helpers/RTCUIApplicationStatusObserver.m
Anders Carlsson 7bca8ca4e2 Obj-C SDK Cleanup
This CL separates the files under sdk/objc into logical directories, replacing
the previous file layout under Framework/.

A long term goal is to have some system set up to generate the files under
sdk/objc/api (the PeerConnection API wrappers) from the C++ code. In the shorter
term the goal is to abstract out shared concepts from these classes in order to
make them as uniform as possible.

The separation into base/, components/, and helpers/ are to differentiate between
the base layer's common protocols, various utilities and the actual platform
specific components.

The old directory layout that resembled a framework's internal layout is not
necessary, since it is generated by the framework target when building it.

Bug: webrtc:9627
Change-Id: Ib084fd83f050ae980649ca99e841f4fb0580bd8f
Reviewed-on: https://webrtc-review.googlesource.com/94142
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Commit-Queue: Anders Carlsson <andersc@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24493}
2018-08-30 10:42:41 +00:00

115 lines
3.9 KiB
Objective-C

/*
* Copyright 2016 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "RTCUIApplicationStatusObserver.h"
#if defined(WEBRTC_IOS)
#import <UIKit/UIKit.h>
#include "rtc_base/checks.h"
@interface RTCUIApplicationStatusObserver ()
@property(nonatomic, assign) BOOL initialized;
@property(nonatomic, assign) UIApplicationState state;
@end
@implementation RTCUIApplicationStatusObserver {
BOOL _initialized;
dispatch_block_t _initializeBlock;
dispatch_semaphore_t _waitForInitializeSemaphore;
UIApplicationState _state;
id<NSObject> _activeObserver;
id<NSObject> _backgroundObserver;
}
@synthesize initialized = _initialized;
@synthesize state = _state;
+ (instancetype)sharedInstance {
static id sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
// Method to make sure observers are added and the initialization block is
// scheduled to run on the main queue.
+ (void)prepareForUse {
__unused RTCUIApplicationStatusObserver *observer = [self sharedInstance];
}
- (id)init {
if (self = [super init]) {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
__weak RTCUIApplicationStatusObserver *weakSelf = self;
_activeObserver = [center addObserverForName:UIApplicationDidBecomeActiveNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
weakSelf.state =
[UIApplication sharedApplication].applicationState;
}];
_backgroundObserver = [center addObserverForName:UIApplicationDidEnterBackgroundNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
weakSelf.state =
[UIApplication sharedApplication].applicationState;
}];
_waitForInitializeSemaphore = dispatch_semaphore_create(1);
_initialized = NO;
_initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^{
weakSelf.state = [UIApplication sharedApplication].applicationState;
weakSelf.initialized = YES;
});
dispatch_async(dispatch_get_main_queue(), _initializeBlock);
}
return self;
}
- (void)dealloc {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:_activeObserver];
[center removeObserver:_backgroundObserver];
}
- (BOOL)isApplicationActive {
// NOTE: The function `dispatch_block_wait` can only legally be called once.
// Because of this, if several threads call the `isApplicationActive` method before
// the `_initializeBlock` has been executed, instead of multiple threads calling
// `dispatch_block_wait`, the other threads need to wait for the first waiting thread
// instead.
if (!_initialized) {
dispatch_semaphore_wait(_waitForInitializeSemaphore, DISPATCH_TIME_FOREVER);
if (!_initialized) {
long ret = dispatch_block_wait(_initializeBlock,
dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_PER_SEC));
RTC_DCHECK_EQ(ret, 0);
}
dispatch_semaphore_signal(_waitForInitializeSemaphore);
}
return _state == UIApplicationStateActive;
}
@end
#endif // WEBRTC_IOS