Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(379)

Side by Side Diff: components/cronet/ios/cronet_consumer/cronet_consumer_app_delegate.mm

Issue 2859693005: Cronet iOS: Move crnet_consumer sample app to Cronet (Closed)
Patch Set: Added explicit dependencies Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "crnet_consumer_app_delegate.h" 5 #import "cronet_consumer_app_delegate.h"
6 6
7 #import <CrNet/CrNet.h> 7 #import <Cronet/Cronet.h>
8 8
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #import "crnet_consumer_view_controller.h" 10 #import "cronet_consumer_view_controller.h"
11 11
12 @implementation CrNetConsumerAppDelegate { 12 @implementation CronetConsumerAppDelegate {
13 NSUInteger _counter; 13 NSUInteger _counter;
14 } 14 }
15 15
16 @synthesize window; 16 @synthesize window;
17 @synthesize viewController; 17 @synthesize viewController;
18 18
19 // Returns a file name to save net internals logging. This method suffixes 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 20 // the ivar |_counter| to the file name so a new name can be obtained by
21 // modifying that. 21 // modifying that.
22 - (NSString*)currentNetLogFileName { 22 - (NSString*)currentNetLogFileName {
23 return [NSString 23 return [NSString
24 stringWithFormat:@"crnet-consumer-net-log%" PRIuNS ".json", _counter]; 24 stringWithFormat:@"cronet-consumer-net-log%" PRIuNS ".json", _counter];
25 } 25 }
26 26
27 - (NSString*)SDCHPrefStoreFileName { 27 - (NSString*)SDCHPrefStoreFileName {
28 NSFileManager* manager = [NSFileManager defaultManager]; 28 NSFileManager* manager = [NSFileManager defaultManager];
29 NSArray* possibleURLs = [manager 29 NSArray* possibleURLs =
30 URLsForDirectory:NSApplicationSupportDirectory 30 [manager URLsForDirectory:NSApplicationSupportDirectory
31 inDomains:NSUserDomainMask]; 31 inDomains:NSUserDomainMask];
32 NSURL* appSupportDir = [possibleURLs firstObject]; 32 NSURL* appSupportDir = [possibleURLs firstObject];
33 if (appSupportDir == nil) 33 if (appSupportDir == nil)
34 return nil; 34 return nil;
35 NSURL* prefStoreFile = [NSURL URLWithString:@"sdch-prefs.json" 35 NSURL* prefStoreFile =
36 relativeToURL:appSupportDir]; 36 [NSURL URLWithString:@"sdch-prefs.json" relativeToURL:appSupportDir];
37 NSError* error = nil; 37 NSError* error = nil;
38 [manager createDirectoryAtURL:appSupportDir 38 [manager createDirectoryAtURL:appSupportDir
39 withIntermediateDirectories:YES 39 withIntermediateDirectories:YES
40 attributes:nil 40 attributes:nil
41 error:&error]; 41 error:&error];
42 return error != nil ? [prefStoreFile path] : nil; 42 return error != nil ? [prefStoreFile path] : nil;
43 } 43 }
44 44
45 - (BOOL)application:(UIApplication*)application 45 - (BOOL)application:(UIApplication*)application
46 didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { 46 didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
47 [CrNet setPartialUserAgent:@"Dummy/1.0"]; 47 [Cronet setUserAgent:@"Dummy/1.0" partial:YES];
48 [CrNet setQuicEnabled:YES]; 48 [Cronet setQuicEnabled:YES];
49 [CrNet setSDCHEnabled:YES withPrefStore:[self SDCHPrefStoreFileName]]; 49 [Cronet start];
50 [CrNet install]; 50 [Cronet startNetLogToFile:[self currentNetLogFileName] logBytes:NO];
51 [CrNet startNetLogToFile:[self currentNetLogFileName] logBytes:NO];
52 51
53 NSURLSessionConfiguration* config = 52 NSURLSessionConfiguration* config =
54 [NSURLSessionConfiguration ephemeralSessionConfiguration]; 53 [NSURLSessionConfiguration ephemeralSessionConfiguration];
55 [CrNet installIntoSessionConfiguration:config]; 54 [Cronet installIntoSessionConfiguration:config];
56 55
57 // Just for fun, don't route chromium.org requests through CrNet. 56 // Just for fun, don't route chromium.org requests through Cronet.
58 // 57 //
59 // |chromiumPrefix| is declared outside the scope of the request block so that 58 // |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 59 // the block references something outside of its own scope, and cannot be
61 // declared as a global block. This makes sure the block is 60 // declared as a global block. This makes sure the block is
62 // an __NSStackBlock__, and verifies the fix for http://crbug.com/436175 . 61 // an __NSStackBlock__, and verifies the fix for http://crbug.com/436175 .
63 NSString *chromiumPrefix = @"www.chromium.org"; 62 NSString* chromiumPrefix = @"www.chromium.org";
64 [CrNet setRequestFilterBlock:^BOOL (NSURLRequest *request) { 63 [Cronet setRequestFilterBlock:^BOOL(NSURLRequest* request) {
65 BOOL isChromiumSite = [[[request URL] host] hasPrefix:chromiumPrefix]; 64 BOOL isChromiumSite = [[[request URL] host] hasPrefix:chromiumPrefix];
66 return !isChromiumSite; 65 return !isChromiumSite;
67 }]; 66 }];
68 67
69 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 68 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
70 self.viewController = 69 self.viewController =
71 [[CrNetConsumerViewController alloc] initWithNibName:nil bundle:nil]; 70 [[CronetConsumerViewController alloc] initWithNibName:nil bundle:nil];
72 self.window.rootViewController = self.viewController; 71 self.window.rootViewController = self.viewController;
73 [self.window makeKeyAndVisible]; 72 [self.window makeKeyAndVisible];
74 73
75 return YES; 74 return YES;
76 } 75 }
77 76
78 - (void)applicationDidEnterBackground:(UIApplication*)application { 77 - (void)applicationDidEnterBackground:(UIApplication*)application {
79 [CrNet stopNetLog]; 78 [Cronet stopNetLog];
80 [CrNet clearCacheWithCompletionCallback:^(int error) {
81 NSLog(@"Cache cleared: %d\n", error);
82 }];
83 } 79 }
84 80
85 - (void)applicationWillEnterForeground:(UIApplication*)application { 81 - (void)applicationWillEnterForeground:(UIApplication*)application {
86 _counter++; 82 _counter++;
87 [CrNet startNetLogToFile:[self currentNetLogFileName] logBytes:NO]; 83 [Cronet startNetLogToFile:[self currentNetLogFileName] logBytes:NO];
88 } 84 }
89 85
90 @end 86 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698