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