Chromium Code Reviews| 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 @interface RTCUIApplicationStatusObserver () | |
| 20 | |
| 21 @property(nonatomic, assign) UIApplicationState state; | |
| 22 | |
| 23 @end | |
| 24 | |
| 19 @implementation RTCUIApplicationStatusObserver { | 25 @implementation RTCUIApplicationStatusObserver { |
| 20 BOOL _initialized; | 26 BOOL _initialized; |
| 21 dispatch_block_t _initializeBlock; | 27 dispatch_block_t _initializeBlock; |
| 22 UIApplicationState _state; | 28 UIApplicationState _state; |
| 29 | |
| 30 id<NSObject> _activeObserver; | |
| 31 id<NSObject> _backgroundObserver; | |
| 23 } | 32 } |
| 24 | 33 |
| 34 @synthesize state; | |
| 35 | |
| 25 + (instancetype)sharedInstance { | 36 + (instancetype)sharedInstance { |
| 26 static id sharedInstance; | 37 static id sharedInstance; |
| 27 static dispatch_once_t onceToken; | 38 static dispatch_once_t onceToken; |
| 28 dispatch_once(&onceToken, ^{ | 39 dispatch_once(&onceToken, ^{ |
| 29 sharedInstance = [[self alloc] init]; | 40 sharedInstance = [[self alloc] init]; |
| 30 }); | 41 }); |
| 31 | 42 |
| 32 return sharedInstance; | 43 return sharedInstance; |
| 33 } | 44 } |
| 34 | 45 |
| 35 - (id)init { | 46 - (id)init { |
| 36 if (self = [super init]) { | 47 if (self = [super init]) { |
| 37 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | 48 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
| 38 [center addObserverForName:UIApplicationDidBecomeActiveNotification | 49 __weak RTCUIApplicationStatusObserver *weakSelf = self; |
| 39 object:nil | 50 _activeObserver = [center addObserverForName:UIApplicationDidBecomeActiveNot ification |
| 40 queue:[NSOperationQueue mainQueue] | 51 object:nil |
| 41 usingBlock:^(NSNotification *note) { | 52 queue:[NSOperationQueue mainQueue] |
| 42 _state = [UIApplication sharedApplication].applicationStat e; | 53 usingBlock:^(NSNotification *note) { |
| 43 }]; | 54 weakSelf.state = |
| 55 [UIApplication sharedApplication].ap plicationState; | |
| 56 }]; | |
| 44 | 57 |
| 45 [center addObserverForName:UIApplicationDidEnterBackgroundNotification | 58 _backgroundObserver = [center addObserverForName:UIApplicationDidEnterBackgr oundNotification |
| 46 object:nil | 59 object:nil |
| 47 queue:[NSOperationQueue mainQueue] | 60 queue:[NSOperationQueue mainQueue ] |
| 48 usingBlock:^(NSNotification *note) { | 61 usingBlock:^(NSNotification *note) { |
| 49 _state = [UIApplication sharedApplication].applicationStat e; | 62 weakSelf.state = |
| 50 }]; | 63 [UIApplication sharedApplication ].applicationState; |
| 64 }]; | |
| 51 | 65 |
| 52 _initialized = NO; | 66 _initialized = NO; |
| 53 _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^ { | 67 _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^ { |
| 54 _state = [UIApplication sharedApplication].applicationState; | 68 _state = [UIApplication sharedApplication].applicationState; |
|
Chuck
2017/09/12 13:25:51
This and _initialized both have an implicit refere
andersc
2017/09/12 13:43:41
Done.
| |
| 55 _initialized = YES; | 69 _initialized = YES; |
| 56 }); | 70 }); |
| 57 | 71 |
| 58 dispatch_async(dispatch_get_main_queue(), _initializeBlock); | 72 dispatch_async(dispatch_get_main_queue(), _initializeBlock); |
| 59 } | 73 } |
| 60 | 74 |
| 61 return self; | 75 return self; |
| 62 } | 76 } |
| 63 | 77 |
| 78 - (void)dealloc { | |
| 79 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | |
| 80 [center removeObserver:_activeObserver]; | |
| 81 [center removeObserver:_backgroundObserver]; | |
| 82 } | |
| 83 | |
| 64 - (BOOL)isApplicationActive { | 84 - (BOOL)isApplicationActive { |
| 65 if (!_initialized) { | 85 if (!_initialized) { |
| 66 long ret = dispatch_block_wait(_initializeBlock, | 86 long ret = dispatch_block_wait(_initializeBlock, |
| 67 dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_ PER_SEC)); | 87 dispatch_time(DISPATCH_TIME_NOW, 10.0 * NSEC_ PER_SEC)); |
| 68 RTC_DCHECK_EQ(ret, 0); | 88 RTC_DCHECK_EQ(ret, 0); |
| 69 } | 89 } |
| 70 return _state == UIApplicationStateActive; | 90 return _state == UIApplicationStateActive; |
| 71 } | 91 } |
| 72 | 92 |
| 73 @end | 93 @end |
| 74 | 94 |
| 75 #endif // WEBRTC_IOS | 95 #endif // WEBRTC_IOS |
| OLD | NEW |