Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(827)

Unified Diff: ios/crnet/crnet_consumer/main.m

Issue 1142383006: CrNet: add pauseable NSURLProtocol and switch to using it (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | ios/crnet/crnet_environment.mm » ('j') | ios/net/crn_http_protocol_handler.mm » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ios/crnet/crnet_consumer/main.m
diff --git a/ios/crnet/crnet_consumer/main.m b/ios/crnet/crnet_consumer/main.m
index 0a3e5f482182645d63278148bac4df7aee5a480b..14af67c10bb6ed34369d3bbe7a754dfce819cd84 100644
--- a/ios/crnet/crnet_consumer/main.m
+++ b/ios/crnet/crnet_consumer/main.m
@@ -3,13 +3,124 @@
// found in the LICENSE file.
#import <Foundation/Foundation.h>
-#import <UIKit/UIKit.h>
+
+#import "CrNet.h"
#import "crnet_consumer_app_delegate.h"
+@interface TestDelegate : NSObject <NSURLSessionDelegate,
+ NSURLSessionDataDelegate,
+ NSURLSessionTaskDelegate>
+
+- (id)initWithSemaphore:(dispatch_semaphore_t)sem;
+
+@end
+
+@implementation TestDelegate {
+ dispatch_semaphore_t _sem;
+}
+
+- (id)initWithSemaphore:(dispatch_semaphore_t)sem {
+ _sem = sem;
+ return self;
+}
+
+- (void)URLSession:(NSURLSession *)session
+didBecomeInvalidWithError:(NSError *)error {
+ NSLog(@"URLSession didBecomeInvalidWithError %@", error);
+}
+
+- (void)URLSession:(NSURLSession *)session
+ task:(NSURLSessionTask *)task
+didCompleteWithError:(NSError *)error {
+ NSLog(@"URLSessionTask didCompleteWithError %@", error);
+ dispatch_semaphore_signal(_sem);
+}
+
+- (void)URLSession:(NSURLSession *)session
+ task:(NSURLSessionTask *)task
+didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
+ completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disp,
+ NSURLCredential* credential))completionHandler {
+ NSLog(@"URLSessionTask didReceiveChallenge %@", challenge);
+ completionHandler(NSURLSessionAuthChallengeUseCredential, nil);
+}
+
+- (void)URLSession:(NSURLSession *)session
+ task:(NSURLSessionTask *)task
+willPerformHTTPRedirection:(NSHTTPURLResponse *)response
+ newRequest:(NSURLRequest *)request
+completionHandler:(void (^)(NSURLRequest *))completionHandler {
+ NSLog(@"URLSessionTask willPerformHttpRedirection %@", request);
+ completionHandler(request);
+}
+
+- (void)URLSession:(NSURLSession *)session
+ dataTask:(NSURLSessionDataTask *)dataTask
+didReceiveResponse:(NSURLResponse *)response
+ completionHandler:(void (^)(NSURLSessionResponseDisposition
+ disposition))completionHandler {
+ NSHTTPURLResponse* resp = (NSHTTPURLResponse *)response;
+ NSLog(@"URLSessionDataTask didReceiveResponse status %ld",
+ (long)resp.statusCode);
+ completionHandler(NSURLSessionResponseAllow);
+}
+
+- (void)URLSession:(NSURLSession *)session
+ dataTask:(NSURLSessionDataTask *)dataTask
+ didReceiveData:(NSData *)data {
+ NSLog(@"URLSessionDataTask didReceiveData %lu bytes",
+ (unsigned long)data.length);
+}
+
+- (void)URLSession:(NSURLSession *)session
+ dataTask:(NSURLSessionDataTask *)dataTask
+ willCacheResponse:(NSCachedURLResponse *)proposedResponse
+ completionHandler:(void (^)(NSCachedURLResponse
+ *cachedResponse))completionHandler {
+ NSLog(@"URLSessionDataTask willCacheResponse %@", proposedResponse);
+ completionHandler(proposedResponse);
+}
+
+@end
+
+void use_crnet(NSURLSessionConfiguration* config) {
+ [CrNet setPartialUserAgent:@"Foo/1.0"];
+ [CrNet install];
+ [CrNet installIntoSessionConfiguration:config];
+}
+
+void fetch(NSURLSession* session, NSString* url) {
+ NSURL* testURL = [NSURL URLWithString:url];
+ NSURLSessionDataTask* task = [session dataTaskWithURL:testURL];
+ NSLog(@"fetch: starting %@", url);
+ [task resume];
+}
+
int main(int argc, char *argv[]) {
- @autoreleasepool {
- return UIApplicationMain(
- argc, argv, nil, NSStringFromClass([CrNetConsumerAppDelegate class]));
- }
+ dispatch_semaphore_t sem = dispatch_semaphore_create(0);
+ TestDelegate* delegate = [[TestDelegate alloc] initWithSemaphore:sem];
+ NSURLSessionConfiguration* config =
+ [NSURLSessionConfiguration ephemeralSessionConfiguration];
+ NSURLSession* session =
+ [NSURLSession sessionWithConfiguration:config
+ delegate:delegate
+ delegateQueue:nil];
+
+ NSLog(@"main: installing crnet");
+ use_crnet(config);
+
+ fetch(session, @"https://www.google.com");
+ fetch(session, @"https://twitter.com");
+ fetch(session, @"https://m.facebook.com");
+ fetch(session, @"https://www.yahoo.com");
+
+ int64_t secs = 1000000000LL;
+ secs *= 5LL;
+ NSLog(@"main: waiting");
+ dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs));
+ dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs));
+ dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs));
+ dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs));
+ NSLog(@"main: timeout, exiting");
}
« no previous file with comments | « no previous file | ios/crnet/crnet_environment.mm » ('j') | ios/net/crn_http_protocol_handler.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698