From: masakih Date: Sun, 9 Feb 2014 09:20:33 +0000 (+0900) Subject: いろいろ追加 X-Git-Tag: 1.0beta~115 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=ed636b925d0fdf43f86e41f4aa5256c7b5043a97;p=kcd%2FKCD.git いろいろ追加 --- diff --git a/KCD.xcodeproj/project.pbxproj b/KCD.xcodeproj/project.pbxproj index b9449aad..a37b28c1 100644 --- a/KCD.xcodeproj/project.pbxproj +++ b/KCD.xcodeproj/project.pbxproj @@ -20,10 +20,13 @@ F4BDEB71187252F30069D0CE /* KCDTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F4BDEB70187252F30069D0CE /* KCDTests.m */; }; F4BDEB7B187253130069D0CE /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4BDEB7A187253130069D0CE /* WebKit.framework */; }; F4C041BA1875CAF5009273EB /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4C041B91875CAF5009273EB /* JavaScriptCore.framework */; }; - F4C118F218A521FF005D5B25 /* HMHTTPProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C118F118A521FF005D5B25 /* HMHTTPProtocol.m */; }; F4C118FA18A67B1C005D5B25 /* CacheStoragePolicy.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C118F518A67B1C005D5B25 /* CacheStoragePolicy.m */; }; F4C118FB18A67B1C005D5B25 /* CanonicalRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C118F718A67B1C005D5B25 /* CanonicalRequest.m */; }; F4C118FC18A67B1C005D5B25 /* CustomHTTPProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C118F918A67B1C005D5B25 /* CustomHTTPProtocol.m */; }; + F4C1190218A6FFC2005D5B25 /* HMQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C1190118A6FFC2005D5B25 /* HMQueue.m */; }; + F4C1190518A71AEA005D5B25 /* HMJSONReciever.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C1190418A71AEA005D5B25 /* HMJSONReciever.m */; }; + F4C1190818A746EB005D5B25 /* HMJSONTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C1190718A746EB005D5B25 /* HMJSONTracker.m */; }; + F4C1190B18A74DF0005D5B25 /* HMJSONCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C1190A18A74DF0005D5B25 /* HMJSONCommand.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -58,14 +61,20 @@ F4BDEB70187252F30069D0CE /* KCDTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KCDTests.m; sourceTree = ""; }; F4BDEB7A187253130069D0CE /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; }; F4C041B91875CAF5009273EB /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; }; - F4C118F018A521FF005D5B25 /* HMHTTPProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMHTTPProtocol.h; sourceTree = ""; }; - F4C118F118A521FF005D5B25 /* HMHTTPProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMHTTPProtocol.m; sourceTree = ""; }; F4C118F418A67B1C005D5B25 /* CacheStoragePolicy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CacheStoragePolicy.h; sourceTree = ""; }; F4C118F518A67B1C005D5B25 /* CacheStoragePolicy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CacheStoragePolicy.m; sourceTree = ""; }; F4C118F618A67B1C005D5B25 /* CanonicalRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CanonicalRequest.h; sourceTree = ""; }; F4C118F718A67B1C005D5B25 /* CanonicalRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CanonicalRequest.m; sourceTree = ""; }; F4C118F818A67B1C005D5B25 /* CustomHTTPProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomHTTPProtocol.h; sourceTree = ""; }; F4C118F918A67B1C005D5B25 /* CustomHTTPProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CustomHTTPProtocol.m; sourceTree = ""; }; + F4C1190018A6FFC2005D5B25 /* HMQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMQueue.h; sourceTree = ""; }; + F4C1190118A6FFC2005D5B25 /* HMQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMQueue.m; sourceTree = ""; }; + F4C1190318A71AEA005D5B25 /* HMJSONReciever.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMJSONReciever.h; sourceTree = ""; }; + F4C1190418A71AEA005D5B25 /* HMJSONReciever.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMJSONReciever.m; sourceTree = ""; }; + F4C1190618A746EB005D5B25 /* HMJSONTracker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMJSONTracker.h; sourceTree = ""; }; + F4C1190718A746EB005D5B25 /* HMJSONTracker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMJSONTracker.m; sourceTree = ""; }; + F4C1190918A74DF0005D5B25 /* HMJSONCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMJSONCommand.h; sourceTree = ""; }; + F4C1190A18A74DF0005D5B25 /* HMJSONCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMJSONCommand.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -136,10 +145,16 @@ isa = PBXGroup; children = ( F4C118F318A67B05005D5B25 /* New Group */, - F4C118F018A521FF005D5B25 /* HMHTTPProtocol.h */, - F4C118F118A521FF005D5B25 /* HMHTTPProtocol.m */, F4BDEB58187252F30069D0CE /* HMAppDelegate.h */, F4BDEB59187252F30069D0CE /* HMAppDelegate.m */, + F4C1190318A71AEA005D5B25 /* HMJSONReciever.h */, + F4C1190418A71AEA005D5B25 /* HMJSONReciever.m */, + F4C1190618A746EB005D5B25 /* HMJSONTracker.h */, + F4C1190718A746EB005D5B25 /* HMJSONTracker.m */, + F4C1190918A74DF0005D5B25 /* HMJSONCommand.h */, + F4C1190A18A74DF0005D5B25 /* HMJSONCommand.m */, + F4C1190018A6FFC2005D5B25 /* HMQueue.h */, + F4C1190118A6FFC2005D5B25 /* HMQueue.m */, F4BDEB5B187252F30069D0CE /* MainMenu.xib */, F4BDEB5E187252F30069D0CE /* Images.xcassets */, F4BDEB4D187252F30069D0CE /* Supporting Files */, @@ -289,11 +304,14 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + F4C1190218A6FFC2005D5B25 /* HMQueue.m in Sources */, F4C118FA18A67B1C005D5B25 /* CacheStoragePolicy.m in Sources */, + F4C1190518A71AEA005D5B25 /* HMJSONReciever.m in Sources */, + F4C1190B18A74DF0005D5B25 /* HMJSONCommand.m in Sources */, F4BDEB53187252F30069D0CE /* main.m in Sources */, F4C118FC18A67B1C005D5B25 /* CustomHTTPProtocol.m in Sources */, + F4C1190818A746EB005D5B25 /* HMJSONTracker.m in Sources */, F4C118FB18A67B1C005D5B25 /* CanonicalRequest.m in Sources */, - F4C118F218A521FF005D5B25 /* HMHTTPProtocol.m in Sources */, F4BDEB5A187252F30069D0CE /* HMAppDelegate.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/KCD/CustomHTTPProtocol.h b/KCD/CustomHTTPProtocol.h index c5f25bae..3a89e068 100644 --- a/KCD/CustomHTTPProtocol.h +++ b/KCD/CustomHTTPProtocol.h @@ -119,6 +119,12 @@ // protocol will not be nil // challenge will not be nil +- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveResponse:(NSURLResponse *)response; +- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveData:(NSData *)data; +- (void)customHTTPProtocolDidFinishLoading:(CustomHTTPProtocol *)protocol; +- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didFailWithError:(NSError *)error; + + - (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat:(NSString *)format arguments:(va_list)argList; // called on an arbitrary thread // protocol may be nil, implying a log message from the class itself diff --git a/KCD/CustomHTTPProtocol.m b/KCD/CustomHTTPProtocol.m index 64acb9db..75772601 100644 --- a/KCD/CustomHTTPProtocol.m +++ b/KCD/CustomHTTPProtocol.m @@ -608,6 +608,11 @@ static NSString * kOurRequestProperty = @"com.apple.dts.CustomHTTPProtocol"; cacheStoragePolicy = NSURLCacheStorageAllowedInMemoryOnly; } } + + id delegate = [[self class] delegate]; + if([delegate respondsToSelector:@selector(customHTTPProtocol:didRecieveResponse:)]) { + [delegate customHTTPProtocol:self didRecieveResponse:response]; + } [[self class] customHTTPProtocol:self logWithFormat:@"received response %@ with cache storage policy %zu", response, (size_t) cacheStoragePolicy]; @@ -645,6 +650,13 @@ static NSString * kOurRequestProperty = @"com.apple.dts.CustomHTTPProtocol"; if (NO) { [[self class] customHTTPProtocol:self logWithFormat:@"received %zu bytes of data", (size_t) [data length]]; } + + id delegate = [[self class] delegate]; + if([delegate respondsToSelector:@selector(customHTTPProtocol:didRecieveData:)]) { + [delegate customHTTPProtocol:self didRecieveData:data]; + } + + [[self client] URLProtocol:self didLoadData:data]; } @@ -659,6 +671,11 @@ static NSString * kOurRequestProperty = @"com.apple.dts.CustomHTTPProtocol"; assert([NSThread currentThread] == self.clientThread); [[self class] customHTTPProtocol:self logWithFormat:@"success"]; + + id delegate = [[self class] delegate]; + if([delegate respondsToSelector:@selector(customHTTPProtocolDidFinishLoading:)]) { + [delegate customHTTPProtocolDidFinishLoading:self]; + } // Just pass the call on to our client. @@ -680,6 +697,11 @@ static NSString * kOurRequestProperty = @"com.apple.dts.CustomHTTPProtocol"; assert([NSThread currentThread] == self.clientThread); [[self class] customHTTPProtocol:self logWithFormat:@"error %@ / %d", [error domain], (int) [error code]]; + + id delegate = [[self class] delegate]; + if([delegate respondsToSelector:@selector(customHTTPProtocol:didFailWithError:)]) { + [delegate customHTTPProtocol:self didFailWithError:error]; + } // Just pass the call on to our client. diff --git a/KCD/HMAppDelegate.h b/KCD/HMAppDelegate.h index 06dd7439..79266599 100644 --- a/KCD/HMAppDelegate.h +++ b/KCD/HMAppDelegate.h @@ -13,7 +13,11 @@ @interface HMAppDelegate : NSObject @property (assign) IBOutlet NSWindow *window; - @property (assign) IBOutlet WebView *webView; + +- (void)logLineReturn:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); +- (void)log:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); + + @end diff --git a/KCD/HMAppDelegate.m b/KCD/HMAppDelegate.m index 2c8f7b6d..33926423 100644 --- a/KCD/HMAppDelegate.m +++ b/KCD/HMAppDelegate.m @@ -9,12 +9,12 @@ #import "HMAppDelegate.h" #import "CustomHTTPProtocol.h" - +#import "HMJSONTracker.h" #import -@interface HMAppDelegate (hoge) - +@interface HMAppDelegate () +@property (retain) HMJSONTracker *tracker; @end @implementation HMAppDelegate @@ -50,29 +50,6 @@ static FILE* logFileP = NULL; } } -- (void)showWebFrame:(WebFrame *)webFrame floor:(NSUInteger)floor -{ - NSUInteger f = floor; - while(f--) [self log:@" "]; - [self logLineReturn:@"%@", [webFrame name]]; - [self logLineReturn:@"%@", [[[webFrame dataSource] representation] documentSource]]; - - for(WebFrame *sub in [webFrame childFrames]) { - [self showWebFrame:sub floor:floor + 1]; - } -} -- (void)showDOM:(DOMNode *)node floor:(NSUInteger)floor -{ - NSUInteger f = floor; - while(f--) [self log:@" "]; - [self logLineReturn:@"%@ -- %d", [node nodeName], [node nodeType]]; - - DOMNodeList *lists = [node childNodes]; - unsigned length = [lists length]; - for(unsigned i = 0; i < length; i++) { - [self showDOM:[lists item:i] floor:floor + 1]; - } -} - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { @@ -86,189 +63,7 @@ static FILE* logFileP = NULL; // [self.webView setMainFrameURL:@"http://www.google.com/"]; - [CustomHTTPProtocol setDelegate:self]; -} - -- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat:(NSString *)format arguments:(va_list)argList -{ - [self logLineReturn:[[NSString alloc] initWithFormat:format arguments:argList]]; + self.tracker = [HMJSONTracker new]; } -// -//- (id)webView:(WebView *)sender identifierForInitialRequest:(NSURLRequest *)request fromDataSource:(WebDataSource *)dataSource -//{ -// NSURL *url = [request URL]; -// NSArray *pathComponents = [url pathComponents]; -// if([pathComponents containsObject:@"kcsapi"]) { -//// [self logLineReturn:@"\nSending Request\n===>\t%@", url]; -//// -//// NSData *body = [request HTTPBody]; -//// if(body && [body length] != 0) { -//// NSString *bodyString = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]; -//// bodyString = [bodyString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; -//// [self logLineReturn:@"===>\t%@", bodyString]; -//// } -// -// return [pathComponents count] > 3 ? -// [NSString stringWithFormat:@"hoge-api-%@-%@", [pathComponents objectAtIndex:2], [pathComponents objectAtIndex:3]] : -// [NSString stringWithFormat:@"hoge-api-%@", [pathComponents objectAtIndex:2]]; -// } -// -// return [NSObject new]; -// -//} -// -//- (NSURLRequest *)webView:(WebView *)sender -// resource:(id)identifier -// willSendRequest:(NSURLRequest *)request -// redirectResponse:(NSURLResponse *)redirectResponse -// fromDataSource:(WebDataSource *)dataSource -//{ -// NSURL *url = [request URL]; -// NSString *filename = [url lastPathComponent]; -// -// if([@"181.swf" isEqualToString:filename] -// || [@"182.swf" isEqualToString:filename] -// || [@"183.swf" isEqualToString:filename] ) { -// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ -// NSURLResponse *response = nil; -// NSError *error = nil; -// NSData *data = -// [NSURLConnection sendSynchronousRequest:request -// returningResponse:&response -// error:&error]; -// if(error) { -// NSLog(@"Could not download main.swf. Reason: %@", error); -// return; -// } -// if(!data || [data length] == 0) { -// NSLog(@"Counld not download main.swf. data is zero."); -// } -// [data writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:filename] -// atomically:YES]; -// NSLog(@"finish download main.swf."); -// -// }); -// } -// -// return request; -//} -// -//- (void)dumpPropertiesObject:(JSObjectRef)object context:(JSContextRef)context floor:(NSUInteger)floor limit:(NSUInteger)limit -//{ -// if(floor > limit) return; -// -// JSPropertyNameArrayRef names = JSObjectCopyPropertyNames(context, object); -// size_t count = JSPropertyNameArrayGetCount(names); -// size_t i = 0; -// for(i = 0; i < count; i++) { -// @autoreleasepool { -// JSStringRef name = JSPropertyNameArrayGetNameAtIndex(names, i); -// JSValueRef value = JSObjectGetProperty(context, object, name, NULL); -// NSString *nameString = (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, name); -// if([nameString isEqualToString:@"inherits"]) continue; -// if([nameString isEqualToString:@"document"]) continue; -// -// JSType type = JSValueGetType(context, value); -// NSUInteger f = floor; -// while(f--) [self log:@" "]; -// [self logLineReturn:@"%@ (%d)", nameString, type]; -// if(type == kJSTypeObject) { -// [self dumpPropertiesObject:value context:context floor:floor + 1 limit:limit]; -// } -// } -// } -//} - -#if 0 -- (void)webView:(WebView *)sender resource:(id)identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource -{ - if([identifier isKindOfClass:[NSString class]]) { -#if 1 -// static BOOL isFirst = YES; -// if(!isFirst) return; -// isFirst = NO; - - JSValueRef error = NULL; - JSContextRef context = [[dataSource webFrame] globalContext]; - JSObjectRef object = JSContextGetGlobalObject(context); - -// [self logLineReturn:@"global object properties"]; -// [self dumpPropertiesObject:object context:context floor:0 limit:5]; - - JSValueRef gadgets = JSObjectGetProperty(context, object, JSStringCreateWithUTF8CString("gadgets"), &error); - if(error) { - NSLog(@"ERROR get property gadgets -> %@", JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error)); - return; - } - -// [self logLineReturn:@"gadgets properties"]; -// [self dumpPropertiesObject:gadgets context:context floor:0 limit:1]; - - JSValueRef jsonData = JSObjectGetProperty(context, gadgets, JSStringCreateWithUTF8CString("json"), &error); - if(error) { - NSLog(@"ERROR get property json -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error)); - return; - } - [self logLineReturn:@"JSON Data Value type -> %d", JSValueGetType(context, jsonData)]; - [self logLineReturn:@"JSON DATA LENGTH -> %d", JSStringGetLength((JSStringRef)jsonData)]; - - JSStringRef json = JSValueCreateJSONString(context, jsonData, 8, &error); - if(error) { - NSLog(@"ERROR create json -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error)); - return; - } - NSString *jsonString = (__bridge NSString *)(JSStringCopyCFString(kCFAllocatorDefault, json)); - [self logLineReturn:@"JSON -> \n%@", jsonString]; - - // global JSON - JSValueRef globalJSONData = JSObjectGetProperty(context, object, JSStringCreateWithUTF8CString("JSON"), &error); - if(error) { - NSLog(@"ERROR get property JSON -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error)); - return; - } - [self logLineReturn:@"JSON Data Value type -> %d", JSValueGetType(context, globalJSONData)]; - [self logLineReturn:@"WINDOW JSON DATA LENGTH -> %d", JSStringGetLength((JSStringRef)globalJSONData)]; - JSStringRef globalJSON = JSValueCreateJSONString(context, globalJSONData, 8, &error); - if(error) { - NSLog(@"ERROR create json -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error)); - return; - } - NSString *globalJSONString = (__bridge NSString *)(JSStringCopyCFString(kCFAllocatorDefault, globalJSON)); - [self logLineReturn:@"WINDOW JSON -> \n%@", globalJSONString]; - - // window JSON - JSObjectRef window = JSObjectGetProperty(context, object, JSStringCreateWithUTF8CString("window"), &error); - - JSValueRef windowJSONData = JSObjectGetProperty(context, window, JSStringCreateWithUTF8CString("JSON"), &error); - if(error) { - NSLog(@"ERROR get property JSON -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error)); - return; - } - [self logLineReturn:@"JSON Data Value type -> %d", JSValueGetType(context, windowJSONData)]; - [self logLineReturn:@"WINDOW JSON DATA LENGTH -> %d", JSStringGetLength((JSStringRef)windowJSONData)]; - JSStringRef windowJSON = JSValueCreateJSONString(context, windowJSONData, 8, &error); - if(error) { - NSLog(@"ERROR create json -> %@", (__bridge NSString *)JSStringCopyCFString(kCFAllocatorDefault, (JSStringRef)error)); - return; - } - NSString *windowJSONString = (__bridge NSString *)(JSStringCopyCFString(kCFAllocatorDefault, windowJSON)); - [self logLineReturn:@"WINDOW JSON -> \n%@", windowJSONString]; -#endif - -// [self showWebFrame:[dataSource webFrame] floor:0]; - -// [self showDOM:[[dataSource webFrame] frameElement] floor:0]; - } -} -#endif - -//- (void)webView:(WebView *)sender resource:(id)identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource -//{ -// NSData *data = [dataSource data]; -// NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; -// -// [self logLineReturn:@"%@\n%@\n\n\n", dataSource.initialRequest.URL, string]; -//} - @end diff --git a/KCD/HMHTTPProtocol.h b/KCD/HMHTTPProtocol.h deleted file mode 100644 index 864904a7..00000000 --- a/KCD/HMHTTPProtocol.h +++ /dev/null @@ -1,13 +0,0 @@ -// -// HMHTTPProtocol.h -// KCD -// -// Created by Hori,Masaki on 2014/02/07. -// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. -// - -#import - -@interface HMHTTPProtocol : NSURLProtocol - -@end diff --git a/KCD/HMHTTPProtocol.m b/KCD/HMHTTPProtocol.m deleted file mode 100644 index cef41558..00000000 --- a/KCD/HMHTTPProtocol.m +++ /dev/null @@ -1,126 +0,0 @@ -// -// HMHTTPProtocol.m -// KCD -// -// Created by Hori,Masaki on 2014/02/07. -// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. -// - -#import "HMHTTPProtocol.h" - -@class NSCFURLProtocol; - -static Class cfURLProtocolClass = Nil; -static id semaphore = nil; -static BOOL inCreation = NO; - -@interface HMHTTPProtocol () -@property (retain) id origin; -@end - -@implementation HMHTTPProtocol - -//+ (void)load -//{ -// static dispatch_once_t onceToken; -// dispatch_once(&onceToken, ^{ -// [NSURLProtocol registerClass:[self class]]; -// cfURLProtocolClass = NSClassFromString(@"NSCFURLProtocol"); -// semaphore = [NSObject new]; -// }); -//} - -- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id )client -{ - self = [super initWithRequest:request cachedResponse:cachedResponse client:client]; - if(self) { -// @synchronized(semaphore) { - inCreation = YES; - _origin = [[cfURLProtocolClass alloc] initWithRequest:request cachedResponse:cachedResponse client:self]; - inCreation = NO; - } -// } - - return self; -} - -+ (BOOL)canInitWithRequest:(NSURLRequest *)request -{ - return !inCreation; -} - -+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request -{ - NSURLRequest *res = nil; -// @synchronized(semaphore) { - inCreation = YES; - res = [cfURLProtocolClass canonicalRequestForRequest:request]; - inCreation = NO; -// } - return res; -} -+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b -{ - BOOL res = NO; -// @synchronized(semaphore) { - inCreation = YES; - res = [cfURLProtocolClass requestIsCacheEquivalent:a toRequest:b]; - inCreation = NO; -// } - return res; -} -- (void)startLoading -{ - [self.origin startLoading]; -} -- (void)stopLoading -{ - [self.origin stopLoading]; -} - -// -- (void)URLProtocol:(NSURLProtocol *)protocol wasRedirectedToRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse -{ - [self.client URLProtocol:protocol wasRedirectedToRequest:request redirectResponse:redirectResponse]; -} -- (void)URLProtocol:(NSURLProtocol *)protocol cachedResponseIsValid:(NSCachedURLResponse *)cachedResponse -{ - [self.client URLProtocol:protocol cachedResponseIsValid:cachedResponse]; -} -- (void)URLProtocol:(NSURLProtocol *)protocol didReceiveResponse:(NSURLResponse *)response cacheStoragePolicy:(NSURLCacheStoragePolicy)policy -{ - // - // - NSLog(@"Enter %s", __PRETTY_FUNCTION__); - - - [self.client URLProtocol:protocol didReceiveResponse:response cacheStoragePolicy:policy]; -} -- (void)URLProtocol:(NSURLProtocol *)protocol didLoadData:(NSData *)data -{ - // - // - NSLog(@"Enter %s", __PRETTY_FUNCTION__); - - - [self.client URLProtocol:protocol didLoadData:data]; -} -- (void)URLProtocolDidFinishLoading:(NSURLProtocol *)protocol -{ - [self.client URLProtocolDidFinishLoading:protocol]; -} -- (void)URLProtocol:(NSURLProtocol *)protocol didFailWithError:(NSError *)error -{ - [self.client URLProtocol:protocol didFailWithError:error]; -} -- (void)URLProtocol:(NSURLProtocol *)protocol didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge -{ - [self.client URLProtocol:protocol didReceiveAuthenticationChallenge:challenge]; -} -- (void)URLProtocol:(NSURLProtocol *)protocol didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge -{ - [self.client URLProtocol:protocol didCancelAuthenticationChallenge:challenge]; -} - - -@end diff --git a/KCD/HMJSONCommand.h b/KCD/HMJSONCommand.h new file mode 100644 index 00000000..ae97440b --- /dev/null +++ b/KCD/HMJSONCommand.h @@ -0,0 +1,18 @@ +// +// HMJSONCommand.h +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import + +@interface HMJSONCommand : NSObject + ++ (HMJSONCommand *)commandForAPI:(NSString *)api; + +@property (retain) NSString *argumentsString; +- (void)doCommand:(id)json; + +@end diff --git a/KCD/HMJSONCommand.m b/KCD/HMJSONCommand.m new file mode 100644 index 00000000..ee402336 --- /dev/null +++ b/KCD/HMJSONCommand.m @@ -0,0 +1,31 @@ +// +// HMJSONCommand.m +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import "HMJSONCommand.h" + +#import "HMAppDelegate.h" + + + +@implementation HMJSONCommand + ++ (HMJSONCommand *)commandForAPI:(NSString *)api +{ + + [[NSApp delegate] logLineReturn:@"%@", api]; + + return nil; +} + +- (void)doCommand:(id)json +{ + NSLog(@"Enter %s", __PRETTY_FUNCTION__); + assert(NO); +} + +@end diff --git a/KCD/HMJSONReciever.h b/KCD/HMJSONReciever.h new file mode 100644 index 00000000..8e11f978 --- /dev/null +++ b/KCD/HMJSONReciever.h @@ -0,0 +1,18 @@ +// +// HMJSONReciever.h +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import + +#import "CustomHTTPProtocol.h" +#import "HMQueue.h" + +@interface HMJSONReciever : NSObject + +@property (retain) HMQueue *queueu; + +@end diff --git a/KCD/HMJSONReciever.m b/KCD/HMJSONReciever.m new file mode 100644 index 00000000..d2bd9e24 --- /dev/null +++ b/KCD/HMJSONReciever.m @@ -0,0 +1,116 @@ +// +// HMJSONReciever.m +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import "HMJSONReciever.h" + +#import "CustomHTTPProtocol.h" +#import "HMAppDelegate.h" + + +@interface HMJSONReciever () +@property (retain) NSMutableDictionary *recievers; +@end + + +@implementation HMJSONReciever + +- (id)init +{ + self = [super init]; + if(self) { + _recievers = [NSMutableDictionary new]; + + [CustomHTTPProtocol setDelegate:self]; + } + return self; +} + +- (void)setProtocol:(NSURLProtocol *)protocol +{ + [self.recievers setObject:[NSMutableData data] + forKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]]; +} +- (NSMutableData *)dataForProtocol:(NSURLProtocol *)protocol +{ + return [self.recievers objectForKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]]; +} +- (void)removeDataForProtocol:(NSURLProtocol *)protocol +{ + [self.recievers removeObjectForKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]]; +} + +- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveResponse:(NSHTTPURLResponse *)response +{ + NSURL *url = protocol.request.URL; + NSString *path = url.path; + while(![path isEqualToString:@"/"]) { + if([path hasSuffix:@"kcsapi"]) { + [self setProtocol:protocol]; + } + path = [path stringByDeletingLastPathComponent]; + } +} + +- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveData:(NSData *)data +{ + NSMutableData *loadedData = [self dataForProtocol:protocol]; + if(!loadedData) return; + + [loadedData appendData:data]; +} + +- (void)customHTTPProtocolDidFinishLoading:(CustomHTTPProtocol *)protocol +{ + NSData *data = [self dataForProtocol:protocol]; + if(!data) return; + +#define JSON_LOG_STRING 0 +#if JSON_LOG_STRING + NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + [self logLineReturn:@"body -> \n%@", string]; +#else + NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + NSArray *elements = [string componentsSeparatedByString:@"="]; + if([elements count] != 2) { + [[NSApp delegate] logLineReturn:@"\e[1mwe could not compose data. api -> %@. Number of elements:\e[22m %ld", protocol.request.URL.path, [elements count]]; + [[NSApp delegate] logLineReturn:@"Original strings -> %@", string]; + [self removeDataForProtocol:protocol]; + return; + } + + + NSData *JSONData = [elements[1] dataUsingEncoding:NSUTF8StringEncoding]; + NSError *error = nil; + id json = [NSJSONSerialization JSONObjectWithData:JSONData + options:NSJSONReadingAllowFragments + error:&error]; + if(error) { + [[NSApp delegate] logLineReturn:@"\e[1m\e[31mFail decode JSON data\e[39m\e[22m %@", error]; + } else { + NSData *requestBodyData = [protocol.request HTTPBody]; + NSString *requestBodyString = [[NSString alloc] initWithData:requestBodyData encoding:NSUTF8StringEncoding]; + [self.queueu enqueue:@{@"api" : protocol.request.URL.path, @"arg": requestBodyString, @"json" : json}]; + } +#endif + + + [self removeDataForProtocol:protocol]; +} +- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didFailWithError:(NSError *)error +{ + [self removeDataForProtocol:protocol]; +} + + +//- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat:(NSString *)format arguments:(va_list)argList +//{ +// [self logLineReturn:[[NSString alloc] initWithFormat:format arguments:argList]]; +//} + + +@end diff --git a/KCD/HMJSONTracker.h b/KCD/HMJSONTracker.h new file mode 100644 index 00000000..eaf8f8a0 --- /dev/null +++ b/KCD/HMJSONTracker.h @@ -0,0 +1,16 @@ +// +// HMJSONTracker.h +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import + +#import "HMJSONReciever.h" + + +@interface HMJSONTracker : NSObject + +@end diff --git a/KCD/HMJSONTracker.m b/KCD/HMJSONTracker.m new file mode 100644 index 00000000..400d2866 --- /dev/null +++ b/KCD/HMJSONTracker.m @@ -0,0 +1,50 @@ +// +// HMJSONTracker.m +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import "HMJSONTracker.h" + +#import "HMQueue.h" +#import "HMJSONCommand.h" + + +@interface HMJSONTracker () +@property (retain) HMQueue *queue; + +@property (retain) HMJSONReciever *reciever; + +@end + +@implementation HMJSONTracker + +- (id)init +{ + self = [super init]; + if(self) { + _queue = [HMQueue new]; + _reciever = [HMJSONReciever new]; + self.reciever.queueu = self.queue; + [self start]; + } + return self; +} + +- (void)start +{ + dispatch_queue_t queue = dispatch_queue_create("HMJSONTracker", 0); + dispatch_async(queue, ^{ + while(YES) { + NSDictionary *item = [self.queue dequeue]; + HMJSONCommand *command = [HMJSONCommand commandForAPI:[item objectForKey:@"api"]]; + command.argumentsString = [item objectForKey:@"arg"]; + [command doCommand:[item objectForKey:@"json"]]; + + } + }); +} + +@end diff --git a/KCD/HMQueue.h b/KCD/HMQueue.h new file mode 100644 index 00000000..d0edc86d --- /dev/null +++ b/KCD/HMQueue.h @@ -0,0 +1,16 @@ +// +// HMQueue.h +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import + +@interface HMQueue : NSObject + +- (id)dequeue; +- (void)enqueue:(id)object; + +@end diff --git a/KCD/HMQueue.m b/KCD/HMQueue.m new file mode 100644 index 00000000..87171de4 --- /dev/null +++ b/KCD/HMQueue.m @@ -0,0 +1,59 @@ +// +// HMQueue.m +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import "HMQueue.h" + +@interface HMQueue () +@property (retain) NSMutableArray *contents; +@property (retain) NSCondition *lock; +@end + +@implementation HMQueue + +- (id)init +{ + self = [super init]; + if(self) { + _contents = [NSMutableArray array]; + _lock = [NSCondition new]; + } + return self; +} + +- (id)dequeue +{ + id object = nil; + + [self.lock lock]; + @try { + while([self.contents count] == 0) { + [self.lock wait]; + } + object = [self.contents lastObject]; + [self.contents removeLastObject]; + } + @finally { + [self.lock unlock]; + } + + return object; +} + +- (void)enqueue:(id)object +{ + [self.lock lock]; + @try { + [self.contents addObject:object]; + [self.lock broadcast]; + } + @finally { + [self.lock unlock]; + } +} + +@end