Skip to content

Commit

Permalink
PGPlugin - Added VERIFY_ARGUMENTS macro, and verifyArguments function…
Browse files Browse the repository at this point in the history
…, for plugin arguments verification, and automatic arg error reporting
  • Loading branch information
shazron committed Jul 28, 2011
1 parent 6acc594 commit 609a645
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
17 changes: 17 additions & 0 deletions PhoneGapLib/Classes/NSMutableArray+QueueAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NSMutableArray+QueueAdditions.h
//
// Created by Shazron Abdullah
// Copyright 2011 Nitobi Software Inc.
//
#import <Foundation/Foundation.h>


@interface NSMutableArray (QueueAdditions)

- (id) pop;
- (id) queueHead;
- (id) dequeue;
- (void) enqueue:(id)obj;

@end
46 changes: 46 additions & 0 deletions PhoneGapLib/Classes/NSMutableArray+QueueAdditions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// NSMutableArray+QueueAdditions.h
//
// Created by Shazron Abdullah
// Copyright 2011 Nitobi Software Inc.
//

#import "NSMutableArray+QueueAdditions.h"

@implementation NSMutableArray (QueueAdditions)

- (id) queueHead
{
if ([self count] == 0) {
return nil;
}

return [self objectAtIndex:0];
}

- (id) dequeue
{
if ([self count] == 0) {
return nil;
}

id head = [self objectAtIndex:0];
if (head != nil) {
[[head retain] autorelease];
[self removeObjectAtIndex:0];
}

return head;
}

- (id) pop
{
return [self dequeue];
}

- (void) enqueue:(id)object
{
[self addObject:object];
}

@end
7 changes: 7 additions & 0 deletions PhoneGapLib/Classes/PGPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "PluginResult.h"
#import "NSMutableArray+QueueAdditions.h"

#define PGPluginHandleOpenURLNotification @"PGPluginHandleOpenURLNotification"

#define VERIFY_ARGUMENTS(args, expectedCount, callbackId) if (![self verifyArguments:args withExpectedCount:expectedCount andCallbackId:callbackId \
callerFileName:__FILE__ callerFunctionName:__PRETTY_FUNCTION__]) { return; }


@class PhoneGapDelegate;

@interface PGPlugin : NSObject {
Expand Down Expand Up @@ -41,5 +46,7 @@
- (UIViewController*) appViewController;

- (NSString*) writeJavascript:(NSString*)javascript;
- (BOOL) verifyArguments:(NSMutableArray*)arguments withExpectedCount:(NSUInteger)expectedCount andCallbackId:(NSString*)callbackId
callerFileName:(const char*)callerFileName callerFunctionName:(const char*)callerFunctionName;

@end
28 changes: 28 additions & 0 deletions PhoneGapLib/Classes/PGPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ - (PGPlugin*) initWithWebView:(UIWebView*)theWebView
return self;
}

/*
The arguments passed in should not included the callbackId.
If argument count is not as expected, it will call the error callback using PluginResult (if callbackId is available),
or it will write to stderr using NSLog.
Usage is through the VERIFY_ARGUMENTS macro.
*/
- (BOOL) verifyArguments:(NSMutableArray*)arguments withExpectedCount:(NSUInteger)expectedCount andCallbackId:(NSString*)callbackId
callerFileName:(const char*)callerFileName callerFunctionName:(const char*)callerFunctionName
{
NSUInteger argc = [arguments count];
BOOL ok = (argc == expectedCount);

if (!ok) {
NSString* errorString = [NSString stringWithFormat:@"Incorrect no. of arguments for plugin: was %d, expected %d", argc, expectedCount];
if (callbackId) {
NSString* callbackId = [arguments objectAtIndex:0];
PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_ERROR messageAsString:errorString];
[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
} else {
NSString* fileName = [[[[NSString alloc] initWithBytes:callerFileName length:strlen(callerFileName) encoding:NSUTF8StringEncoding] autorelease] lastPathComponent];
NSLog(@"%@::%s - Error: %@", fileName, callerFunctionName, errorString);
}
}

return ok;
}

/*
// NOTE: for onPause and onResume, calls into JavaScript must not call or trigger any blocking UI, like alerts
- (void) onPause {}
Expand Down
14 changes: 13 additions & 1 deletion PhoneGapLib/PhoneGapLib.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@
30A9DFEB13A7EBC1009AA950 /* PGPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 30E33AF113A7E24B00594D64 /* PGPlugin.m */; };
30B39EBE13D0268B0009682A /* PGSplashScreen.h in Headers */ = {isa = PBXBuildFile; fileRef = 30B39EBC13D0268B0009682A /* PGSplashScreen.h */; };
30B39EBF13D0268B0009682A /* PGSplashScreen.m in Sources */ = {isa = PBXBuildFile; fileRef = 30B39EBD13D0268B0009682A /* PGSplashScreen.m */; };
30B39EC013D0268B0009682A /* PGSplashScreen.h in Headers */ = {isa = PBXBuildFile; fileRef = 30B39EBC13D0268B0009682A /* PGSplashScreen.h */; };
30B39EC013D0268B0009682A /* PGSplashScreen.h in Headers */ = {isa = PBXBuildFile; fileRef = 30B39EBC13D0268B0009682A /* PGSplashScreen.h */; settings = {ATTRIBUTES = (Public, ); }; };
30B39EC113D0268B0009682A /* PGSplashScreen.m in Sources */ = {isa = PBXBuildFile; fileRef = 30B39EBD13D0268B0009682A /* PGSplashScreen.m */; };
30E33AF213A7E24B00594D64 /* PGPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 30E33AF013A7E24B00594D64 /* PGPlugin.h */; };
30E33AF313A7E24B00594D64 /* PGPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 30E33AF113A7E24B00594D64 /* PGPlugin.m */; };
30E563CF13E217EC00C949AA /* NSMutableArray+QueueAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 30E563CD13E217EC00C949AA /* NSMutableArray+QueueAdditions.h */; };
30E563D013E217EC00C949AA /* NSMutableArray+QueueAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 30E563CE13E217EC00C949AA /* NSMutableArray+QueueAdditions.m */; };
30E563D113E217EC00C949AA /* NSMutableArray+QueueAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 30E563CD13E217EC00C949AA /* NSMutableArray+QueueAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
30E563D213E217EC00C949AA /* NSMutableArray+QueueAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 30E563CE13E217EC00C949AA /* NSMutableArray+QueueAdditions.m */; };
8887FD661090FBE7009987E8 /* Camera.h in Headers */ = {isa = PBXBuildFile; fileRef = 8887FD261090FBE7009987E8 /* Camera.h */; };
8887FD671090FBE7009987E8 /* Camera.m in Sources */ = {isa = PBXBuildFile; fileRef = 8887FD271090FBE7009987E8 /* Camera.m */; };
8887FD681090FBE7009987E8 /* Categories.h in Headers */ = {isa = PBXBuildFile; fileRef = 8887FD281090FBE7009987E8 /* Categories.h */; };
Expand Down Expand Up @@ -158,6 +162,8 @@
30B39EBD13D0268B0009682A /* PGSplashScreen.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PGSplashScreen.m; path = Classes/PGSplashScreen.m; sourceTree = "<group>"; };
30E33AF013A7E24B00594D64 /* PGPlugin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PGPlugin.h; path = Classes/PGPlugin.h; sourceTree = "<group>"; };
30E33AF113A7E24B00594D64 /* PGPlugin.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PGPlugin.m; path = Classes/PGPlugin.m; sourceTree = "<group>"; };
30E563CD13E217EC00C949AA /* NSMutableArray+QueueAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSMutableArray+QueueAdditions.h"; path = "Classes/NSMutableArray+QueueAdditions.h"; sourceTree = "<group>"; };
30E563CE13E217EC00C949AA /* NSMutableArray+QueueAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSMutableArray+QueueAdditions.m"; path = "Classes/NSMutableArray+QueueAdditions.m"; sourceTree = "<group>"; };
8887FD261090FBE7009987E8 /* Camera.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Camera.h; path = Classes/Camera.h; sourceTree = "<group>"; };
8887FD271090FBE7009987E8 /* Camera.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Camera.m; path = Classes/Camera.m; sourceTree = "<group>"; };
8887FD281090FBE7009987E8 /* Categories.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Categories.h; path = Classes/Categories.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -262,6 +268,8 @@
888700D710922F56009987E8 /* Commands */ = {
isa = PBXGroup;
children = (
30E563CD13E217EC00C949AA /* NSMutableArray+QueueAdditions.h */,
30E563CE13E217EC00C949AA /* NSMutableArray+QueueAdditions.m */,
30B39EBC13D0268B0009682A /* PGSplashScreen.h */,
30B39EBD13D0268B0009682A /* PGSplashScreen.m */,
30E33AF013A7E24B00594D64 /* PGPlugin.h */,
Expand Down Expand Up @@ -394,6 +402,7 @@
30A9DFE913A7EB9F009AA950 /* PGPlugin.h in Headers */,
302965BD13A94E9D007046C5 /* PGDebug.h in Headers */,
30B39EC013D0268B0009682A /* PGSplashScreen.h in Headers */,
30E563D113E217EC00C949AA /* NSMutableArray+QueueAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -431,6 +440,7 @@
30E33AF213A7E24B00594D64 /* PGPlugin.h in Headers */,
302965BC13A94E9D007046C5 /* PGDebug.h in Headers */,
30B39EBE13D0268B0009682A /* PGSplashScreen.h in Headers */,
30E563CF13E217EC00C949AA /* NSMutableArray+QueueAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -591,6 +601,7 @@
303259AC136B325100982B63 /* FileTransfer.m in Sources */,
30956FFD138F317200FC3563 /* PGMotion.m in Sources */,
30B39EC113D0268B0009682A /* PGSplashScreen.m in Sources */,
30E563D213E217EC00C949AA /* NSMutableArray+QueueAdditions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -626,6 +637,7 @@
1F584B9C1385A28A00ED25E8 /* Capture.m in Sources */,
30E33AF313A7E24B00594D64 /* PGPlugin.m in Sources */,
30B39EBF13D0268B0009682A /* PGSplashScreen.m in Sources */,
30E563D013E217EC00C949AA /* NSMutableArray+QueueAdditions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 609a645

Please sign in to comment.