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

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

Issue 2740473002: DownloadManagerController now uses WebState (Closed)
Patch Set: 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 - (id)initWithWebState:(web::WebState*)webState downloadURL:(const GURL&)url {
sdefresne 2017/03/07 17:15:49 ditto
pkl (ping after 24h if needed) 2017/03/07 22:07:19 Done.
522 requestContextGetter:(URLRequestContextGetter*)requestContextGetter
523 storeKitLauncher:(id<StoreKitLauncher>)storeLauncher {
524 self = [super initWithNibName:@"DownloadManagerController" url:url]; 524 self = [super initWithNibName:@"DownloadManagerController" url:url];
525 if (self) { 525 if (self) {
526 _downloadManagerId = g_download_manager_id++; 526 _downloadManagerId = g_download_manager_id++;
527 _propertyReleaser_DownloadManagerController.Init( 527 _propertyReleaser_DownloadManagerController.Init(
528 self, [DownloadManagerController class]); 528 self, [DownloadManagerController class]);
529 529
530 _requestContextGetter = requestContextGetter; 530 DCHECK(webState);
531 _webState = webState;
531 _headFetcherDelegate.reset(new DownloadHeadDelegate(self)); 532 _headFetcherDelegate.reset(new DownloadHeadDelegate(self));
532 _contentFetcherDelegate.reset(new DownloadContentDelegate(self)); 533 _contentFetcherDelegate.reset(new DownloadContentDelegate(self));
533 _downloadFilePath = base::FilePath(); 534 _downloadFilePath = base::FilePath();
534 _storeKitLauncher.reset(storeLauncher);
535 535
536 [_documentContainer 536 [_documentContainer
537 setBackgroundColor:UIColorFromRGB(kUndownloadedDocumentColor)]; 537 setBackgroundColor:UIColorFromRGB(kUndownloadedDocumentColor)];
538 538
539 _isFileTypeLabelCentered = YES; 539 _isFileTypeLabelCentered = YES;
540 _isDisplayingError = NO; 540 _isDisplayingError = NO;
541 _didSuccessfullyFinishHeadFetch = NO; 541 _didSuccessfullyFinishHeadFetch = NO;
542 542
543 NSString* downloadText = 543 NSString* downloadText =
544 l10n_util::GetNSString(IDS_IOS_DOWNLOAD_MANAGER_DOWNLOAD); 544 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 947 // TODO(fulbright): Investigate moving this code into -wasShown instead
948 // (-wasShown is currently not being called on this class even when it's 948 // (-wasShown is currently not being called on this class even when it's
949 // implemented). 949 // implemented).
950 - (void)start { 950 - (void)start {
951 [self startHeadFetch]; 951 [self startHeadFetch];
952 } 952 }
953 953
954 - (void)startHeadFetch { 954 - (void)startHeadFetch {
955 _fetcher = URLFetcher::Create([self url], URLFetcher::HEAD, 955 _fetcher = URLFetcher::Create([self url], URLFetcher::HEAD,
956 _headFetcherDelegate.get()); 956 _headFetcherDelegate.get());
957 _fetcher->SetRequestContext(_requestContextGetter.get()); 957 _fetcher->SetRequestContext(
958 _webState->GetBrowserState()->GetRequestContext());
958 [[NetworkActivityIndicatorManager sharedInstance] 959 [[NetworkActivityIndicatorManager sharedInstance]
959 startNetworkTaskForGroup:[self getNetworkActivityKey]]; 960 startNetworkTaskForGroup:[self getNetworkActivityKey]];
960 _fetcher->Start(); 961 _fetcher->Start();
961 } 962 }
962 963
963 - (void)onHeadFetchComplete { 964 - (void)onHeadFetchComplete {
964 [[NetworkActivityIndicatorManager sharedInstance] 965 [[NetworkActivityIndicatorManager sharedInstance]
965 stopNetworkTaskForGroup:[self getNetworkActivityKey]]; 966 stopNetworkTaskForGroup:[self getNetworkActivityKey]];
966 int responseCode = _fetcher->GetResponseCode(); 967 int responseCode = _fetcher->GetResponseCode();
967 UMA_HISTOGRAM_SPARSE_SLOWLY( 968 UMA_HISTOGRAM_SPARSE_SLOWLY(
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
1258 if (![DownloadManagerController 1259 if (![DownloadManagerController
1259 fetchDownloadsDirectoryFilePath:&downloadsDirectoryPath]) { 1260 fetchDownloadsDirectoryFilePath:&downloadsDirectoryPath]) {
1260 [self displayError]; 1261 [self displayError];
1261 return; 1262 return;
1262 } 1263 }
1263 _downloadFilePath = downloadsDirectoryPath.Append( 1264 _downloadFilePath = downloadsDirectoryPath.Append(
1264 base::SysNSStringToUTF8([_fileNameLabel text])); 1265 base::SysNSStringToUTF8([_fileNameLabel text]));
1265 1266
1266 _fetcher = URLFetcher::Create([self url], URLFetcher::GET, 1267 _fetcher = URLFetcher::Create([self url], URLFetcher::GET,
1267 _contentFetcherDelegate.get()); 1268 _contentFetcherDelegate.get());
1268 _fetcher->SetRequestContext(_requestContextGetter.get()); 1269 _fetcher->SetRequestContext(
1270 _webState->GetBrowserState()->GetRequestContext());
1269 base::SequencedWorkerPool::SequenceToken sequenceToken = 1271 base::SequencedWorkerPool::SequenceToken sequenceToken =
1270 web::WebThread::GetBlockingPool()->GetSequenceToken(); 1272 web::WebThread::GetBlockingPool()->GetSequenceToken();
1271 _fetcher->SaveResponseToFileAtPath( 1273 _fetcher->SaveResponseToFileAtPath(
1272 _downloadFilePath, 1274 _downloadFilePath,
1273 web::WebThread::GetBlockingPool()->GetSequencedTaskRunner(sequenceToken)); 1275 web::WebThread::GetBlockingPool()->GetSequencedTaskRunner(sequenceToken));
1274 [[NetworkActivityIndicatorManager sharedInstance] 1276 [[NetworkActivityIndicatorManager sharedInstance]
1275 startNetworkTaskForGroup:[self getNetworkActivityKey]]; 1277 startNetworkTaskForGroup:[self getNetworkActivityKey]];
1276 _fetcher->Start(); 1278 _fetcher->Start();
1277 1279
1278 self.downloadStartedTime = [NSDate date]; 1280 self.downloadStartedTime = [NSDate date];
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
1569 if (!showedMenu) { 1571 if (!showedMenu) {
1570 [self displayUnableToOpenFileDialog]; 1572 [self displayUnableToOpenFileDialog];
1571 } 1573 }
1572 } 1574 }
1573 1575
1574 - (IBAction)googleDriveButtonTapped:(id)sender { 1576 - (IBAction)googleDriveButtonTapped:(id)sender {
1575 [self openGoogleDriveInAppStore]; 1577 [self openGoogleDriveInAppStore];
1576 } 1578 }
1577 1579
1578 - (void)openGoogleDriveInAppStore { 1580 - (void)openGoogleDriveInAppStore {
1579 [[InstallationNotifier sharedInstance] 1581 StoreKitTabHelper* helper = StoreKitTabHelper::FromWebState(_webState);
1580 registerForInstallationNotifications:self 1582 if (helper) {
1581 withSelector:@selector(hideGoogleDriveButton) 1583 [[InstallationNotifier sharedInstance]
1582 forScheme:[_googleDriveMetadata anyScheme]]; 1584 registerForInstallationNotifications:self
1583 1585 withSelector:@selector(hideGoogleDriveButton)
1584 [_storeKitLauncher openAppStore:[_googleDriveMetadata appId]]; 1586 forScheme:[_googleDriveMetadata anyScheme]];
1587 helper->OpenAppStore([_googleDriveMetadata appId]);
1588 }
1585 } 1589 }
1586 1590
1587 - (NSString*)getNetworkActivityKey { 1591 - (NSString*)getNetworkActivityKey {
1588 return 1592 return
1589 [NSString stringWithFormat: 1593 [NSString stringWithFormat:
1590 @"DownloadManagerController.NetworkActivityIndicatorKey.%d", 1594 @"DownloadManagerController.NetworkActivityIndicatorKey.%d",
1591 _downloadManagerId]; 1595 _downloadManagerId];
1592 } 1596 }
1593 1597
1594 + (BOOL)fetchDownloadsDirectoryFilePath:(base::FilePath*)path { 1598 + (BOOL)fetchDownloadsDirectoryFilePath:(base::FilePath*)path {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1636 1640
1637 #pragma mark - CRWNativeContent 1641 #pragma mark - CRWNativeContent
1638 1642
1639 - (void)close { 1643 - (void)close {
1640 // Makes sure that all outstanding network requests are shut down before 1644 // Makes sure that all outstanding network requests are shut down before
1641 // this controller is closed. 1645 // this controller is closed.
1642 _fetcher.reset(); 1646 _fetcher.reset();
1643 } 1647 }
1644 1648
1645 @end 1649 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698