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
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
|
#include <BrowserView.h>
/* Indirect Delegate {{{ */
@interface IndirectDelegate : NSProxy {
_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;
}
- (BOOL) respondsToSelector:(SEL)sel {
return delegate_ == nil ? FALSE : [delegate_ respondsToSelector:sel];
}
- (NSMethodSignature *) methodSignatureForSelector:(SEL)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
/* }}} */
@interface WebView (Cydia)
- (void) setScriptDebugDelegate:(id)delegate;
- (void) _setFormDelegate:(id)delegate;
- (void) _setUIKitDelegate:(id)delegate;
- (void) setWebMailDelegate:(id)delegate;
- (void) _setLayoutInterval:(float)interval;
@end
/* Web Scripting {{{ */
@interface CydiaObject : NSObject {
id indirect_;
}
- (id) initWithDelegate:(IndirectDelegate *)indirect;
@end
@implementation CydiaObject
- (void) dealloc {
[indirect_ release];
[super dealloc];
}
- (id) initWithDelegate:(IndirectDelegate *)indirect {
if ((self = [super init]) != nil) {
indirect_ = [indirect retain];
} return self;
}
+ (NSArray *) _attributeKeys {
return [NSArray arrayWithObjects:@"device", nil];
}
- (NSArray *) attributeKeys {
return [[self class] _attributeKeys];
}
+ (BOOL) isKeyExcludedFromWebScript:(const char *)name {
return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name];
}
- (NSString *) device {
return [[UIDevice currentDevice] uniqueIdentifier];
}
+ (NSString *) webScriptNameForSelector:(SEL)selector {
if (selector == @selector(close))
return @"close";
else if (selector == @selector(getPackageById:))
return @"getPackageById";
else if (selector == @selector(setAutoPopup:))
return @"setAutoPopup";
else if (selector == @selector(setButtonImage:withStyle:toFunction:))
return @"setButtonImage";
else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
return @"setButtonTitle";
else if (selector == @selector(setPopupHook:))
return @"setPopupHook";
else if (selector == @selector(setViewportWidth:))
return @"setViewportWidth";
else if (selector == @selector(supports:))
return @"supports";
else if (selector == @selector(du:))
return @"du";
else if (selector == @selector(statfs:))
return @"statfs";
else
return nil;
}
+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
return [self webScriptNameForSelector:selector] == nil;
}
- (BOOL) supports:(NSString *)feature {
return [feature isEqualToString:@"window.open"];
}
- (Package *) getPackageById:(NSString *)id {
return [[Database sharedInstance] packageWithName:id];
}
- (NSArray *) statfs:(NSString *)path {
struct statfs stat;
if (path == nil || statfs([path UTF8String], &stat) == -1)
return nil;
return [NSArray arrayWithObjects:
[NSNumber numberWithUnsignedLong:stat.f_bsize],
[NSNumber numberWithUnsignedLong:stat.f_blocks],
[NSNumber numberWithUnsignedLong:stat.f_bfree],
nil];
}
- (NSNumber *) du:(NSString *)path {
NSNumber *value(nil);
int fds[2];
_assert(pipe(fds) != -1);
pid_t pid(ExecFork());
if (pid == 0) {
_assert(dup2(fds[1], 1) != -1);
_assert(close(fds[0]) != -1);
_assert(close(fds[1]) != -1);
execlp("du", "du", "-s", [path UTF8String], NULL);
exit(1);
_assert(false);
}
_assert(close(fds[1]) != -1);
if (FILE *du = fdopen(fds[0], "r")) {
char line[1024];
while (fgets(line, sizeof(line), du) != NULL) {
size_t length(strlen(line));
while (length != 0 && line[length - 1] == '\n')
line[--length] = '\0';
if (char *tab = strchr(line, '\t')) {
*tab = '\0';
value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)];
}
}
fclose(du);
} else _assert(close(fds[0]));
int status;
wait:
if (waitpid(pid, &status, 0) == -1)
if (errno == EINTR)
goto wait;
else _assert(false);
return value;
}
- (void) close {
[indirect_ close];
}
- (void) setAutoPopup:(BOOL)popup {
[indirect_ setAutoPopup:popup];
}
- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
[indirect_ setButtonImage:button withStyle:style toFunction:function];
}
- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
[indirect_ setButtonTitle:button withStyle:style toFunction:function];
}
- (void) setPopupHook:(id)function {
[indirect_ setPopupHook:function];
}
- (void) setViewportWidth:(float)width {
[indirect_ setViewportWidth:width];
}
@end
/* }}} */
@implementation BrowserView
#if ShowInternals
#include "Internals.h"
#endif
- (void) dealloc {
#if LogBrowser
NSLog(@"[BrowserView dealloc]");
#endif
if (challenge_ != nil)
[challenge_ release];
WebView *webview = [webview_ webView];
[webview setFrameLoadDelegate:nil];
[webview setResourceLoadDelegate:nil];
[webview setUIDelegate:nil];
[webview setScriptDebugDelegate:nil];
[webview setPolicyDelegate:nil];
[webview setDownloadDelegate:nil];
[webview _setFormDelegate:nil];
[webview _setUIKitDelegate:nil];
[webview setWebMailDelegate:nil];
[webview setEditingDelegate:nil];
[webview_ setDelegate:nil];
[webview_ setGestureDelegate:nil];
[webview_ setFormEditingDelegate:nil];
[webview_ setInteractionDelegate:nil];
//NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[webview close];
#if RecycleWebViews
[webview_ removeFromSuperview];
[Documents_ addObject:[webview_ autorelease]];
#else
[webview_ release];
#endif
[indirect_ setDelegate:nil];
[indirect_ release];
[cydia_ release];
[scroller_ setDelegate:nil];
if (button_ != nil)
[button_ release];
if (style_ != nil)
[style_ release];
if (function_ != nil)
[function_ release];
if (closer_ != nil)
[closer_ release];
[scroller_ release];
[indicator_ release];
if (confirm_ != nil)
[confirm_ release];
if (title_ != nil)
[title_ release];
[super dealloc];
}
- (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
[self loadRequest:[NSURLRequest
requestWithURL:url
cachePolicy:policy
timeoutInterval:30.0
]];
}
- (void) loadURL:(NSURL *)url {
[self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
}
- (NSMutableURLRequest *) _addHeadersToRequest:(NSURLRequest *)request {
NSMutableURLRequest *copy = [request mutableCopy];
if (Machine_ != NULL)
[copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
if (UniqueID_ != nil)
[copy setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
if (Role_ != nil)
[copy setValue:Role_ forHTTPHeaderField:@"X-Role"];
return copy;
}
- (void) loadRequest:(NSURLRequest *)request {
pushed_ = true;
error_ = false;
[webview_ loadRequest:request];
}
- (void) reloadURL {
if (request_ == nil)
return;
if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
[self loadRequest:request_];
else {
UIActionSheet *sheet = [[[UIActionSheet alloc]
initWithTitle:@"Are you sure you want to submit this form again?"
buttons:[NSArray arrayWithObjects:@"Cancel", @"Submit", nil]
defaultButtonIndex:0
delegate:self
context:@"submit"
] autorelease];
[sheet setNumberOfRows:1];
[sheet popupAlertAnimated:YES];
}
}
- (WebView *) webView {
return [webview_ webView];
}
- (UIWebDocumentView *) documentView {
return webview_;
}
- (void) _fixScroller {
CGRect bounds([webview_ documentBounds]);
#if LogBrowser
NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
#endif
float extra;
if (!editing_)
extra = 0;
else {
UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
CGRect peripheral([assistant peripheralFrame]);
#if LogBrowser
NSLog(@"per:%f", peripheral.size.height);
#endif
extra = peripheral.size.height;
}
CGRect subrect([scroller_ frame]);
subrect.size.height -= extra;
[scroller_ setScrollerIndicatorSubrect:subrect];
NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
[webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
CGSize size(size_);
size.height += extra;
[scroller_ setContentSize:size];
[scroller_ releaseRubberBandIfNecessary];
}
- (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
size_ = frame.size;
#if LogBrowser
NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
#endif
[self _fixScroller];
}
- (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
[self view:sender didSetFrame:frame];
}
- (void) pushPage:(RVPage *)page {
[page setDelegate:delegate_];
[self setBackButtonTitle:title_];
[book_ pushPage:page];
}
- (void) _pushPage {
if (pushed_)
return;
[self autorelease];
pushed_ = true;
[book_ pushPage:self];
}
- (void) swapPage:(RVPage *)page {
[page setDelegate:delegate_];
if (pushed_)
[book_ swapPage:page];
else
[book_ pushPage:page];
}
- (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
#if LogBrowser
NSLog(@"getSpecial:%@", url);
#endif
NSString *href([url absoluteString]);
NSString *scheme([[url scheme] lowercaseString]);
RVPage *page = nil;
if ([href hasPrefix:@"apptapp://package/"])
page = [delegate_ pageForPackage:[href substringFromIndex:18]];
else if ([scheme isEqualToString:@"cydia"]) {
page = [delegate_ pageForURL:url hasTag:NULL];
if (page == nil)
return false;
} else if (![scheme isEqualToString:@"apptapp"])
return false;
if (page != nil)
if (swap)
[self swapPage:page];
else
[self pushPage:page];
return true;
}
- (void) webViewShow:(WebView *)sender {
/* XXX: this is where I cry myself to sleep */
}
- (bool) _allowJavaScriptPanel {
return true;
}
- (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
if (![self _allowJavaScriptPanel])
return;
[self retain];
UIActionSheet *sheet = [[[UIActionSheet alloc]
initWithTitle:nil
buttons:[NSArray arrayWithObjects:@"OK", nil]
defaultButtonIndex:0
delegate:self
context:@"alert"
] autorelease];
[sheet setBodyText:message];
[sheet popupAlertAnimated:YES];
}
- (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
if (![self _allowJavaScriptPanel])
return NO;
UIActionSheet *sheet = [[[UIActionSheet alloc]
initWithTitle:nil
buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil]
defaultButtonIndex:0
delegate:indirect_
context:@"confirm"
] autorelease];
[sheet setNumberOfRows:1];
[sheet setBodyText:message];
[sheet popupAlertAnimated:YES];
NSRunLoop *loop([NSRunLoop currentRunLoop]);
NSDate *future([NSDate distantFuture]);
while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
NSNumber *confirm([confirm_ autorelease]);
confirm_ = nil;
return [confirm boolValue];
}
- (void) setAutoPopup:(BOOL)popup {
popup_ = popup;
}
- (void) setPopupHook:(id)function {
if (closer_ != nil)
[closer_ autorelease];
closer_ = function == nil ? nil : [function retain];
}
- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
if (button_ != nil)
[button_ autorelease];
button_ = 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 reloadButtons];
}
- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
if (button_ != nil)
[button_ autorelease];
button_ = 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 reloadButtons];
}
- (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
editing_ = true;
}
- (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
[self _fixScroller];
}
- (void) webViewDidEndEditingFormElements:(WebView *)sender {
editing_ = false;
[self _fixScroller];
}
- (void) webViewClose:(WebView *)sender {
[book_ close];
}
- (void) close {
[book_ close];
}
- (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
[window setValue:cydia_ forKey:@"cydia"];
}
- (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
NSLog(@"err:%@", error);
}
- (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
#if LogBrowser
NSLog(@"nwa:%@", name);
#endif
if (NSURL *url = [request URL]) {
if (name == nil) unknown: {
if (![self getSpecial:url swap:NO]) {
NSString *scheme([[url scheme] lowercaseString]);
if ([scheme isEqualToString:@"mailto"])
[delegate_ openMailToURL:url];
else goto use;
}
} else if ([name isEqualToString:@"_open"])
[delegate_ openURL:url];
else if ([name isEqualToString:@"_popup"]) {
NSString *scheme([[url scheme] lowercaseString]);
if ([scheme isEqualToString:@"mailto"])
[delegate_ openMailToURL:url];
else {
RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
[book setHook:indirect_];
RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
if (page == nil) {
/* XXX: call createWebViewWithRequest instead? */
[self setBackButtonTitle:title_];
BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
[browser loadURL:url];
page = browser;
}
[book setDelegate:delegate_];
[page setDelegate:delegate_];
[book setPage:page];
[book_ pushBook:book];
}
} else goto unknown;
[listener ignore];
} else use:
[listener use];
}
- (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
if ([WebView canShowMIMEType:type])
[listener use];
else {
// XXX: handle more mime types!
[listener ignore];
WebView *webview([webview_ webView]);
if (frame == [webview mainFrame])
[UIApp openURL:[request URL]];
}
}
- (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
if (request == nil) ignore: {
[listener ignore];
return;
}
NSURL *url([request URL]);
if (url == nil) use: {
if (!error_ && [frame parentFrame] == nil) {
if (request_ != nil)
[request_ autorelease];
request_ = [request retain];
#if LogBrowser
NSLog(@"dpn:%@", request_);
#endif
}
[listener use];
WebView *webview([webview_ webView]);
if (frame == [webview mainFrame])
[self _pushPage];
return;
}
#if LogBrowser
else NSLog(@"nav:%@:%@", url, [action description]);
#endif
const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
if (
[capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
[capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
) {
open:
[UIApp openURL:url];
goto ignore;
}
int store(_not(int));
if (NSURL *itms = [url itmsURL:&store]) {
#if LogBrowser
NSLog(@"itms#%@#%u#%@", url, store, itms);
#endif
if (
store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
store == 2 && [capability containsObject:@"com.apple.AppStore"]
) {
url = itms;
goto open;
}
}
NSString *scheme([[url scheme] lowercaseString]);
if ([scheme isEqualToString:@"tel"]) {
// XXX: intelligence
goto open;
}
if ([scheme isEqualToString:@"mailto"]) {
[delegate_ openMailToURL:url];
goto ignore;
}
if ([self getSpecial:url swap:YES])
goto ignore;
else if ([WebView _canHandleRequest:request])
goto use;
else if ([url isSpringboardHandledURL])
goto open;
else
goto use;
}
- (void) webView:(WebView *)sender setStatusText:(NSString *)text {
//lprintf("Status:%s\n", [text UTF8String]);
}
- (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
NSString *context([sheet context]);
if ([context isEqualToString:@"alert"]) {
[self autorelease];
[sheet dismiss];
} else if ([context isEqualToString:@"confirm"]) {
switch (button) {
case 1:
confirm_ = [NSNumber numberWithBool:YES];
break;
case 2:
confirm_ = [NSNumber numberWithBool:NO];
break;
}
[sheet dismiss];
} else if ([context isEqualToString:@"challenge"]) {
id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
switch (button) {
case 1: {
NSString *username([[sheet textFieldAtIndex:0] text]);
NSString *password([[sheet textFieldAtIndex:1] text]);
NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
[sender useCredential:credential forAuthenticationChallenge:challenge_];
} break;
case 2:
[sender cancelAuthenticationChallenge:challenge_];
break;
default:
_assert(false);
}
[challenge_ release];
challenge_ = nil;
[sheet dismiss];
} else if ([context isEqualToString:@"submit"]) {
switch (button) {
case 1:
break;
case 2:
if (request_ != nil)
[webview_ loadRequest:request_];
break;
default:
_assert(false);
}
[sheet dismiss];
}
}
- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
challenge_ = [challenge retain];
NSURLProtectionSpace *space([challenge protectionSpace]);
NSString *realm([space realm]);
if (realm == nil)
realm = @"";
UIActionSheet *sheet = [[[UIActionSheet alloc]
initWithTitle:realm
buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
defaultButtonIndex:0
delegate:self
context:@"challenge"
] autorelease];
[sheet setNumberOfRows:1];
[sheet addTextFieldWithValue:@"" label:@"username"];
[sheet addTextFieldWithValue:@"" label:@"password"];
UITextField *username([sheet textFieldAtIndex:0]); {
UITextInputTraits *traits([username textInputTraits]);
[traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[traits setAutocorrectionType:UITextAutocorrectionTypeNo];
[traits setKeyboardType:UIKeyboardTypeASCIICapable];
[traits setReturnKeyType:UIReturnKeyNext];
}
UITextField *password([sheet textFieldAtIndex:1]); {
UITextInputTraits *traits([password textInputTraits]);
[traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[traits setAutocorrectionType:UITextAutocorrectionTypeNo];
[traits setKeyboardType:UIKeyboardTypeASCIICapable];
// XXX: UIReturnKeyDone
[traits setReturnKeyType:UIReturnKeyNext];
[traits setSecureTextEntry:YES];
}
[sheet popupAlertAnimated:YES];
}
- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
return [self _addHeadersToRequest:request];
}
- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
//- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
#if LogBrowser
NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
//NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
#endif
NSNumber *value([features objectForKey:@"width"]);
float width(value == nil ? 0 : [value floatValue]);
RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
/* XXX: deal with cydia:// pages */
BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
if (features != nil && popup_) {
[book setDelegate:delegate_];
[book setHook:indirect_];
[browser setDelegate:delegate_];
[browser loadRequest:request];
[book setPage:browser];
[book_ pushBook:book];
} else if (request == nil) {
[self setBackButtonTitle:title_];
[browser setDelegate:delegate_];
[browser retain];
} else {
[self pushPage:browser];
[browser loadRequest:request];
}
return [browser webView];
}
- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
//return [self webView:sender createWebViewWithRequest:request userGesture:YES];
}
- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
if ([frame parentFrame] != nil)
return;
title_ = [title retain];
[book_ reloadTitleForPage:self];
}
- (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
if ([frame parentFrame] != nil)
return;
[webview_ resignFirstResponder];
reloading_ = false;
loading_ = true;
[self reloadButtons];
if (title_ != nil) {
[title_ release];
title_ = nil;
}
if (button_ != nil) {
[button_ release];
button_ = nil;
}
if (style_ != nil) {
[style_ release];
style_ = nil;
}
if (function_ != nil) {
[function_ release];
function_ = nil;
}
if (closer_ != nil) {
[closer_ release];
closer_ = nil;
}
[book_ reloadTitleForPage:self];
[scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
[scroller_ setZoomScale:1 duration:0];
CGRect webrect = [scroller_ bounds];
webrect.size.height = 0;
[webview_ setFrame:webrect];
}
- (void) _finishLoading {
if (!reloading_) {
loading_ = false;
[self reloadButtons];
}
}
- (bool) isLoading {
return loading_;
}
- (void) reloadButtons {
if ([self isLoading])
[indicator_ startAnimation];
else
[indicator_ stopAnimation];
[super reloadButtons];
}
- (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
}
- (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
}
- (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
}
- (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
[self _pushPage];
return [webview_ webView:sender didCommitLoadForFrame:frame];
}
- (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
}
- (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
if ([frame parentFrame] == nil) {
[self _finishLoading];
if (DOMDocument *document = [frame DOMDocument])
if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
for (DOMHTMLBodyElement *body in 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 pinStripeColor];
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 pinStripeColor]];
break;
}
}
return [webview_ webView:sender didFinishLoadForFrame:frame];
}
- (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
if ([frame parentFrame] != nil)
return;
if (reloading_)
return;
[self _finishLoading];
[self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
[[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
[[error localizedDescription] stringByAddingPercentEscapes]
]]];
error_ = true;
}
- (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
#if LogBrowser || ForSaurik
lprintf("Console:%s\n", [[dictionary description] UTF8String]);
#endif
}
- (void) setViewportWidth:(float)width {
width_ = width ? width != 0 : [[self class] defaultWidth];
[webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
}
- (id) initWithBook:(RVBook *)book forWidth:(float)width {
if ((self = [super initWithBook:book]) != nil) {
loading_ = false;
popup_ = false;
struct CGRect bounds = [self bounds];
scroller_ = [[UIScroller alloc] initWithFrame:bounds];
[self addSubview:scroller_];
[scroller_ setShowBackgroundShadow:NO];
[scroller_ setFixedBackgroundPattern:YES];
[scroller_ setBackgroundColor:[UIColor pinStripeColor]];
[scroller_ setScrollingEnabled:YES];
[scroller_ setAdjustForContentSizeChange:YES];
[scroller_ setClipsSubviews:YES];
[scroller_ setAllowsRubberBanding:YES];
[scroller_ setScrollDecelerationFactor:0.99];
[scroller_ setDelegate:self];
CGRect webrect = [scroller_ bounds];
webrect.size.height = 0;
WebView *webview;
#if RecycleWebViews
webview_ = [Documents_ lastObject];
if (webview_ != nil) {
webview_ = [webview_ retain];
webview = [webview_ webView];
[Documents_ removeLastObject];
[webview_ setFrame:webrect];
} else {
#else
if (true) {
#endif
webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
webview = [webview_ webView];
// XXX: this is terribly (too?) expensive
//[webview_ setDrawsBackground:NO];
[webview setPreferencesIdentifier:@"Cydia"];
[webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
[webview_ setAllowsMessaging:YES];
[webview_ setTilingEnabled:YES];
[webview_ setDrawsGrid:NO];
[webview_ setLogsTilingChanges:NO];
[webview_ setTileMinificationFilter:kCAFilterNearest];
[webview_ setDetectsPhoneNumbers:NO];
[webview_ setAutoresizes:YES];
[webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
[webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
[webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
//[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
[webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
[webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
[webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
[webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
[webview_ _setDocumentType:0x4];
[webview_ setZoomsFocusedFormControl:YES];
[webview_ setContentsPosition:7];
[webview_ setEnabledGestures:0xa];
[webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
[webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
[webview_ setSmoothsFonts:YES];
[webview_ setAllowsImageSheet:YES];
[webview _setUsesLoaderCache:YES];
[webview setGroupName:@"CydiaGroup"];
[webview _setLayoutInterval:0];
}
[self setViewportWidth:width];
[webview_ setDelegate:self];
[webview_ setGestureDelegate:self];
[webview_ setFormEditingDelegate:self];
[webview_ setInteractionDelegate:self];
[scroller_ addSubview:webview_];
//NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
[indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
NSString *application = package == nil ? @"Cydia" : [NSString
stringWithFormat:@"Cydia/%@",
[package installed]
];
if (Product_ != nil)
application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
if (Build_ != nil)
application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
if (Safari_ != nil)
application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
/* XXX: lookup application directory? */
/*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"])
if (NSString *version = [safari objectForKey:@"SafariProductVersion"])
application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/
[webview setApplicationNameForUserAgent:application];
indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
[webview setFrameLoadDelegate:self];
[webview setResourceLoadDelegate:indirect_];
[webview setUIDelegate:self];
[webview setScriptDebugDelegate:self];
[webview setPolicyDelegate:self];
[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
} return self;
}
- (id) initWithBook:(RVBook *)book {
return [self initWithBook:book forWidth:0];
}
- (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
[webview_ redrawScaledDocument];
}
- (void) callFunction:(WebScriptObject *)function {
WebView *webview([webview_ webView]);
WebFrame *frame([webview mainFrame]);
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);
}
[delegate_ clearFirstResponder];
JSObjectRef object([function JSObject]);
JSGlobalContextRef context([frame globalContext]);
JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
if (settings != NULL)
settings->setJavaScriptCanOpenWindowsAutomatically(no);
}
- (void) didCloseBook:(RVBook *)book {
if (closer_ != nil)
[self callFunction:closer_];
}
- (void) __rightButtonClicked {
reloading_ = true;
[self reloadURL];
}
- (void) _rightButtonClicked {
if (function_ == nil)
[self __rightButtonClicked];
else
[self callFunction:function_];
}
- (id) _rightButtonTitle {
return @"Reload";
}
- (id) rightButtonTitle {
return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
}
- (UINavigationButtonStyle) rightButtonStyle {
if (style_ == nil) normal:
return UINavigationButtonStyleNormal;
else if ([style_ isEqualToString:@"Normal"])
return UINavigationButtonStyleNormal;
else if ([style_ isEqualToString:@"Back"])
return UINavigationButtonStyleBack;
else if ([style_ isEqualToString:@"Highlighted"])
return UINavigationButtonStyleHighlighted;
else if ([style_ isEqualToString:@"Destructive"])
return UINavigationButtonStyleDestructive;
else goto normal;
}
- (NSString *) title {
return title_ == nil ? @"Loading" : title_;
}
- (NSString *) backButtonTitle {
return @"Browser";
}
- (void) setPageActive:(BOOL)active {
if (!active)
[indicator_ removeFromSuperview];
else
[[book_ navigationBar] addSubview:indicator_];
}
- (void) resetViewAnimated:(BOOL)animated {
}
- (void) setPushed:(bool)pushed {
pushed_ = pushed;
}
+ (float) defaultWidth {
return 980;
}
@end
|