summaryrefslogtreecommitdiff
path: root/CyteKit
diff options
context:
space:
mode:
authorJay Freeman (saurik) <saurik@saurik.com>2017-03-04 20:53:33 -0800
committerJay Freeman (saurik) <saurik@saurik.com>2017-03-04 20:53:33 -0800
commit300a26c1c0cdcc6f86880b6d70e7bdf81d210b4b (patch)
tree81491ae36666bc92649b05e65bdc024e00ae8cbd /CyteKit
parent217e22f0f4f84e74a3649d5e292c283b0c468b7f (diff)
Move cyte diversions and user agents into CyteKit.
Diffstat (limited to 'CyteKit')
-rw-r--r--CyteKit/WebViewController.h9
-rw-r--r--CyteKit/WebViewController.mm66
2 files changed, 73 insertions, 2 deletions
diff --git a/CyteKit/WebViewController.h b/CyteKit/WebViewController.h
index 61c4c1a..477828c 100644
--- a/CyteKit/WebViewController.h
+++ b/CyteKit/WebViewController.h
@@ -30,6 +30,10 @@
@class IndirectDelegate;
+@interface Diversion : NSObject
+- (id) initWithFrom:(NSString *)from to:(NSString *)to;
+@end
+
@protocol CyteWebViewControllerDelegate
- (void) retainNetworkActivityIndicator;
- (void) releaseNetworkActivityIndicator;
@@ -68,6 +72,9 @@
- (id) initWithWidth:(float)width;
- (id) initWithWidth:(float)width ofClass:(Class)_class;
++ (void) setApplicationNameForUserAgent:(NSString *)userAgent;
+- (NSString *) applicationNameForUserAgent;
+
- (void) callFunction:(WebScriptObject *)function;
- (void) reloadURLWithCache:(BOOL)cache;
@@ -107,6 +114,8 @@
- (void) registerFrame:(WebFrame *)frame;
++ (void) addDiversion:(Diversion *)diversion;
+
@end
#endif//CyteKit_WebViewController_H
diff --git a/CyteKit/WebViewController.mm b/CyteKit/WebViewController.mm
index 40b1200..9d1866a 100644
--- a/CyteKit/WebViewController.mm
+++ b/CyteKit/WebViewController.mm
@@ -57,6 +57,57 @@ float CYScrollViewDecelerationRateNormal;
@end
+// Diversion {{{
+static _H<NSMutableSet> Diversions_;
+
+@implementation Diversion {
+ RegEx pattern_;
+ _H<NSString> key_;
+ _H<NSString> format_;
+}
+
+- (id) initWithFrom:(NSString *)from to:(NSString *)to {
+ if ((self = [super init]) != nil) {
+ pattern_ = [from UTF8String];
+ key_ = from;
+ format_ = to;
+ } return self;
+}
+
+- (NSString *) divert:(NSString *)url {
+ return !pattern_(url) ? nil : pattern_->*format_;
+}
+
++ (NSURL *) divertURL:(NSURL *)url {
+ divert:
+ NSString *href([url absoluteString]);
+
+ for (Diversion *diversion in (id) Diversions_)
+ if (NSString *diverted = [diversion divert:href]) {
+#if !ForRelease
+ NSLog(@"div: %@", diverted);
+#endif
+ url = [NSURL URLWithString:diverted];
+ goto divert;
+ }
+
+ return url;
+}
+
+- (NSString *) key {
+ return key_;
+}
+
+- (NSUInteger) hash {
+ return [key_ hash];
+}
+
+- (BOOL) isEqual:(Diversion *)object {
+ return self == object || [self class] == [object class] && [key_ isEqual:[object key]];
+}
+
+@end
+// }}}
/* Indirect Delegate {{{ */
@implementation IndirectDelegate
@@ -178,6 +229,8 @@ float CYScrollViewDecelerationRateNormal;
CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
else // XXX: this actually might be fast on some older systems: we should look into this
CYScrollViewDecelerationRateNormal = 0.998;
+
+ Diversions_ = [NSMutableSet setWithCapacity:0];
}
- (bool) retainsNetworkActivityIndicator {
@@ -215,8 +268,12 @@ float CYScrollViewDecelerationRateNormal;
return (CyteWebViewController *) (IndirectDelegate *) indirect_;
}
++ (void) addDiversion:(Diversion *)diversion {
+ [Diversions_ addObject:diversion];
+}
+
- (NSURL *) URLWithURL:(NSURL *)url {
- return url;
+ return [Diversion divertURL:url];
}
- (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy referrer:(NSString *)referrer {
@@ -981,8 +1038,13 @@ float CYScrollViewDecelerationRateNormal;
} return self;
}
+static _H<NSString> UserAgent_;
++ (void) setApplicationNameForUserAgent:(NSString *)userAgent {
+ UserAgent_ = userAgent;
+}
+
- (NSString *) applicationNameForUserAgent {
- return nil;
+ return UserAgent_;
}
- (void) loadView {