Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ios / crnet / crnet_consumer / main.mm
blob42601c0e482c8d7f00142e3ba8d52774a4b03a39
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import <Foundation/Foundation.h>
7 #import "CrNet.h"
9 #import "crnet_consumer_app_delegate.h"
11 @interface TestDelegate : NSObject<NSURLSessionDelegate,
12                                    NSURLSessionDataDelegate,
13                                    NSURLSessionTaskDelegate>
15 - (id)initWithSemaphore:(dispatch_semaphore_t)sem;
17 @end
19 @implementation TestDelegate {
20   dispatch_semaphore_t _sem;
23 - (id)initWithSemaphore:(dispatch_semaphore_t)sem {
24   _sem = sem;
25   return self;
28 - (void)URLSession:(NSURLSession*)session
29     didBecomeInvalidWithError:(NSError*)error {
30   NSLog(@"URLSession didBecomeInvalidWithError %@", error);
33 - (void)URLSession:(NSURLSession*)session
34                     task:(NSURLSessionTask*)task
35     didCompleteWithError:(NSError*)error {
36   NSLog(@"URLSessionTask didCompleteWithError %@", error);
37   dispatch_semaphore_signal(_sem);
40 - (void)URLSession:(NSURLSession*)session
41                    task:(NSURLSessionTask*)task
42     didReceiveChallenge:(NSURLAuthenticationChallenge*)challenge
43       completionHandler:
44           (void (^)(NSURLSessionAuthChallengeDisposition disp,
45                     NSURLCredential* credential))completionHandler {
46   NSLog(@"URLSessionTask didReceiveChallenge %@", challenge);
47   completionHandler(NSURLSessionAuthChallengeUseCredential, nil);
50 - (void)URLSession:(NSURLSession*)session
51                           task:(NSURLSessionTask*)task
52     willPerformHTTPRedirection:(NSHTTPURLResponse*)response
53                     newRequest:(NSURLRequest*)request
54              completionHandler:(void (^)(NSURLRequest*))completionHandler {
55   NSLog(@"URLSessionTask willPerformHttpRedirection %@", request);
56   completionHandler(request);
59 - (void)URLSession:(NSURLSession*)session
60               dataTask:(NSURLSessionDataTask*)dataTask
61     didReceiveResponse:(NSURLResponse*)response
62      completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))
63                            completionHandler {
64   NSHTTPURLResponse* resp = (NSHTTPURLResponse*)response;
65   NSLog(@"URLSessionDataTask didReceiveResponse status %ld",
66         (long)resp.statusCode);
67   completionHandler(NSURLSessionResponseAllow);
70 - (void)URLSession:(NSURLSession*)session
71           dataTask:(NSURLSessionDataTask*)dataTask
72     didReceiveData:(NSData*)data {
73   NSLog(@"URLSessionDataTask didReceiveData %lu bytes",
74         (unsigned long)data.length);
77 - (void)URLSession:(NSURLSession*)session
78              dataTask:(NSURLSessionDataTask*)dataTask
79     willCacheResponse:(NSCachedURLResponse*)proposedResponse
80     completionHandler:
81         (void (^)(NSCachedURLResponse* cachedResponse))completionHandler {
82   NSLog(@"URLSessionDataTask willCacheResponse %@", proposedResponse);
83   completionHandler(proposedResponse);
86 @end
88 void use_crnet(NSURLSessionConfiguration* config) {
89   [CrNet setPartialUserAgent:@"Foo/1.0"];
90   [CrNet install];
91   [CrNet installIntoSessionConfiguration:config];
94 void fetch(NSURLSession* session, NSString* url) {
95   NSURL* testURL = [NSURL URLWithString:url];
96   NSURLSessionDataTask* task = [session dataTaskWithURL:testURL];
97   NSLog(@"fetch: starting %@", url);
98   [task resume];
101 int main(int argc, char *argv[]) {
102   dispatch_semaphore_t sem = dispatch_semaphore_create(0);
103   TestDelegate* delegate = [[TestDelegate alloc] initWithSemaphore:sem];
104   NSURLSessionConfiguration* config =
105       [NSURLSessionConfiguration ephemeralSessionConfiguration];
106   NSURLSession* session = [NSURLSession sessionWithConfiguration:config
107                                                         delegate:delegate
108                                                    delegateQueue:nil];
110   NSLog(@"main: installing crnet");
111   use_crnet(config);
113   fetch(session, @"https://www.google.com");
114   fetch(session, @"https://twitter.com");
115   fetch(session, @"https://m.facebook.com");
116   fetch(session, @"https://www.yahoo.com");
118   int64_t secs = 1000000000LL;
119   secs *= 5LL;
120   NSLog(@"main: waiting");
121   dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs));
122   dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs));
123   dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs));
124   dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs));
125   NSLog(@"main: timeout, exiting");