OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/web/web_state/js/crw_js_invoke_parameter_queue.h" |
| 6 |
| 7 #import "base/mac/scoped_nsobject.h" |
| 8 #include "url/gurl.h" |
| 9 |
| 10 @implementation CRWJSInvokeParameters { |
| 11 base::scoped_nsobject<NSString> _commandString; |
| 12 BOOL _userIsInteracting; |
| 13 base::scoped_nsobject<NSString> _windowId; |
| 14 GURL _originURL; |
| 15 } |
| 16 |
| 17 @synthesize userIsInteracting = _userIsInteracting; |
| 18 |
| 19 - (id)initWithCommandString:(NSString*)commandString |
| 20 userIsInteracting:(BOOL)userIsInteracting |
| 21 originURL:(const GURL&)originURL |
| 22 forWindowId:(NSString*)windowId { |
| 23 if ((self = [super init])) { |
| 24 _commandString.reset([commandString copy]); |
| 25 _userIsInteracting = userIsInteracting; |
| 26 _windowId.reset([windowId copy]); |
| 27 _originURL = originURL; |
| 28 } |
| 29 return self; |
| 30 } |
| 31 |
| 32 - (NSString*)commandString { |
| 33 return _commandString.get(); |
| 34 } |
| 35 |
| 36 - (NSString*)windowId { |
| 37 return _windowId.get(); |
| 38 } |
| 39 |
| 40 - (const GURL&)originURL { |
| 41 return _originURL; |
| 42 } |
| 43 |
| 44 @end |
| 45 |
| 46 @implementation CRWJSInvokeParameterQueue { |
| 47 base::scoped_nsobject<NSMutableArray> _queue; |
| 48 } |
| 49 |
| 50 - (id)init { |
| 51 if ((self = [super init])) { |
| 52 // Under normal circumstainces there will be maximum one message queued. |
| 53 _queue.reset([[NSMutableArray arrayWithCapacity:1] retain]); |
| 54 } |
| 55 return self; |
| 56 } |
| 57 |
| 58 - (BOOL)isEmpty { |
| 59 return [_queue count] == 0; |
| 60 } |
| 61 |
| 62 - (NSUInteger)queueLength { |
| 63 return [_queue count]; |
| 64 } |
| 65 |
| 66 - (void)addCommandString:(NSString*)commandString |
| 67 userIsInteracting:(BOOL)userIsInteracting |
| 68 originURL:(const GURL&)originURL |
| 69 forWindowId:(NSString*)windowId { |
| 70 base::scoped_nsobject<CRWJSInvokeParameters> invokeParameters( |
| 71 [[CRWJSInvokeParameters alloc] initWithCommandString:commandString |
| 72 userIsInteracting:userIsInteracting |
| 73 originURL:originURL |
| 74 forWindowId:windowId]); |
| 75 [_queue addObject:invokeParameters]; |
| 76 } |
| 77 |
| 78 - (void)removeCommandString:(NSString*)commandString { |
| 79 NSMutableArray* commandsToRemove = [NSMutableArray array]; |
| 80 for (CRWJSInvokeParameters* params in _queue.get()) { |
| 81 NSRange range = |
| 82 [[params commandString] rangeOfString:commandString |
| 83 options:NSCaseInsensitiveSearch]; |
| 84 if (range.location != NSNotFound) |
| 85 [commandsToRemove addObject:params]; |
| 86 } |
| 87 [_queue removeObjectsInArray:commandsToRemove]; |
| 88 } |
| 89 |
| 90 - (CRWJSInvokeParameters*)popInvokeParameters { |
| 91 if (![_queue count]) |
| 92 return nil; |
| 93 CRWJSInvokeParameters* invokeParameters = |
| 94 [[[_queue objectAtIndex:0] retain] autorelease]; |
| 95 [_queue removeObjectAtIndex:0]; |
| 96 return invokeParameters; |
| 97 } |
| 98 |
| 99 @end |
OLD | NEW |