Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(794)

Side by Side Diff: ios/chrome/app/deferred_initialization_runner.mm

Issue 2453033002: Revert of [ObjC ARC] Converts ios/chrome/app to ARC. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
9 #include "base/logging.h" 10 #include "base/logging.h"
10 #include "base/mac/scoped_block.h" 11 #include "base/mac/scoped_block.h"
11 #include "base/mac/scoped_nsobject.h" 12 #include "base/mac/scoped_nsobject.h"
12 13
13 #if !defined(__has_feature) || !__has_feature(objc_arc)
14 #error "This file requires ARC support."
15 #endif
16
17 // An object encapsulating the deferred execution of a block of initialization 14 // An object encapsulating the deferred execution of a block of initialization
18 // code. 15 // code.
19 @interface DeferredInitializationBlock : NSObject { 16 @interface DeferredInitializationBlock : NSObject {
20 // A string to reference the initialization block. 17 // A string to reference the initialization block.
21 base::scoped_nsobject<NSString> _name; 18 base::scoped_nsobject<NSString> _name;
22 // A block of code to execute. 19 // A block of code to execute.
23 base::mac::ScopedBlock<ProceduralBlock> _runBlock; 20 base::mac::ScopedBlock<ProceduralBlock> _runBlock;
24 } 21 }
25 22
26 - (instancetype)init NS_UNAVAILABLE; 23 - (instancetype)init NS_UNAVAILABLE;
(...skipping 16 matching lines...) Expand all
43 - (instancetype)init { 40 - (instancetype)init {
44 NOTREACHED(); 41 NOTREACHED();
45 return nil; 42 return nil;
46 } 43 }
47 44
48 - (instancetype)initWithName:(NSString*)name block:(ProceduralBlock)block { 45 - (instancetype)initWithName:(NSString*)name block:(ProceduralBlock)block {
49 DCHECK(block); 46 DCHECK(block);
50 self = [super init]; 47 self = [super init];
51 if (self) { 48 if (self) {
52 _name.reset([name copy]); 49 _name.reset([name copy]);
53 _runBlock.reset(block); 50 _runBlock.reset(block, base::scoped_policy::RETAIN);
54 } 51 }
55 return self; 52 return self;
56 } 53 }
57 54
58 - (void)run { 55 - (void)run {
59 DCHECK([NSThread isMainThread]); 56 DCHECK([NSThread isMainThread]);
60 ProceduralBlock deferredBlock = _runBlock.get(); 57 ProceduralBlock deferredBlock = _runBlock.get();
61 if (!deferredBlock) 58 if (!deferredBlock)
62 return; 59 return;
63 deferredBlock(); 60 deferredBlock();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 static DeferredInitializationRunner* instance = nil; 95 static DeferredInitializationRunner* instance = nil;
99 dispatch_once(&once, ^{ 96 dispatch_once(&once, ^{
100 instance = [[DeferredInitializationRunner alloc] init]; 97 instance = [[DeferredInitializationRunner alloc] init];
101 }); 98 });
102 return instance; 99 return instance;
103 } 100 }
104 101
105 - (instancetype)init { 102 - (instancetype)init {
106 self = [super init]; 103 self = [super init];
107 if (self) { 104 if (self) {
108 _blocksNameQueue.reset([NSMutableArray array]); 105 _blocksNameQueue.reset([[NSMutableArray array] retain]);
109 _runBlocks.reset([NSMutableDictionary dictionary]); 106 _runBlocks.reset([[NSMutableDictionary dictionary] retain]);
110 _isBlockScheduled = NO; 107 _isBlockScheduled = NO;
111 _delayBetweenBlocks = 0.2; 108 _delayBetweenBlocks = 0.2;
112 _delayBeforeFirstBlock = 3.0; 109 _delayBeforeFirstBlock = 3.0;
113 } 110 }
114 return self; 111 return self;
115 } 112 }
116 113
117 - (void)enqueueBlockNamed:(NSString*)name block:(ProceduralBlock)block { 114 - (void)enqueueBlockNamed:(NSString*)name block:(ProceduralBlock)block {
118 DCHECK(name); 115 DCHECK(name);
119 DCHECK([NSThread isMainThread]); 116 DCHECK([NSThread isMainThread]);
(...skipping 13 matching lines...) Expand all
133 DCHECK([NSThread isMainThread]); 130 DCHECK([NSThread isMainThread]);
134 _isBlockScheduled = NO; 131 _isBlockScheduled = NO;
135 NSString* nextBlockName = [_blocksNameQueue firstObject]; 132 NSString* nextBlockName = [_blocksNameQueue firstObject];
136 if (!nextBlockName) 133 if (!nextBlockName)
137 return; 134 return;
138 135
139 DeferredInitializationBlock* nextBlock = 136 DeferredInitializationBlock* nextBlock =
140 [_runBlocks objectForKey:nextBlockName]; 137 [_runBlocks objectForKey:nextBlockName];
141 DCHECK(nextBlock); 138 DCHECK(nextBlock);
142 139
143 __weak DeferredInitializationRunner* weakSelf = self; 140 base::WeakNSObject<DeferredInitializationRunner> weakSelf(self);
141
144 dispatch_after( 142 dispatch_after(
145 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), 143 dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)),
146 dispatch_get_main_queue(), ^{ 144 dispatch_get_main_queue(), ^{
147 [nextBlock run]; 145 [nextBlock run];
148 [weakSelf scheduleNextBlockWithDelay:[weakSelf delayBetweenBlocks]]; 146 [weakSelf scheduleNextBlockWithDelay:[weakSelf delayBetweenBlocks]];
149 }); 147 });
150 148
151 _isBlockScheduled = YES; 149 _isBlockScheduled = YES;
152 [_blocksNameQueue removeObjectAtIndex:0]; 150 [_blocksNameQueue removeObjectAtIndex:0];
153 } 151 }
154 152
155 - (void)runBlockIfNecessary:(NSString*)name { 153 - (void)runBlockIfNecessary:(NSString*)name {
156 DCHECK([NSThread isMainThread]); 154 DCHECK([NSThread isMainThread]);
157 [[_runBlocks objectForKey:name] run]; 155 [[_runBlocks objectForKey:name] run];
158 } 156 }
159 157
160 - (void)cancelBlockNamed:(NSString*)name { 158 - (void)cancelBlockNamed:(NSString*)name {
161 DCHECK([NSThread isMainThread]); 159 DCHECK([NSThread isMainThread]);
162 DCHECK(name); 160 DCHECK(name);
163 [_blocksNameQueue removeObject:name]; 161 [_blocksNameQueue removeObject:name];
164 [[_runBlocks objectForKey:name] cancel]; 162 [[_runBlocks objectForKey:name] cancel];
165 [_runBlocks removeObjectForKey:name]; 163 [_runBlocks removeObjectForKey:name];
166 } 164 }
167 165
168 - (NSUInteger)numberOfBlocksRemaining { 166 - (NSUInteger)numberOfBlocksRemaining {
169 return [_runBlocks count]; 167 return [_runBlocks count];
170 } 168 }
171 169
172 @end 170 @end
OLDNEW
« no previous file with comments | « ios/chrome/app/application_delegate/memory_warning_helper.mm ('k') | ios/chrome/app/safe_mode_crashing_modules_config.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698