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

Side by Side Diff: ios/chrome/browser/ui/downloads/download_manager_controller.mm

Issue 2740473002: DownloadManagerController now uses WebState (Closed)
Patch Set: handles nil StoreKitTabHelper Created 3 years, 9 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 "ios/chrome/browser/ui/downloads/download_manager_controller.h" 5 #import "ios/chrome/browser/ui/downloads/download_manager_controller.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/ios/weak_nsobject.h" 12 #include "base/ios/weak_nsobject.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/mac/bind_objc_block.h" 14 #include "base/mac/bind_objc_block.h"
15 #include "base/mac/objc_property_releaser.h" 15 #include "base/mac/objc_property_releaser.h"
16 #include "base/mac/scoped_nsobject.h" 16 #include "base/mac/scoped_nsobject.h"
17 #include "base/memory/ref_counted.h" 17 #include "base/memory/ref_counted.h"
18 #include "base/metrics/histogram_macros.h" 18 #include "base/metrics/histogram_macros.h"
19 #include "base/metrics/user_metrics.h" 19 #include "base/metrics/user_metrics.h"
20 #include "base/metrics/user_metrics_action.h" 20 #include "base/metrics/user_metrics_action.h"
21 #include "base/strings/sys_string_conversions.h" 21 #include "base/strings/sys_string_conversions.h"
22 #include "base/threading/sequenced_worker_pool.h" 22 #include "base/threading/sequenced_worker_pool.h"
23 #include "components/strings/grit/components_strings.h" 23 #include "components/strings/grit/components_strings.h"
24 #import "ios/chrome/browser/installation_notifier.h" 24 #import "ios/chrome/browser/installation_notifier.h"
25 #include "ios/chrome/browser/native_app_launcher/ios_appstore_ids.h" 25 #include "ios/chrome/browser/native_app_launcher/ios_appstore_ids.h"
26 #import "ios/chrome/browser/store_kit/store_kit_launcher.h" 26 #import "ios/chrome/browser/store_kit/store_kit_tab_helper.h"
27 #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" 27 #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h"
28 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" 28 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h"
29 #import "ios/chrome/browser/ui/network_activity_indicator_manager.h" 29 #import "ios/chrome/browser/ui/network_activity_indicator_manager.h"
30 #include "ios/chrome/browser/ui/ui_util.h" 30 #include "ios/chrome/browser/ui/ui_util.h"
31 #import "ios/chrome/browser/ui/uikit_ui_util.h" 31 #import "ios/chrome/browser/ui/uikit_ui_util.h"
32 #include "ios/chrome/grit/ios_strings.h" 32 #include "ios/chrome/grit/ios_strings.h"
33 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" 33 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
34 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_metad ata.h" 34 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_metad ata.h"
35 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_white list_manager.h" 35 #import "ios/public/provider/chrome/browser/native_app_launcher/native_app_white list_manager.h"
36 #import "ios/third_party/material_components_ios/src/components/ActivityIndicato r/src/MaterialActivityIndicator.h" 36 #import "ios/third_party/material_components_ios/src/components/ActivityIndicato r/src/MaterialActivityIndicator.h"
37 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate rialButtons.h" 37 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate rialButtons.h"
38 #import "ios/third_party/material_components_ios/src/components/Typography/src/M aterialTypography.h" 38 #import "ios/third_party/material_components_ios/src/components/Typography/src/M aterialTypography.h"
39 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h" 39 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h"
40 #include "ios/web/public/browser_state.h"
41 #include "ios/web/public/web_state/web_state.h"
40 #include "ios/web/public/web_thread.h" 42 #include "ios/web/public/web_thread.h"
41 #include "net/base/filename_util.h" 43 #include "net/base/filename_util.h"
42 #include "net/http/http_response_headers.h" 44 #include "net/http/http_response_headers.h"
43 #include "net/http/http_util.h" 45 #include "net/http/http_util.h"
44 #include "net/url_request/url_fetcher.h" 46 #include "net/url_request/url_fetcher.h"
45 #include "net/url_request/url_fetcher_delegate.h" 47 #include "net/url_request/url_fetcher_delegate.h"
46 #include "net/url_request/url_request_context_getter.h" 48 #include "net/url_request/url_request_context_getter.h"
47 #include "net/url_request/url_request_status.h" 49 #include "net/url_request/url_request_status.h"
48 #include "ui/base/l10n/l10n_util_mac.h" 50 #include "ui/base/l10n/l10n_util_mac.h"
49 #import "ui/gfx/ios/uikit_util.h" 51 #import "ui/gfx/ios/uikit_util.h"
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 // header field in the initial HEAD request. This is set to |kNoFileSizeGiven| 369 // header field in the initial HEAD request. This is set to |kNoFileSizeGiven|
368 // if the Content-Length header is not given. 370 // if the Content-Length header is not given.
369 long long _totalFileSize; 371 long long _totalFileSize;
370 372
371 // YES if |_fileTypeLabel| is vertically centered in |_documentContainer|. NO 373 // YES if |_fileTypeLabel| is vertically centered in |_documentContainer|. NO
372 // if |_fileTypeLabel| is lower to account for another view in 374 // if |_fileTypeLabel| is lower to account for another view in
373 // |_documentContainer|. 375 // |_documentContainer|.
374 BOOL _isFileTypeLabelCentered; 376 BOOL _isFileTypeLabelCentered;
375 BOOL _isDisplayingError; 377 BOOL _isDisplayingError;
376 BOOL _didSuccessfullyFinishHeadFetch; 378 BOOL _didSuccessfullyFinishHeadFetch;
377 scoped_refptr<URLRequestContextGetter> _requestContextGetter; 379 // WebState provides access to the *TabHelper objects.
380 web::WebState* _webState;
378 std::unique_ptr<URLFetcher> _fetcher; 381 std::unique_ptr<URLFetcher> _fetcher;
379 std::unique_ptr<DownloadHeadDelegate> _headFetcherDelegate; 382 std::unique_ptr<DownloadHeadDelegate> _headFetcherDelegate;
380 std::unique_ptr<DownloadContentDelegate> _contentFetcherDelegate; 383 std::unique_ptr<DownloadContentDelegate> _contentFetcherDelegate;
381 base::WeakNSProtocol<id<StoreKitLauncher>> _storeKitLauncher;
382 base::FilePath _downloadFilePath; 384 base::FilePath _downloadFilePath;
383 base::scoped_nsobject<MDCActivityIndicator> _activityIndicator; 385 base::scoped_nsobject<MDCActivityIndicator> _activityIndicator;
384 // Set to YES when a download begins and is used to determine if the 386 // Set to YES when a download begins and is used to determine if the
385 // DownloadFileResult histogram needs to be recorded on -dealloc. 387 // DownloadFileResult histogram needs to be recorded on -dealloc.
386 BOOL _recordDownloadResultHistogram; 388 BOOL _recordDownloadResultHistogram;
387 // Set to YES when a file is downloaded and is used to determine if the 389 // Set to YES when a file is downloaded and is used to determine if the
388 // DownloadedFileAction histogram needs to be recorded on -dealloc. 390 // DownloadedFileAction histogram needs to be recorded on -dealloc.
389 BOOL _recordFileActionHistogram; 391 BOOL _recordFileActionHistogram;
390 base::mac::ObjCPropertyReleaser _propertyReleaser_DownloadManagerController; 392 base::mac::ObjCPropertyReleaser _propertyReleaser_DownloadManagerController;
391 } 393 }
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 @synthesize landscapeConstraintsArray = _landscapeConstraintsArray; 513 @synthesize landscapeConstraintsArray = _landscapeConstraintsArray;
512 @synthesize portraitActionBarOneButtonConstraintsArray = 514 @synthesize portraitActionBarOneButtonConstraintsArray =
513 _portraitActionBarOneButtonConstraintsArray; 515 _portraitActionBarOneButtonConstraintsArray;
514 @synthesize portraitActionBarTwoButtonConstraintsArray = 516 @synthesize portraitActionBarTwoButtonConstraintsArray =
515 _portraitActionBarTwoButtonConstraintsArray; 517 _portraitActionBarTwoButtonConstraintsArray;
516 @synthesize fileTypeLabelCentered = _fileTypeLabelCentered; 518 @synthesize fileTypeLabelCentered = _fileTypeLabelCentered;
517 @synthesize downloadStartedTime = _downloadStartedTime; 519 @synthesize downloadStartedTime = _downloadStartedTime;
518 @synthesize fractionDownloaded = _fractionDownloaded; 520 @synthesize fractionDownloaded = _fractionDownloaded;
519 @synthesize googleDriveMetadata = _googleDriveMetadata; 521 @synthesize googleDriveMetadata = _googleDriveMetadata;
520 522
521 - (id)initWithURL:(const GURL&)url 523 - (instancetype)initWithWebState:(web::WebState*)webState
522 requestContextGetter:(URLRequestContextGetter*)requestContextGetter 524 downloadURL:(const GURL&)url {
523 storeKitLauncher:(id<StoreKitLauncher>)storeLauncher {
524 self = [super initWithNibName:@"DownloadManagerController" url:url]; 525 self = [super initWithNibName:@"DownloadManagerController" url:url];
525 if (self) { 526 if (self) {
526 _downloadManagerId = g_download_manager_id++; 527 _downloadManagerId = g_download_manager_id++;
527 _propertyReleaser_DownloadManagerController.Init( 528 _propertyReleaser_DownloadManagerController.Init(
528 self, [DownloadManagerController class]); 529 self, [DownloadManagerController class]);
529 530
530 _requestContextGetter = requestContextGetter; 531 DCHECK(webState);
532 _webState = webState;
531 _headFetcherDelegate.reset(new DownloadHeadDelegate(self)); 533 _headFetcherDelegate.reset(new DownloadHeadDelegate(self));
532 _contentFetcherDelegate.reset(new DownloadContentDelegate(self)); 534 _contentFetcherDelegate.reset(new DownloadContentDelegate(self));
533 _downloadFilePath = base::FilePath(); 535 _downloadFilePath = base::FilePath();
534 _storeKitLauncher.reset(storeLauncher);
535 536
536 [_documentContainer 537 [_documentContainer
537 setBackgroundColor:UIColorFromRGB(kUndownloadedDocumentColor)]; 538 setBackgroundColor:UIColorFromRGB(kUndownloadedDocumentColor)];
538 539
539 _isFileTypeLabelCentered = YES; 540 _isFileTypeLabelCentered = YES;
540 _isDisplayingError = NO; 541 _isDisplayingError = NO;
541 _didSuccessfullyFinishHeadFetch = NO; 542 _didSuccessfullyFinishHeadFetch = NO;
542 543
543 NSString* downloadText = 544 NSString* downloadText =
544 l10n_util::GetNSString(IDS_IOS_DOWNLOAD_MANAGER_DOWNLOAD); 545 l10n_util::GetNSString(IDS_IOS_DOWNLOAD_MANAGER_DOWNLOAD);
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
947 // TODO(fulbright): Investigate moving this code into -wasShown instead 948 // TODO(fulbright): Investigate moving this code into -wasShown instead
948 // (-wasShown is currently not being called on this class even when it's 949 // (-wasShown is currently not being called on this class even when it's
949 // implemented). 950 // implemented).
950 - (void)start { 951 - (void)start {
951 [self startHeadFetch]; 952 [self startHeadFetch];
952 } 953 }
953 954
954 - (void)startHeadFetch { 955 - (void)startHeadFetch {
955 _fetcher = URLFetcher::Create([self url], URLFetcher::HEAD, 956 _fetcher = URLFetcher::Create([self url], URLFetcher::HEAD,
956 _headFetcherDelegate.get()); 957 _headFetcherDelegate.get());
957 _fetcher->SetRequestContext(_requestContextGetter.get()); 958 _fetcher->SetRequestContext(
959 _webState->GetBrowserState()->GetRequestContext());
958 [[NetworkActivityIndicatorManager sharedInstance] 960 [[NetworkActivityIndicatorManager sharedInstance]
959 startNetworkTaskForGroup:[self getNetworkActivityKey]]; 961 startNetworkTaskForGroup:[self getNetworkActivityKey]];
960 _fetcher->Start(); 962 _fetcher->Start();
961 } 963 }
962 964
963 - (void)onHeadFetchComplete { 965 - (void)onHeadFetchComplete {
964 [[NetworkActivityIndicatorManager sharedInstance] 966 [[NetworkActivityIndicatorManager sharedInstance]
965 stopNetworkTaskForGroup:[self getNetworkActivityKey]]; 967 stopNetworkTaskForGroup:[self getNetworkActivityKey]];
966 int responseCode = _fetcher->GetResponseCode(); 968 int responseCode = _fetcher->GetResponseCode();
967 UMA_HISTOGRAM_SPARSE_SLOWLY( 969 UMA_HISTOGRAM_SPARSE_SLOWLY(
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
1060 l10n_util::GetNSString(IDS_IOS_DOWNLOAD_MANAGER_UNABLE_TO_OPEN_FILE); 1062 l10n_util::GetNSString(IDS_IOS_DOWNLOAD_MANAGER_UNABLE_TO_OPEN_FILE);
1061 NSString* message = 1063 NSString* message =
1062 l10n_util::GetNSString(IDS_IOS_DOWNLOAD_MANAGER_NO_APP_MESSAGE); 1064 l10n_util::GetNSString(IDS_IOS_DOWNLOAD_MANAGER_NO_APP_MESSAGE);
1063 1065
1064 _alertCoordinator.reset([[AlertCoordinator alloc] 1066 _alertCoordinator.reset([[AlertCoordinator alloc]
1065 initWithBaseViewController:topViewController 1067 initWithBaseViewController:topViewController
1066 title:title 1068 title:title
1067 message:message]); 1069 message:message]);
1068 1070
1069 // |googleDriveMetadata| contains the information necessary to either launch 1071 // |googleDriveMetadata| contains the information necessary to either launch
1070 // |the Google Drive app or navigate to its StoreKit page. If the metadata is 1072 // the Google Drive app or navigate to its StoreKit page. If the metadata is
1071 // |not present, do not show the upload button at all. 1073 // not present, do not show the upload button at all.
1072 if (self.googleDriveMetadata) { 1074 StoreKitTabHelper* tabHelper = StoreKitTabHelper::FromWebState(_webState);
1075 if (self.googleDriveMetadata && tabHelper) {
1073 NSString* googleDriveButtonTitle = 1076 NSString* googleDriveButtonTitle =
1074 l10n_util::GetNSString(IDS_IOS_DOWNLOAD_MANAGER_UPLOAD_TO_GOOGLE_DRIVE); 1077 l10n_util::GetNSString(IDS_IOS_DOWNLOAD_MANAGER_UPLOAD_TO_GOOGLE_DRIVE);
1075 base::WeakNSObject<DownloadManagerController> weakSelf(self); 1078 base::WeakNSObject<DownloadManagerController> weakSelf(self);
1076 [_alertCoordinator addItemWithTitle:googleDriveButtonTitle 1079 [_alertCoordinator addItemWithTitle:googleDriveButtonTitle
1077 action:^{ 1080 action:^{
1078 [weakSelf openGoogleDriveInAppStore]; 1081 [weakSelf openGoogleDriveInAppStore];
1079 } 1082 }
1080 style:UIAlertActionStyleDefault]; 1083 style:UIAlertActionStyleDefault];
1081 } 1084 }
1082 [_alertCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_CANCEL) 1085 [_alertCoordinator addItemWithTitle:l10n_util::GetNSString(IDS_CANCEL)
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
1258 if (![DownloadManagerController 1261 if (![DownloadManagerController
1259 fetchDownloadsDirectoryFilePath:&downloadsDirectoryPath]) { 1262 fetchDownloadsDirectoryFilePath:&downloadsDirectoryPath]) {
1260 [self displayError]; 1263 [self displayError];
1261 return; 1264 return;
1262 } 1265 }
1263 _downloadFilePath = downloadsDirectoryPath.Append( 1266 _downloadFilePath = downloadsDirectoryPath.Append(
1264 base::SysNSStringToUTF8([_fileNameLabel text])); 1267 base::SysNSStringToUTF8([_fileNameLabel text]));
1265 1268
1266 _fetcher = URLFetcher::Create([self url], URLFetcher::GET, 1269 _fetcher = URLFetcher::Create([self url], URLFetcher::GET,
1267 _contentFetcherDelegate.get()); 1270 _contentFetcherDelegate.get());
1268 _fetcher->SetRequestContext(_requestContextGetter.get()); 1271 _fetcher->SetRequestContext(
1272 _webState->GetBrowserState()->GetRequestContext());
1269 base::SequencedWorkerPool::SequenceToken sequenceToken = 1273 base::SequencedWorkerPool::SequenceToken sequenceToken =
1270 web::WebThread::GetBlockingPool()->GetSequenceToken(); 1274 web::WebThread::GetBlockingPool()->GetSequenceToken();
1271 _fetcher->SaveResponseToFileAtPath( 1275 _fetcher->SaveResponseToFileAtPath(
1272 _downloadFilePath, 1276 _downloadFilePath,
1273 web::WebThread::GetBlockingPool()->GetSequencedTaskRunner(sequenceToken)); 1277 web::WebThread::GetBlockingPool()->GetSequencedTaskRunner(sequenceToken));
1274 [[NetworkActivityIndicatorManager sharedInstance] 1278 [[NetworkActivityIndicatorManager sharedInstance]
1275 startNetworkTaskForGroup:[self getNetworkActivityKey]]; 1279 startNetworkTaskForGroup:[self getNetworkActivityKey]];
1276 _fetcher->Start(); 1280 _fetcher->Start();
1277 1281
1278 self.downloadStartedTime = [NSDate date]; 1282 self.downloadStartedTime = [NSDate date];
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
1569 if (!showedMenu) { 1573 if (!showedMenu) {
1570 [self displayUnableToOpenFileDialog]; 1574 [self displayUnableToOpenFileDialog];
1571 } 1575 }
1572 } 1576 }
1573 1577
1574 - (IBAction)googleDriveButtonTapped:(id)sender { 1578 - (IBAction)googleDriveButtonTapped:(id)sender {
1575 [self openGoogleDriveInAppStore]; 1579 [self openGoogleDriveInAppStore];
1576 } 1580 }
1577 1581
1578 - (void)openGoogleDriveInAppStore { 1582 - (void)openGoogleDriveInAppStore {
1579 [[InstallationNotifier sharedInstance] 1583 StoreKitTabHelper* helper = StoreKitTabHelper::FromWebState(_webState);
1580 registerForInstallationNotifications:self 1584 if (helper) {
1581 withSelector:@selector(hideGoogleDriveButton) 1585 [[InstallationNotifier sharedInstance]
1582 forScheme:[_googleDriveMetadata anyScheme]]; 1586 registerForInstallationNotifications:self
1583 1587 withSelector:@selector(hideGoogleDriveButton)
1584 [_storeKitLauncher openAppStore:[_googleDriveMetadata appId]]; 1588 forScheme:[_googleDriveMetadata anyScheme]];
1589 helper->OpenAppStore([_googleDriveMetadata appId]);
1590 }
1585 } 1591 }
1586 1592
1587 - (NSString*)getNetworkActivityKey { 1593 - (NSString*)getNetworkActivityKey {
1588 return 1594 return
1589 [NSString stringWithFormat: 1595 [NSString stringWithFormat:
1590 @"DownloadManagerController.NetworkActivityIndicatorKey.%d", 1596 @"DownloadManagerController.NetworkActivityIndicatorKey.%d",
1591 _downloadManagerId]; 1597 _downloadManagerId];
1592 } 1598 }
1593 1599
1594 + (BOOL)fetchDownloadsDirectoryFilePath:(base::FilePath*)path { 1600 + (BOOL)fetchDownloadsDirectoryFilePath:(base::FilePath*)path {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1636 1642
1637 #pragma mark - CRWNativeContent 1643 #pragma mark - CRWNativeContent
1638 1644
1639 - (void)close { 1645 - (void)close {
1640 // Makes sure that all outstanding network requests are shut down before 1646 // Makes sure that all outstanding network requests are shut down before
1641 // this controller is closed. 1647 // this controller is closed.
1642 _fetcher.reset(); 1648 _fetcher.reset();
1643 } 1649 }
1644 1650
1645 @end 1651 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698