Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / web / net / clients / crw_passkit_network_client.mm
blob833349419a2ad50c3343a081c26b0cf8a26f94b1
1 // Copyright 2012 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 #include "ios/web/net/clients/crw_passkit_network_client.h"
7 #include "base/mac/bind_objc_block.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "ios/web/net/clients/crw_passkit_delegate.h"
10 #include "ios/web/net/request_tracker_impl.h"
11 #include "net/base/net_errors.h"
13 @implementation CRWPassKitNetworkClient {
14   id<CRWPassKitDelegate> _delegate;
15   // Cache the PassKit object until data has been completely passed in.
16   base::scoped_nsobject<NSMutableData> _passKitData;
17   base::WeakPtr<net::RequestTracker> _tracker;
20 - (instancetype)initWithTracker:(base::WeakPtr<net::RequestTracker>)tracker
21                        delegate:(id<CRWPassKitDelegate>)delegate {
22   if ((self = [super init])) {
23     DCHECK(delegate);
24     _delegate = delegate;
25     _tracker = tracker;
26   }
27   return self;
30 #pragma mark - CRNNetworkClientProtocol
32 - (void)didFailWithNSErrorCode:(NSInteger)nsErrorCode
33                   netErrorCode:(int)netErrorCode {
34   // If we fail, finish loading and return a nil PassKit object.
35   _passKitData.reset();
36   [self didFinishLoading];
39 - (void)didLoadData:(NSData*)data {
40   [_passKitData appendData:data];
43 - (void)didReceiveResponse:(NSURLResponse*)response {
44   _passKitData.reset([[NSMutableData alloc] init]);
47 - (void)wasRedirectedToRequest:(NSURLRequest*)request
48                  nativeRequest:(net::URLRequest*)nativeRequest
49               redirectResponse:(NSURLResponse*)redirectResponse {
50   NOTREACHED();
53 - (void)didFinishLoading {
54   // Tell |_delegate| that the PassKit object has loaded.
55   base::scoped_nsobject<NSData> passKitData([_passKitData copy]);
56   if (_tracker) {
57     web::RequestTrackerImpl::PostUITaskIfOpen(
58         _tracker,
59         base::BindBlock(^{ [_delegate handlePassKitObject:passKitData]; }));
60   }
62   [super didFailWithNSErrorCode:NSURLErrorCancelled
63                    netErrorCode:net::ERR_ABORTED];
66 - (void)setUserName:(NSString*)name password:(NSString*)password {
67   NOTREACHED();
70 - (void)cancelAuthentication {
71   NOTREACHED();
74 @end