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