blob: c92964a79ea363ecb4052f585f6b94fe469b188e [file] [log] [blame]
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#import <WebKit/WebKit.h>
#import "CDVInvokedUrlCommand.h"
@implementation CDVInvokedUrlCommand
@synthesize arguments = _arguments;
@synthesize callbackId = _callbackId;
@synthesize cmdClassName = _cmdClassName;
@synthesize methodName = _methodName;
+ (CDVInvokedUrlCommand*) commandFromJson:(NSArray*) jsonEntry {
return [[CDVInvokedUrlCommand alloc] initFromJson:jsonEntry];
}
- (id) initFromJson:(NSArray*) jsonEntry {
id tmp = jsonEntry[0];
NSString* callbackId = tmp == [NSNull null] ? nil : tmp;
NSString* className = jsonEntry[1];
NSString* methodName = jsonEntry[2];
NSMutableArray* arguments = jsonEntry[3];
return [self initWithArguments:arguments
callbackId:callbackId
className:className
methodName:methodName];
}
- (id) initWithArguments:(NSArray*) arguments
callbackId:(NSString*) callbackId
className:(NSString*) className
methodName:(NSString*) methodName {
self = [super init];
if (self != nil) {
_arguments = arguments;
_callbackId = callbackId;
_cmdClassName = className;
_methodName = methodName;
}
[self massageArguments];
return self;
}
- (void) massageArguments {
NSMutableArray* newArgs = nil;
for (NSUInteger i = 0, count = [_arguments count]; i < count; ++i) {
id arg = _arguments[i];
if (![arg isKindOfClass:[NSDictionary class]]) {
continue;
}
NSDictionary* dict = arg;
NSString* type = dict[@"CDVType"];
if (!type || ![type isEqualToString:@"ArrayBuffer"]) {
continue;
}
NSString* data = dict[@"data"];
if (!data) {
continue;
}
if (newArgs == nil) {
newArgs = [NSMutableArray arrayWithArray:_arguments];
_arguments = newArgs;
}
newArgs[i] = [[NSData alloc] initWithBase64EncodedString:data options:0];
}
}
- (id) argumentAtIndex:(NSUInteger) index {
return [self argumentAtIndex:index withDefault:nil];
}
- (id) argumentAtIndex:(NSUInteger) index withDefault:(id) defaultValue {
return [self argumentAtIndex:index withDefault:defaultValue andClass:nil];
}
- (id) argumentAtIndex:(NSUInteger) index withDefault:(id) defaultValue andClass:(Class) aClass {
if (index >= [_arguments count]) {
return defaultValue;
}
id ret = _arguments[index];
if (ret == [NSNull null] || ret == [WebUndefined undefined]) {
ret = defaultValue;
}
if ((aClass != nil) && ![ret isKindOfClass:aClass]) {
ret = defaultValue;
}
return ret;
}
@end