]> git.saurik.com Git - cydget.git/blob - LockScreen.mm
f1963e73bcf61f87cf3dea78fe9b06fc7558f04a
[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 "Parser.h"
73 #include "JSGlobalData.h"
74
75 @interface WebView (UICaboodle)
76 - (void) setScriptDebugDelegate:(id)delegate;
77 - (void) _setFormDelegate:(id)delegate;
78 - (void) _setUIKitDelegate:(id)delegate;
79 - (void) setWebMailDelegate:(id)delegate;
80 - (void) _setLayoutInterval:(float)interval;
81 @end
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]\n", \
90 trace_++, __FILE__, __LINE__, __FUNCTION__\
91 ); \
92 } while (false)
93
94 @protocol CydgetController
95 - (NSDictionary *) currentConfiguration;
96 @end
97
98 static void (*CYSetupContext)(JSGlobalContextRef);
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 [super dealloc];
300 }
301
302 + (float) defaultWidth {
303 return 980;
304 }
305
306 - (void) _setTileDrawingEnabled:(BOOL)enabled {
307 //[document_ setTileDrawingEnabled:enabled];
308 }
309
310 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
311 [self _setTileDrawingEnabled:NO];
312 }
313
314 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
315 [self _setTileDrawingEnabled:YES];
316 [document_ redrawScaledDocument];
317 }
318
319 - (void) setViewportWidth:(float)width {
320 width_ = width != 0 ? width : [[self class] defaultWidth];
321 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
322 }
323
324 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
325 [self _setTileDrawingEnabled:NO];
326 }
327
328 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
329 [self _setTileDrawingEnabled:YES];
330 }
331
332 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
333 [self _setTileDrawingEnabled:YES];
334 }
335
336 - (void) loadRequest:(NSURLRequest *)request {
337 error_ = false;
338
339 WebThreadLock();
340 [document_ loadRequest:request];
341 WebThreadUnlock();
342 }
343
344 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
345 [self loadRequest:[NSURLRequest
346 requestWithURL:url
347 cachePolicy:policy
348 timeoutInterval:30.0
349 ]];
350 }
351
352 - (void) loadURL:(NSURL *)url {
353 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
354 }
355
356 - (id) init {
357 CGRect frame = {{0, 0}, {320, 480}};
358 frame.size.height -= GSDefaultStatusBarHeight();
359
360 if ((self = [super initWithFrame:frame]) != nil) {
361 struct CGRect bounds([self bounds]);
362
363 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
364 [self addSubview:scroller_];
365
366 [scroller_ setFixedBackgroundPattern:YES];
367 [scroller_ setBackgroundColor:[UIColor blackColor]];
368
369 [scroller_ setScrollingEnabled:YES];
370 [scroller_ setClipsSubviews:YES];
371 [scroller_ setAllowsRubberBanding:YES];
372
373 [scroller_ setDelegate:self];
374 [scroller_ setBounces:YES];
375 [scroller_ setScrollHysteresis:8];
376 [scroller_ setThumbDetectionEnabled:NO];
377 [scroller_ setDirectionalScrolling:YES];
378 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
379 [scroller_ setEventMode:YES];
380 [scroller_ setShowBackgroundShadow:NO]; /* YES */
381 [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
382 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
383
384 CGRect rect([scroller_ bounds]);
385 //rect.size.height = 0;
386
387 WebThreadLock();
388
389 document_ = [[UIWebDocumentView alloc] initWithFrame:rect];
390 WebView *webview([document_ webView]);
391
392 [document_ setBackgroundColor:[UIColor blackColor]];
393 if ([document_ respondsToSelector:@selector(setDrawsBackground:)])
394 [document_ setDrawsBackground:NO];
395 [webview setDrawsBackground:NO];
396
397 [webview setPreferencesIdentifier:@"WebCycript"];
398
399 [document_ setTileSize:CGSizeMake(rect.size.width, 500)];
400
401 if ([document_ respondsToSelector:@selector(enableReachability)])
402 [document_ enableReachability];
403
404 [document_ setAllowsMessaging:YES];
405
406 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
407 [document_ useSelectionAssistantWithMode:0];
408
409 [document_ setTilingEnabled:YES];
410 [document_ setDrawsGrid:NO];
411 [document_ setLogsTilingChanges:NO];
412 [document_ setTileMinificationFilter:kCAFilterNearest];
413
414 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
415 /* XXX: abstractify */
416 [document_ setDataDetectorTypes:0x80000000];
417 else
418 [document_ setDetectsPhoneNumbers:NO];
419
420 [document_ setAutoresizes:YES];
421
422 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
423 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
424 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
425 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
426
427 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
428
429 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
430 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
431 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
432
433 [document_ _setDocumentType:0x4];
434
435 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
436 [document_ setZoomsFocusedFormControl:YES];
437 [document_ setContentsPosition:7];
438 [document_ setEnabledGestures:0xa];
439 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
440 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
441
442 [document_ setSmoothsFonts:YES];
443 [document_ setAllowsImageSheet:YES];
444 [webview _setUsesLoaderCache:YES];
445
446 [webview setGroupName:@"CydgetGroup"];
447
448 WebPreferences *preferences([webview preferences]);
449
450 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
451 [webview _setLayoutInterval:0];
452 else
453 [preferences _setLayoutInterval:0];
454
455 [self setViewportWidth:0];
456
457 [document_ setDelegate:self];
458 [document_ setGestureDelegate:self];
459 [document_ setFormEditingDelegate:self];
460 [document_ setInteractionDelegate:self];
461
462 [scroller_ addSubview:document_];
463
464 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
465
466 indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self];
467
468 [webview setFrameLoadDelegate:indirect_];
469 [webview setPolicyDelegate:indirect_];
470 [webview setResourceLoadDelegate:indirect_];
471 [webview setUIDelegate:indirect_];
472
473 /* XXX: do not turn this on under penalty of extreme pain */
474 [webview setScriptDebugDelegate:nil];
475
476 WebThreadUnlock();
477
478 CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]);
479 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
480 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
481
482 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
483 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
484
485 NSDictionary *configuration([$CydgetController currentConfiguration]);
486
487 cycript_ = [[configuration objectForKey:@"Cycript"] boolValue];
488
489 scrollable_ = [[configuration objectForKey:@"Scrollable"] boolValue];
490 [scroller_ setScrollingEnabled:scrollable_];
491
492 NSString *homepage([configuration objectForKey:@"Homepage"]);
493 [self loadURL:[NSURL URLWithString:homepage]];
494 } return self;
495 }
496
497 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
498 [self retain];
499
500 UIActionSheet *sheet = [[[UIActionSheet alloc]
501 initWithTitle:nil
502 buttons:[NSArray arrayWithObjects:@"OK", nil]
503 defaultButtonIndex:0
504 delegate:self
505 context:@"alert"
506 ] autorelease];
507
508 [sheet setBodyText:message];
509 [sheet popupAlertAnimated:YES];
510 }
511
512 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
513 [self retain];
514
515 UIActionSheet *sheet = [[[UIActionSheet alloc]
516 initWithTitle:nil
517 buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil]
518 defaultButtonIndex:0
519 delegate:indirect_
520 context:@"confirm"
521 ] autorelease];
522
523 [sheet setNumberOfRows:1];
524 [sheet setBodyText:message];
525 [sheet popupAlertAnimated:YES];
526
527 NSRunLoop *loop([NSRunLoop currentRunLoop]);
528 NSDate *future([NSDate distantFuture]);
529
530 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
531
532 NSNumber *confirm([confirm_ autorelease]);
533 confirm_ = nil;
534
535 [self autorelease];
536 return [confirm boolValue];
537 }
538
539 /* XXX: WebThreadLock? */
540 - (void) _fixScroller:(CGRect)bounds {
541 float extra;
542 if (!editing_)
543 extra = 0;
544 else {
545 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
546 CGRect peripheral([assistant peripheralFrame]);
547 extra = peripheral.size.height;
548 }
549
550 CGRect subrect([scroller_ frame]);
551 subrect.size.height -= [TPBottomLockBar defaultHeight];
552 subrect.size.height -= extra;
553 [scroller_ setScrollerIndicatorSubrect:subrect];
554
555 #undef NSSize
556 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
557 [document_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
558
559 CGSize size(size_);
560 size.height += extra;
561 size.height += [TPBottomLockBar defaultHeight];
562 [scroller_ setContentSize:size];
563
564 [scroller_ releaseRubberBandIfNecessary];
565 }
566
567 - (void) fixScroller {
568 CGRect bounds([document_ documentBounds]);
569 [self _fixScroller:bounds];
570 }
571
572 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
573 size_ = frame.size;
574 [self _fixScroller:frame];
575 }
576
577 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
578 [self view:sender didSetFrame:frame];
579 }
580
581 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
582 editing_ = true;
583 }
584
585 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
586 [self fixScroller];
587 }
588
589 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
590 editing_ = false;
591 [self fixScroller];
592 }
593
594 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
595 if (cycript_ && CYSetupContext != NULL) {
596 WebView *webview([document_ webView]);
597 WebFrame *frame([webview mainFrame]);
598 JSGlobalContextRef context([frame globalContext]);
599 CYSetupContext(context);
600 }
601 }
602
603 - (bool) isLoading {
604 return [loading_ count] != 0;
605 }
606
607 - (void) reloadButtons {
608 if ([self isLoading])
609 [indicator_ startAnimation];
610 else
611 [indicator_ stopAnimation];
612 }
613
614 - (void) _finishLoading {
615 size_t count([loading_ count]);
616 /*if (count == 0)
617 [self autorelease];*/
618 if (reloading_ || count != 0)
619 return;
620 [self reloadButtons];
621 }
622
623 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
624 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
625 }
626
627 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
628 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
629 }
630
631 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
632 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
633 }
634
635 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
636 return [document_ webView:sender didCommitLoadForFrame:frame];
637 }
638
639 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
640 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
641 }
642
643 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
644 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
645 [self _finishLoading];
646 return [document_ webView:sender didFinishLoadForFrame:frame];
647 }
648
649 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
650 /*if ([loading_ count] == 0)
651 [self retain];*/
652 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
653
654 if ([frame parentFrame] == nil) {
655 [document_ resignFirstResponder];
656
657 reloading_ = false;
658
659 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
660
661 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
662 [scroller_ setZoomScale:1 duration:0];
663 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
664 [scroller_ _setZoomScale:1 duration:0];
665 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
666 [scroller_ setZoomScale:1 animated:NO];*/
667
668 CGRect rect([scroller_ bounds]);
669 //rect.size.height = 0;
670 [document_ setFrame:rect];
671 }
672
673 [self reloadButtons];
674 }
675
676 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
677 /*if ([frame parentFrame] == nil)
678 [self autorelease];*/
679
680 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
681 [self _finishLoading];
682
683 if (reloading_)
684 return;
685
686 if ([frame parentFrame] == nil) {
687 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
688 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
689 [[error localizedDescription] stringByAddingPercentEscapes]
690 ]]];
691
692 error_ = true;
693 }
694 }
695
696 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
697 [self _didFailWithError:error forFrame:frame];
698 }
699
700 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
701 [self _didFailWithError:error forFrame:frame];
702 }
703
704 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
705 fprintf(stderr, "Console:%s\n", [[dictionary description] UTF8String]);
706 }
707
708 @end
709
710 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
711 }
712
713 @end
714
715 /*extern "C" void _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(JSC::Parser *, JSC::JSGlobalData *, int *, JSC::UString *);
716 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::Parser *_this, JSC::JSGlobalData *data, int *line, JSC::UString *message) {
717 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, data, line, message);
718 }*/
719
720 /* Cydget:// Protocol {{{ */
721 @interface CydgetURLProtocol : NSURLProtocol {
722 }
723
724 @end
725
726 @implementation CydgetURLProtocol
727
728 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
729 NSURL *url([request URL]);
730 if (url == nil)
731 return NO;
732 NSString *scheme([[url scheme] lowercaseString]);
733 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
734 return NO;
735 return YES;
736 }
737
738 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
739 return request;
740 }
741
742 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
743 id<NSURLProtocolClient> client([self client]);
744 if (icon == nil)
745 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
746 else {
747 NSData *data(UIImagePNGRepresentation(icon));
748
749 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
750 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
751 [client URLProtocol:self didLoadData:data];
752 [client URLProtocolDidFinishLoading:self];
753 }
754 }
755
756 - (void) startLoading {
757 id<NSURLProtocolClient> client([self client]);
758 NSURLRequest *request([self request]);
759
760 NSURL *url([request URL]);
761 NSString *href([url absoluteString]);
762
763 NSString *path([href substringFromIndex:9]);
764 NSRange slash([path rangeOfString:@"/"]);
765
766 NSString *command;
767 if (slash.location == NSNotFound) {
768 command = path;
769 path = nil;
770 } else {
771 command = [path substringToIndex:slash.location];
772 path = [path substringFromIndex:(slash.location + 1)];
773 }
774
775 if ([command isEqualToString:@"_UIImageWithName"]) {
776 if (path == nil)
777 goto fail;
778 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
779 UIImage *icon(_UIImageWithName(path));
780 [self _returnPNGWithImage:icon forRequest:request];
781 } else fail: {
782 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
783 }
784 }
785
786 - (void) stopLoading {
787 }
788
789 @end
790 /* }}} */
791
792 @implementation WebCycriptLockScreenController
793
794 + (void) initialize {
795 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
796 //MSHookFunction(&_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
797 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
798 CYSetupContext = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CYSetupContext"));
799 }
800
801 + (id) rootViewController {
802 return [[[self alloc] init] autorelease];
803 }
804
805 - (void) loadView {
806 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];
807 }
808
809 @end