OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #import "cronet_sample_app_delegate.h" |
| 6 |
| 7 #import "components/cronet/ios/Cronet.h" |
| 8 |
| 9 #include "base/format_macros.h" |
| 10 #import "cronet_sample_view_controller.h" |
| 11 |
| 12 @implementation CronetSampleAppDelegate { |
| 13 NSUInteger _counter; |
| 14 dispatch_queue_t _backgroundRequestQueue; |
| 15 } |
| 16 |
| 17 @synthesize window; |
| 18 @synthesize viewController; |
| 19 |
| 20 // Returns a file name to save net internals logging. This method suffixes |
| 21 // the ivar |_counter| to the file name so a new name can be obtained by |
| 22 // modifying that. |
| 23 - (NSString*)currentNetLogFileName { |
| 24 return [NSString |
| 25 stringWithFormat:@"cronet_sample-net-log%" PRIuNS ".json", _counter]; |
| 26 } |
| 27 |
| 28 - (BOOL)application:(UIApplication*)application |
| 29 didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { |
| 30 [Cronet setUserAgent:@"Dummy/1.0" partial:YES]; |
| 31 [Cronet setQuicEnabled:YES]; |
| 32 [Cronet start]; |
| 33 [Cronet startNetLogToFile:[self currentNetLogFileName] logBytes:NO]; |
| 34 [Cronet registerHttpProtocolHandler]; |
| 35 |
| 36 // Just for fun, route chromium.org requests through Cronet. |
| 37 // |
| 38 // |chromiumPrefix| is declared outside the scope of the request block so that |
| 39 // the block references something outside of its own scope, and cannot be |
| 40 // declared as a global block. This makes sure the block is |
| 41 // an __NSStackBlock__, and verifies the fix for http://crbug.com/436175 . |
| 42 NSString* chromiumPrefix = @"www.chromium.org"; |
| 43 [Cronet setRequestFilterBlock:^BOOL(NSURLRequest* request) { |
| 44 BOOL isChromiumSite = [[[request URL] host] hasPrefix:chromiumPrefix]; |
| 45 NSLog(@"RequestFilter:%@ Handling:%d", request, isChromiumSite); |
| 46 return isChromiumSite; |
| 47 }]; |
| 48 |
| 49 NSURLSessionConfiguration* config = |
| 50 [NSURLSessionConfiguration ephemeralSessionConfiguration]; |
| 51 [Cronet installIntoSessionConfiguration:config]; |
| 52 |
| 53 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
| 54 self.viewController = |
| 55 [[CronetSampleViewController alloc] initWithNibName:nil bundle:nil]; |
| 56 self.window.rootViewController = self.viewController; |
| 57 [self.window makeKeyAndVisible]; |
| 58 |
| 59 _backgroundRequestQueue = |
| 60 dispatch_queue_create("Background Request Queue", NULL); |
| 61 |
| 62 return YES; |
| 63 } |
| 64 |
| 65 - (void)performBackgroundRequest { |
| 66 NSURL* url = [NSURL URLWithString:@"http://www.chromium.org"]; |
| 67 NSData* receivedData = [NSData dataWithContentsOfURL:url]; |
| 68 NSLog(@"ReceivedData: %@", receivedData); |
| 69 dispatch_async(_backgroundRequestQueue, ^{ |
| 70 [self performBackgroundRequest]; |
| 71 }); |
| 72 } |
| 73 |
| 74 - (void)applicationDidEnterBackground:(UIApplication*)application { |
| 75 [Cronet stopNetLog]; |
| 76 } |
| 77 |
| 78 - (void)applicationWillEnterForeground:(UIApplication*)application { |
| 79 _counter++; |
| 80 [Cronet startNetLogToFile:[self currentNetLogFileName] logBytes:NO]; |
| 81 } |
| 82 |
| 83 - (void)applicationWillTerminate:(UIApplication*)application { |
| 84 NSLog(@"Cronet Sample Will Terminate"); |
| 85 } |
| 86 |
| 87 @end |
OLD | NEW |