]> git.saurik.com Git - cydget.git/blob - LockScreen.mm
Finished implementing text/cycript.
[cydget.git] / LockScreen.mm
1 /* CydgetScript - open-source IntelliDial replacement
2 * Copyright (C) 2009 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
40 #import <GraphicsServices/GraphicsServices.h>
41 #import <UIKit/UIKit.h>
42 #import <AddressBook/AddressBook.h>
43
44 #import <SpringBoardUI/SBAwayViewPluginController.h>
45 #import <TelephonyUI/TPBottomLockBar.h>
46
47 #import <QuartzCore/CALayer.h>
48 // XXX: fix the minimum requirement
49 extern NSString * const kCAFilterNearest;
50
51 #include <WebKit/DOMCSSPrimitiveValue.h>
52 #include <WebKit/DOMCSSStyleDeclaration.h>
53 #include <WebKit/DOMDocument.h>
54 #include <WebKit/DOMHTMLBodyElement.h>
55 #include <WebKit/DOMNodeList.h>
56 #include <WebKit/DOMRGBColor.h>
57
58 #include <WebKit/WebFrame.h>
59 #include <WebKit/WebPolicyDelegate.h>
60 #include <WebKit/WebPreferences.h>
61 #include <WebKit/WebScriptObject.h>
62
63 #import <WebKit/WebView.h>
64 #import <WebKit/WebView-WebPrivate.h>
65
66 #include <WebCore/Page.h>
67 #include <WebCore/Settings.h>
68
69 #include <WebCore/WebCoreThread.h>
70 #include <WebKit/WebPreferences-WebPrivate.h>
71
72 #include "JSGlobalData.h"
73 #include "SourceCode.h"
74
75 #include <apr-1/apr_pools.h>
76
77 @interface WebView (UICaboodle)
78 - (void) setScriptDebugDelegate:(id)delegate;
79 - (void) _setFormDelegate:(id)delegate;
80 - (void) _setUIKitDelegate:(id)delegate;
81 - (void) setWebMailDelegate:(id)delegate;
82 - (void) _setLayoutInterval:(float)interval;
83 @end
84
85 #define _transient
86 #define _forever for (;;)
87
88 _disused static unsigned trace_;
89
90 #define _trace() do { \
91 NSLog(@"_trace(%u)@%s:%u[%s]\n", \
92 trace_++, __FILE__, __LINE__, __FUNCTION__\
93 ); \
94 } while (false)
95
96 @protocol CydgetController
97 - (NSDictionary *) currentConfiguration;
98 @end
99
100 static Class $CydgetController(objc_getClass("CydgetController"));
101
102 @interface NSString (UIKit)
103 - (NSString *) stringByAddingPercentEscapes;
104 @end
105
106 @implementation UIWebDocumentView (WebCycript)
107
108 - (void) _setScrollerOffset:(CGPoint)offset {
109 UIScroller *scroller([self _scroller]);
110
111 CGSize size([scroller contentSize]);
112 CGSize bounds([scroller bounds].size);
113
114 CGPoint max;
115 max.x = size.width - bounds.width;
116 max.y = size.height - bounds.height;
117
118 // wtf Apple?!
119 if (max.x < 0)
120 max.x = 0;
121 if (max.y < 0)
122 max.y = 0;
123
124 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
125 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
126
127 [scroller setOffset:offset];
128 }
129
130 @end
131
132 /* WebCycript Delegate {{{ */
133 @interface WebCycriptDelegate : NSObject {
134 _transient volatile id delegate_;
135 }
136
137 - (void) setDelegate:(id)delegate;
138 - (id) initWithDelegate:(id)delegate;
139 @end
140
141 @implementation WebCycriptDelegate
142
143 - (void) setDelegate:(id)delegate {
144 delegate_ = delegate;
145 }
146
147 - (id) initWithDelegate:(id)delegate {
148 delegate_ = delegate;
149 return self;
150 }
151
152 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
153 if (delegate_ != nil)
154 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
155 }
156
157 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
158 if (delegate_ != nil)
159 return [delegate_ webView:sender didCommitLoadForFrame:frame];
160 }
161
162 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
163 if (delegate_ != nil)
164 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
165 }
166
167 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
168 if (delegate_ != nil)
169 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
170 }
171
172 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
173 if (delegate_ != nil)
174 return [delegate_ webView:sender didFinishLoadForFrame:frame];
175 }
176
177 /*- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
178 if (delegate_ != nil)
179 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
180 }*/
181
182 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
183 if (delegate_ != nil)
184 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
185 }
186
187 /*- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
188 if (delegate_ != nil)
189 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
190 }*/
191
192 /*- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
193 if (delegate_ != nil)
194 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
195 return nil;
196 }*/
197
198 - (IMP) methodForSelector:(SEL)sel {
199 if (IMP method = [super methodForSelector:sel])
200 return method;
201 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
202 return NULL;
203 }
204
205 - (BOOL) respondsToSelector:(SEL)sel {
206 if ([super respondsToSelector:sel])
207 return YES;
208 // XXX: WebThreadCreateNSInvocation returns nil
209 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
210 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
211 }
212
213 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
214 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
215 return method;
216 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
217 if (delegate_ != nil)
218 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
219 return sig;
220 // XXX: I fucking hate Apple so very very bad
221 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
222 }
223
224 - (void) forwardInvocation:(NSInvocation *)inv {
225 SEL sel = [inv selector];
226 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
227 [inv invokeWithTarget:delegate_];
228 }
229
230 @end
231 /* }}} */
232
233 @interface WebCydgetLockScreenView : UIView {
234 WebCycriptDelegate *indirect_;
235 UIProgressIndicator *indicator_;
236 UIScroller *scroller_;
237 UIWebDocumentView *document_;
238
239 bool cycript_;
240 bool scrollable_;
241
242 float width_;
243 CGSize size_;
244 bool editing_;
245
246 NSNumber *confirm_;
247
248 NSMutableSet *loading_;
249 bool error_;
250 bool reloading_;
251 }
252
253 @end
254
255 @implementation WebCydgetLockScreenView
256
257 //#include "UICaboodle/UCInternal.h"
258
259 - (void) dealloc {
260 WebThreadLock();
261
262 WebView *webview([document_ webView]);
263 [webview setFrameLoadDelegate:nil];
264 [webview setResourceLoadDelegate:nil];
265 [webview setUIDelegate:nil];
266 [webview setScriptDebugDelegate:nil];
267 [webview setPolicyDelegate:nil];
268
269 /* XXX: these are set by UIWebDocumentView
270 [webview setDownloadDelegate:nil];
271 [webview _setFormDelegate:nil];
272 [webview _setUIKitDelegate:nil];
273 [webview setEditingDelegate:nil];*/
274
275 /* XXX: no one sets this, ever
276 [webview setWebMailDelegate:nil];*/
277
278 [document_ setDelegate:nil];
279 [document_ setGestureDelegate:nil];
280 [document_ setFormEditingDelegate:nil];
281 [document_ setInteractionDelegate:nil];
282
283 [indirect_ setDelegate:nil];
284
285 [webview close];
286 [document_ release];
287
288 [indirect_ release];
289
290 WebThreadUnlock();
291
292 [scroller_ setDelegate:nil];
293
294 if (confirm_ != nil)
295 [confirm_ release];
296
297 [scroller_ release];
298 [indicator_ release];
299 [loading_ release];
300 [super dealloc];
301 }
302
303 + (float) defaultWidth {
304 return 980;
305 }
306
307 - (void) _setTileDrawingEnabled:(BOOL)enabled {
308 //[document_ setTileDrawingEnabled:enabled];
309 }
310
311 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
312 [self _setTileDrawingEnabled:NO];
313 }
314
315 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
316 [self _setTileDrawingEnabled:YES];
317 [document_ redrawScaledDocument];
318 }
319
320 - (void) setViewportWidth:(float)width {
321 width_ = width != 0 ? width : [[self class] defaultWidth];
322 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
323 }
324
325 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
326 [self _setTileDrawingEnabled:NO];
327 }
328
329 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
330 [self _setTileDrawingEnabled:YES];
331 }
332
333 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
334 [self _setTileDrawingEnabled:YES];
335 }
336
337 - (void) loadRequest:(NSURLRequest *)request {
338 error_ = false;
339
340 WebThreadLock();
341 [document_ loadRequest:request];
342 WebThreadUnlock();
343 }
344
345 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
346 [self loadRequest:[NSURLRequest
347 requestWithURL:url
348 cachePolicy:policy
349 timeoutInterval:30.0
350 ]];
351 }
352
353 - (void) loadURL:(NSURL *)url {
354 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
355 }
356
357 - (id) init {
358 CGRect frame = {{0, 0}, {320, 480}};
359 frame.size.height -= GSDefaultStatusBarHeight();
360
361 if ((self = [super initWithFrame:frame]) != nil) {
362 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
363
364 struct CGRect bounds([self bounds]);
365
366 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
367 [self addSubview:scroller_];
368
369 [scroller_ setFixedBackgroundPattern:YES];
370 [scroller_ setBackgroundColor:[UIColor blackColor]];
371
372 [scroller_ setScrollingEnabled:YES];
373 [scroller_ setClipsSubviews:YES];
374 [scroller_ setAllowsRubberBanding:YES];
375
376 [scroller_ setDelegate:self];
377 [scroller_ setBounces:YES];
378 [scroller_ setScrollHysteresis:8];
379 [scroller_ setThumbDetectionEnabled:NO];
380 [scroller_ setDirectionalScrolling:YES];
381 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
382 [scroller_ setEventMode:YES];
383 [scroller_ setShowBackgroundShadow:NO]; /* YES */
384 [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
385 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
386
387 CGRect rect([scroller_ bounds]);
388 //rect.size.height = 0;
389
390 WebThreadLock();
391
392 document_ = [[UIWebDocumentView alloc] initWithFrame:rect];
393 WebView *webview([document_ webView]);
394
395 [document_ setBackgroundColor:[UIColor blackColor]];
396 if ([document_ respondsToSelector:@selector(setDrawsBackground:)])
397 [document_ setDrawsBackground:NO];
398 [webview setDrawsBackground:NO];
399
400 [webview setPreferencesIdentifier:@"WebCycript"];
401
402 [document_ setTileSize:CGSizeMake(rect.size.width, 500)];
403
404 if ([document_ respondsToSelector:@selector(enableReachability)])
405 [document_ enableReachability];
406
407 [document_ setAllowsMessaging:YES];
408
409 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
410 [document_ useSelectionAssistantWithMode:0];
411
412 [document_ setTilingEnabled:YES];
413 [document_ setDrawsGrid:NO];
414 [document_ setLogsTilingChanges:NO];
415 [document_ setTileMinificationFilter:kCAFilterNearest];
416
417 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
418 /* XXX: abstractify */
419 [document_ setDataDetectorTypes:0x80000000];
420 else
421 [document_ setDetectsPhoneNumbers:NO];
422
423 [document_ setAutoresizes:YES];
424
425 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
426 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
427 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
428 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
429
430 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
431
432 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
433 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
434 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
435
436 [document_ _setDocumentType:0x4];
437
438 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
439 [document_ setZoomsFocusedFormControl:YES];
440 [document_ setContentsPosition:7];
441 [document_ setEnabledGestures:0xa];
442 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
443 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
444
445 [document_ setSmoothsFonts:YES];
446 [document_ setAllowsImageSheet:YES];
447 [webview _setUsesLoaderCache:YES];
448
449 [webview setGroupName:@"CydgetGroup"];
450
451 WebPreferences *preferences([webview preferences]);
452
453 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
454 [webview _setLayoutInterval:0];
455 else
456 [preferences _setLayoutInterval:0];
457
458 [self setViewportWidth:0];
459
460 [document_ setDelegate:self];
461 [document_ setGestureDelegate:self];
462 [document_ setFormEditingDelegate:self];
463 [document_ setInteractionDelegate:self];
464
465 [scroller_ addSubview:document_];
466
467 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
468
469 indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self];
470
471 [webview setFrameLoadDelegate:indirect_];
472 [webview setPolicyDelegate:indirect_];
473 [webview setResourceLoadDelegate:indirect_];
474 [webview setUIDelegate:indirect_];
475
476 /* XXX: do not turn this on under penalty of extreme pain */
477 [webview setScriptDebugDelegate:nil];
478
479 WebThreadUnlock();
480
481 CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]);
482 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
483 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
484
485 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
486 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
487
488 NSDictionary *configuration([$CydgetController currentConfiguration]);
489
490 cycript_ = [[configuration objectForKey:@"Cycript"] boolValue];
491
492 scrollable_ = [[configuration objectForKey:@"Scrollable"] boolValue];
493 [scroller_ setScrollingEnabled:scrollable_];
494
495 NSString *homepage([configuration objectForKey:@"Homepage"]);
496 [self loadURL:[NSURL URLWithString:homepage]];
497 } return self;
498 }
499
500 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
501 [self retain];
502
503 UIActionSheet *sheet = [[[UIActionSheet alloc]
504 initWithTitle:nil
505 buttons:[NSArray arrayWithObjects:@"OK", nil]
506 defaultButtonIndex:0
507 delegate:self
508 context:@"alert"
509 ] autorelease];
510
511 [sheet setBodyText:message];
512 [sheet popupAlertAnimated:YES];
513 }
514
515 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
516 [self retain];
517
518 UIActionSheet *sheet = [[[UIActionSheet alloc]
519 initWithTitle:nil
520 buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil]
521 defaultButtonIndex:0
522 delegate:indirect_
523 context:@"confirm"
524 ] autorelease];
525
526 [sheet setNumberOfRows:1];
527 [sheet setBodyText:message];
528 [sheet popupAlertAnimated:YES];
529
530 NSRunLoop *loop([NSRunLoop currentRunLoop]);
531 NSDate *future([NSDate distantFuture]);
532
533 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
534
535 NSNumber *confirm([confirm_ autorelease]);
536 confirm_ = nil;
537
538 [self autorelease];
539 return [confirm boolValue];
540 }
541
542 /* XXX: WebThreadLock? */
543 - (void) _fixScroller:(CGRect)bounds {
544 float extra;
545 if (!editing_)
546 extra = 0;
547 else {
548 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
549 CGRect peripheral([assistant peripheralFrame]);
550 extra = peripheral.size.height;
551 }
552
553 CGRect subrect([scroller_ frame]);
554 subrect.size.height -= [TPBottomLockBar defaultHeight];
555 subrect.size.height -= extra;
556 [scroller_ setScrollerIndicatorSubrect:subrect];
557
558 #undef NSSize
559 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
560 [document_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
561
562 CGSize size(size_);
563 size.height += extra;
564 size.height += [TPBottomLockBar defaultHeight];
565 [scroller_ setContentSize:size];
566
567 [scroller_ releaseRubberBandIfNecessary];
568 }
569
570 - (void) fixScroller {
571 CGRect bounds([document_ documentBounds]);
572 [self _fixScroller:bounds];
573 }
574
575 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
576 size_ = frame.size;
577 [self _fixScroller:frame];
578 }
579
580 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
581 [self view:sender didSetFrame:frame];
582 }
583
584 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
585 editing_ = true;
586 }
587
588 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
589 [self fixScroller];
590 }
591
592 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
593 editing_ = false;
594 [self fixScroller];
595 }
596
597 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
598 if (cycript_)
599 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
600 if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CYSetupContext"))) {
601 WebView *webview([document_ webView]);
602 WebFrame *frame([webview mainFrame]);
603 JSGlobalContextRef context([frame globalContext]);
604 CYSetupContext(context);
605 }
606 }
607
608 - (bool) isLoading {
609 return [loading_ count] != 0;
610 }
611
612 - (void) reloadButtons {
613 if ([self isLoading]) {
614 [UIApp setNetworkActivityIndicatorVisible:YES];
615 [indicator_ startAnimation];
616 } else {
617 [UIApp setNetworkActivityIndicatorVisible:NO];
618 [indicator_ stopAnimation];
619 }
620 }
621
622 - (void) _finishLoading {
623 size_t count([loading_ count]);
624 /*if (count == 0)
625 [self autorelease];*/
626 if (reloading_ || count != 0)
627 return;
628 [self reloadButtons];
629 }
630
631 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
632 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
633 }
634
635 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
636 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
637 }
638
639 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
640 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
641 }
642
643 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
644 return [document_ webView:sender didCommitLoadForFrame:frame];
645 }
646
647 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
648 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
649 }
650
651 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
652 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
653 [self _finishLoading];
654 return [document_ webView:sender didFinishLoadForFrame:frame];
655 }
656
657 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
658 /*if ([loading_ count] == 0)
659 [self retain];*/
660 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
661
662 if ([frame parentFrame] == nil) {
663 [document_ resignFirstResponder];
664
665 reloading_ = false;
666
667 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
668
669 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
670 [scroller_ setZoomScale:1 duration:0];
671 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
672 [scroller_ _setZoomScale:1 duration:0];
673 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
674 [scroller_ setZoomScale:1 animated:NO];*/
675
676 CGRect rect([scroller_ bounds]);
677 //rect.size.height = 0;
678 [document_ setFrame:rect];
679 }
680
681 [self reloadButtons];
682 }
683
684 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
685 /*if ([frame parentFrame] == nil)
686 [self autorelease];*/
687
688 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
689 [self _finishLoading];
690
691 if (reloading_)
692 return;
693
694 if ([frame parentFrame] == nil) {
695 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
696 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
697 [[error localizedDescription] stringByAddingPercentEscapes]
698 ]]];
699
700 error_ = true;
701 }
702 }
703
704 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
705 [self _didFailWithError:error forFrame:frame];
706 }
707
708 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
709 [self _didFailWithError:error forFrame:frame];
710 }
711
712 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
713 fprintf(stderr, "Console:%s\n", [[dictionary description] UTF8String]);
714 }
715
716 @end
717
718 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
719 }
720
721 @end
722
723 #include <string>
724
725 static bool cycript_;
726 static bool jscript_;
727
728 static void SetParser(bool cycript, bool jscript) {
729 cycript_ = cycript;
730 jscript_ = jscript;
731 }
732
733 static bool GetParser0() {
734 return cycript_;
735 }
736
737 static bool GetParser1() {
738 return jscript_;
739 }
740
741 static void Cycriptify(apr_pool_t *pool, const uint16_t *&data, size_t &size) {
742 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
743 if (void (*CYParseUChar)(apr_pool_t *, const uint16_t **, size_t *) = reinterpret_cast<void (*)(apr_pool_t *, const uint16_t **, size_t *)>(dlsym(handle, "CYParseWebCore")))
744 CYParseUChar(pool, &data, &size);
745 }
746
747 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
748 if (!GetParser0())
749 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
750 else {
751 SetParser(false, true);
752 JSC::SourceCode *source(*_this);
753 const uint16_t *data(source->data());
754 size_t size(source->length());
755 apr_pool_t *pool;
756 apr_pool_create(&pool, NULL);
757 Cycriptify(pool, data, size);
758 std::string stuff(data, data + size);
759 JSC::SourceCode code(JSC::makeSource(JSC::UString(data, size)));
760 *_this = &code;
761 __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
762 apr_pool_destroy(pool);
763 *_this = source;
764 }
765 }
766
767 MSHook(void, _ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, void *_this, int start, const UChar *code, unsigned length, int *source, int *line, JSC::UString *message) {
768 if (!GetParser0())
769 return __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, code, length, source, line, message);
770 else {
771 const uint16_t *data(code);
772 size_t size(length);
773 apr_pool_t *pool;
774 apr_pool_create(&pool, NULL);
775 Cycriptify(pool, data, size);
776 __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, data, size, source, line, message);
777 apr_pool_destroy(pool);
778 }
779 }
780
781 struct State {
782 unsigned state;
783 };
784
785 MSHook(State, _ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, State state) {
786 SetParser(false, true);
787 state = __ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE(state);
788 SetParser(false, false);
789 return state;
790 }
791
792 MSHook(void, _ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, void *resource) {
793 SetParser(false, true);
794 __ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE(resource);
795 SetParser(false, false);
796 }
797
798 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
799 bool jscript;
800
801 if (!GetParser1()) through:
802 jscript = __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
803 else if (mime == "text/cycript") {
804 SetParser(true, true);
805 jscript = true;
806 } else
807 goto through;
808
809 return jscript;
810 }
811
812 /* Cydget:// Protocol {{{ */
813 @interface CydgetURLProtocol : NSURLProtocol {
814 }
815
816 @end
817
818 @implementation CydgetURLProtocol
819
820 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
821 NSURL *url([request URL]);
822 if (url == nil)
823 return NO;
824 NSString *scheme([[url scheme] lowercaseString]);
825 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
826 return NO;
827 return YES;
828 }
829
830 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
831 return request;
832 }
833
834 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
835 id<NSURLProtocolClient> client([self client]);
836 if (icon == nil)
837 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
838 else {
839 NSData *data(UIImagePNGRepresentation(icon));
840
841 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
842 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
843 [client URLProtocol:self didLoadData:data];
844 [client URLProtocolDidFinishLoading:self];
845 }
846 }
847
848 - (void) startLoading {
849 id<NSURLProtocolClient> client([self client]);
850 NSURLRequest *request([self request]);
851
852 NSURL *url([request URL]);
853 NSString *href([url absoluteString]);
854
855 NSString *path([href substringFromIndex:9]);
856 NSRange slash([path rangeOfString:@"/"]);
857
858 NSString *command;
859 if (slash.location == NSNotFound) {
860 command = path;
861 path = nil;
862 } else {
863 command = [path substringToIndex:slash.location];
864 path = [path substringFromIndex:(slash.location + 1)];
865 }
866
867 if ([command isEqualToString:@"_UIImageWithName"]) {
868 if (path == nil)
869 goto fail;
870 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
871 UIImage *icon(_UIImageWithName(path));
872 [self _returnPNGWithImage:icon forRequest:request];
873 } else fail: {
874 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
875 }
876 }
877
878 - (void) stopLoading {
879 }
880
881 @end
882 /* }}} */
883
884 template <typename Type_>
885 static void nlset(Type_ &function, struct nlist *nl, size_t index) {
886 struct nlist &name(nl[index]);
887 uintptr_t value(name.n_value);
888 if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
889 value |= 0x00000001;
890 function = reinterpret_cast<Type_>(value);
891 }
892
893 template <typename Type_>
894 static void dlset(Type_ &function, const char *name) {
895 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
896 }
897
898 @implementation WebCycriptLockScreenController
899
900 + (void) initialize {
901 apr_initialize();
902
903 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
904
905 void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
906 dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
907 if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
908 MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
909
910 void (*_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE)(void *, int, const UChar *, unsigned, int *, int *, JSC::UString *);
911 dlset(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, "_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE");
912 if (_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE != NULL)
913 MSHookFunction(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, MSHake(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE));
914
915 struct nlist nl[4];
916 memset(nl, 0, sizeof(nl));
917 nl[0].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE";
918 nl[1].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE";
919 nl[2].n_un.n_name = (char *) "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE";
920 nlist("/System/Library/PrivateFrameworks/WebCore.framework/WebCore", nl);
921
922 State (*_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE)(State);
923 nlset(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, nl, 0);
924 MSHookFunction(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE));
925
926 void (*_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE)(void *);
927 nlset(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, nl, 1);
928 MSHookFunction(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, MSHake(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE));
929
930 bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &);
931 nlset(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, nl, 2);
932 MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
933 }
934
935 + (id) rootViewController {
936 return [[[self alloc] init] autorelease];
937 }
938
939 - (void) loadView {
940 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];
941 }
942
943 @end