OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "RTCUIApplicationStatusObserver.h" | 11 #include "RTCUIApplicationStatusObserver.h" |
12 | 12 |
13 #if defined(WEBRTC_IOS) | 13 #if defined(WEBRTC_IOS) |
14 | 14 |
15 #import <UIKit/UIKit.h> | 15 #import <UIKit/UIKit.h> |
16 | 16 |
17 #include "webrtc/rtc_base/checks.h" | 17 #include "webrtc/rtc_base/checks.h" |
18 | 18 |
19 @implementation RTCUIApplicationStatusObserver { | 19 @implementation RTCUIApplicationStatusObserver { |
20 BOOL _initialized; | 20 BOOL _initialized; |
21 dispatch_block_t _initializeBlock; | 21 dispatch_block_t _initializeBlock; |
22 dispatch_semaphore_t _waitForInitializeSemaphore; | |
22 UIApplicationState _state; | 23 UIApplicationState _state; |
23 } | 24 } |
24 | 25 |
25 + (instancetype)sharedInstance { | 26 + (instancetype)sharedInstance { |
26 static id sharedInstance; | 27 static id sharedInstance; |
27 static dispatch_once_t onceToken; | 28 static dispatch_once_t onceToken; |
28 dispatch_once(&onceToken, ^{ | 29 dispatch_once(&onceToken, ^{ |
29 sharedInstance = [[self alloc] init]; | 30 sharedInstance = [[self alloc] init]; |
30 }); | 31 }); |
31 | 32 |
(...skipping 10 matching lines...) Expand all Loading... | |
42 _state = [UIApplication sharedApplication].applicationStat e; | 43 _state = [UIApplication sharedApplication].applicationStat e; |
43 }]; | 44 }]; |
44 | 45 |
45 [center addObserverForName:UIApplicationDidEnterBackgroundNotification | 46 [center addObserverForName:UIApplicationDidEnterBackgroundNotification |
46 object:nil | 47 object:nil |
47 queue:[NSOperationQueue mainQueue] | 48 queue:[NSOperationQueue mainQueue] |
48 usingBlock:^(NSNotification *note) { | 49 usingBlock:^(NSNotification *note) { |
49 _state = [UIApplication sharedApplication].applicationStat e; | 50 _state = [UIApplication sharedApplication].applicationStat e; |
50 }]; | 51 }]; |
51 | 52 |
53 _waitForInitializeSemaphore = dispatch_semaphore_create(1); | |
52 _initialized = NO; | 54 _initialized = NO; |
53 _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^ { | 55 _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^ { |
54 _state = [UIApplication sharedApplication].applicationState; | 56 _state = [UIApplication sharedApplication].applicationState; |
55 _initialized = YES; | 57 _initialized = YES; |
56 }); | 58 }); |
57 | 59 |
58 dispatch_async(dispatch_get_main_queue(), _initializeBlock); | 60 dispatch_async(dispatch_get_main_queue(), _initializeBlock); |
59 } | 61 } |
60 | 62 |
61 return self; | 63 return self; |
62 } | 64 } |
63 | 65 |
64 - (BOOL)isApplicationActive { | 66 - (BOOL)isApplicationActive { |
65 if (!_initialized) { | 67 if (!_initialized) { |
66 long ret = dispatch_block_wait(_initializeBlock, | 68 dispatch_semaphore_wait(_waitForInitializeSemaphore, DISPATCH_TIME_FOREVER); |
daniela-webrtc
2017/09/13 09:02:50
This code is becoming slightly complex now. Would
andersc
2017/09/13 09:19:51
Acknowledged.
| |
67 dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_ PER_SEC)); | 69 if (!_initialized) { |
daniela-webrtc
2017/09/13 09:02:50
I guess we want to synchronize only if the observa
andersc
2017/09/13 09:19:51
Not sure what you mean. Since the `dispatch_block_
daniela-webrtc
2017/09/13 09:30:49
Acknowledged.
daniela-webrtc
2017/09/13 09:30:49
Missed that there were two if statements. The comm
| |
68 RTC_DCHECK_EQ(ret, 0); | 70 long ret = dispatch_block_wait(_initializeBlock, |
71 dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSE C_PER_SEC)); | |
72 RTC_DCHECK_EQ(ret, 0); | |
73 } | |
74 dispatch_semaphore_signal(_waitForInitializeSemaphore); | |
69 } | 75 } |
70 return _state == UIApplicationStateActive; | 76 return _state == UIApplicationStateActive; |
71 } | 77 } |
72 | 78 |
73 @end | 79 @end |
74 | 80 |
75 #endif // WEBRTC_IOS | 81 #endif // WEBRTC_IOS |
OLD | NEW |