]> git.saurik.com Git - cydget.git/blob - LockScreen.mm
56999ca36c636d8f9d1ccb5b816c7c12e1737fd2
[cydget.git] / LockScreen.mm
1 /* Cydget - open-source AwayView plugin multiplexer
2 * Copyright (C) 2009-2011 Jay Freeman (saurik)
3 */
4
5 /*
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
17 * distribution.
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <substrate.h>
39 #include <sys/sysctl.h>
40
41 #import <GraphicsServices/GraphicsServices.h>
42 #import <UIKit/UIKit.h>
43 #import <AddressBook/AddressBook.h>
44
45 #import <SpringBoard/SBStatusBarController.h>
46 #import <SpringBoardUI/SBAwayViewPluginController.h>
47 #import <TelephonyUI/TPBottomLockBar.h>
48
49 #import <QuartzCore/CALayer.h>
50 // XXX: fix the minimum requirement
51 extern NSString * const kCAFilterNearest;
52
53 #include <WebKit/DOMCSSPrimitiveValue.h>
54 #include <WebKit/DOMCSSStyleDeclaration.h>
55 #include <WebKit/DOMDocument.h>
56 #include <WebKit/DOMHTMLBodyElement.h>
57 #include <WebKit/DOMNodeList.h>
58 #include <WebKit/DOMRGBColor.h>
59
60 #include <WebKit/WebFrame.h>
61 #include <WebKit/WebPolicyDelegate.h>
62 #include <WebKit/WebPreferences.h>
63 #include <WebKit/WebScriptObject.h>
64
65 #import <WebKit/WebView.h>
66 #import <WebKit/WebView-WebPrivate.h>
67
68 #include <WebCore/Page.h>
69 #include <WebCore/Settings.h>
70
71 #include <WebCore/WebCoreThread.h>
72 #include <WebKit/WebPreferences-WebPrivate.h>
73
74 #include "JSGlobalData.h"
75
76 #include "SourceCode.h"
77
78 #include <apr-1/apr_pools.h>
79 #include <pcre.h>
80
81 #define _transient
82 #define _forever for (;;)
83
84 _disused static unsigned trace_;
85
86 #define _trace() do { \
87 NSLog(@"_trace(%u)@%s:%u[%s](%p)\n", \
88 trace_++, __FILE__, __LINE__, __FUNCTION__, pthread_self() \
89 ); \
90 } while (false)
91
92 #define _assert(test) do \
93 if (!(test)) { \
94 fprintf(stderr, "_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \
95 exit(-1); \
96 } \
97 while (false)
98
99 #define _syscall(expr) \
100 do if ((long) (expr) != -1) \
101 break; \
102 else switch (errno) { \
103 case EINTR: \
104 continue; \
105 default: \
106 _assert(false); \
107 } while (true)
108
109 @protocol CydgetController
110 - (NSDictionary *) currentConfiguration;
111 @end
112
113 static Class $CydgetController(objc_getClass("CydgetController"));
114
115 static bool iOS32, iOS4;
116
117 @interface NSString (UIKit)
118 - (NSString *) stringByAddingPercentEscapes;
119 @end
120
121 @implementation UIWebDocumentView (WebCycript)
122
123 - (void) _setScrollerOffset:(CGPoint)offset {
124 UIScroller *scroller([self _scroller]);
125
126 CGSize size([scroller contentSize]);
127 CGSize bounds([scroller bounds].size);
128
129 CGPoint max;
130 max.x = size.width - bounds.width;
131 max.y = size.height - bounds.height;
132
133 // wtf Apple?!
134 if (max.x < 0)
135 max.x = 0;
136 if (max.y < 0)
137 max.y = 0;
138
139 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
140 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
141
142 [scroller setOffset:offset];
143 }
144
145 @end
146
147 /* Perl-Compatible RegEx {{{ */
148 class Pcre {
149 private:
150 pcre *code_;
151 pcre_extra *study_;
152 int capture_;
153 int *matches_;
154 const char *data_;
155
156 public:
157 Pcre(const char *regex, int options = 0) :
158 study_(NULL)
159 {
160 const char *error;
161 int offset;
162 code_ = pcre_compile(regex, options, &error, &offset, NULL);
163
164 if (code_ == NULL)
165 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** Pcre(,): [%u] %s", offset, error] userInfo:nil];
166
167 pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
168 matches_ = new int[(capture_ + 1) * 3];
169 }
170
171 ~Pcre() {
172 pcre_free(code_);
173 delete matches_;
174 }
175
176 NSString *operator [](size_t match) {
177 return [[[NSString alloc] initWithBytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2]) encoding:NSUTF8StringEncoding] autorelease];
178 }
179
180 bool operator ()(NSString *data) {
181 // XXX: length is for characters, not for bytes
182 return operator ()([data UTF8String], [data length]);
183 }
184
185 bool operator ()(const char *data, size_t size) {
186 data_ = data;
187 return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
188 }
189 };
190 /* }}} */
191
192 static float CYScrollViewDecelerationRateNormal;
193
194 @interface UIScrollView (Apple)
195 - (void) setDecelerationRate:(float)value;
196 - (void) setScrollingEnabled:(BOOL)enabled;
197 @end
198
199 @interface UIWebView (Apple)
200 - (void) setDataDetectorTypes:(int)types;
201 - (UIScrollView *) _scrollView;
202 - (UIScroller *) _scroller;
203 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message;
204 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
205 @end
206
207 @interface WebView (Apple)
208 - (void) _setLayoutInterval:(float)interval;
209 - (void) _setAllowsMessaging:(BOOL)allows;
210 - (void) setShouldUpdateWhileOffscreen:(BOOL)update;
211 @end
212
213 @protocol CydgetWebViewDelegate //<UIWebViewDelegate>
214 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
215 @end
216
217 @class UIWebViewWebViewDelegate;
218
219 @interface CydgetWebView : UIWebView {
220 }
221
222 @end
223
224 @implementation CydgetWebView
225
226 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
227 [listener use];
228 }
229
230 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
231 NSObject<CydgetWebViewDelegate> *delegate([self delegate]);
232 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
233 [delegate webView:view didClearWindowObject:window forFrame:frame];
234 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
235 [super webView:view didClearWindowObject:window forFrame:frame];
236 }
237
238 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
239 NSLog(@"addMessageToConsole:%@", message);
240 }
241
242 @end
243
244 @interface WebCydgetLockScreenView : UIView {
245 CydgetWebView *webview_;
246 UIScrollView *scroller_;
247 NSString *cycript_;
248 }
249
250 @end
251
252 @implementation WebCydgetLockScreenView
253
254 //#include "UICaboodle/UCInternal.h"
255
256 - (void) dealloc {
257 [webview_ setDelegate:nil];
258 [webview_ release];
259 [super dealloc];
260 }
261
262 - (void) loadRequest:(NSURLRequest *)request {
263 [webview_ loadRequest:request];
264 }
265
266 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
267 [self loadRequest:[NSURLRequest
268 requestWithURL:url
269 cachePolicy:policy
270 timeoutInterval:30.0
271 ]];
272 }
273
274 - (void) loadURL:(NSURL *)url {
275 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
276 }
277
278 - (id) init {
279 CGRect frame = {{0, 0}, {320, 480}};
280 frame.size.height -= 20; //[[[$SBStatusBarController sharedStatusBarController] statusBarView] frame].size.height;
281
282 if ((self = [super initWithFrame:frame]) != nil) {
283 CGRect bounds([self bounds]);
284 bounds.size.height -= [TPBottomLockBar defaultHeight];
285
286 webview_ = [[CydgetWebView alloc] initWithFrame:bounds];
287 [webview_ setDelegate:self];
288 [self addSubview:webview_];
289
290 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
291 [webview_ setDataDetectorTypes:0x80000000];
292 else
293 [webview_ setDetectsPhoneNumbers:NO];
294
295 [webview_ setScalesPageToFit:YES];
296
297 UIWebDocumentView *document([webview_ _documentView]);
298 WebView *webview([document webView]);
299 WebPreferences *preferences([webview preferences]);
300
301 [document setTileSize:CGSizeMake(bounds.size.width, 500)];
302
303 [document setBackgroundColor:[UIColor blackColor]];
304 [document setDrawsBackground:NO];
305
306 [webview setPreferencesIdentifier:@"WebCycript"];
307
308 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
309 [webview _setLayoutInterval:0];
310 else
311 [preferences _setLayoutInterval:0];
312
313 [preferences setCacheModel:WebCacheModelDocumentViewer];
314 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
315 [preferences setOfflineWebApplicationCacheEnabled:YES];
316
317 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
318 [webview setShouldUpdateWhileOffscreen:NO];
319
320 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
321 [document setAllowsMessaging:YES];
322 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
323 [webview _setAllowsMessaging:YES];
324
325 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
326 scroller_ = [webview_ _scrollView];
327
328 [scroller_ setDirectionalLockEnabled:YES];
329 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
330 [scroller_ setDelaysContentTouches:NO];
331
332 [scroller_ setCanCancelContentTouches:YES];
333
334 [scroller_ setAlwaysBounceVertical:NO];
335 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
336 UIScroller *scroller([webview_ _scroller]);
337 scroller_ = (UIScrollView *) scroller;
338
339 [scroller setDirectionalScrolling:YES];
340 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
341 [scroller setScrollHysteresis:0]; /* 8 */
342
343 [scroller setThumbDetectionEnabled:NO];
344 }
345
346 [scroller_ setFixedBackgroundPattern:YES];
347 [scroller_ setBackgroundColor:[UIColor blackColor]];
348 [scroller_ setClipsSubviews:NO];
349
350 [scroller_ setBounces:YES];
351 [scroller_ setShowBackgroundShadow:NO]; /* YES */
352
353 [self setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
354 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
355
356 NSDictionary *configuration([$CydgetController currentConfiguration]);
357
358 cycript_ = [configuration objectForKey:@"CycriptURLs"];
359
360 [scroller_ setScrollingEnabled:[[configuration objectForKey:@"Scrollable"] boolValue]];
361
362 NSString *homepage([configuration objectForKey:@"Homepage"]);
363 [self loadURL:[NSURL URLWithString:homepage]];
364 } return self;
365 }
366
367 - (void) webView:(WebView *)webview didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
368 if (cycript_ != nil)
369 if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
370 if (Pcre([cycript_ UTF8String], 0 /*XXX:PCRE_UTF8*/)(href))
371 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
372 if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
373 WebFrame *frame([webview mainFrame]);
374 JSGlobalContextRef context([frame globalContext]);
375 CYSetupContext(context);
376 }
377 }
378
379 @end
380
381 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
382 }
383
384 @end
385
386 #include <string>
387
388 struct State {
389 unsigned state;
390 };
391
392 // String Helpers {{{
393 static const UChar *(*_ZNK7WebCore6String10charactersEv)(const WebCore::String *);
394 static const UChar *(*_ZN7WebCore6String29charactersWithNullTerminationEv)(const WebCore::String *);
395 static unsigned (*_ZNK7WebCore6String6lengthEv)(const WebCore::String *);
396
397 static bool StringGet(const WebCore::String &string, const UChar *&data, size_t &length) {
398 bool terminated;
399
400 if (_ZNK7WebCore6String10charactersEv != NULL) {
401 data = (*_ZNK7WebCore6String10charactersEv)(&string);
402 terminated = false;
403 } else if (_ZN7WebCore6String29charactersWithNullTerminationEv != NULL) {
404 data = (*_ZN7WebCore6String29charactersWithNullTerminationEv)(&string);
405 terminated = true;
406 } else return false;
407
408 if (_ZNK7WebCore6String6lengthEv != NULL)
409 length = (*_ZNK7WebCore6String6lengthEv)(&string);
410 else if (terminated)
411 for (length = 0; data[length] != 0; ++length);
412 else return false;
413
414 return true;
415 }
416
417 static bool StringEquals(const WebCore::String &string, const char *value) {
418 const UChar *data;
419 size_t size;
420 if (!StringGet(string, data, size))
421 return false;
422
423 size_t length(strlen(value));
424 if (size != length)
425 return false;
426
427 for (size_t index(0); index != length; ++index)
428 if (data[index] != value[index])
429 return false;
430
431 return true;
432 }
433 // }}}
434 // State Machine {{{
435 static bool cycript_;
436
437 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
438 if (!StringEquals(mime, "text/cycript")) {
439 cycript_ = false;
440 return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
441 }
442
443 static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
444 if (handle == NULL)
445 return false;
446
447 cycript_ = true;
448 return true;
449 }
450 // }}}
451 // Script Compiler {{{
452 static void Log(const WebCore::String &string) {
453 #if 0
454 const UChar *data;
455 size_t length;
456 if (!StringGet(string, data, length))
457 return;
458
459 UChar terminated[length + 1];
460 terminated[length] = 0;
461 memcpy(terminated, data, length * 2);
462 NSLog(@"wtf %p:%zu:%S:", &string, length, terminated);
463 #endif
464 }
465
466 static void Cycriptify(apr_pool_t *pool, const uint16_t *&data, size_t &size) {
467 cycript_ = false;
468
469 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
470 if (void (*CydgetPoolParse)(apr_pool_t *, const uint16_t **, size_t *) = reinterpret_cast<void (*)(apr_pool_t *, const uint16_t **, size_t *)>(dlsym(handle, "CydgetPoolParse")))
471 CydgetPoolParse(pool, &data, &size);
472 }
473
474 static void (*_ZN7WebCore6String6appendEPKtj)(WebCore::String *, const UChar *, unsigned);
475 static void (*_ZN7WebCore6String8truncateEj)(WebCore::String *, unsigned);
476
477 static void Cycriptify(const WebCore::String &source, int *psize = NULL) {
478 if (!cycript_)
479 return;
480
481 const UChar *data;
482 size_t length;
483
484 if (!StringGet(source, data, length)) {
485 return;
486 }
487
488 size_t size(length);
489
490 apr_pool_t *pool;
491 apr_pool_create(&pool, NULL);
492
493 Cycriptify(pool, data, size);
494
495 WebCore::String &script(const_cast<WebCore::String &>(source));
496
497 _ZN7WebCore6String8truncateEj(&script, 0);
498 _ZN7WebCore6String6appendEPKtj(&script, data, size);
499
500 if (psize != NULL)
501 *psize = size;
502
503 apr_pool_destroy(pool);
504
505 Log(source);
506 }
507 // }}}
508
509 extern "C" void *_ZN3JSC7UString3Rep14nullBaseStringE __attribute__((__weak_import__));
510 extern "C" void *_ZN3JSC7UString3Rep7destroyEv __attribute__((__weak_import__));
511 extern "C" void *_ZN3JSC7UStringC1EPKti __attribute__((__weak_import__));
512 extern "C" void *_ZN3JSC7UStringC1EPKc __attribute__((__weak_import__));
513 extern "C" void *_ZNK3JSC7UString6substrEii __attribute__((__weak_import__));
514 extern "C" void *_ZN3WTF10fastMallocEm __attribute__((__weak_import__));
515 extern "C" void WTFReportAssertionFailure(const char *, int, const char *, const char *) __attribute__((__weak_import__));
516 extern "C" void *_ZN3WTF8fastFreeEPv __attribute__((__weak_import__));
517
518 bool CYWeakHell() {
519 return
520 &_ZN3JSC7UString3Rep14nullBaseStringE == NULL ||
521 &_ZN3JSC7UString3Rep7destroyEv == NULL ||
522 &_ZN3JSC7UStringC1EPKti == NULL ||
523 &_ZN3JSC7UStringC1EPKc == NULL ||
524 &_ZNK3JSC7UString6substrEii == NULL ||
525 &_ZN3WTF10fastMallocEm == NULL ||
526 &WTFReportAssertionFailure == NULL ||
527 &_ZN3WTF8fastFreeEPv == NULL ||
528 false;
529 }
530
531 static WebCore::String *string;
532
533 // iOS 2.x
534 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, void *_this, const WebCore::String &string, State state, const WebCore::String &url, int line) {
535 Cycriptify(string);
536 return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i(_this, string, state, url, line);
537 }
538
539 // iOS 3.x
540 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
541 if (cycript_) {
542 JSC::SourceCode *source(_this[iOS32 ? 6 : 0]);
543 const uint16_t *data(source->data());
544 size_t size(source->length());
545
546 apr_pool_t *pool;
547 apr_pool_create(&pool, NULL);
548
549 Cycriptify(pool, data, size);
550 source->~SourceCode();
551 // XXX: I actually don't have the original URL here: pants
552 new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
553
554 apr_pool_destroy(pool);
555
556 }
557
558 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
559 }
560
561 // iOS 4.x cdata
562 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, void *_this, const WebCore::String &source, const WebCore::KURL &url, int line) {
563 Cycriptify(source);
564 return __ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi(_this, source, url, line);
565 }
566
567 // iOS 4.x @src=
568 MSHook(const WebCore::String &, _ZN7WebCore12CachedScript6scriptEv, void *_this) {
569 const WebCore::String &script(__ZN7WebCore12CachedScript6scriptEv(_this));
570 string = const_cast<WebCore::String *>(&script);
571 Log(script);
572 return script;
573 }
574
575 // iOS 4.x @src=
576 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, void *_this, void *source, State state) {
577 if (string != NULL) {
578 if (iOS4)
579 Cycriptify(*string, reinterpret_cast<int *>(source) + 3);
580 else
581 Cycriptify(*string);
582 }
583 string = NULL;
584 return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE(_this, source, state);
585 }
586
587 /* Cydget:// Protocol {{{ */
588 @interface CydgetURLProtocol : NSURLProtocol {
589 }
590
591 @end
592
593 @implementation CydgetURLProtocol
594
595 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
596 NSURL *url([request URL]);
597 if (url == nil)
598 return NO;
599 NSString *scheme([[url scheme] lowercaseString]);
600 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
601 return NO;
602 return YES;
603 }
604
605 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
606 return request;
607 }
608
609 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
610 id<NSURLProtocolClient> client([self client]);
611 if (icon == nil)
612 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
613 else {
614 NSData *data(UIImagePNGRepresentation(icon));
615
616 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
617 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
618 [client URLProtocol:self didLoadData:data];
619 [client URLProtocolDidFinishLoading:self];
620 }
621 }
622
623 - (void) startLoading {
624 id<NSURLProtocolClient> client([self client]);
625 NSURLRequest *request([self request]);
626
627 NSURL *url([request URL]);
628 NSString *href([url absoluteString]);
629
630 NSString *path([href substringFromIndex:9]);
631 NSRange slash([path rangeOfString:@"/"]);
632
633 NSString *command;
634 if (slash.location == NSNotFound) {
635 command = path;
636 path = nil;
637 } else {
638 command = [path substringToIndex:slash.location];
639 path = [path substringFromIndex:(slash.location + 1)];
640 }
641
642 if ([command isEqualToString:@"_UIImageWithName"]) {
643 if (path == nil)
644 goto fail;
645 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
646 UIImage *icon(_UIImageWithName(path));
647 [self _returnPNGWithImage:icon forRequest:request];
648 } else fail: {
649 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
650 }
651 }
652
653 - (void) stopLoading {
654 }
655
656 @end
657 /* }}} */
658 /* Cydget-CGI:// Protocol {{{ */
659 @interface CydgetCGIURLProtocol : NSURLProtocol {
660 pid_t pid_;
661 CFHTTPMessageRef http_;
662 NSFileHandle *handle_;
663 }
664
665 @end
666
667 @implementation CydgetCGIURLProtocol
668
669 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
670 NSURL *url([request URL]);
671 if (url == nil)
672 return NO;
673 NSString *scheme([[url scheme] lowercaseString]);
674 if (scheme == nil || ![scheme isEqualToString:@"cydget-cgi"])
675 return NO;
676 return YES;
677 }
678
679 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
680 return request;
681 }
682
683 - (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client {
684 if ((self = [super initWithRequest:request cachedResponse:response client:client]) != nil) {
685 pid_ = -1;
686 } return self;
687 }
688
689 - (void) startLoading {
690 id<NSURLProtocolClient> client([self client]);
691 NSURLRequest *request([self request]);
692 NSURL *url([request URL]);
693
694 NSString *path([url path]);
695 if (path == nil) {
696 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
697 return;
698 }
699
700 NSFileManager *manager([NSFileManager defaultManager]);
701 if (![manager fileExistsAtPath:path]) {
702 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
703 return;
704 }
705
706 int fds[2];
707 _assert(pipe(fds) != -1);
708
709 _assert(pid_ == -1);
710 pid_ = fork();
711 if (pid_ == -1) {
712 _assert(close(fds[0]) != -1);
713 _assert(close(fds[1]) != -1);
714 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
715 return;
716 }
717
718 if (pid_ == 0) {
719 const char *script([path UTF8String]);
720
721 setenv("GATEWAY_INTERFACE", "CGI/1.1", true);
722 setenv("SCRIPT_FILENAME", script, true);
723 NSString *query([url query]);
724 if (query != nil)
725 setenv("QUERY_STRING", [query UTF8String], true);
726
727 _assert(dup2(fds[1], 1) != -1);
728 _assert(close(fds[0]) != -1);
729 _assert(close(fds[1]) != -1);
730
731 execl(script, script, NULL);
732 exit(1);
733 _assert(false);
734 }
735
736 _assert(close(fds[1]) != -1);
737
738 _assert(http_ == NULL);
739 http_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
740 CFHTTPMessageAppendBytes(http_, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17);
741
742 _assert(handle_ == nil);
743 handle_ = [[NSFileHandle alloc] initWithFileDescriptor:fds[0] closeOnDealloc:YES];
744
745 [[NSNotificationCenter defaultCenter]
746 addObserver:self
747 selector:@selector(onRead:)
748 name:@"NSFileHandleReadCompletionNotification"
749 object:handle_
750 ];
751
752 [handle_ readInBackgroundAndNotify];
753 }
754
755 - (void) onRead:(NSNotification *)notification {
756 NSFileHandle *handle([notification object]);
757
758 NSData *data([[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]);
759
760 if (size_t length = [data length]) {
761 CFHTTPMessageAppendBytes(http_, reinterpret_cast<const UInt8 *>([data bytes]), length);
762 [handle readInBackgroundAndNotify];
763 } else {
764 id<NSURLProtocolClient> client([self client]);
765
766 CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http_, CFSTR("Content-type")));
767 if (mime == NULL)
768 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]];
769 else {
770 NSURLRequest *request([self request]);
771
772 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]);
773 CFRelease(mime);
774
775 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
776
777 CFDataRef body(CFHTTPMessageCopyBody(http_));
778 [client URLProtocol:self didLoadData:(NSData *)body];
779 CFRelease(body);
780
781 [client URLProtocolDidFinishLoading:self];
782 }
783
784 CFRelease(http_);
785 http_ = NULL;
786 }
787 }
788
789 //[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]];
790
791 - (void) stopLoading_ {
792 [[NSNotificationCenter defaultCenter] removeObserver:self];
793
794 if (handle_ != nil) {
795 [handle_ release];
796 handle_ = nil;
797 }
798
799 if (pid_ != -1) {
800 kill(pid_, SIGTERM);
801 int status;
802 _syscall(waitpid(pid_, &status, 0));
803 pid_ = -1;
804 }
805 }
806
807 - (void) stopLoading {
808 [self
809 performSelectorOnMainThread:@selector(stopLoading_)
810 withObject:nil
811 waitUntilDone:NO
812 ];
813 }
814
815 @end
816 /* }}} */
817
818 template <typename Type_>
819 static void nlset(Type_ &function, struct nlist *nl, size_t index) {
820 struct nlist &name(nl[index]);
821 uintptr_t value(name.n_value);
822 if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
823 value |= 0x00000001;
824 function = reinterpret_cast<Type_>(value);
825 }
826
827 template <typename Type_>
828 static void dlset(Type_ &function, const char *name) {
829 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
830 }
831
832 template <typename Type_>
833 static void msset_(Type_ &function, const char *name, MSImageRef handle) {
834 function = reinterpret_cast<Type_>(MSFindSymbol(handle, name));
835 }
836
837 #define msset(function, handle) \
838 msset_(function, "_" #function, handle)
839
840 @implementation WebCycriptLockScreenController
841
842 static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
843 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
844 if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
845 [uiWebView webView:view addMessageToConsole:message];
846 }
847
848 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
849 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
850 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
851 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
852 }
853
854 + (void) initialize {
855 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
856 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
857 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
858 }
859
860 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
861 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
862 else // XXX: this actually might be fast on some older systems: we should look into this
863 CYScrollViewDecelerationRateNormal = 0.998;
864
865 iOS4 = kCFCoreFoundationVersionNumber >= 550.32;
866 iOS32 = !iOS4 && kCFCoreFoundationVersionNumber >= 478.61;
867
868 int maxproc;
869 size_t size(sizeof(maxproc));
870 if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1)
871 NSLog(@"sysctlbyname(\"kern.maxproc\", ?)");
872 else if (maxproc < 72) {
873 maxproc = 72;
874 if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1)
875 NSLog(@"sysctlbyname(\"kern.maxproc\", #)");
876 }
877
878 apr_initialize();
879
880 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
881 [NSURLProtocol registerClass:[CydgetCGIURLProtocol class]];
882
883 if (!iOS4) {
884 void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
885 dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
886 if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
887 MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
888 }
889
890 struct nlist nl[9];
891 memset(nl, 0, sizeof(nl));
892
893 nl[0].n_un.n_name = (char *) "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE";
894
895 nl[1].n_un.n_name = (char *) "__ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi";
896
897 nl[2].n_un.n_name = (char *) "__ZN7WebCore12CachedScript6scriptEv";
898 nl[3].n_un.n_name = (char *) "__ZNK7WebCore20StringSourceProvider6sourceEv";
899
900 nl[4].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i";
901 nl[5].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE";
902
903 nl[6].n_un.n_name = (char *) "__ZN7WebCore6String6appendEPKtj";
904 nl[7].n_un.n_name = (char *) "__ZN7WebCore6String8truncateEj";
905
906 nlist("/System/Library/PrivateFrameworks/WebCore.framework/WebCore", nl);
907
908 bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &);
909 nlset(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, nl, 0);
910 if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE != NULL)
911 MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
912
913 void (*_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi)(void *, const WebCore::String &, const WebCore::KURL &, int);
914 nlset(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, nl, 1);
915 if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi != NULL)
916 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi));
917
918 if (iOS4) {
919 const WebCore::String &(*_ZN7WebCore12CachedScript6scriptEv)(void *);
920 nlset(_ZN7WebCore12CachedScript6scriptEv, nl, 2);
921 if (_ZN7WebCore12CachedScript6scriptEv != NULL)
922 MSHookFunction(_ZN7WebCore12CachedScript6scriptEv, MSHake(_ZN7WebCore12CachedScript6scriptEv));
923 }
924
925 State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i)(void *, const WebCore::String &, State, const WebCore::String &, int);
926 nlset(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, nl, 4);
927 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i != NULL)
928 MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i));
929
930 if (iOS4) {
931 State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE)(void *, void *, State);
932 nlset(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, nl, 5);
933 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE != NULL)
934 MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE));
935 }
936
937 nlset(_ZN7WebCore6String6appendEPKtj, nl, 6);
938 nlset(_ZN7WebCore6String8truncateEj, nl, 7);
939
940 MSImageRef JavaScriptCore(MSGetImageByName("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore"));
941 MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore"));
942
943 if (_ZN7WebCore6String6appendEPKtj == NULL)
944 msset(_ZN7WebCore6String6appendEPKtj, JavaScriptCore);
945
946 if (_ZN7WebCore6String8truncateEj == NULL)
947 msset(_ZN7WebCore6String8truncateEj, JavaScriptCore);
948
949 msset(_ZNK7WebCore6String10charactersEv, WebCore);
950 msset(_ZN7WebCore6String29charactersWithNullTerminationEv, JavaScriptCore);
951 msset(_ZNK7WebCore6String6lengthEv, WebCore);
952 }
953
954 + (id) rootViewController {
955 return [[[self alloc] init] autorelease];
956 }
957
958 - (void) loadView {
959 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];
960 }
961
962 @end
963
964 MSClassHook(WebView)
965 MSMetaClassHook(WebView)
966
967 MSClassMessageHook0(void, WebView, enableWebThread) {
968 NSLog(@"-[WebView enableWebThread]");
969 }