| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/content_suggestions/content_suggestions_mediator.h" | 5 #import "ios/chrome/browser/content_suggestions/content_suggestions_mediator.h" |
| 6 | 6 |
| 7 #include "base/mac/bind_objc_block.h" | 7 #include "base/mac/bind_objc_block.h" |
| 8 #include "base/mac/foundation_util.h" | 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/optional.h" | 10 #include "base/optional.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 @interface ContentSuggestionsMediator ()<ContentSuggestionsImageFetcher, | 47 @interface ContentSuggestionsMediator ()<ContentSuggestionsImageFetcher, |
| 48 ContentSuggestionsServiceObserver, | 48 ContentSuggestionsServiceObserver, |
| 49 MostVisitedSitesObserving> { | 49 MostVisitedSitesObserving> { |
| 50 // Bridge for this class to become an observer of a ContentSuggestionsService. | 50 // Bridge for this class to become an observer of a ContentSuggestionsService. |
| 51 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; | 51 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; |
| 52 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; | 52 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; |
| 53 std::unique_ptr<ntp_tiles::MostVisitedSitesObserverBridge> _mostVisitedBridge; | 53 std::unique_ptr<ntp_tiles::MostVisitedSitesObserverBridge> _mostVisitedBridge; |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Most visited items from the MostVisitedSites service (copied upon receiving | 56 // Most visited items from the MostVisitedSites service currently displayed. |
| 57 // the callback). | |
| 58 @property(nonatomic, strong) | 57 @property(nonatomic, strong) |
| 59 NSMutableArray<ContentSuggestionsMostVisitedItem*>* mostVisitedItems; | 58 NSMutableArray<ContentSuggestionsMostVisitedItem*>* mostVisitedItems; |
| 59 // Most visited items from the MostVisitedSites service (copied upon receiving |
| 60 // the callback). Those items are up to date with the model. |
| 61 @property(nonatomic, strong) |
| 62 NSMutableArray<ContentSuggestionsMostVisitedItem*>* freshMostVisitedItems; |
| 60 // Section Info for the Most Visited section. | 63 // Section Info for the Most Visited section. |
| 61 @property(nonatomic, strong) | 64 @property(nonatomic, strong) |
| 62 ContentSuggestionsSectionInformation* mostVisitedSectionInfo; | 65 ContentSuggestionsSectionInformation* mostVisitedSectionInfo; |
| 63 // Whether the page impression has been recorded. | 66 // Whether the page impression has been recorded. |
| 64 @property(nonatomic, assign) BOOL recordedPageImpression; | 67 @property(nonatomic, assign) BOOL recordedPageImpression; |
| 65 // The ContentSuggestionsService, serving suggestions. | 68 // The ContentSuggestionsService, serving suggestions. |
| 66 @property(nonatomic, assign) | 69 @property(nonatomic, assign) |
| 67 ntp_snippets::ContentSuggestionsService* contentService; | 70 ntp_snippets::ContentSuggestionsService* contentService; |
| 68 // Map the section information created to the relevant category. | 71 // Map the section information created to the relevant category. |
| 69 @property(nonatomic, strong, nonnull) | 72 @property(nonatomic, strong, nonnull) |
| 70 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, | 73 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, |
| 71 ContentSuggestionsSectionInformation*>* | 74 ContentSuggestionsSectionInformation*>* |
| 72 sectionInformationByCategory; | 75 sectionInformationByCategory; |
| 73 // FaviconAttributesProvider to fetch the favicon for the suggestions. | 76 // FaviconAttributesProvider to fetch the favicon for the suggestions. |
| 74 @property(nonatomic, nullable, strong) | 77 @property(nonatomic, nullable, strong) |
| 75 FaviconAttributesProvider* attributesProvider; | 78 FaviconAttributesProvider* attributesProvider; |
| 76 | 79 |
| 77 @end | 80 @end |
| 78 | 81 |
| 79 @implementation ContentSuggestionsMediator | 82 @implementation ContentSuggestionsMediator |
| 80 | 83 |
| 81 @synthesize mostVisitedItems = _mostVisitedItems; | 84 @synthesize mostVisitedItems = _mostVisitedItems; |
| 85 @synthesize freshMostVisitedItems = _freshMostVisitedItems; |
| 82 @synthesize mostVisitedSectionInfo = _mostVisitedSectionInfo; | 86 @synthesize mostVisitedSectionInfo = _mostVisitedSectionInfo; |
| 83 @synthesize recordedPageImpression = _recordedPageImpression; | 87 @synthesize recordedPageImpression = _recordedPageImpression; |
| 84 @synthesize contentService = _contentService; | 88 @synthesize contentService = _contentService; |
| 85 @synthesize dataSink = _dataSink; | 89 @synthesize dataSink = _dataSink; |
| 86 @synthesize sectionInformationByCategory = _sectionInformationByCategory; | 90 @synthesize sectionInformationByCategory = _sectionInformationByCategory; |
| 87 @synthesize attributesProvider = _attributesProvider; | 91 @synthesize attributesProvider = _attributesProvider; |
| 88 @synthesize commandHandler = _commandHandler; | 92 @synthesize commandHandler = _commandHandler; |
| 89 | 93 |
| 90 #pragma mark - Public | 94 #pragma mark - Public |
| 91 | 95 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 120 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo]; | 124 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo]; |
| 121 ntp_snippets::ContentSuggestion::ID suggestion_id = | 125 ntp_snippets::ContentSuggestion::ID suggestion_id = |
| 122 ntp_snippets::ContentSuggestion::ID([categoryWrapper category], | 126 ntp_snippets::ContentSuggestion::ID([categoryWrapper category], |
| 123 suggestionIdentifier.IDInSection); | 127 suggestionIdentifier.IDInSection); |
| 124 | 128 |
| 125 self.contentService->DismissSuggestion(suggestion_id); | 129 self.contentService->DismissSuggestion(suggestion_id); |
| 126 } | 130 } |
| 127 | 131 |
| 128 - (void)blacklistMostVisitedURL:(GURL)URL { | 132 - (void)blacklistMostVisitedURL:(GURL)URL { |
| 129 _mostVisitedSites->AddOrRemoveBlacklistedUrl(URL, true); | 133 _mostVisitedSites->AddOrRemoveBlacklistedUrl(URL, true); |
| 134 [self useFreshMostVisited]; |
| 130 } | 135 } |
| 131 | 136 |
| 132 - (void)whitelistMostVisitedURL:(GURL)URL { | 137 - (void)whitelistMostVisitedURL:(GURL)URL { |
| 133 _mostVisitedSites->AddOrRemoveBlacklistedUrl(URL, false); | 138 _mostVisitedSites->AddOrRemoveBlacklistedUrl(URL, false); |
| 139 [self useFreshMostVisited]; |
| 134 } | 140 } |
| 135 | 141 |
| 136 #pragma mark - ContentSuggestionsDataSource | 142 #pragma mark - ContentSuggestionsDataSource |
| 137 | 143 |
| 138 - (NSArray<ContentSuggestionsSectionInformation*>*)sectionsInfo { | 144 - (NSArray<ContentSuggestionsSectionInformation*>*)sectionsInfo { |
| 139 NSMutableArray<ContentSuggestionsSectionInformation*>* sectionsInfo = | 145 NSMutableArray<ContentSuggestionsSectionInformation*>* sectionsInfo = |
| 140 [NSMutableArray array]; | 146 [NSMutableArray array]; |
| 141 | 147 |
| 142 if (self.mostVisitedItems.count > 0) { | 148 if (self.mostVisitedItems.count > 0) { |
| 143 [sectionsInfo addObject:self.mostVisitedSectionInfo]; | 149 [sectionsInfo addObject:self.mostVisitedSectionInfo]; |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 } | 368 } |
| 363 | 369 |
| 364 callback([image.ToUIImage() copy]); | 370 callback([image.ToUIImage() copy]); |
| 365 })); | 371 })); |
| 366 } | 372 } |
| 367 | 373 |
| 368 #pragma mark - MostVisitedSitesObserving | 374 #pragma mark - MostVisitedSitesObserving |
| 369 | 375 |
| 370 - (void)onMostVisitedURLsAvailable: | 376 - (void)onMostVisitedURLsAvailable: |
| 371 (const ntp_tiles::NTPTilesVector&)mostVisited { | 377 (const ntp_tiles::NTPTilesVector&)mostVisited { |
| 372 self.mostVisitedItems = [NSMutableArray array]; | 378 self.freshMostVisitedItems = [NSMutableArray array]; |
| 373 for (const ntp_tiles::NTPTile& tile : mostVisited) { | 379 for (const ntp_tiles::NTPTile& tile : mostVisited) { |
| 374 [self.mostVisitedItems | 380 [self.freshMostVisitedItems |
| 375 addObject:ConvertNTPTile(tile, self.mostVisitedSectionInfo)]; | 381 addObject:ConvertNTPTile(tile, self.mostVisitedSectionInfo)]; |
| 376 } | 382 } |
| 377 | 383 |
| 378 [self.dataSink reloadSection:self.mostVisitedSectionInfo]; | 384 if ([self.mostVisitedItems count] > 0) { |
| 385 // If some content is already displayed to the user, do not update without a |
| 386 // user action. |
| 387 return; |
| 388 } |
| 389 |
| 390 [self useFreshMostVisited]; |
| 379 | 391 |
| 380 if (mostVisited.size() && !self.recordedPageImpression) { | 392 if (mostVisited.size() && !self.recordedPageImpression) { |
| 381 self.recordedPageImpression = YES; | 393 self.recordedPageImpression = YES; |
| 382 RecordPageImpression(mostVisited); | 394 RecordPageImpression(mostVisited); |
| 383 } | 395 } |
| 384 } | 396 } |
| 385 | 397 |
| 386 - (void)onIconMadeAvailable:(const GURL&)siteURL { | 398 - (void)onIconMadeAvailable:(const GURL&)siteURL { |
| 387 for (ContentSuggestionsMostVisitedItem* item in self.mostVisitedItems) { | 399 for (ContentSuggestionsMostVisitedItem* item in self.mostVisitedItems) { |
| 388 if (item.URL == siteURL) { | 400 if (item.URL == siteURL) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 callback(contentSuggestions); | 469 callback(contentSuggestions); |
| 458 } | 470 } |
| 459 | 471 |
| 460 // Returns whether the |sectionInfo| is associated with a category from the | 472 // Returns whether the |sectionInfo| is associated with a category from the |
| 461 // content suggestions service. | 473 // content suggestions service. |
| 462 - (BOOL)isRelatedToContentSuggestionsService: | 474 - (BOOL)isRelatedToContentSuggestionsService: |
| 463 (ContentSuggestionsSectionInformation*)sectionInfo { | 475 (ContentSuggestionsSectionInformation*)sectionInfo { |
| 464 return sectionInfo != self.mostVisitedSectionInfo; | 476 return sectionInfo != self.mostVisitedSectionInfo; |
| 465 } | 477 } |
| 466 | 478 |
| 479 // Replaces the Most Visited items currently displayed by the most recent ones. |
| 480 - (void)useFreshMostVisited { |
| 481 self.mostVisitedItems = self.freshMostVisitedItems; |
| 482 [self.dataSink reloadSection:self.mostVisitedSectionInfo]; |
| 483 } |
| 484 |
| 467 @end | 485 @end |
| OLD | NEW |