| Index: components/cronet/ios/sample/cronet_sample_app_delegate.mm
|
| diff --git a/components/cronet/ios/sample/cronet_sample_app_delegate.mm b/components/cronet/ios/sample/cronet_sample_app_delegate.mm
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..898092fa94bfb46694a88490949a861ee63ad864
|
| --- /dev/null
|
| +++ b/components/cronet/ios/sample/cronet_sample_app_delegate.mm
|
| @@ -0,0 +1,87 @@
|
| +// Copyright 2017 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 "cronet_sample_app_delegate.h"
|
| +
|
| +#import "components/cronet/ios/Cronet.h"
|
| +
|
| +#include "base/format_macros.h"
|
| +#import "cronet_sample_view_controller.h"
|
| +
|
| +@implementation CronetSampleAppDelegate {
|
| + NSUInteger _counter;
|
| + dispatch_queue_t _backgroundRequestQueue;
|
| +}
|
| +
|
| +@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:@"cronet_sample-net-log%" PRIuNS ".json", _counter];
|
| +}
|
| +
|
| +- (BOOL)application:(UIApplication*)application
|
| + didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
|
| + [Cronet setUserAgent:@"Dummy/1.0" partial:YES];
|
| + [Cronet setQuicEnabled:YES];
|
| + [Cronet start];
|
| + [Cronet startNetLogToFile:[self currentNetLogFileName] logBytes:NO];
|
| + [Cronet registerHttpProtocolHandler];
|
| +
|
| + // Just for fun, route chromium.org requests through Cronet.
|
| + //
|
| + // |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";
|
| + [Cronet setRequestFilterBlock:^BOOL(NSURLRequest* request) {
|
| + BOOL isChromiumSite = [[[request URL] host] hasPrefix:chromiumPrefix];
|
| + NSLog(@"RequestFilter:%@ Handling:%d", request, isChromiumSite);
|
| + return isChromiumSite;
|
| + }];
|
| +
|
| + NSURLSessionConfiguration* config =
|
| + [NSURLSessionConfiguration ephemeralSessionConfiguration];
|
| + [Cronet installIntoSessionConfiguration:config];
|
| +
|
| + self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
| + self.viewController =
|
| + [[CronetSampleViewController alloc] initWithNibName:nil bundle:nil];
|
| + self.window.rootViewController = self.viewController;
|
| + [self.window makeKeyAndVisible];
|
| +
|
| + _backgroundRequestQueue =
|
| + dispatch_queue_create("Background Request Queue", NULL);
|
| +
|
| + return YES;
|
| +}
|
| +
|
| +- (void)performBackgroundRequest {
|
| + NSURL* url = [NSURL URLWithString:@"http://www.chromium.org"];
|
| + NSData* receivedData = [NSData dataWithContentsOfURL:url];
|
| + NSLog(@"ReceivedData: %@", receivedData);
|
| + dispatch_async(_backgroundRequestQueue, ^{
|
| + [self performBackgroundRequest];
|
| + });
|
| +}
|
| +
|
| +- (void)applicationDidEnterBackground:(UIApplication*)application {
|
| + [Cronet stopNetLog];
|
| +}
|
| +
|
| +- (void)applicationWillEnterForeground:(UIApplication*)application {
|
| + _counter++;
|
| + [Cronet startNetLogToFile:[self currentNetLogFileName] logBytes:NO];
|
| +}
|
| +
|
| +- (void)applicationWillTerminate:(UIApplication*)application {
|
| + NSLog(@"Cronet Sample Will Terminate");
|
| +}
|
| +
|
| +@end
|
|
|