| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/app/deferred_initialization_runner.h" | 5 #import "ios/chrome/app/deferred_initialization_runner.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #import "base/ios/weak_nsobject.h" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/mac/scoped_block.h" | 10 #include "base/mac/scoped_block.h" |
| 12 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
| 13 | 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 14 // An object encapsulating the deferred execution of a block of initialization | 17 // An object encapsulating the deferred execution of a block of initialization |
| 15 // code. | 18 // code. |
| 16 @interface DeferredInitializationBlock : NSObject { | 19 @interface DeferredInitializationBlock : NSObject { |
| 17 // A string to reference the initialization block. | 20 // A string to reference the initialization block. |
| 18 base::scoped_nsobject<NSString> _name; | 21 base::scoped_nsobject<NSString> _name; |
| 19 // A block of code to execute. | 22 // A block of code to execute. |
| 20 base::mac::ScopedBlock<ProceduralBlock> _runBlock; | 23 base::mac::ScopedBlock<ProceduralBlock> _runBlock; |
| 21 } | 24 } |
| 22 | 25 |
| 23 - (instancetype)init NS_UNAVAILABLE; | 26 - (instancetype)init NS_UNAVAILABLE; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 40 - (instancetype)init { | 43 - (instancetype)init { |
| 41 NOTREACHED(); | 44 NOTREACHED(); |
| 42 return nil; | 45 return nil; |
| 43 } | 46 } |
| 44 | 47 |
| 45 - (instancetype)initWithName:(NSString*)name block:(ProceduralBlock)block { | 48 - (instancetype)initWithName:(NSString*)name block:(ProceduralBlock)block { |
| 46 DCHECK(block); | 49 DCHECK(block); |
| 47 self = [super init]; | 50 self = [super init]; |
| 48 if (self) { | 51 if (self) { |
| 49 _name.reset([name copy]); | 52 _name.reset([name copy]); |
| 50 _runBlock.reset(block, base::scoped_policy::RETAIN); | 53 _runBlock.reset(block); |
| 51 } | 54 } |
| 52 return self; | 55 return self; |
| 53 } | 56 } |
| 54 | 57 |
| 55 - (void)run { | 58 - (void)run { |
| 56 DCHECK([NSThread isMainThread]); | 59 DCHECK([NSThread isMainThread]); |
| 57 ProceduralBlock deferredBlock = _runBlock.get(); | 60 ProceduralBlock deferredBlock = _runBlock.get(); |
| 58 if (!deferredBlock) | 61 if (!deferredBlock) |
| 59 return; | 62 return; |
| 60 deferredBlock(); | 63 deferredBlock(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 static DeferredInitializationRunner* instance = nil; | 98 static DeferredInitializationRunner* instance = nil; |
| 96 dispatch_once(&once, ^{ | 99 dispatch_once(&once, ^{ |
| 97 instance = [[DeferredInitializationRunner alloc] init]; | 100 instance = [[DeferredInitializationRunner alloc] init]; |
| 98 }); | 101 }); |
| 99 return instance; | 102 return instance; |
| 100 } | 103 } |
| 101 | 104 |
| 102 - (instancetype)init { | 105 - (instancetype)init { |
| 103 self = [super init]; | 106 self = [super init]; |
| 104 if (self) { | 107 if (self) { |
| 105 _blocksNameQueue.reset([[NSMutableArray array] retain]); | 108 _blocksNameQueue.reset([NSMutableArray array]); |
| 106 _runBlocks.reset([[NSMutableDictionary dictionary] retain]); | 109 _runBlocks.reset([NSMutableDictionary dictionary]); |
| 107 _isBlockScheduled = NO; | 110 _isBlockScheduled = NO; |
| 108 _delayBetweenBlocks = 0.2; | 111 _delayBetweenBlocks = 0.2; |
| 109 _delayBeforeFirstBlock = 3.0; | 112 _delayBeforeFirstBlock = 3.0; |
| 110 } | 113 } |
| 111 return self; | 114 return self; |
| 112 } | 115 } |
| 113 | 116 |
| 114 - (void)enqueueBlockNamed:(NSString*)name block:(ProceduralBlock)block { | 117 - (void)enqueueBlockNamed:(NSString*)name block:(ProceduralBlock)block { |
| 115 DCHECK(name); | 118 DCHECK(name); |
| 116 DCHECK([NSThread isMainThread]); | 119 DCHECK([NSThread isMainThread]); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 130 DCHECK([NSThread isMainThread]); | 133 DCHECK([NSThread isMainThread]); |
| 131 _isBlockScheduled = NO; | 134 _isBlockScheduled = NO; |
| 132 NSString* nextBlockName = [_blocksNameQueue firstObject]; | 135 NSString* nextBlockName = [_blocksNameQueue firstObject]; |
| 133 if (!nextBlockName) | 136 if (!nextBlockName) |
| 134 return; | 137 return; |
| 135 | 138 |
| 136 DeferredInitializationBlock* nextBlock = | 139 DeferredInitializationBlock* nextBlock = |
| 137 [_runBlocks objectForKey:nextBlockName]; | 140 [_runBlocks objectForKey:nextBlockName]; |
| 138 DCHECK(nextBlock); | 141 DCHECK(nextBlock); |
| 139 | 142 |
| 140 base::WeakNSObject<DeferredInitializationRunner> weakSelf(self); | 143 __weak DeferredInitializationRunner* weakSelf = self; |
| 141 | |
| 142 dispatch_after( | 144 dispatch_after( |
| 143 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), | 145 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), |
| 144 dispatch_get_main_queue(), ^{ | 146 dispatch_get_main_queue(), ^{ |
| 145 [nextBlock run]; | 147 [nextBlock run]; |
| 146 [weakSelf scheduleNextBlockWithDelay:[weakSelf delayBetweenBlocks]]; | 148 [weakSelf scheduleNextBlockWithDelay:[weakSelf delayBetweenBlocks]]; |
| 147 }); | 149 }); |
| 148 | 150 |
| 149 _isBlockScheduled = YES; | 151 _isBlockScheduled = YES; |
| 150 [_blocksNameQueue removeObjectAtIndex:0]; | 152 [_blocksNameQueue removeObjectAtIndex:0]; |
| 151 } | 153 } |
| 152 | 154 |
| 153 - (void)runBlockIfNecessary:(NSString*)name { | 155 - (void)runBlockIfNecessary:(NSString*)name { |
| 154 DCHECK([NSThread isMainThread]); | 156 DCHECK([NSThread isMainThread]); |
| 155 [[_runBlocks objectForKey:name] run]; | 157 [[_runBlocks objectForKey:name] run]; |
| 156 } | 158 } |
| 157 | 159 |
| 158 - (void)cancelBlockNamed:(NSString*)name { | 160 - (void)cancelBlockNamed:(NSString*)name { |
| 159 DCHECK([NSThread isMainThread]); | 161 DCHECK([NSThread isMainThread]); |
| 160 DCHECK(name); | 162 DCHECK(name); |
| 161 [_blocksNameQueue removeObject:name]; | 163 [_blocksNameQueue removeObject:name]; |
| 162 [[_runBlocks objectForKey:name] cancel]; | 164 [[_runBlocks objectForKey:name] cancel]; |
| 163 [_runBlocks removeObjectForKey:name]; | 165 [_runBlocks removeObjectForKey:name]; |
| 164 } | 166 } |
| 165 | 167 |
| 166 - (NSUInteger)numberOfBlocksRemaining { | 168 - (NSUInteger)numberOfBlocksRemaining { |
| 167 return [_runBlocks count]; | 169 return [_runBlocks count]; |
| 168 } | 170 } |
| 169 | 171 |
| 170 @end | 172 @end |
| OLD | NEW |