1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
|
#include <UIKit/UIKit.h>
#include "iPhonePrivate.h"
#include "UCPlatform.h"
#include <UICaboodle/BrowserView.h>
#include <UICaboodle/UCLocalize.h>
//#include <QuartzCore/CALayer.h>
// XXX: fix the minimum requirement
extern NSString * const kCAFilterNearest;
#include <WebCore/WebCoreThread.h>
#include <WebKit/WebPolicyDelegate.h>
#include <WebKit/WebPreferences.h>
#include <WebKit/DOMCSSPrimitiveValue.h>
#include <WebKit/DOMCSSStyleDeclaration.h>
#include <WebKit/DOMDocument.h>
#include <WebKit/DOMHTMLBodyElement.h>
#include <WebKit/DOMRGBColor.h>
//#include <WebCore/Page.h>
//#include <WebCore/Settings.h>
#include "substrate.h"
#define ForSaurik 0
template <typename Type_>
static inline void CYRelease(Type_ &value) {
if (value != nil) {
[value release];
value = nil;
}
}
float CYScrollViewDecelerationRateNormal;
@interface WebView (Apple)
- (void) _setLayoutInterval:(float)interval;
@end
@interface WebPreferences (Apple)
+ (void) _setInitialDefaultTextEncodingToSystemEncoding;
- (void) _setLayoutInterval:(NSInteger)interval;
- (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
@end
/* Indirect Delegate {{{ */
@interface IndirectDelegate : NSObject <
HookProtocol
> {
_transient volatile id delegate_;
}
- (void) setDelegate:(id)delegate;
- (id) initWithDelegate:(id)delegate;
@end
@implementation IndirectDelegate
- (void) setDelegate:(id)delegate {
delegate_ = delegate;
}
- (id) initWithDelegate:(id)delegate {
delegate_ = delegate;
return self;
}
- (void) didDismissModalViewController {
if (delegate_ != nil)
return [delegate_ didDismissModalViewController];
}
- (IMP) methodForSelector:(SEL)sel {
if (IMP method = [super methodForSelector:sel])
return method;
fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
return NULL;
}
- (BOOL) respondsToSelector:(SEL)sel {
if ([super respondsToSelector:sel])
return YES;
// XXX: WebThreadCreateNSInvocation returns nil
//fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
}
- (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
return method;
//fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
if (delegate_ != nil)
if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
return sig;
// XXX: I fucking hate Apple so very very bad
return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}
- (void) forwardInvocation:(NSInvocation *)inv {
SEL sel = [inv selector];
if (delegate_ != nil && [delegate_ respondsToSelector:sel])
[inv invokeWithTarget:delegate_];
}
@end
/* }}} */
@implementation WebScriptObject (UICaboodle)
- (NSUInteger) count {
id length([self valueForKey:@"length"]);
if ([length respondsToSelector:@selector(intValue)])
return [length intValue];
else
return 0;
}
- (id) objectAtIndex:(unsigned)index {
return [self webScriptValueAtIndex:index];
}
@end
// CYWebPolicyDecision* {{{
enum CYWebPolicyDecision {
CYWebPolicyDecisionUnknown,
CYWebPolicyDecisionDownload,
CYWebPolicyDecisionIgnore,
CYWebPolicyDecisionUse,
};
@interface CYWebPolicyDecisionMediator : NSObject <
WebPolicyDecisionListener
> {
id<WebPolicyDecisionListener> listener_;
CYWebPolicyDecision decision_;
}
- (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
- (CYWebPolicyDecision) decision;
- (bool) decided;
- (bool) decide;
@end
@implementation CYWebPolicyDecisionMediator
- (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
if ((self = [super init]) != nil) {
listener_ = listener;
} return self;
}
- (CYWebPolicyDecision) decision {
return decision_;
}
- (bool) decided {
return decision_ != CYWebPolicyDecisionUnknown;
}
- (bool) decide {
switch (decision_) {
case CYWebPolicyDecisionUnknown:
default:
NSLog(@"CYWebPolicyDecisionUnknown");
return false;
case CYWebPolicyDecisionDownload: [listener_ download]; break;
case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
case CYWebPolicyDecisionUse: [listener_ use]; break;
}
return true;
}
- (void) download {
decision_ = CYWebPolicyDecisionDownload;
}
- (void) ignore {
decision_ = CYWebPolicyDecisionIgnore;
}
- (void) use {
decision_ = CYWebPolicyDecisionUse;
}
@end
// }}}
@implementation CYWebView : UIWebView
- (id) initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame]) != nil) {
} return self;
}
- (void) dealloc {
[super dealloc];
}
- (id<CYWebViewDelegate>) delegate {
return (id<CYWebViewDelegate>) [super delegate];
}
/*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
id<CYWebViewDelegate> delegate([self delegate]);
WebView *created(nil);
if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
created = [delegate webView:view createWebViewWithRequest:request];
if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
created = [super webView:view createWebViewWithRequest:request];
return created;
}*/
// webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
id<CYWebViewDelegate> delegate([self delegate]);
CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
[delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
[super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
[mediator decide];
}
// }}}
// webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) {
UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
[uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener];
}
- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
id<CYWebViewDelegate> delegate([self delegate]);
CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
[delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
[super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
[mediator decide];
}
// }}}
// webView:didClearWindowObject:forFrame: (3.2+) {{{
static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
[uiWebView webView:view didClearWindowObject:window forFrame:frame];
}
- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
id<CYWebViewDelegate> delegate([self delegate]);
if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
[delegate webView:view didClearWindowObject:window forFrame:frame];
if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
[super webView:view didClearWindowObject:window forFrame:frame];
}
// }}}
// webView:didFailLoadWithError:forFrame: (2.0+) {{{
- (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
id<CYWebViewDelegate> delegate([self delegate]);
if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
[delegate webView:view didFailLoadWithError:error forFrame:frame];
if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
[super webView:view didFailLoadWithError:error forFrame:frame];
}
// }}}
// webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
- (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
id<CYWebViewDelegate> delegate([self delegate]);
if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
[delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
[super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
}
// }}}
// webView:didFinishLoadForFrame: (2.0+) {{{
- (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
id<CYWebViewDelegate> delegate([self delegate]);
if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
[delegate webView:view didFinishLoadForFrame:frame];
if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
[super webView:view didFinishLoadForFrame:frame];
}
// }}}
// webView:didReceiveTitle:forFrame: (3.2+) {{{
static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
[uiWebView webView:view didReceiveTitle:title forFrame:frame];
}
- (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
id<CYWebViewDelegate> delegate([self delegate]);
if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
[delegate webView:view didReceiveTitle:title forFrame:frame];
if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
[super webView:view didReceiveTitle:title forFrame:frame];
}
// }}}
// webView:didStartProvisionalLoadForFrame: (2.0+) {{{
- (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
id<CYWebViewDelegate> delegate([self delegate]);
if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
[delegate webView:view didStartProvisionalLoadForFrame:frame];
if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
[super webView:view didStartProvisionalLoadForFrame:frame];
}
// }}}
// webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
return request;
}
- (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
id<CYWebViewDelegate> delegate([self delegate]);
if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
return request;
}
// }}}
// webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
- (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
id<CYWebViewDelegate> delegate([self delegate]);
if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
// XXX: check delegate
if ([delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame])
[super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
}
// }}}
// webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
- (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
id<CYWebViewDelegate> delegate([self delegate]);
if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
// XXX: check delegate
if ([delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame])
return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
return NO;
}
// }}}
// webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
- (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
id<CYWebViewDelegate> delegate([self delegate]);
if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
// XXX: check delegate
if ([delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame])
return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
return nil;
}
// }}}
// webViewClose: (3.2+) {{{
static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
if ([uiWebView respondsToSelector:@selector(webViewClose:)])
[uiWebView webViewClose:view];
}
- (void) webViewClose:(WebView *)view {
id<CYWebViewDelegate> delegate([self delegate]);
if ([delegate respondsToSelector:@selector(webViewClose:)])
[delegate webViewClose:view];
if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
[super webViewClose:view];
}
// }}}
+ (void) initialize {
if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
}
}
@end
#define ShowInternals 0
#define LogBrowser 1
#define lprintf(args...) fprintf(stderr, args)
@implementation BrowserController
#if ShowInternals
#include "UICaboodle/UCInternal.h"
#endif
+ (void) _initialize {
[WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
else // XXX: this actually might be fast on some older systems: we should look into this
CYScrollViewDecelerationRateNormal = 0.998;
}
- (void) dealloc {
#if LogBrowser
NSLog(@"[BrowserController dealloc]");
#endif
[webview_ setDelegate:nil];
[indirect_ setDelegate:nil];
[indirect_ release];
if (challenge_ != nil)
[challenge_ release];
//NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
if (custom_ != nil)
[custom_ release];
if (style_ != nil)
[style_ release];
if (function_ != nil)
[function_ release];
if (closer_ != nil)
[closer_ release];
if (sensitive_ != nil)
[sensitive_ release];
if (title_ != nil)
[title_ release];
[reloaditem_ release];
[loadingitem_ release];
[indicator_ release];
[super dealloc];
}
- (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
[self loadRequest:[NSURLRequest
requestWithURL:url
cachePolicy:policy
timeoutInterval:120.0
]];
}
- (void) loadURL:(NSURL *)url {
[self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
}
- (void) loadRequest:(NSURLRequest *)request {
#if LogBrowser
NSLog(@"loadRequest:%@", request);
#endif
error_ = false;
WebThreadLock();
[webview_ loadRequest:request];
WebThreadUnlock();
}
- (void) reloadURL {
if (request_ == nil)
return;
if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
[self loadRequest:request_];
else {
UIAlertView *alert = [[[UIAlertView alloc]
initWithTitle:UCLocalize("RESUBMIT_FORM")
message:nil
delegate:self
cancelButtonTitle:UCLocalize("CANCEL")
otherButtonTitles:UCLocalize("SUBMIT"), nil
] autorelease];
[alert setContext:@"submit"];
[alert show];
}
}
- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
if (custom_ != nil)
[custom_ autorelease];
custom_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
if (style_ != nil)
[style_ autorelease];
style_ = style == nil ? nil : [style retain];
if (function_ != nil)
[function_ autorelease];
function_ = function == nil ? nil : [function retain];
[self applyRightButton];
}
- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
if (custom_ != nil)
[custom_ autorelease];
custom_ = button == nil ? nil : [button retain];
if (style_ != nil)
[style_ autorelease];
style_ = style == nil ? nil : [style retain];
if (function_ != nil)
[function_ autorelease];
function_ = function == nil ? nil : [function retain];
[self applyRightButton];
}
- (void) setPopupHook:(id)function {
if (closer_ != nil)
[closer_ autorelease];
closer_ = function == nil ? nil : [function retain];
}
- (void) setViewportWidth:(float)width {
width_ = width != 0 ? width : [[self class] defaultWidth];
[[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
}
- (void) _openMailToURL:(NSURL *)url {
[[UIApplication sharedApplication] openURL:url];// asPanel:YES];
}
- (bool) _allowJavaScriptPanel {
return true;
}
- (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
[loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
[self _didFinishLoading];
if ([error code] == NSURLErrorCancelled)
return;
if ([frame parentFrame] == nil) {
[self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
[[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
[[error localizedDescription] stringByAddingPercentEscapes]
]]];
error_ = true;
}
}
// CYWebViewDelegate {{{
- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
#if LogBrowser
NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
#endif
if ([frame parentFrame] == nil) {
if (!error_) {
if (request_ != nil)
[request_ autorelease];
if (request == nil)
request_ = nil;
else
request_ = [request retain];
}
}
}
- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
#if LogBrowser
NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
#endif
NSURL *url([request URL]);
if (url == nil)
return;
if ([frame isEqualToString:@"_open"])
[delegate_ openURL:url];
NSString *scheme([[url scheme] lowercaseString]);
if ([scheme isEqualToString:@"mailto"])
[self _openMailToURL:url];
CYViewController *page([delegate_ pageForURL:url hasTag:NULL]);
if (page == nil) {
BrowserController *browser([[[class_ alloc] init] autorelease]);
[browser loadRequest:request];
page = browser;
}
[page setDelegate:delegate_];
if (![frame isEqualToString:@"_popup"]) {
[[self navigationItem] setTitle:title_];
[[self navigationController] pushViewController:page animated:YES];
} else {
UCNavigationController *navigation([[[UCNavigationController alloc] init] autorelease]);
[navigation setHook:indirect_];
[navigation setDelegate:delegate_];
[navigation setViewControllers:[NSArray arrayWithObject:page]];
[[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
initWithTitle:UCLocalize("CLOSE")
style:UIBarButtonItemStylePlain
target:page
action:@selector(close)
] autorelease]];
[[self navigationController] presentModalViewController:navigation animated:YES];
}
[listener ignore];
}
- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
}
- (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
#if LogBrowser
NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
#endif
[self _didFailWithError:error forFrame:frame];
}
- (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
#if LogBrowser
NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
#endif
[self _didFailWithError:error forFrame:frame];
}
- (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
[loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
if ([frame parentFrame] == nil) {
if (DOMDocument *document = [frame DOMDocument])
if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
for (DOMHTMLBodyElement *body in (id) bodies) {
DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
bool colored(false);
if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
DOMRGBColor *rgb([color getRGBColorValue]);
float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
UIColor *uic(nil);
if (red == 0xc7 && green == 0xce && blue == 0xd5)
uic = [UIColor groupTableViewBackgroundColor];
else if (alpha != 0)
uic = [UIColor
colorWithRed:(red / 255)
green:(green / 255)
blue:(blue / 255)
alpha:alpha
];
if (uic != nil) {
colored = true;
[scroller_ setBackgroundColor:uic];
}
}
}
if (!colored)
[scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
break;
}
}
[self _didFinishLoading];
}
- (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
if ([frame parentFrame] != nil)
return;
title_ = [title retain];
[[self navigationItem] setTitle:title_];
}
- (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
[loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
if ([frame parentFrame] == nil) {
CYRelease(title_);
CYRelease(custom_);
CYRelease(style_);
CYRelease(function_);
CYRelease(closer_);
// XXX: do we still need to do this?
[[self navigationItem] setTitle:nil];
}
[self _didStartLoading];
}
- (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
#if LogBrowser
NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
#endif
return request;
}
- (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
return [self _allowJavaScriptPanel];
}
- (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
return [self _allowJavaScriptPanel];
}
- (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
return [self _allowJavaScriptPanel];
}
- (void) webViewClose:(WebView *)view {
[self close];
}
// }}}
- (void) close {
[[self navigationController] dismissModalViewControllerAnimated:YES];
}
- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
NSString *context([alert context]);
if ([context isEqualToString:@"sensitive"]) {
switch (button) {
case 1:
sensitive_ = [NSNumber numberWithBool:YES];
break;
case 2:
sensitive_ = [NSNumber numberWithBool:NO];
break;
}
[alert dismissWithClickedButtonIndex:-1 animated:YES];
} else if ([context isEqualToString:@"challenge"]) {
id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
switch (button) {
case 1: {
NSString *username([[alert textFieldAtIndex:0] text]);
NSString *password([[alert textFieldAtIndex:1] text]);
NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
[sender useCredential:credential forAuthenticationChallenge:challenge_];
} break;
case 2:
[sender cancelAuthenticationChallenge:challenge_];
break;
_nodefault
}
[challenge_ release];
challenge_ = nil;
[alert dismissWithClickedButtonIndex:-1 animated:YES];
} else if ([context isEqualToString:@"submit"]) {
switch (button) {
case 1:
break;
case 2:
if (request_ != nil) {
WebThreadLock();
[webview_ loadRequest:request_];
WebThreadUnlock();
}
break;
_nodefault
}
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
}
- (UIBarButtonItemStyle) rightButtonStyle {
if (style_ == nil) normal:
return UIBarButtonItemStylePlain;
else if ([style_ isEqualToString:@"Normal"])
return UIBarButtonItemStylePlain;
else if ([style_ isEqualToString:@"Highlighted"])
return UIBarButtonItemStyleDone;
else goto normal;
}
- (UIBarButtonItem *) customButton {
return [[[UIBarButtonItem alloc]
initWithTitle:custom_
style:[self rightButtonStyle]
target:self
action:@selector(customButtonClicked)
] autorelease];
}
- (UIBarButtonItem *) rightButton {
return reloaditem_;
}
- (void) applyLoadingTitle {
[[self navigationItem] setTitle:UCLocalize("LOADING")];
}
- (void) applyRightButton {
if ([self isLoading]) {
[[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
// XXX: why do we do this again here?
[[loadingitem_ view] addSubview:indicator_];
[self applyLoadingTitle];
} else if (custom_ != nil) {
[[self navigationItem] setRightBarButtonItem:[self customButton] animated:YES];
} else {
[[self navigationItem] setRightBarButtonItem:[self rightButton] animated:YES];
}
}
- (void) _didStartLoading {
[self applyRightButton];
}
- (void) _didFinishLoading {
if ([loading_ count] != 0)
return;
[self applyRightButton];
// XXX: wtf?
if (![self isLoading])
[[self navigationItem] setTitle:title_];
}
- (bool) isLoading {
return [loading_ count] != 0;
}
- (id) initWithWidth:(float)width ofClass:(Class)_class {
if ((self = [super init]) != nil) {
class_ = _class;
loading_ = [[NSMutableSet alloc] initWithCapacity:5];
indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
[webview_ setDelegate:self];
[self setView:webview_];
if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
[webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
else
[webview_ setDetectsPhoneNumbers:NO];
[webview_ setScalesPageToFit:YES];
UIWebDocumentView *document([webview_ _documentView]);
// XXX: I think this improves scrolling; the hardcoded-ness sucks
[document setTileSize:CGSizeMake(320, 500)];
[document setBackgroundColor:[UIColor clearColor]];
[document setDrawsBackground:NO];
WebView *webview([document webView]);
WebPreferences *preferences([webview preferences]);
// XXX: I have no clue if I actually /want/ this modification
if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
[webview _setLayoutInterval:0];
else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
[preferences _setLayoutInterval:0];
[preferences setCacheModel:WebCacheModelDocumentBrowser];
[preferences setOfflineWebApplicationCacheEnabled:YES];
if ([webview_ respondsToSelector:@selector(_scrollView)]) {
scroller_ = [webview_ _scrollView];
[scroller_ setDirectionalLockEnabled:YES];
[scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
[scroller_ setDelaysContentTouches:NO];
[scroller_ setCanCancelContentTouches:YES];
} else if ([webview_ respondsToSelector:@selector(_scroller)]) {
UIScroller *scroller([webview_ _scroller]);
scroller_ = (UIScrollView *) scroller;
[scroller setDirectionalScrolling:YES];
// XXX: we might be better off /not/ setting this on older systems
[scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
[scroller setScrollHysteresis:0]; /* 8 */
[scroller setThumbDetectionEnabled:NO];
// use NO with UIApplicationUseLegacyEvents(YES)
[scroller setEventMode:YES];
// XXX: this is handled by setBounces, right?
//[scroller setAllowsRubberBanding:YES];
}
[scroller_ setFixedBackgroundPattern:YES];
[scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
[scroller_ setClipsSubviews:YES];
[scroller_ setBounces:YES];
[scroller_ setScrollingEnabled:YES];
[scroller_ setShowBackgroundShadow:NO];
[self setViewportWidth:width];
reloaditem_ = [[UIBarButtonItem alloc]
initWithTitle:UCLocalize("RELOAD")
style:[self rightButtonStyle]
target:self
action:@selector(reloadButtonClicked)
];
loadingitem_ = [[UIBarButtonItem alloc]
initWithTitle:@" "
style:UIBarButtonItemStylePlain
target:self
action:@selector(reloadButtonClicked)
];
CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(15, 5, indsize.width, indsize.height)];
[indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
[indicator_ startAnimation];
[[loadingitem_ view] addSubview:indicator_];
[webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
} return self;
}
- (id) initWithWidth:(float)width {
return [self initWithWidth:width ofClass:[self class]];
}
- (id) init {
return [self initWithWidth:0];
}
- (void) didDismissModalViewController {
if (closer_ != nil)
[self callFunction:closer_];
}
- (void) callFunction:(WebScriptObject *)function {
WebThreadLock();
WebView *webview([[webview_ _documentView] webView]);
WebFrame *frame([webview mainFrame]);
WebPreferences *preferences([webview preferences]);
bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
[preferences setJavaScriptCanOpenWindowsAutomatically:NO];
/*id _private(MSHookIvar<id>(webview, "_private"));
WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
WebCore::Settings *settings(page == NULL ? NULL : page->settings());
bool no;
if (settings == NULL)
no = 0;
else {
no = settings->JavaScriptCanOpenWindowsAutomatically();
settings->setJavaScriptCanOpenWindowsAutomatically(true);
}*/
if (UIWindow *window = [[self view] window])
if (UIResponder *responder = [window firstResponder])
[responder resignFirstResponder];
JSObjectRef object([function JSObject]);
JSGlobalContextRef context([frame globalContext]);
JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
/*if (settings != NULL)
settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
[preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
WebThreadUnlock();
}
- (void) reloadButtonClicked {
[self reloadURL];
}
- (void) _customButtonClicked {
[self reloadButtonClicked];
}
- (void) customButtonClicked {
#if !AlwaysReload
if (function_ != nil)
[self callFunction:function_];
else
#endif
[self _customButtonClicked];
}
+ (float) defaultWidth {
return 980;
}
@end
|