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