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 #ifndef IOS_CHROME_APP_DEFERRED_INITIALIZATION_RUNNER_H_ | 5 #ifndef IOS_CHROME_APP_DEFERRED_INITIALIZATION_RUNNER_H_ |
6 #define IOS_CHROME_APP_DEFERRED_INITIALIZATION_RUNNER_H_ | 6 #define IOS_CHROME_APP_DEFERRED_INITIALIZATION_RUNNER_H_ |
7 | 7 |
8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
9 | 9 |
10 #include "base/ios/block_types.h" | 10 #include "base/ios/block_types.h" |
11 | 11 |
12 // A singleton object to run initialization code asynchronously. Blocks are | 12 // A singleton object to run initialization code asynchronously. Blocks are |
13 // scheduled to be run after a delay. The block is named when added to the | 13 // scheduled to be run after a delay. The block is named when added to the |
14 // singleton so that other code can force a deferred block to be run | 14 // singleton so that other code can force a deferred block to be run |
15 // synchronously if necessary. | 15 // synchronously if necessary. |
16 @interface DeferredInitializationRunner : NSObject | 16 @interface DeferredInitializationRunner : NSObject |
17 | 17 |
18 // Returns singleton instance. | 18 // Returns singleton instance. |
19 + (DeferredInitializationRunner*)sharedInstance; | 19 + (DeferredInitializationRunner*)sharedInstance; |
20 | 20 |
| 21 // Deprecated, use |enqueueBlockNamed:block:| instead. |
21 // Schedules |block| to be run after |delaySeconds| on the current queue. | 22 // Schedules |block| to be run after |delaySeconds| on the current queue. |
22 // This |block| is stored as |name| so code can force this initialization to | 23 // This |block| is stored as |name| so code can force this initialization to |
23 // be run synchronously if necessary. This method may be called more than | 24 // be run synchronously if necessary. This method may be called more than |
24 // once with the same |name| parameter. Any block with the same |name| | 25 // once with the same |name| parameter. Any block with the same |name| |
25 // cancels a previously scheduled block of the same |name| if the block has | 26 // cancels a previously scheduled block of the same |name| if the block has |
26 // not been run yet. | 27 // not been run yet. |
27 - (void)runBlockNamed:(NSString*)name | 28 - (void)runBlockNamed:(NSString*)name |
28 after:(NSTimeInterval)delaySeconds | 29 after:(NSTimeInterval)delaySeconds |
29 block:(ProceduralBlock)block; | 30 block:(ProceduralBlock)block; |
30 | 31 |
| 32 // Stores |block| under |name| to a queue of blocks to run. All blocks are run |
| 33 // sequentially with a small delay before the first block and between each |
| 34 // successive block. If a block is already registered under |name|, it is |
| 35 // replaced with |block| unless it has already been run. |
| 36 - (void)enqueueBlockNamed:(NSString*)name block:(ProceduralBlock)block; |
| 37 |
31 // Looks up a previously scheduled block of |name|. If block has not been | 38 // Looks up a previously scheduled block of |name|. If block has not been |
32 // run yet, run it synchronously now. | 39 // run yet, run it synchronously now. |
33 - (void)runBlockIfNecessary:(NSString*)name; | 40 - (void)runBlockIfNecessary:(NSString*)name; |
34 | 41 |
35 // Cancels a previously scheduled block of |name|. This is a no-op if the | 42 // Cancels a previously scheduled block of |name|. This is a no-op if the |
36 // block has already been executed. | 43 // block has already been executed. |
37 - (void)cancelBlockNamed:(NSString*)name; | 44 - (void)cancelBlockNamed:(NSString*)name; |
38 | 45 |
39 // Number of blocks that have been registered but not executed yet. | 46 // Number of blocks that have been registered but not executed yet. |
40 // Exposed for testing. | 47 // Exposed for testing. |
41 @property(nonatomic, readonly) NSUInteger numberOfBlocksRemaining; | 48 @property(nonatomic, readonly) NSUInteger numberOfBlocksRemaining; |
42 | 49 |
43 @end | 50 @end |
44 | 51 |
| 52 @interface DeferredInitializationRunner (ExposedForTesting) |
| 53 |
| 54 // Time interval between two blocks. Default value is 200ms. |
| 55 @property(nonatomic, assign) NSTimeInterval delayBetweenBlocks; |
| 56 |
| 57 // Time interval before running the first block. To override default value of |
| 58 // 3s, set this property before the first call to |-enqueueBlockNamed:block:|. |
| 59 @property(nonatomic, assign) NSTimeInterval delayBeforeFirstBlock; |
| 60 |
| 61 @end |
| 62 |
45 #endif // IOS_CHROME_APP_DEFERRED_INITIALIZATION_RUNNER_H_ | 63 #endif // IOS_CHROME_APP_DEFERRED_INITIALIZATION_RUNNER_H_ |
OLD | NEW |