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 - (BOOL)application:(UIApplication *)application
27 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
28 [CrNet setPartialUserAgent:@"Dummy/1.0"];
29 [CrNet setQuicEnabled:YES];
30 // Always use QUIC if able.
31 [CrNet setAlternateProtocolThreshold:0.0];
33 [CrNet startNetLogToFile:[self currentNetLogFileName] logBytes:NO];
35 NSURLSessionConfiguration* config =
36 [NSURLSessionConfiguration ephemeralSessionConfiguration];
37 [CrNet installIntoSessionConfiguration:config];
39 // Just for fun, don't route chromium.org requests through CrNet.
41 // |chromiumPrefix| is declared outside the scope of the request block so that
42 // the block references something outside of its own scope, and cannot be
43 // declared as a global block. This makes sure the block is
44 // an __NSStackBlock__, and verifies the fix for http://crbug.com/436175 .
45 NSString *chromiumPrefix = @"www.chromium.org";
46 [CrNet setRequestFilterBlock:^BOOL (NSURLRequest *request) {
47 BOOL isChromiumSite = [[[request URL] host] hasPrefix:chromiumPrefix];
48 return !isChromiumSite;
51 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
53 [[CrNetConsumerViewController alloc] initWithNibName:nil bundle:nil];
54 self.window.rootViewController = self.viewController;
55 [self.window makeKeyAndVisible];
60 - (void)applicationDidEnterBackground:(UIApplication *)application {
62 [CrNet clearCacheWithCompletionCallback:^(int error) {
63 NSLog(@"Cache cleared: %d\n", error);
67 - (void)applicationWillEnterForeground:(UIApplication *)application {
69 [CrNet startNetLogToFile:[self currentNetLogFileName] logBytes:NO];