Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "ios/chrome/browser/ui/history/history_collection_view_controller.h" | 5 #include "ios/chrome/browser/ui/history/history_collection_view_controller.h" |
| 6 | 6 |
| 7 #import <MobileCoreServices/MobileCoreServices.h> | 7 #import <MobileCoreServices/MobileCoreServices.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #import "base/ios/weak_nsobject.h" | 11 #import "base/ios/weak_nsobject.h" |
| 12 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
| 13 #import "base/mac/objc_property_releaser.h" | 13 #import "base/mac/objc_property_releaser.h" |
| 14 #include "base/mac/scoped_nsobject.h" | 14 #include "base/mac/scoped_nsobject.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "components/browsing_data/core/history_notice_utils.h" | 17 #include "components/browsing_data/core/history_notice_utils.h" |
| 18 #include "components/strings/grit/components_strings.h" | 18 #include "components/strings/grit/components_strings.h" |
| 19 #include "components/url_formatter/url_formatter.h" | 19 #include "components/url_formatter/url_formatter.h" |
| 20 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 20 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 21 #include "ios/chrome/browser/chrome_url_constants.h" | 21 #include "ios/chrome/browser/chrome_url_constants.h" |
| 22 #import "ios/chrome/browser/signin/authentication_service.h" | 22 #import "ios/chrome/browser/signin/authentication_service.h" |
| 23 #include "ios/chrome/browser/signin/authentication_service_factory.h" | 23 #include "ios/chrome/browser/signin/authentication_service_factory.h" |
| 24 #include "ios/chrome/browser/sync/sync_setup_service.h" | |
| 25 #include "ios/chrome/browser/sync/sync_setup_service_factory.h" | |
| 24 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom e.h" | 26 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom e.h" |
| 25 #import "ios/chrome/browser/ui/collection_view/cells/activity_indicator_cell.h" | 27 #import "ios/chrome/browser/ui/collection_view/cells/activity_indicator_cell.h" |
| 26 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 28 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 27 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h " | 29 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h " |
| 28 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 30 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 29 #import "ios/chrome/browser/ui/context_menu/context_menu_coordinator.h" | 31 #import "ios/chrome/browser/ui/context_menu/context_menu_coordinator.h" |
| 30 #include "ios/chrome/browser/ui/history/history_entries_status_item.h" | 32 #include "ios/chrome/browser/ui/history/history_entries_status_item.h" |
| 31 #include "ios/chrome/browser/ui/history/history_entry.h" | 33 #include "ios/chrome/browser/ui/history/history_entry.h" |
| 32 #include "ios/chrome/browser/ui/history/history_entry_inserter.h" | 34 #include "ios/chrome/browser/ui/history/history_entry_inserter.h" |
| 33 #import "ios/chrome/browser/ui/history/history_entry_item.h" | 35 #import "ios/chrome/browser/ui/history/history_entry_item.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 - (void)fetchHistoryForQuery:(NSString*)query | 102 - (void)fetchHistoryForQuery:(NSString*)query |
| 101 priorToTime:(const base::Time&)time; | 103 priorToTime:(const base::Time&)time; |
| 102 // Updates header section to provide relevant information about the currently | 104 // Updates header section to provide relevant information about the currently |
| 103 // displayed history entries. | 105 // displayed history entries. |
| 104 - (void)updateEntriesStatusMessage; | 106 - (void)updateEntriesStatusMessage; |
| 105 // Removes selected items from the visible collection, but does not delete them | 107 // Removes selected items from the visible collection, but does not delete them |
| 106 // from browser history. | 108 // from browser history. |
| 107 - (void)removeSelectedItemsFromCollection; | 109 - (void)removeSelectedItemsFromCollection; |
| 108 // Removes all items in the collection that are not included in entries. | 110 // Removes all items in the collection that are not included in entries. |
| 109 - (void)filterForHistoryEntries:(NSArray*)entries; | 111 - (void)filterForHistoryEntries:(NSArray*)entries; |
| 112 // Adds loading indicator to the top of the history collection, if one is not | |
| 113 // already present. | |
| 114 - (void)addLoadingIndicator; | |
| 110 // Displays context menu on cell pressed with gestureRecognizer. | 115 // Displays context menu on cell pressed with gestureRecognizer. |
| 111 - (void)displayContextMenuInvokedByGestureRecognizer: | 116 - (void)displayContextMenuInvokedByGestureRecognizer: |
| 112 (UILongPressGestureRecognizer*)gestureRecognizer; | 117 (UILongPressGestureRecognizer*)gestureRecognizer; |
| 113 // Opens URL in the current tab and dismisses the history view. | 118 // Opens URL in the current tab and dismisses the history view. |
| 114 - (void)openURL:(const GURL&)URL; | 119 - (void)openURL:(const GURL&)URL; |
| 115 // Opens URL in a new non-incognito tab and dismisses the history view. | 120 // Opens URL in a new non-incognito tab and dismisses the history view. |
| 116 - (void)openURLInNewTab:(const GURL&)URL; | 121 - (void)openURLInNewTab:(const GURL&)URL; |
| 117 // Opens URL in a new incognito tab and dismisses the history view. | 122 // Opens URL in a new incognito tab and dismisses the history view. |
| 118 - (void)openURLInNewIncognitoTab:(const GURL&)URL; | 123 - (void)openURLInNewIncognitoTab:(const GURL&)URL; |
| 119 // Copies URL to the clipboard. | 124 // Copies URL to the clipboard. |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 inSectionWithIdentifier: | 313 inSectionWithIdentifier: |
| 309 [self.entryInserter sectionIdentifierForTimestamp:item.timestamp]]; | 314 [self.entryInserter sectionIdentifierForTimestamp:item.timestamp]]; |
| 310 } | 315 } |
| 311 } | 316 } |
| 312 | 317 |
| 313 #pragma mark - HistoryServiceFacadeDelegate | 318 #pragma mark - HistoryServiceFacadeDelegate |
| 314 | 319 |
| 315 - (void)historyServiceFacade:(HistoryServiceFacade*)facade | 320 - (void)historyServiceFacade:(HistoryServiceFacade*)facade |
| 316 didReceiveQueryResult:(HistoryServiceFacade::QueryResult)result { | 321 didReceiveQueryResult:(HistoryServiceFacade::QueryResult)result { |
| 317 self.loading = NO; | 322 self.loading = NO; |
| 318 // Remove loading indicator. | 323 // If history sync is enabled and there hasn't been a response from synced |
| 319 CollectionViewItem* headerItem = [self.collectionViewModel | 324 // history, try fetching again. |
| 320 itemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; | 325 SyncSetupService* syncSetupService = |
| 321 if ([headerItem.cellClass isSubclassOfClass:[ActivityIndicatorCell class]]) { | 326 SyncSetupServiceFactory::GetForBrowserState(_browserState); |
| 322 [self.collectionViewModel removeItemWithType:kItemTypeEnumZero | 327 if (syncSetupService->IsSyncEnabled() && |
| 323 fromSectionWithIdentifier:kSectionIdentifierEnumZero]; | 328 syncSetupService->IsDataTypeEnabled(syncer::HISTORY_DELETE_DIRECTIVES) && |
| 324 [self.collectionView | 329 !result.sync_returned) { |
| 325 deleteItemsAtIndexPaths:@[ [NSIndexPath indexPathForItem:0 | 330 [self showHistoryMatchingQuery:_currentQuery]; |
| 326 inSection:0] ]]; | 331 return; |
| 327 } | 332 } |
| 328 | 333 |
| 329 // If there are no results and no URLs have been loaded, report that no | 334 // If there are no results and no URLs have been loaded, report that no |
| 330 // history entries were found. | 335 // history entries were found. |
| 331 if (result.entries.empty() && !self.hasHistoryEntries) { | 336 if (result.entries.empty() && !self.hasHistoryEntries) { |
| 332 DCHECK(self.entriesType == NO_ENTRIES); | 337 DCHECK(self.entriesType == NO_ENTRIES); |
| 333 [self updateEntriesStatusMessage]; | 338 [self updateEntriesStatusMessage]; |
| 334 [self.delegate historyCollectionViewControllerDidChangeEntries:self]; | 339 [self.delegate historyCollectionViewControllerDidChangeEntries:self]; |
| 335 return; | 340 return; |
| 336 } | 341 } |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 519 [self.collectionViewModel itemAtIndexPath:indexPath]); | 524 [self.collectionViewModel itemAtIndexPath:indexPath]); |
| 520 [self fetchHistoryForQuery:_currentQuery priorToTime:lastItem.timestamp]; | 525 [self fetchHistoryForQuery:_currentQuery priorToTime:lastItem.timestamp]; |
| 521 } | 526 } |
| 522 } | 527 } |
| 523 | 528 |
| 524 #pragma mark - Private methods | 529 #pragma mark - Private methods |
| 525 | 530 |
| 526 - (void)fetchHistoryForQuery:(NSString*)query | 531 - (void)fetchHistoryForQuery:(NSString*)query |
| 527 priorToTime:(const base::Time&)time { | 532 priorToTime:(const base::Time&)time { |
| 528 self.loading = YES; | 533 self.loading = YES; |
| 529 // Add loading indicator if nothing else is shown. | 534 // Add loading indicator if no items are shown. |
| 530 if (!self.hasHistoryEntries && !self.isSearching) { | 535 if (!self.hasHistoryEntries && !self.isSearching) { |
| 531 [self.collectionView performBatchUpdates:^{ | 536 [self addLoadingIndicator]; |
| 532 NSIndexPath* indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; | |
| 533 if ([self.collectionViewModel hasItemAtIndexPath:indexPath]) { | |
| 534 [self.collectionViewModel | |
| 535 removeItemWithType:kItemTypeEnumZero | |
| 536 fromSectionWithIdentifier:kSectionIdentifierEnumZero]; | |
| 537 [self.collectionView deleteItemsAtIndexPaths:@[ indexPath ]]; | |
| 538 } | |
| 539 CollectionViewItem* loadingIndicatorItem = [[[CollectionViewItem alloc] | |
| 540 initWithType:kItemTypeEnumZero] autorelease]; | |
| 541 loadingIndicatorItem.cellClass = [ActivityIndicatorCell class]; | |
| 542 [self.collectionViewModel addItem:loadingIndicatorItem | |
| 543 toSectionWithIdentifier:kEntriesStatusSectionIdentifier]; | |
| 544 [self.collectionView insertItemsAtIndexPaths:@[ indexPath ]]; | |
| 545 } | |
| 546 completion:nil]; | |
| 547 } | 537 } |
| 548 | 538 |
| 549 BOOL fetchAllHistory = !query || [query isEqualToString:@""]; | 539 BOOL fetchAllHistory = !query || [query isEqualToString:@""]; |
| 550 base::string16 queryString = | 540 base::string16 queryString = |
| 551 fetchAllHistory ? base::string16() : base::SysNSStringToUTF16(query); | 541 fetchAllHistory ? base::string16() : base::SysNSStringToUTF16(query); |
| 552 history::QueryOptions options; | 542 history::QueryOptions options; |
| 553 options.end_time = time; | 543 options.end_time = time; |
| 554 options.duplicate_policy = | 544 options.duplicate_policy = |
| 555 fetchAllHistory ? history::QueryOptions::REMOVE_DUPLICATES_PER_DAY | 545 fetchAllHistory ? history::QueryOptions::REMOVE_DUPLICATES_PER_DAY |
| 556 : history::QueryOptions::REMOVE_ALL_DUPLICATES; | 546 : history::QueryOptions::REMOVE_ALL_DUPLICATES; |
| 557 options.max_count = kMaxFetchCount; | 547 options.max_count = kMaxFetchCount; |
| 558 options.matching_algorithm = | 548 options.matching_algorithm = |
| 559 query_parser::MatchingAlgorithm::ALWAYS_PREFIX_SEARCH; | 549 query_parser::MatchingAlgorithm::ALWAYS_PREFIX_SEARCH; |
| 560 _historyServiceFacade->QueryOtherFormsOfBrowsingHistory(); | |
| 561 _historyServiceFacade->QueryHistory(queryString, options); | 550 _historyServiceFacade->QueryHistory(queryString, options); |
| 562 // Also determine whether notice regarding other forms of browsing history | 551 // Also determine whether notice regarding other forms of browsing history |
| 563 // should be shown. | 552 // should be shown. |
| 564 _historyServiceFacade->QueryOtherFormsOfBrowsingHistory(); | 553 _historyServiceFacade->QueryOtherFormsOfBrowsingHistory(); |
| 565 } | 554 } |
| 566 | 555 |
| 567 - (void)updateEntriesStatusMessage { | 556 - (void)updateEntriesStatusMessage { |
| 568 CollectionViewItem* entriesStatusItem = nil; | 557 CollectionViewItem* entriesStatusItem = nil; |
| 569 if (!self.hasHistoryEntries) { | 558 if (!self.hasHistoryEntries) { |
| 570 CollectionViewTextItem* noResultsItem = [[[CollectionViewTextItem alloc] | 559 CollectionViewTextItem* noResultsItem = [[[CollectionViewTextItem alloc] |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 660 selectItemAtIndexPath:indexPath | 649 selectItemAtIndexPath:indexPath |
| 661 animated:NO | 650 animated:NO |
| 662 scrollPosition:UICollectionViewScrollPositionNone]; | 651 scrollPosition:UICollectionViewScrollPositionNone]; |
| 663 } | 652 } |
| 664 } | 653 } |
| 665 } | 654 } |
| 666 } | 655 } |
| 667 [self removeSelectedItemsFromCollection]; | 656 [self removeSelectedItemsFromCollection]; |
| 668 } | 657 } |
| 669 | 658 |
| 659 - (void)addLoadingIndicator { | |
| 660 NSIndexPath* indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; | |
| 661 if ([self.collectionViewModel hasItemAtIndexPath:indexPath] && | |
| 662 [[[self.collectionViewModel itemAtIndexPath:indexPath] cellClass] | |
| 663 isSubclassOfClass:[ActivityIndicatorCell class]]) { | |
| 664 // Do not add indicator a sectond time. | |
|
lpromero
2016/12/20 23:13:42
second*
Can't you look instead in the model then
Jackie Quinn
2016/12/20 23:18:10
Done. There is no ActivityIndicatorItem, only the
lpromero
2016/12/20 23:26:23
That's where giving a specific type to the item ca
Jackie Quinn
2016/12/20 23:33:21
If the sole purpose is checking for the type of it
lpromero
2016/12/20 23:38:51
I meant just the enum value you pass to the initia
Jackie Quinn
2016/12/21 00:02:05
Oh, I see. Yeah that sounds reasonable. Updated!
| |
| 665 return; | |
| 666 } | |
| 667 | |
| 668 [self.collectionView performBatchUpdates:^{ | |
| 669 if ([self.collectionViewModel hasItemAtIndexPath:indexPath]) { | |
| 670 [self.collectionViewModel removeItemWithType:kItemTypeEnumZero | |
| 671 fromSectionWithIdentifier:kSectionIdentifierEnumZero]; | |
| 672 [self.collectionView deleteItemsAtIndexPaths:@[ indexPath ]]; | |
| 673 } | |
| 674 CollectionViewItem* loadingIndicatorItem = [[[CollectionViewItem alloc] | |
| 675 initWithType:kItemTypeEnumZero] autorelease]; | |
| 676 loadingIndicatorItem.cellClass = [ActivityIndicatorCell class]; | |
| 677 [self.collectionViewModel addItem:loadingIndicatorItem | |
| 678 toSectionWithIdentifier:kEntriesStatusSectionIdentifier]; | |
| 679 [self.collectionView insertItemsAtIndexPaths:@[ indexPath ]]; | |
| 680 } | |
| 681 completion:nil]; | |
| 682 } | |
| 683 | |
| 670 #pragma mark Context Menu | 684 #pragma mark Context Menu |
| 671 | 685 |
| 672 - (void)displayContextMenuInvokedByGestureRecognizer: | 686 - (void)displayContextMenuInvokedByGestureRecognizer: |
| 673 (UILongPressGestureRecognizer*)gestureRecognizer { | 687 (UILongPressGestureRecognizer*)gestureRecognizer { |
| 674 if (gestureRecognizer.numberOfTouches != 1 || self.editing || | 688 if (gestureRecognizer.numberOfTouches != 1 || self.editing || |
| 675 gestureRecognizer.state != UIGestureRecognizerStateBegan) { | 689 gestureRecognizer.state != UIGestureRecognizerStateBegan) { |
| 676 return; | 690 return; |
| 677 } | 691 } |
| 678 | 692 |
| 679 CGPoint touchLocation = | 693 CGPoint touchLocation = |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 782 NSData* plainText = [base::SysUTF8ToNSString(URL.spec()) | 796 NSData* plainText = [base::SysUTF8ToNSString(URL.spec()) |
| 783 dataUsingEncoding:NSUTF8StringEncoding]; | 797 dataUsingEncoding:NSUTF8StringEncoding]; |
| 784 NSDictionary* copiedItem = @{ | 798 NSDictionary* copiedItem = @{ |
| 785 (NSString*)kUTTypeURL : net::NSURLWithGURL(URL), | 799 (NSString*)kUTTypeURL : net::NSURLWithGURL(URL), |
| 786 (NSString*)kUTTypeUTF8PlainText : plainText, | 800 (NSString*)kUTTypeUTF8PlainText : plainText, |
| 787 }; | 801 }; |
| 788 [[UIPasteboard generalPasteboard] setItems:@[ copiedItem ]]; | 802 [[UIPasteboard generalPasteboard] setItems:@[ copiedItem ]]; |
| 789 } | 803 } |
| 790 | 804 |
| 791 @end | 805 @end |
| OLD | NEW |