-
Notifications
You must be signed in to change notification settings - Fork 0
/
XHRBridge.m
210 lines (179 loc) · 5.65 KB
/
XHRBridge.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#ifdef USE_TI_UIWEBVIEW
#import "TiBase.h"
#import "XHRBridge.h"
#import "TiHost.h"
#import "TiProxy.h"
#import "SBJSON.h"
#import "TiModule.h"
#import "Mimetypes.h"
static XHRBridge *xhrBridge = nil;
@implementation AppProtocolHandler
+ (NSString*) specialProtocolScheme
{
return @"app";
}
+ (void) registerSpecialProtocol
{
static BOOL inited = NO;
if ( ! inited )
{
[NSURLProtocol registerClass:[AppProtocolHandler class]];
inited = YES;
}
}
+ (BOOL)canInitWithRequest:(NSURLRequest *)theRequest
{
NSString *theScheme = [[theRequest URL] scheme];
return [theScheme isEqual:[self specialProtocolScheme]];
}
+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b;
{
return [super requestIsCacheEquivalent:a toRequest:b];
}
+(NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
return request;
}
-(void)handleAppToTiRequest
{
id<NSURLProtocolClient> client = [self client];
NSURLRequest *request = [self request];
NSURL *url = [request URL];
NSArray *parts = [[[url path] substringFromIndex:1] componentsSeparatedByString:@"/"];
NSString *pageToken = [[parts objectAtIndex:0] stringByReplacingOccurrencesOfString:@"_TiA0_" withString:@""];
NSString *module = [parts objectAtIndex:1];
NSString *method = [parts objectAtIndex:2];
NSString *prearg = [url query];
NSString *arguments = prearg==nil ? @"" : [prearg stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
SBJSON *decoder = [[[SBJSON alloc] init] autorelease];
NSError *error = nil;
NSDictionary *event = [decoder fragmentWithString:arguments error:&error];
id<TiEvaluator> context = [[xhrBridge host] contextForToken:pageToken];
TiModule *tiModule = (TiModule*)[[xhrBridge host] moduleNamed:module context:context];
[tiModule setExecutionContext:context];
BOOL executed = YES;
NSString *name = [event objectForKey:@"name"];
if ([method isEqualToString:@"fireEvent"])
{
[tiModule fireEvent:name withObject:[event objectForKey:@"event"]];
}
else if ([method isEqualToString:@"addEventListener"])
{
id listenerid = [event objectForKey:@"id"];
[tiModule addEventListener:[NSArray arrayWithObjects:name,listenerid,nil]];
}
else if ([method isEqualToString:@"removeEventListener"])
{
id listenerid = [event objectForKey:@"id"];
[tiModule removeEventListener:[NSArray arrayWithObjects:name,listenerid,nil]];
}
else if ([method isEqualToString:@"log"])
{
NSString *level = [event objectForKey:@"level"];
NSString *message = [event objectForKey:@"message"];
[tiModule performSelector:@selector(log:) withObject:[NSArray arrayWithObjects:level,message,nil]];
}
else
{
executed = NO;
}
NSData *data = executed ? [NSData data] : nil;
if (data!=nil)
{
NSURLCacheStoragePolicy caching = NSURLCacheStorageNotAllowed;
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:url MIMEType:@"text/plain" expectedContentLength:[data length] textEncodingName:@"utf-8"];
[client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:caching];
[client URLProtocol:self didLoadData:data];
[client URLProtocolDidFinishLoading:self];
[response release];
}
else
{
NSLog(@"[ERROR] Error loading %@",url);
[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
[client URLProtocolDidFinishLoading:self];
}
}
- (void)startLoading
{
id<NSURLProtocolClient> client = [self client];
NSURLRequest *request = [self request];
NSURL *url = [request URL];
// check to see if this is a bridge request through a webview
if ([[url path] hasPrefix:@"/_TiA0_"])
{
[self handleAppToTiRequest];
return;
}
#ifdef DEBUG
NSLog(@"[DEBUG] app protocol, loading: %@",url);
#endif
// see if it's a compiled resource
NSData *data = [TiUtils loadAppResource:url];
if (data==nil)
{
// check to see if it's a local resource in the bundle, could be
// a bundled image, etc. - or we could be running from XCode :)
NSString *urlpath = [url path];
if ([urlpath characterAtIndex:0]=='/')
{
if ([[NSFileManager defaultManager] fileExistsAtPath:urlpath])
{
data = [[[NSData alloc] initWithContentsOfFile:urlpath] autorelease];
}
}
if (data==nil)
{
NSString *resourceurl = [TiHost resourcePath];
NSString *path = [NSString stringWithFormat:@"%@%@",resourceurl,urlpath];
data = [[[NSData alloc] initWithContentsOfFile:path] autorelease];
}
}
if (data!=nil)
{
NSURLCacheStoragePolicy caching = NSURLCacheStorageAllowedInMemoryOnly;
NSString *mime = [Mimetypes mimeTypeForExtension:[url path]];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:url MIMEType:mime expectedContentLength:[data length] textEncodingName:@"utf-8"];
[client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:caching];
[client URLProtocol:self didLoadData:data];
[client URLProtocolDidFinishLoading:self];
[response release];
}
else
{
NSLog(@"[ERROR] Error loading %@",url);
[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
[client URLProtocolDidFinishLoading:self];
}
}
- (void)stopLoading
{
}
@end
@implementation XHRBridge
-(id)init
{
if (self = [super init])
{
xhrBridge = self;
}
return self;
}
-(void)boot:(id)callback url:(NSURL*)url preload:(NSDictionary*)preload
{
[AppProtocolHandler registerSpecialProtocol];
}
-(void)shutdown
{
}
-(void)gc
{
}
@end
#endif