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.
5 #import "ios/web/web_state/js/crw_js_invoke_parameter_queue.h"
7 #import "base/mac/scoped_nsobject.h"
10 @implementation CRWJSInvokeParameters {
11 base::scoped_nsobject<NSString> _commandString;
12 BOOL _userIsInteracting;
13 base::scoped_nsobject<NSString> _windowId;
17 @synthesize userIsInteracting = _userIsInteracting;
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;
32 - (NSString*)commandString {
33 return _commandString.get();
36 - (NSString*)windowId {
37 return _windowId.get();
40 - (const GURL&)originURL {
46 @implementation CRWJSInvokeParameterQueue {
47 base::scoped_nsobject<NSMutableArray> _queue;
51 if ((self = [super init])) {
52 // Under normal circumstainces there will be maximum one message queued.
53 _queue.reset([[NSMutableArray arrayWithCapacity:1] retain]);
59 return [_queue count] == 0;
62 - (NSUInteger)queueLength {
63 return [_queue count];
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
74 forWindowId:windowId]);
75 [_queue addObject:invokeParameters];
78 - (void)removeCommandString:(NSString*)commandString {
79 NSMutableArray* commandsToRemove = [NSMutableArray array];
80 for (CRWJSInvokeParameters* params in _queue.get()) {
82 [[params commandString] rangeOfString:commandString
83 options:NSCaseInsensitiveSearch];
84 if (range.location != NSNotFound)
85 [commandsToRemove addObject:params];
87 [_queue removeObjectsInArray:commandsToRemove];
90 - (CRWJSInvokeParameters*)popInvokeParameters {
93 CRWJSInvokeParameters* invokeParameters =
94 [[[_queue objectAtIndex:0] retain] autorelease];
95 [_queue removeObjectAtIndex:0];
96 return invokeParameters;