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