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 data from the MostVisitedSites service (copied upon receiving | 56 // Most visited items from the MostVisitedSites service (copied upon receiving |
57 // the callback). | 57 // the callback). |
58 @property(nonatomic, assign) std::vector<ntp_tiles::NTPTile> mostVisitedData; | 58 @property(nonatomic, strong) |
| 59 NSMutableArray<ContentSuggestionsMostVisitedItem*>* mostVisitedItems; |
59 // Section Info for the Most Visited section. | 60 // Section Info for the Most Visited section. |
60 @property(nonatomic, strong) | 61 @property(nonatomic, strong) |
61 ContentSuggestionsSectionInformation* mostVisitedSectionInfo; | 62 ContentSuggestionsSectionInformation* mostVisitedSectionInfo; |
62 // Whether the page impression has been recorded. | 63 // Whether the page impression has been recorded. |
63 @property(nonatomic, assign) BOOL recordedPageImpression; | 64 @property(nonatomic, assign) BOOL recordedPageImpression; |
64 // The ContentSuggestionsService, serving suggestions. | 65 // The ContentSuggestionsService, serving suggestions. |
65 @property(nonatomic, assign) | 66 @property(nonatomic, assign) |
66 ntp_snippets::ContentSuggestionsService* contentService; | 67 ntp_snippets::ContentSuggestionsService* contentService; |
67 // Map the section information created to the relevant category. | 68 // Map the section information created to the relevant category. |
68 @property(nonatomic, strong, nonnull) | 69 @property(nonatomic, strong, nonnull) |
69 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, | 70 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, |
70 ContentSuggestionsSectionInformation*>* | 71 ContentSuggestionsSectionInformation*>* |
71 sectionInformationByCategory; | 72 sectionInformationByCategory; |
72 // FaviconAttributesProvider to fetch the favicon for the suggestions. | 73 // FaviconAttributesProvider to fetch the favicon for the suggestions. |
73 @property(nonatomic, nullable, strong) | 74 @property(nonatomic, nullable, strong) |
74 FaviconAttributesProvider* attributesProvider; | 75 FaviconAttributesProvider* attributesProvider; |
75 | 76 |
76 @end | 77 @end |
77 | 78 |
78 @implementation ContentSuggestionsMediator | 79 @implementation ContentSuggestionsMediator |
79 | 80 |
80 @synthesize mostVisitedData = _mostVisitedData; | 81 @synthesize mostVisitedItems = _mostVisitedItems; |
81 @synthesize mostVisitedSectionInfo = _mostVisitedSectionInfo; | 82 @synthesize mostVisitedSectionInfo = _mostVisitedSectionInfo; |
82 @synthesize recordedPageImpression = _recordedPageImpression; | 83 @synthesize recordedPageImpression = _recordedPageImpression; |
83 @synthesize contentService = _contentService; | 84 @synthesize contentService = _contentService; |
84 @synthesize dataSink = _dataSink; | 85 @synthesize dataSink = _dataSink; |
85 @synthesize sectionInformationByCategory = _sectionInformationByCategory; | 86 @synthesize sectionInformationByCategory = _sectionInformationByCategory; |
86 @synthesize attributesProvider = _attributesProvider; | 87 @synthesize attributesProvider = _attributesProvider; |
87 @synthesize commandHandler = _commandHandler; | 88 @synthesize commandHandler = _commandHandler; |
88 | 89 |
89 #pragma mark - Public | 90 #pragma mark - Public |
90 | 91 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 | 124 |
124 self.contentService->DismissSuggestion(suggestion_id); | 125 self.contentService->DismissSuggestion(suggestion_id); |
125 } | 126 } |
126 | 127 |
127 #pragma mark - ContentSuggestionsDataSource | 128 #pragma mark - ContentSuggestionsDataSource |
128 | 129 |
129 - (NSArray<ContentSuggestionsSectionInformation*>*)sectionsInfo { | 130 - (NSArray<ContentSuggestionsSectionInformation*>*)sectionsInfo { |
130 NSMutableArray<ContentSuggestionsSectionInformation*>* sectionsInfo = | 131 NSMutableArray<ContentSuggestionsSectionInformation*>* sectionsInfo = |
131 [NSMutableArray array]; | 132 [NSMutableArray array]; |
132 | 133 |
133 if (!self.mostVisitedData.empty()) { | 134 if (self.mostVisitedItems.count > 0) { |
134 [sectionsInfo addObject:self.mostVisitedSectionInfo]; | 135 [sectionsInfo addObject:self.mostVisitedSectionInfo]; |
135 } | 136 } |
136 | 137 |
137 std::vector<ntp_snippets::Category> categories = | 138 std::vector<ntp_snippets::Category> categories = |
138 self.contentService->GetCategories(); | 139 self.contentService->GetCategories(); |
139 | 140 |
140 for (auto& category : categories) { | 141 for (auto& category : categories) { |
141 ContentSuggestionsCategoryWrapper* categoryWrapper = | 142 ContentSuggestionsCategoryWrapper* categoryWrapper = |
142 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; | 143 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; |
143 if (!self.sectionInformationByCategory[categoryWrapper]) { | 144 if (!self.sectionInformationByCategory[categoryWrapper]) { |
144 [self addSectionInformationForCategory:category]; | 145 [self addSectionInformationForCategory:category]; |
145 } | 146 } |
146 [sectionsInfo addObject:self.sectionInformationByCategory[categoryWrapper]]; | 147 [sectionsInfo addObject:self.sectionInformationByCategory[categoryWrapper]]; |
147 } | 148 } |
148 | 149 |
149 return sectionsInfo; | 150 return sectionsInfo; |
150 } | 151 } |
151 | 152 |
152 - (NSArray<CSCollectionViewItem*>*)itemsForSectionInfo: | 153 - (NSArray<CSCollectionViewItem*>*)itemsForSectionInfo: |
153 (ContentSuggestionsSectionInformation*)sectionInfo { | 154 (ContentSuggestionsSectionInformation*)sectionInfo { |
154 NSMutableArray<CSCollectionViewItem*>* convertedSuggestions = | 155 NSMutableArray<CSCollectionViewItem*>* convertedSuggestions = |
155 [NSMutableArray array]; | 156 [NSMutableArray array]; |
156 | 157 |
157 if (sectionInfo == self.mostVisitedSectionInfo) { | 158 if (sectionInfo == self.mostVisitedSectionInfo) { |
158 for (const ntp_tiles::NTPTile& tile : self.mostVisitedData) { | 159 [convertedSuggestions addObjectsFromArray:self.mostVisitedItems]; |
159 [convertedSuggestions | |
160 addObject:ConvertNTPTile(tile, self.mostVisitedSectionInfo)]; | |
161 } | |
162 } else { | 160 } else { |
163 ntp_snippets::Category category = | 161 ntp_snippets::Category category = |
164 [[self categoryWrapperForSectionInfo:sectionInfo] category]; | 162 [[self categoryWrapperForSectionInfo:sectionInfo] category]; |
165 | 163 |
166 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = | 164 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = |
167 self.contentService->GetSuggestionsForCategory(category); | 165 self.contentService->GetSuggestionsForCategory(category); |
168 [self addSuggestions:suggestions | 166 [self addSuggestions:suggestions |
169 fromCategory:category | 167 fromCategory:category |
170 toItemArray:convertedSuggestions]; | 168 toItemArray:convertedSuggestions]; |
171 } | 169 } |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 } | 354 } |
357 | 355 |
358 callback([image.ToUIImage() copy]); | 356 callback([image.ToUIImage() copy]); |
359 })); | 357 })); |
360 } | 358 } |
361 | 359 |
362 #pragma mark - MostVisitedSitesObserving | 360 #pragma mark - MostVisitedSitesObserving |
363 | 361 |
364 - (void)onMostVisitedURLsAvailable: | 362 - (void)onMostVisitedURLsAvailable: |
365 (const ntp_tiles::NTPTilesVector&)mostVisited { | 363 (const ntp_tiles::NTPTilesVector&)mostVisited { |
366 self.mostVisitedData = mostVisited; | 364 self.mostVisitedItems = [NSMutableArray array]; |
| 365 for (const ntp_tiles::NTPTile& tile : mostVisited) { |
| 366 [self.mostVisitedItems |
| 367 addObject:ConvertNTPTile(tile, self.mostVisitedSectionInfo)]; |
| 368 } |
| 369 |
367 [self.dataSink reloadSection:self.mostVisitedSectionInfo]; | 370 [self.dataSink reloadSection:self.mostVisitedSectionInfo]; |
368 | 371 |
369 if (mostVisited.size() && !self.recordedPageImpression) { | 372 if (mostVisited.size() && !self.recordedPageImpression) { |
370 self.recordedPageImpression = YES; | 373 self.recordedPageImpression = YES; |
371 RecordPageImpression(mostVisited); | 374 RecordPageImpression(mostVisited); |
372 } | 375 } |
373 } | 376 } |
374 | 377 |
375 - (void)onIconMadeAvailable:(const GURL&)siteURL { | 378 - (void)onIconMadeAvailable:(const GURL&)siteURL { |
376 [self.dataSink faviconAvailableForURL:siteURL]; | 379 for (ContentSuggestionsMostVisitedItem* item in self.mostVisitedItems) { |
| 380 if (item.URL == siteURL) { |
| 381 [self.dataSink faviconAvailableForItem:item]; |
| 382 return; |
| 383 } |
| 384 } |
377 } | 385 } |
378 | 386 |
379 #pragma mark - Private | 387 #pragma mark - Private |
380 | 388 |
381 // Converts the |suggestions| from |category| to CSCollectionViewItem and adds | 389 // Converts the |suggestions| from |category| to CSCollectionViewItem and adds |
382 // them to the |contentArray| if the category is available. | 390 // them to the |contentArray| if the category is available. |
383 - (void)addSuggestions: | 391 - (void)addSuggestions: |
384 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions | 392 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions |
385 fromCategory:(ntp_snippets::Category&)category | 393 fromCategory:(ntp_snippets::Category&)category |
386 toItemArray:(NSMutableArray<CSCollectionViewItem*>*)itemArray { | 394 toItemArray:(NSMutableArray<CSCollectionViewItem*>*)itemArray { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 } | 450 } |
443 | 451 |
444 // Returns whether the |sectionInfo| is associated with a category from the | 452 // Returns whether the |sectionInfo| is associated with a category from the |
445 // content suggestions service. | 453 // content suggestions service. |
446 - (BOOL)isRelatedToContentSuggestionsService: | 454 - (BOOL)isRelatedToContentSuggestionsService: |
447 (ContentSuggestionsSectionInformation*)sectionInfo { | 455 (ContentSuggestionsSectionInformation*)sectionInfo { |
448 return sectionInfo != self.mostVisitedSectionInfo; | 456 return sectionInfo != self.mostVisitedSectionInfo; |
449 } | 457 } |
450 | 458 |
451 @end | 459 @end |
OLD | NEW |