summaryrefslogtreecommitdiffstats
path: root/ios/crnet/crnet_consumer/crnet_consumer_app_delegate.mm
blob: b224eda5f1a9a6e03cdcbf16d2cb6ad01bf2e6c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "crnet_consumer_app_delegate.h"

#import "CrNet.h"
#include "base/format_macros.h"
#import "crnet_consumer_view_controller.h"

@implementation CrNetConsumerAppDelegate {
  NSUInteger _counter;
}

@synthesize window;
@synthesize viewController;

// Returns a file name to save net internals logging. This method suffixes
// the ivar |_counter| to the file name so a new name can be obtained by
// modifying that.
- (NSString*)currentNetLogFileName {
  return [NSString
      stringWithFormat:@"crnet-consumer-net-log%" PRIuNS ".json", _counter];
}

- (NSString*)SDCHPrefStoreFileName {
  NSFileManager* manager = [NSFileManager defaultManager];
  NSArray* possibleURLs = [manager
      URLsForDirectory:NSApplicationSupportDirectory
             inDomains:NSUserDomainMask];
  NSURL* appSupportDir = [possibleURLs firstObject];
  if (appSupportDir == nil)
    return nil;
  NSURL* prefStoreFile = [NSURL URLWithString:@"sdch-prefs.json"
                                relativeToURL:appSupportDir];
  NSError* error = nil;
  [manager createDirectoryAtURL:appSupportDir
      withIntermediateDirectories:YES
                       attributes:nil
                            error:&error];
  return error != nil ? [prefStoreFile path] : nil;
}

- (BOOL)application:(UIApplication*)application
    didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
  [CrNet setPartialUserAgent:@"Dummy/1.0"];
  [CrNet setQuicEnabled:YES];
  [CrNet setSDCHEnabled:YES withPrefStore:[self SDCHPrefStoreFileName]];
  [CrNet install];
  [CrNet startNetLogToFile:[self currentNetLogFileName] logBytes:NO];

  NSURLSessionConfiguration* config =
      [NSURLSessionConfiguration ephemeralSessionConfiguration];
  [CrNet installIntoSessionConfiguration:config];

  // Just for fun, don't route chromium.org requests through CrNet.
  //
  // |chromiumPrefix| is declared outside the scope of the request block so that
  // the block references something outside of its own scope, and cannot be
  // declared as a global block. This makes sure the block is
  // an __NSStackBlock__, and verifies the fix for http://crbug.com/436175 .
  NSString *chromiumPrefix = @"www.chromium.org";
  [CrNet setRequestFilterBlock:^BOOL (NSURLRequest *request) {
      BOOL isChromiumSite = [[[request URL] host] hasPrefix:chromiumPrefix];
      return !isChromiumSite;
  }];

  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  self.viewController =
      [[CrNetConsumerViewController alloc] initWithNibName:nil bundle:nil];
  self.window.rootViewController = self.viewController;
  [self.window makeKeyAndVisible];

  return YES;
}

- (void)applicationDidEnterBackground:(UIApplication*)application {
  [CrNet stopNetLog];
  [CrNet clearCacheWithCompletionCallback:^(int error) {
    NSLog(@"Cache cleared: %d\n", error);
  }];
}

- (void)applicationWillEnterForeground:(UIApplication*)application {
  _counter++;
  [CrNet startNetLogToFile:[self currentNetLogFileName] logBytes:NO];
}

@end