Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "RTCUIApplicationStatusObserver.h" |
| 12 | |
| 13 | #if defined(WEBRTC_IOS) |
| 14 | |
| 15 | #import <UIKit/UIKit.h> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 17 | #include "rtc_base/checks.h" |
andersc | cca0006 | 2017-08-23 06:04:17 -0700 | [diff] [blame] | 18 | |
andersc | 4090f38 | 2017-09-13 02:18:36 -0700 | [diff] [blame] | 19 | @interface RTCUIApplicationStatusObserver () |
| 20 | |
| 21 | @property(nonatomic, assign) BOOL initialized; |
| 22 | @property(nonatomic, assign) UIApplicationState state; |
| 23 | |
| 24 | @end |
| 25 | |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 26 | @implementation RTCUIApplicationStatusObserver { |
andersc | cca0006 | 2017-08-23 06:04:17 -0700 | [diff] [blame] | 27 | BOOL _initialized; |
| 28 | dispatch_block_t _initializeBlock; |
andersc | 9a85f07 | 2017-09-13 07:31:46 -0700 | [diff] [blame] | 29 | dispatch_semaphore_t _waitForInitializeSemaphore; |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 30 | UIApplicationState _state; |
andersc | 4090f38 | 2017-09-13 02:18:36 -0700 | [diff] [blame] | 31 | |
| 32 | id<NSObject> _activeObserver; |
| 33 | id<NSObject> _backgroundObserver; |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 34 | } |
| 35 | |
andersc | 4090f38 | 2017-09-13 02:18:36 -0700 | [diff] [blame] | 36 | @synthesize initialized = _initialized; |
| 37 | @synthesize state = _state; |
| 38 | |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 39 | + (instancetype)sharedInstance { |
| 40 | static id sharedInstance; |
| 41 | static dispatch_once_t onceToken; |
| 42 | dispatch_once(&onceToken, ^{ |
| 43 | sharedInstance = [[self alloc] init]; |
| 44 | }); |
| 45 | |
| 46 | return sharedInstance; |
| 47 | } |
| 48 | |
andersc | 9a85f07 | 2017-09-13 07:31:46 -0700 | [diff] [blame] | 49 | // Method to make sure observers are added and the initialization block is |
| 50 | // scheduled to run on the main queue. |
| 51 | + (void)prepareForUse { |
| 52 | __unused RTCUIApplicationStatusObserver *observer = [self sharedInstance]; |
| 53 | } |
| 54 | |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 55 | - (id)init { |
| 56 | if (self = [super init]) { |
| 57 | NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
andersc | 4090f38 | 2017-09-13 02:18:36 -0700 | [diff] [blame] | 58 | __weak RTCUIApplicationStatusObserver *weakSelf = self; |
| 59 | _activeObserver = [center addObserverForName:UIApplicationDidBecomeActiveNotification |
| 60 | object:nil |
| 61 | queue:[NSOperationQueue mainQueue] |
| 62 | usingBlock:^(NSNotification *note) { |
| 63 | weakSelf.state = |
| 64 | [UIApplication sharedApplication].applicationState; |
| 65 | }]; |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 66 | |
andersc | 4090f38 | 2017-09-13 02:18:36 -0700 | [diff] [blame] | 67 | _backgroundObserver = [center addObserverForName:UIApplicationDidEnterBackgroundNotification |
| 68 | object:nil |
| 69 | queue:[NSOperationQueue mainQueue] |
| 70 | usingBlock:^(NSNotification *note) { |
| 71 | weakSelf.state = |
| 72 | [UIApplication sharedApplication].applicationState; |
| 73 | }]; |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 74 | |
andersc | 9a85f07 | 2017-09-13 07:31:46 -0700 | [diff] [blame] | 75 | _waitForInitializeSemaphore = dispatch_semaphore_create(1); |
andersc | cca0006 | 2017-08-23 06:04:17 -0700 | [diff] [blame] | 76 | _initialized = NO; |
| 77 | _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^{ |
andersc | 4090f38 | 2017-09-13 02:18:36 -0700 | [diff] [blame] | 78 | weakSelf.state = [UIApplication sharedApplication].applicationState; |
| 79 | weakSelf.initialized = YES; |
andersc | cca0006 | 2017-08-23 06:04:17 -0700 | [diff] [blame] | 80 | }); |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 81 | |
andersc | cca0006 | 2017-08-23 06:04:17 -0700 | [diff] [blame] | 82 | dispatch_async(dispatch_get_main_queue(), _initializeBlock); |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return self; |
| 86 | } |
| 87 | |
andersc | 4090f38 | 2017-09-13 02:18:36 -0700 | [diff] [blame] | 88 | - (void)dealloc { |
| 89 | NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
| 90 | [center removeObserver:_activeObserver]; |
| 91 | [center removeObserver:_backgroundObserver]; |
| 92 | } |
| 93 | |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 94 | - (BOOL)isApplicationActive { |
andersc | 9a85f07 | 2017-09-13 07:31:46 -0700 | [diff] [blame] | 95 | // NOTE: The function `dispatch_block_wait` can only legally be called once. |
| 96 | // Because of this, if several threads call the `isApplicationActive` method before |
| 97 | // the `_initializeBlock` has been executed, instead of multiple threads calling |
| 98 | // `dispatch_block_wait`, the other threads need to wait for the first waiting thread |
| 99 | // instead. |
andersc | cca0006 | 2017-08-23 06:04:17 -0700 | [diff] [blame] | 100 | if (!_initialized) { |
andersc | 9a85f07 | 2017-09-13 07:31:46 -0700 | [diff] [blame] | 101 | dispatch_semaphore_wait(_waitForInitializeSemaphore, DISPATCH_TIME_FOREVER); |
| 102 | if (!_initialized) { |
| 103 | long ret = dispatch_block_wait(_initializeBlock, |
| 104 | dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_PER_SEC)); |
| 105 | RTC_DCHECK_EQ(ret, 0); |
| 106 | } |
| 107 | dispatch_semaphore_signal(_waitForInitializeSemaphore); |
andersc | cca0006 | 2017-08-23 06:04:17 -0700 | [diff] [blame] | 108 | } |
Anders Carlsson | fc30975 | 2017-06-12 14:16:20 +0200 | [diff] [blame] | 109 | return _state == UIApplicationStateActive; |
| 110 | } |
| 111 | |
| 112 | @end |
| 113 | |
| 114 | #endif // WEBRTC_IOS |