]> git.saurik.com Git - cydia.git/commitdiff
Add HTTP Referer: tracking.
authorJay Freeman (saurik) <saurik@saurik.com>
Fri, 25 Mar 2011 08:46:50 +0000 (01:46 -0700)
committerJay Freeman (saurik) <saurik@saurik.com>
Fri, 25 Mar 2011 08:46:50 +0000 (01:46 -0700)
CyteKit/WebViewController.h
CyteKit/WebViewController.mm
MobileCydia.mm

index cb9af9792f3bdcfd25743233afb6e8390e54207e..c2e64d53cf48c0fe29a7fc8fae3224359c11c77a 100644 (file)
@@ -53,7 +53,7 @@
 @protocol CyteWebViewControllerDelegate
 - (void) retainNetworkActivityIndicator;
 - (void) releaseNetworkActivityIndicator;
-- (CyteViewController *) pageForURL:(NSURL *)url forExternal:(BOOL)external;
+- (CyteViewController *) pageForURL:(NSURL *)url forExternal:(BOOL)external withReferrer:(NSString *)referrer;
 - (void) unloadData;
 @end
 
 
 - (void) setRequest:(NSURLRequest *)request;
 - (void) setURL:(NSURL *)url;
+- (void) setURL:(NSURL *)url withReferrer:(NSString *)referrer;
 
 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy;
 - (void) loadURL:(NSURL *)url;
index 4d4d250ebf2359d5515efa6b0e7ecb8eac078e42..9c85b29703b54a222dc48e52b9a155c2940ab5b3 100644 (file)
@@ -166,12 +166,16 @@ float CYScrollViewDecelerationRateNormal;
     return url;
 }
 
-- (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
-    return [NSURLRequest
+- (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy referrer:(NSString *)referrer {
+    NSMutableURLRequest *request([NSMutableURLRequest
         requestWithURL:[self URLWithURL:url]
         cachePolicy:policy
         timeoutInterval:DefaultTimeout_
-    ];
+    ]);
+
+    [request setValue:referrer forHTTPHeaderField:@"Referer"];
+
+    return request;
 }
 
 - (void) setRequest:(NSURLRequest *)request {
@@ -180,11 +184,15 @@ float CYScrollViewDecelerationRateNormal;
 }
 
 - (void) setURL:(NSURL *)url {
-    [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]];
+    [self setURL:url withReferrer:nil];
+}
+
+- (void) setURL:(NSURL *)url withReferrer:(NSString *)referrer {
+    [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy referrer:referrer]];
 }
 
 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
-    [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
+    [self loadRequest:[self requestWithURL:url cachePolicy:policy referrer:nil]];
 }
 
 - (void) loadURL:(NSURL *)url {
@@ -371,11 +379,20 @@ float CYScrollViewDecelerationRateNormal;
     }
 }
 
-- (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
+- (void) pushRequest:(NSURLRequest *)request forAction:(NSDictionary *)action asPop:(bool)pop {
+    WebFrame *frame(nil);
+    if (NSDictionary *WebActionElement = [action objectForKey:@"WebActionElementKey"])
+        frame = [WebActionElement objectForKey:@"WebElementFrame"];
+    if (frame == nil)
+        frame = [[[[self webView] _documentView] webView] mainFrame];
+
+    WebDataSource *source([frame provisionalDataSource] ?: [frame dataSource]);
+    NSString *referrer([request valueForHTTPHeaderField:@"Referer"] ?: [[[source request] URL] absoluteString]);
+
     NSURL *url([request URL]);
 
     // XXX: filter to internal usage?
-    CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
+    CyteViewController *page([delegate_ pageForURL:url forExternal:NO withReferrer:referrer]);
 
     if (page == nil) {
         CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
@@ -423,7 +440,7 @@ float CYScrollViewDecelerationRateNormal;
 
 - (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);
+    NSLog(@"decidePolicyForNavigationAction:%@ request:%@ %@ frame:%@", action, request, [request allHTTPHeaderFields], frame);
 #endif
 
     if ([frame parentFrame] == nil) {
@@ -432,7 +449,7 @@ float CYScrollViewDecelerationRateNormal;
 
             if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
                 if (url != nil)
-                    [self pushRequest:request asPop:NO];
+                    [self pushRequest:request forAction:action asPop:NO];
                 [listener ignore];
             }
         }
@@ -446,23 +463,23 @@ float CYScrollViewDecelerationRateNormal;
                 request_ = request;
 }
 
-- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
+- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
 #if LogBrowser
-    NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
+    NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ %@ newFrameName:%@", action, request, [request allHTTPHeaderFields], name);
 #endif
 
     NSURL *url([request URL]);
     if (url == nil)
         return;
 
-    if ([frame isEqualToString:@"_open"])
+    if ([name isEqualToString:@"_open"])
         [delegate_ openURL:url];
     else {
         NSString *scheme([[url scheme] lowercaseString]);
         if ([scheme isEqualToString:@"mailto"])
             [self _openMailToURL:url];
         else
-            [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
+            [self pushRequest:request forAction:action asPop:[name isEqualToString:@"_popup"]];
     }
 
     [listener ignore];
index 74c8993f08267d1833d4d722544c6be160b387a3..e879c9be066c6b59624c7a9e32bdfc86fa26d9c6 100644 (file)
@@ -885,7 +885,6 @@ static NSString *CYHex(NSData *data, bool reverse = false) {
 - (void) showSettings;
 - (UIProgressHUD *) addProgressHUD;
 - (void) removeProgressHUD:(UIProgressHUD *)hud;
-- (CyteViewController *) pageForPackage:(NSString *)name;
 - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item;
 - (void) reloadDataWithInvocation:(NSInvocation *)invocation;
 @end
@@ -5940,7 +5939,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     _H<UIBarButtonItem> button_;
 }
 
-- (id) initWithDatabase:(Database *)database forPackage:(NSString *)name;
+- (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer;
 
 @end
 
@@ -6038,12 +6037,12 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 }
 #endif
 
-- (id) initWithDatabase:(Database *)database forPackage:(NSString *)name {
+- (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer {
     if ((self = [super init]) != nil) {
         database_ = database;
         buttons_ = [NSMutableArray arrayWithCapacity:4];
         name_ = name == nil ? @"" : [NSString stringWithString:name];
-        [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/package/%@", UI_, (id) name_]]];
+        [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/package/%@", UI_, (id) name_]] withReferrer:referrer];
     } return self;
 }
 
@@ -6119,6 +6118,10 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 
 @implementation PackageListController
 
+- (NSURL *) referrerURL {
+    return [self navigationURL];
+}
+
 - (bool) isSummarized {
     return false;
 }
@@ -6217,7 +6220,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 }
 
 - (void) didSelectPackage:(Package *)package {
-    CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id]] autorelease]);
+    CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id] withReferrer:[[self referrerURL] absoluteString]] autorelease]);
     [view setDelegate:delegate_];
     [[self navigationController] pushViewController:view animated:YES];
 }
@@ -7295,6 +7298,14 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
 
 @implementation SectionController
 
+- (NSURL *) referrerURL {
+    NSString *name = section_;
+    if (name == nil)
+        name = @"all";
+
+    return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/sections/%@", UI_, [name stringByAddingPercentEscapesIncludingReserved]]];
+}
+
 - (NSURL *) navigationURL {
     NSString *name = section_;
     if (name == nil)
@@ -7360,7 +7371,15 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
     if ([frame isEqualToString:@"_open"])
         [delegate_ openURL:url];
     else {
-        CyteViewController *controller([delegate_ pageForURL:url forExternal:NO] ?: [[[CydiaWebViewController alloc] initWithRequest:request] autorelease]);
+        WebFrame *frame(nil);
+        if (NSDictionary *WebActionElement = [action objectForKey:@"WebActionElementKey"])
+            frame = [WebActionElement objectForKey:@"WebElementFrame"];
+        if (frame == nil)
+            frame = [view mainFrame];
+
+        WebDataSource *source([frame provisionalDataSource] ?: [frame dataSource]);
+
+        CyteViewController *controller([delegate_ pageForURL:url forExternal:NO withReferrer:([request valueForHTTPHeaderField:@"Referer"] ?: [[[source request] URL] absoluteString])] ?: [[[CydiaWebViewController alloc] initWithRequest:request] autorelease]);
         [controller setDelegate:delegate_];
         [[self navigationController] pushViewController:controller animated:YES];
     }
@@ -7685,7 +7704,7 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
 
 - (NSIndexPath *) tableView:(UITableView *)table willSelectRowAtIndexPath:(NSIndexPath *)path {
     Package *package([self packageAtIndexPath:path]);
-    CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id]] autorelease]);
+    CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id] withReferrer:[NSString stringWithFormat:@"%@/#!/changes/", UI_]] autorelease]);
     [view setDelegate:delegate_];
     [[self navigationController] pushViewController:view animated:YES];
     return path;
@@ -7782,7 +7801,15 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
     if ([frame isEqualToString:@"_open"])
         [delegate_ openURL:url];
     else {
-        CyteViewController *controller([delegate_ pageForURL:url forExternal:NO] ?: [[[CydiaWebViewController alloc] initWithRequest:request] autorelease]);
+        WebFrame *frame(nil);
+        if (NSDictionary *WebActionElement = [action objectForKey:@"WebActionElementKey"])
+            frame = [WebActionElement objectForKey:@"WebElementFrame"];
+        if (frame == nil)
+            frame = [view mainFrame];
+
+        WebDataSource *source([frame provisionalDataSource] ?: [frame dataSource]);
+
+        CyteViewController *controller([delegate_ pageForURL:url forExternal:NO withReferrer:([request valueForHTTPHeaderField:@"Referer"] ?: [[[source request] URL] absoluteString])] ?: [[[CydiaWebViewController alloc] initWithRequest:request] autorelease]);
         [controller setDelegate:delegate_];
         [[self navigationController] pushViewController:controller animated:YES];
     }
@@ -7971,6 +7998,10 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
 
 @implementation SearchController
 
+- (NSURL *) referrerURL {
+    return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/search?q=%@", UI_, [([search_ text] ?: @"") stringByAddingPercentEscapesIncludingReserved]]];
+}
+
 - (NSURL *) navigationURL {
     if ([search_ text] == nil || [[search_ text] isEqualToString:@""])
         return [NSURL URLWithString:@"cydia://search"];
@@ -8287,6 +8318,10 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
 
 @implementation InstalledController
 
+- (NSURL *) referrerURL {
+    return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/installed/", UI_]];
+}
+
 - (NSURL *) navigationURL {
     return [NSURL URLWithString:@"cydia://installed"];
 }
@@ -8462,6 +8497,10 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
 
 @implementation SourceController
 
+- (NSURL *) referrerURL {
+    return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/sources/%@", UI_, [key_ stringByAddingPercentEscapesIncludingReserved]]];
+}
+
 - (NSURL *) navigationURL {
     return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sources/%@", [key_ stringByAddingPercentEscapesIncludingReserved]]];
 }
@@ -9801,11 +9840,11 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
     [window_ setUserInteractionEnabled:YES];
 }
 
-- (CyteViewController *) pageForPackage:(NSString *)name {
-    return [[[CYPackageController alloc] initWithDatabase:database_ forPackage:name] autorelease];
+- (CyteViewController *) pageForPackage:(NSString *)name withReferrer:(NSString *)referrer {
+    return [[[CYPackageController alloc] initWithDatabase:database_ forPackage:name withReferrer:referrer] autorelease];
 }
 
-- (CyteViewController *) pageForURL:(NSURL *)url forExternal:(BOOL)external {
+- (CyteViewController *) pageForURL:(NSURL *)url forExternal:(BOOL)external withReferrer:(NSString *)referrer {
     NSString *scheme([[url scheme] lowercaseString]);
     if ([[url absoluteString] length] <= [scheme length] + 3)
         return nil;
@@ -9813,7 +9852,7 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
     NSArray *components([path componentsSeparatedByString:@"/"]);
 
     if ([scheme isEqualToString:@"apptapp"] && [components count] > 0 && [[components objectAtIndex:0] isEqualToString:@"package"]) {
-        CyteViewController *controller([self pageForPackage:[components objectAtIndex:1]]);
+        CyteViewController *controller([self pageForPackage:[components objectAtIndex:1] withReferrer:referrer]);
         if (controller != nil)
             [controller setDelegate:self];
         return controller;
@@ -9866,7 +9905,7 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
         NSString *argument = [components objectAtIndex:1];
 
         if ([base isEqualToString:@"package"]) {
-            controller = [self pageForPackage:argument];
+            controller = [self pageForPackage:argument withReferrer:referrer];
         }
 
         if (!external && [base isEqualToString:@"search"]) {
@@ -9914,7 +9953,7 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
 }
 
 - (BOOL) openCydiaURL:(NSURL *)url forExternal:(BOOL)external {
-    CyteViewController *page([self pageForURL:url forExternal:external]);
+    CyteViewController *page([self pageForURL:url forExternal:external withReferrer:nil]);
 
     if (page != nil)
         [tabbar_ setUnselectedViewController:page];
@@ -10217,7 +10256,7 @@ _trace();
         for (unsigned int nav = 0; nav < [stack count]; nav++) {
             NSString *addr = [stack objectAtIndex:nav];
             NSURL *url = [NSURL URLWithString:addr];
-            CyteViewController *page = [self pageForURL:url forExternal:NO];
+            CyteViewController *page = [self pageForURL:url forExternal:NO withReferrer:nil];
             if (page != nil)
                 [current addObject:page];
         }