OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/web/web_state/crw_pass_kit_downloader.h" | 5 #import "ios/web/web_state/crw_pass_kit_downloader.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/mac/scoped_block.h" | 9 #include "base/mac/scoped_block.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
13 #import "ios/web/crw_network_activity_indicator_manager.h" | |
14 #include "net/http/http_response_headers.h" | 13 #include "net/http/http_response_headers.h" |
15 #include "net/url_request/url_fetcher.h" | 14 #include "net/url_request/url_fetcher.h" |
16 #include "net/url_request/url_fetcher_delegate.h" | 15 #include "net/url_request/url_fetcher_delegate.h" |
17 #include "net/url_request/url_request_context_getter.h" | 16 #include "net/url_request/url_request_context_getter.h" |
18 | 17 |
19 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
20 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
21 #endif | 20 #endif |
22 | 21 |
23 using net::URLFetcher; | 22 using net::URLFetcher; |
(...skipping 23 matching lines...) Expand all Loading... |
47 }; | 46 }; |
48 } // namespace | 47 } // namespace |
49 | 48 |
50 @interface CRWPassKitDownloader () | 49 @interface CRWPassKitDownloader () |
51 | 50 |
52 // The method called by PassKitFetcherDelegate when the download is complete. | 51 // The method called by PassKitFetcherDelegate when the download is complete. |
53 // If data is successfully downloaded, it converts the response to | 52 // If data is successfully downloaded, it converts the response to |
54 // NSData and passes the result to |_completionHandler|. | 53 // NSData and passes the result to |_completionHandler|. |
55 - (void)didFinishDownload; | 54 - (void)didFinishDownload; |
56 | 55 |
57 // Returns key for CRWNetworkActivityIndicatorManager. | |
58 - (NSString*)networkActivityKey; | |
59 | |
60 // Reports Download.IOSDownloadPassKitResult UMA metric. | 56 // Reports Download.IOSDownloadPassKitResult UMA metric. |
61 - (void)reportUMAPassKitResult:(DownloadPassKitResult)result; | 57 - (void)reportUMAPassKitResult:(DownloadPassKitResult)result; |
62 | 58 |
63 @end | 59 @end |
64 | 60 |
65 namespace { | 61 namespace { |
66 | 62 |
67 // Unique ID for CRWNetworkActivityIndicatorManager. | |
68 int g_pass_kit_downloader_class_id = 0; | |
69 | |
70 // A delegate for the URLFetcher to tell the CRWPassKitDownloader that the | 63 // A delegate for the URLFetcher to tell the CRWPassKitDownloader that the |
71 // download is complete. | 64 // download is complete. |
72 class PassKitFetcherDelegate : public URLFetcherDelegate { | 65 class PassKitFetcherDelegate : public URLFetcherDelegate { |
73 public: | 66 public: |
74 explicit PassKitFetcherDelegate(CRWPassKitDownloader* owner) | 67 explicit PassKitFetcherDelegate(CRWPassKitDownloader* owner) |
75 : owner_(owner) {} | 68 : owner_(owner) {} |
76 void OnURLFetchComplete(const URLFetcher* source) override { | 69 void OnURLFetchComplete(const URLFetcher* source) override { |
77 [owner_ didFinishDownload]; | 70 [owner_ didFinishDownload]; |
78 } | 71 } |
79 | 72 |
(...skipping 11 matching lines...) Expand all Loading... |
91 // URLFetcher with which PassKit data is downloaded. It is initialized | 84 // URLFetcher with which PassKit data is downloaded. It is initialized |
92 // whenever |downloadPassKitFileWithURL| is called. | 85 // whenever |downloadPassKitFileWithURL| is called. |
93 std::unique_ptr<URLFetcher> _fetcher; | 86 std::unique_ptr<URLFetcher> _fetcher; |
94 | 87 |
95 // Delegate to bridge between URLFetcher callback and CRWPassKitDownlaoder. | 88 // Delegate to bridge between URLFetcher callback and CRWPassKitDownlaoder. |
96 std::unique_ptr<PassKitFetcherDelegate> _fetcherDelegate; | 89 std::unique_ptr<PassKitFetcherDelegate> _fetcherDelegate; |
97 | 90 |
98 // Context getter which is passed to the URLFetcher, as required by | 91 // Context getter which is passed to the URLFetcher, as required by |
99 // URLFetcher API. | 92 // URLFetcher API. |
100 scoped_refptr<URLRequestContextGetter> _requestContextGetter; | 93 scoped_refptr<URLRequestContextGetter> _requestContextGetter; |
101 | |
102 // Network activity ID for this instance of CRWPassKitDownloader. | |
103 int _passKitDownloaderID; | |
104 } | 94 } |
105 | 95 |
106 #pragma mark - Public Methods | 96 #pragma mark - Public Methods |
107 | 97 |
108 - (instancetype)initWithContextGetter:(net::URLRequestContextGetter*)getter | 98 - (instancetype)initWithContextGetter:(net::URLRequestContextGetter*)getter |
109 completionHandler:(web::PassKitCompletionHandler)handler { | 99 completionHandler:(web::PassKitCompletionHandler)handler { |
110 self = [super init]; | 100 self = [super init]; |
111 if (self) { | 101 if (self) { |
112 DCHECK(getter); | 102 DCHECK(getter); |
113 DCHECK(handler); | 103 DCHECK(handler); |
114 _completionHandler.reset([handler copy]); | 104 _completionHandler.reset([handler copy]); |
115 _fetcherDelegate.reset(new PassKitFetcherDelegate(self)); | 105 _fetcherDelegate.reset(new PassKitFetcherDelegate(self)); |
116 _requestContextGetter = getter; | 106 _requestContextGetter = getter; |
117 _passKitDownloaderID = g_pass_kit_downloader_class_id++; | |
118 } | 107 } |
119 return self; | 108 return self; |
120 } | 109 } |
121 | 110 |
122 - (instancetype)init { | 111 - (instancetype)init { |
123 NOTREACHED(); | 112 NOTREACHED(); |
124 return nil; | 113 return nil; |
125 } | 114 } |
126 | 115 |
127 - (void)dealloc { | |
128 [[CRWNetworkActivityIndicatorManager sharedInstance] | |
129 clearNetworkTasksForGroup:[self networkActivityKey]]; | |
130 } | |
131 | |
132 - (BOOL)isMIMETypePassKitType:(NSString*)MIMEType { | 116 - (BOOL)isMIMETypePassKitType:(NSString*)MIMEType { |
133 return [MIMEType isEqualToString:@"application/vnd.apple.pkpass"]; | 117 return [MIMEType isEqualToString:@"application/vnd.apple.pkpass"]; |
134 } | 118 } |
135 | 119 |
136 - (void)downloadPassKitFileWithURL:(const GURL&)URL { | 120 - (void)downloadPassKitFileWithURL:(const GURL&)URL { |
137 _fetcher = URLFetcher::Create(URL, URLFetcher::GET, _fetcherDelegate.get()); | 121 _fetcher = URLFetcher::Create(URL, URLFetcher::GET, _fetcherDelegate.get()); |
138 _fetcher->SetRequestContext(_requestContextGetter.get()); | 122 _fetcher->SetRequestContext(_requestContextGetter.get()); |
139 CRWNetworkActivityIndicatorManager* sharedManager = | |
140 [CRWNetworkActivityIndicatorManager sharedInstance]; | |
141 // Verifies that there are not any network tasks associated with this instance | |
142 // before starting another task, so that this method is idempotent. | |
143 if (![sharedManager numNetworkTasksForGroup:[self networkActivityKey]]) | |
144 [sharedManager startNetworkTaskForGroup:[self networkActivityKey]]; | |
145 _fetcher->Start(); | 123 _fetcher->Start(); |
146 } | 124 } |
147 | 125 |
148 - (void)cancelPendingDownload { | 126 - (void)cancelPendingDownload { |
149 _fetcher.reset(); | 127 _fetcher.reset(); |
150 CRWNetworkActivityIndicatorManager* sharedManager = | |
151 [CRWNetworkActivityIndicatorManager sharedInstance]; | |
152 // Verifies that there is a network task associated with this instance | |
153 // before stopping a task, so that this method is idempotent. | |
154 if ([sharedManager numNetworkTasksForGroup:[self networkActivityKey]]) | |
155 [sharedManager stopNetworkTaskForGroup:[self networkActivityKey]]; | |
156 } | 128 } |
157 | 129 |
158 #pragma mark - Private Methods | 130 #pragma mark - Private Methods |
159 | 131 |
160 - (void)didFinishDownload { | 132 - (void)didFinishDownload { |
161 [[CRWNetworkActivityIndicatorManager sharedInstance] | |
162 stopNetworkTaskForGroup:[self networkActivityKey]]; | |
163 int responseCode = _fetcher->GetResponseCode(); | 133 int responseCode = _fetcher->GetResponseCode(); |
164 std::string response; | 134 std::string response; |
165 // If the download failed, pass nil to |_completionHandler| and log which | 135 // If the download failed, pass nil to |_completionHandler| and log which |
166 // kind of failure it was. | 136 // kind of failure it was. |
167 if (!_fetcher->GetStatus().is_success() || responseCode != 200 || | 137 if (!_fetcher->GetStatus().is_success() || responseCode != 200 || |
168 !_fetcher->GetResponseAsString(&response)) { | 138 !_fetcher->GetResponseAsString(&response)) { |
169 DownloadPassKitResult errorType = | 139 DownloadPassKitResult errorType = |
170 (responseCode == 401 || responseCode == 403) | 140 (responseCode == 401 || responseCode == 403) |
171 ? DOWNLOAD_PASS_KIT_UNAUTHORIZED_FAILURE | 141 ? DOWNLOAD_PASS_KIT_UNAUTHORIZED_FAILURE |
172 : DOWNLOAD_PASS_KIT_OTHER_FAILURE; | 142 : DOWNLOAD_PASS_KIT_OTHER_FAILURE; |
(...skipping 11 matching lines...) Expand all Loading... |
184 DownloadPassKitResult successOrFailureLogging = | 154 DownloadPassKitResult successOrFailureLogging = |
185 ([self isMIMETypePassKitType:convertedMIMEType]) | 155 ([self isMIMETypePassKitType:convertedMIMEType]) |
186 ? DOWNLOAD_PASS_KIT_SUCCESSFUL | 156 ? DOWNLOAD_PASS_KIT_SUCCESSFUL |
187 : DOWNLOAD_PASS_KIT_WRONG_MIME_TYPE_FAILURE; | 157 : DOWNLOAD_PASS_KIT_WRONG_MIME_TYPE_FAILURE; |
188 [self reportUMAPassKitResult:successOrFailureLogging]; | 158 [self reportUMAPassKitResult:successOrFailureLogging]; |
189 NSData* data = | 159 NSData* data = |
190 [NSData dataWithBytes:response.c_str() length:response.length()]; | 160 [NSData dataWithBytes:response.c_str() length:response.length()]; |
191 _completionHandler.get()(data); | 161 _completionHandler.get()(data); |
192 } | 162 } |
193 | 163 |
194 - (NSString*)networkActivityKey { | |
195 return [NSString | |
196 stringWithFormat:@"PassKitDownloader.NetworkActivityIndicatorKey.%d", | |
197 _passKitDownloaderID]; | |
198 } | |
199 | |
200 - (void)reportUMAPassKitResult:(DownloadPassKitResult)result { | 164 - (void)reportUMAPassKitResult:(DownloadPassKitResult)result { |
201 UMA_HISTOGRAM_ENUMERATION(kUMADownloadPassKitResult, result, | 165 UMA_HISTOGRAM_ENUMERATION(kUMADownloadPassKitResult, result, |
202 DOWNLOAD_PASS_KIT_RESULT_COUNT); | 166 DOWNLOAD_PASS_KIT_RESULT_COUNT); |
203 } | 167 } |
204 | 168 |
205 @end | 169 @end |
OLD | NEW |