Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "ios/net/crn_http_protocol_handler_pauseable_proxy.h" | |
| 6 | |
| 7 @interface CRNHTTPProtocolHandlerProxyWithClientThread () {} | |
| 8 | |
| 9 - (void)performSelectorOnClientThread:(SEL)aSelector withObject:(id)arg; | |
| 10 | |
| 11 @end | |
| 12 | |
| 13 // The |QueuedSelector| class represents a single queued callback, with the | |
|
marq (ping after 24h)
2015/05/22 16:56:53
Just use NSInvocation to instead of a custom class
Elly Fong-Jones
2015/05/26 21:16:09
Done.
| |
| 14 // selector to be performed and the object on which to perform it. | |
| 15 @interface QueuedSelector : NSObject { | |
| 16 SEL _selector; | |
| 17 id _object; | |
| 18 } | |
| 19 | |
| 20 - (id)initWithSelector:(SEL)selector | |
| 21 withObject:(id)object; | |
| 22 - (SEL)selector; | |
| 23 - (id)object; | |
| 24 | |
| 25 @end | |
| 26 | |
| 27 @implementation QueuedSelector | |
| 28 | |
| 29 - (id)initWithSelector:(SEL)selector | |
| 30 withObject:(id)object { | |
| 31 if ((self = [super init])) { | |
| 32 _selector = selector; | |
| 33 _object = object; | |
| 34 } | |
| 35 return self; | |
| 36 } | |
| 37 | |
| 38 - (SEL)selector { | |
| 39 return _selector; | |
| 40 } | |
| 41 | |
| 42 - (id)object { | |
| 43 return _object; | |
| 44 } | |
| 45 | |
| 46 @end | |
| 47 | |
| 48 @interface CRNHTTPProtocolHandlerPauseableProxy () { | |
| 49 BOOL _paused; | |
| 50 NSMutableArray* _queuedSelectors; | |
| 51 } | |
| 52 | |
| 53 @end | |
| 54 | |
| 55 @implementation CRNHTTPProtocolHandlerPauseableProxy | |
| 56 | |
| 57 - (instancetype)initWithProtocol:(NSURLProtocol*)protocol | |
| 58 clientThread:(NSThread*)clientThread | |
| 59 runLoopMode:(NSString*)mode { | |
| 60 if ((self = [super initWithProtocol:protocol | |
| 61 clientThread:clientThread | |
| 62 runLoopMode:mode])) { | |
| 63 _paused = NO; | |
| 64 _queuedSelectors = [[NSMutableArray alloc] init]; | |
| 65 } | |
| 66 return self; | |
| 67 } | |
| 68 | |
| 69 // Overridden from CRNHTTPProtocolHandlerProxyWithClientThread. If |_paused| is | |
| 70 // YES, we queue the selector/object pair in |_queuedSelectors|; otherwise, we | |
| 71 // invoke the given selector directly on the client thread. | |
| 72 - (void)performSelectorOnClientThread:(SEL)aSelector withObject:(id)arg { | |
| 73 if (!_paused) { | |
| 74 [super performSelectorOnClientThread:aSelector withObject:arg]; | |
| 75 } else { | |
| 76 QueuedSelector* qs = [[QueuedSelector alloc] | |
| 77 initWithSelector:aSelector | |
| 78 withObject:arg]; | |
|
marq (ping after 24h)
2015/05/22 16:56:53
(This is a memory leak, BTW).
| |
| 79 [_queuedSelectors addObject:qs]; | |
|
marq (ping after 24h)
2015/05/22 16:56:53
Try this:
scoped_nsobject<NSInvocation> qs([[NSIn
| |
| 80 } | |
| 81 } | |
| 82 | |
| 83 - (void)pause { | |
| 84 _paused = YES; | |
| 85 } | |
| 86 | |
| 87 - (void)resume { | |
| 88 _paused = NO; | |
| 89 for (QueuedSelector* qs : _queuedSelectors) { | |
|
marq (ping after 24h)
2015/05/22 16:56:53
for (NSInvocation* qs : _queuedSelectors) {
[sel
| |
| 90 [self performSelectorOnClientThread:[qs selector] withObject:[qs object]]; | |
|
marq (ping after 24h)
2015/05/22 16:56:53
What if [self pause] is a queued selector, or exec
| |
| 91 } | |
| 92 [_queuedSelectors removeAllObjects]; | |
| 93 } | |
| 94 | |
| 95 @end | |
| OLD | NEW |