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])) {
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.
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 {
53 - (void)didFinishLoading {
54 // Tell |_delegate| that the PassKit object has loaded.
55 base::scoped_nsobject<NSData> passKitData([_passKitData copy]);
57 web::RequestTrackerImpl::PostUITaskIfOpen(
59 base::BindBlock(^{ [_delegate handlePassKitObject:passKitData]; }));
62 [super didFailWithNSErrorCode:NSURLErrorCancelled
63 netErrorCode:net::ERR_ABORTED];
66 - (void)setUserName:(NSString*)name password:(NSString*)password {
70 - (void)cancelAuthentication {