| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #import "crnet_consumer_app_delegate.h" | |
| 6 | |
| 7 #import <CrNet/CrNet.h> | |
| 8 | |
| 9 #include "base/format_macros.h" | |
| 10 #import "crnet_consumer_view_controller.h" | |
| 11 | |
| 12 @implementation CrNetConsumerAppDelegate { | |
| 13 NSUInteger _counter; | |
| 14 } | |
| 15 | |
| 16 @synthesize window; | |
| 17 @synthesize viewController; | |
| 18 | |
| 19 // Returns a file name to save net internals logging. This method suffixes | |
| 20 // the ivar |_counter| to the file name so a new name can be obtained by | |
| 21 // modifying that. | |
| 22 - (NSString*)currentNetLogFileName { | |
| 23 return [NSString | |
| 24 stringWithFormat:@"crnet-consumer-net-log%" PRIuNS ".json", _counter]; | |
| 25 } | |
| 26 | |
| 27 - (NSString*)SDCHPrefStoreFileName { | |
| 28 NSFileManager* manager = [NSFileManager defaultManager]; | |
| 29 NSArray* possibleURLs = [manager | |
| 30 URLsForDirectory:NSApplicationSupportDirectory | |
| 31 inDomains:NSUserDomainMask]; | |
| 32 NSURL* appSupportDir = [possibleURLs firstObject]; | |
| 33 if (appSupportDir == nil) | |
| 34 return nil; | |
| 35 NSURL* prefStoreFile = [NSURL URLWithString:@"sdch-prefs.json" | |
| 36 relativeToURL:appSupportDir]; | |
| 37 NSError* error = nil; | |
| 38 [manager createDirectoryAtURL:appSupportDir | |
| 39 withIntermediateDirectories:YES | |
| 40 attributes:nil | |
| 41 error:&error]; | |
| 42 return error != nil ? [prefStoreFile path] : nil; | |
| 43 } | |
| 44 | |
| 45 - (BOOL)application:(UIApplication*)application | |
| 46 didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { | |
| 47 [CrNet setPartialUserAgent:@"Dummy/1.0"]; | |
| 48 [CrNet setQuicEnabled:YES]; | |
| 49 [CrNet setSDCHEnabled:YES withPrefStore:[self SDCHPrefStoreFileName]]; | |
| 50 [CrNet install]; | |
| 51 [CrNet startNetLogToFile:[self currentNetLogFileName] logBytes:NO]; | |
| 52 | |
| 53 NSURLSessionConfiguration* config = | |
| 54 [NSURLSessionConfiguration ephemeralSessionConfiguration]; | |
| 55 [CrNet installIntoSessionConfiguration:config]; | |
| 56 | |
| 57 // Just for fun, don't route chromium.org requests through CrNet. | |
| 58 // | |
| 59 // |chromiumPrefix| is declared outside the scope of the request block so that | |
| 60 // the block references something outside of its own scope, and cannot be | |
| 61 // declared as a global block. This makes sure the block is | |
| 62 // an __NSStackBlock__, and verifies the fix for http://crbug.com/436175 . | |
| 63 NSString *chromiumPrefix = @"www.chromium.org"; | |
| 64 [CrNet setRequestFilterBlock:^BOOL (NSURLRequest *request) { | |
| 65 BOOL isChromiumSite = [[[request URL] host] hasPrefix:chromiumPrefix]; | |
| 66 return !isChromiumSite; | |
| 67 }]; | |
| 68 | |
| 69 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
| 70 self.viewController = | |
| 71 [[CrNetConsumerViewController alloc] initWithNibName:nil bundle:nil]; | |
| 72 self.window.rootViewController = self.viewController; | |
| 73 [self.window makeKeyAndVisible]; | |
| 74 | |
| 75 return YES; | |
| 76 } | |
| 77 | |
| 78 - (void)applicationDidEnterBackground:(UIApplication*)application { | |
| 79 [CrNet stopNetLog]; | |
| 80 [CrNet clearCacheWithCompletionCallback:^(int error) { | |
| 81 NSLog(@"Cache cleared: %d\n", error); | |
| 82 }]; | |
| 83 } | |
| 84 | |
| 85 - (void)applicationWillEnterForeground:(UIApplication*)application { | |
| 86 _counter++; | |
| 87 [CrNet startNetLogToFile:[self currentNetLogFileName] logBytes:NO]; | |
| 88 } | |
| 89 | |
| 90 @end | |
| OLD | NEW |