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

Side by Side Diff: ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm

Issue 2877513003: ContentSuggestionsDataSource returns CollectionViewItem (Closed)
Patch Set: Reviewable Created 3 years, 7 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 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/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
9 #include "base/optional.h" 10 #include "base/optional.h"
10 #include "components/favicon/core/large_icon_service.h" 11 #include "components/favicon/core/large_icon_service.h"
11 #include "components/ntp_snippets/category.h" 12 #include "components/ntp_snippets/category.h"
12 #include "components/ntp_snippets/category_info.h" 13 #include "components/ntp_snippets/category_info.h"
13 #include "components/ntp_snippets/content_suggestion.h" 14 #include "components/ntp_snippets/content_suggestion.h"
14 #include "components/ntp_tiles/most_visited_sites.h" 15 #include "components/ntp_tiles/most_visited_sites.h"
15 #include "components/ntp_tiles/ntp_tile.h" 16 #include "components/ntp_tiles/ntp_tile.h"
16 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra pper.h" 17 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra pper.h"
17 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid ge_observer.h" 18 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid ge_observer.h"
18 #import "ios/chrome/browser/content_suggestions/mediator_util.h" 19 #import "ios/chrome/browser/content_suggestions/mediator_util.h"
19 #include "ios/chrome/browser/ntp_tiles/most_visited_sites_observer_bridge.h" 20 #include "ios/chrome/browser/ntp_tiles/most_visited_sites_observer_bridge.h"
20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" 21 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_ite m.h"
22 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_mos t_visited_item.h"
23 #import "ios/chrome/browser/ui/content_suggestions/cells/suggested_content.h"
21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_commands. h" 24 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_commands. h"
22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h" 25 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h"
23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h" 26 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h"
24 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion _identifier.h" 27 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion _identifier.h"
25 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion s_section_information.h" 28 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion s_section_information.h"
26 #import "ios/chrome/browser/ui/favicon/favicon_attributes_provider.h" 29 #import "ios/chrome/browser/ui/favicon/favicon_attributes_provider.h"
27 #import "ios/chrome/browser/ui/reading_list/reading_list_collection_view_item.h"
28 #import "ios/chrome/browser/ui/reading_list/reading_list_utils.h"
29 #include "ui/gfx/image/image.h" 30 #include "ui/gfx/image/image.h"
30 31
31 #if !defined(__has_feature) || !__has_feature(objc_arc) 32 #if !defined(__has_feature) || !__has_feature(objc_arc)
32 #error "This file requires ARC support." 33 #error "This file requires ARC support."
33 #endif 34 #endif
34 35
35 namespace { 36 namespace {
36 37
38 using CSCollectionViewItem = CollectionViewItem<SuggestedContent>;
39
37 // Size of the favicon returned by the provider. 40 // Size of the favicon returned by the provider.
38 const CGFloat kDefaultFaviconSize = 16; 41 const CGFloat kDefaultFaviconSize = 48;
39 // Maximum number of most visited tiles fetched. 42 // Maximum number of most visited tiles fetched.
40 const NSInteger kMaxNumMostVisitedTiles = 8; 43 const NSInteger kMaxNumMostVisitedTiles = 8;
41 44
42 } // namespace 45 } // namespace
43 46
44 @interface ContentSuggestionsMediator ()<ContentSuggestionsImageFetcher, 47 @interface ContentSuggestionsMediator ()<ContentSuggestionsImageFetcher,
45 ContentSuggestionsServiceObserver, 48 ContentSuggestionsServiceObserver,
46 MostVisitedSitesObserving> { 49 MostVisitedSitesObserving> {
47 // Bridge for this class to become an observer of a ContentSuggestionsService. 50 // Bridge for this class to become an observer of a ContentSuggestionsService.
48 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; 51 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge;
(...skipping 14 matching lines...) Expand all
63 ntp_snippets::ContentSuggestionsService* contentService; 66 ntp_snippets::ContentSuggestionsService* contentService;
64 // Map the section information created to the relevant category. 67 // Map the section information created to the relevant category.
65 @property(nonatomic, strong, nonnull) 68 @property(nonatomic, strong, nonnull)
66 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, 69 NSMutableDictionary<ContentSuggestionsCategoryWrapper*,
67 ContentSuggestionsSectionInformation*>* 70 ContentSuggestionsSectionInformation*>*
68 sectionInformationByCategory; 71 sectionInformationByCategory;
69 // FaviconAttributesProvider to fetch the favicon for the suggestions. 72 // FaviconAttributesProvider to fetch the favicon for the suggestions.
70 @property(nonatomic, nullable, strong) 73 @property(nonatomic, nullable, strong)
71 FaviconAttributesProvider* attributesProvider; 74 FaviconAttributesProvider* attributesProvider;
72 75
73 // Converts the |suggestions| from |category| to ContentSuggestion and adds them
74 // to the |contentArray| if the category is available.
75 - (void)addSuggestions:
76 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions
77 fromCategory:(ntp_snippets::Category&)category
78 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray;
79
80 // Adds the section information for |category| in
81 // self.sectionInformationByCategory.
82 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category;
83
84 // Returns a CategoryWrapper acting as a key for this section info.
85 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo:
86 (ContentSuggestionsSectionInformation*)sectionInfo;
87
88 @end 76 @end
89 77
90 @implementation ContentSuggestionsMediator 78 @implementation ContentSuggestionsMediator
91 79
92 @synthesize mostVisitedData = _mostVisitedData; 80 @synthesize mostVisitedData = _mostVisitedData;
93 @synthesize mostVisitedSectionInfo = _mostVisitedSectionInfo; 81 @synthesize mostVisitedSectionInfo = _mostVisitedSectionInfo;
94 @synthesize recordedPageImpression = _recordedPageImpression; 82 @synthesize recordedPageImpression = _recordedPageImpression;
95 @synthesize contentService = _contentService; 83 @synthesize contentService = _contentService;
96 @synthesize dataSink = _dataSink; 84 @synthesize dataSink = _dataSink;
97 @synthesize sectionInformationByCategory = _sectionInformationByCategory; 85 @synthesize sectionInformationByCategory = _sectionInformationByCategory;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo]; 119 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo];
132 ntp_snippets::ContentSuggestion::ID suggestion_id = 120 ntp_snippets::ContentSuggestion::ID suggestion_id =
133 ntp_snippets::ContentSuggestion::ID([categoryWrapper category], 121 ntp_snippets::ContentSuggestion::ID([categoryWrapper category],
134 suggestionIdentifier.IDInSection); 122 suggestionIdentifier.IDInSection);
135 123
136 self.contentService->DismissSuggestion(suggestion_id); 124 self.contentService->DismissSuggestion(suggestion_id);
137 } 125 }
138 126
139 #pragma mark - ContentSuggestionsDataSource 127 #pragma mark - ContentSuggestionsDataSource
140 128
141 - (NSArray<ContentSuggestion*>*)allSuggestions { 129 - (NSArray<ContentSuggestionsSectionInformation*>*)sectionsInfo {
142 NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array]; 130 NSMutableArray<ContentSuggestionsSectionInformation*>* sectionsInfo =
131 [NSMutableArray array];
143 132
144 [self addMostVisitedToArray:dataHolders]; 133 if (!self.mostVisitedData.empty()) {
134 [sectionsInfo addObject:self.mostVisitedSectionInfo];
135 }
145 136
146 [self addContentSuggestionsToArray:dataHolders]; 137 std::vector<ntp_snippets::Category> categories =
138 self.contentService->GetCategories();
147 139
148 return dataHolders; 140 for (auto& category : categories) {
141 ContentSuggestionsCategoryWrapper* categoryWrapper =
142 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category];
143 if (!self.sectionInformationByCategory[categoryWrapper]) {
144 [self addSectionInformationForCategory:category];
145 }
146 [sectionsInfo addObject:self.sectionInformationByCategory[categoryWrapper]];
147 }
148
149 return sectionsInfo;
149 } 150 }
150 151
151 - (NSArray<ContentSuggestion*>*)suggestionsForSection: 152 - (NSArray<CSCollectionViewItem*>*)itemsForSectionInfo:
152 (ContentSuggestionsSectionInformation*)sectionInfo { 153 (ContentSuggestionsSectionInformation*)sectionInfo {
153 NSMutableArray* convertedSuggestions = [NSMutableArray array]; 154 NSMutableArray<CSCollectionViewItem*>* convertedSuggestions =
155 [NSMutableArray array];
154 156
155 if (sectionInfo == self.mostVisitedSectionInfo) { 157 if (sectionInfo == self.mostVisitedSectionInfo) {
156 [self addMostVisitedToArray:convertedSuggestions]; 158 for (const ntp_tiles::NTPTile& tile : self.mostVisitedData) {
159 [convertedSuggestions
160 addObject:ConvertNTPTile(tile, self.mostVisitedSectionInfo)];
161 }
157 } else { 162 } else {
158 ntp_snippets::Category category = 163 ntp_snippets::Category category =
159 [[self categoryWrapperForSectionInfo:sectionInfo] category]; 164 [[self categoryWrapperForSectionInfo:sectionInfo] category];
160 165
161 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = 166 const std::vector<ntp_snippets::ContentSuggestion>& suggestions =
162 self.contentService->GetSuggestionsForCategory(category); 167 self.contentService->GetSuggestionsForCategory(category);
163 [self addSuggestions:suggestions 168 [self addSuggestions:suggestions
164 fromCategory:category 169 fromCategory:category
165 toArray:convertedSuggestions]; 170 toItemArray:convertedSuggestions];
166 } 171 }
167 172
168 return convertedSuggestions; 173 return convertedSuggestions;
169 } 174 }
170 175
171 - (id<ContentSuggestionsImageFetcher>)imageFetcher { 176 - (id<ContentSuggestionsImageFetcher>)imageFetcher {
172 return self; 177 return self;
173 } 178 }
174 179
175 - (void)fetchMoreSuggestionsKnowing: 180 - (void)fetchMoreSuggestionsKnowing:
176 (NSArray<ContentSuggestionIdentifier*>*)knownSuggestions 181 (NSArray<ContentSuggestionIdentifier*>*)knownSuggestions
177 fromSectionInfo: 182 fromSectionInfo:
178 (ContentSuggestionsSectionInformation*)sectionInfo 183 (ContentSuggestionsSectionInformation*)sectionInfo
179 callback:(MoreSuggestionsFetched)callback { 184 callback:(MoreSuggestionsFetched)callback {
180 if (![self isRelatedToContentSuggestionsService:sectionInfo]) 185 if (![self isRelatedToContentSuggestionsService:sectionInfo]) {
186 callback(nil);
181 return; 187 return;
182
183 std::set<std::string> known_suggestion_ids;
184 for (ContentSuggestionIdentifier* identifier in knownSuggestions) {
185 if (identifier.sectionInfo != sectionInfo)
186 continue;
187 known_suggestion_ids.insert(identifier.IDInSection);
188 } 188 }
189 189
190 ContentSuggestionsCategoryWrapper* wrapper = 190 ContentSuggestionsCategoryWrapper* wrapper =
191 [self categoryWrapperForSectionInfo:sectionInfo]; 191 [self categoryWrapperForSectionInfo:sectionInfo];
192 192
193 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = 193 base::Optional<ntp_snippets::CategoryInfo> categoryInfo =
194 self.contentService->GetCategoryInfo([wrapper category]); 194 self.contentService->GetCategoryInfo([wrapper category]);
195 195
196 if (!categoryInfo) { 196 if (!categoryInfo) {
197 callback(nil);
197 return; 198 return;
198 } 199 }
200
199 switch (categoryInfo->additional_action()) { 201 switch (categoryInfo->additional_action()) {
200 case ntp_snippets::ContentSuggestionsAdditionalAction::NONE: 202 case ntp_snippets::ContentSuggestionsAdditionalAction::NONE:
203 callback(nil);
201 return; 204 return;
202 205
203 case ntp_snippets::ContentSuggestionsAdditionalAction::VIEW_ALL: 206 case ntp_snippets::ContentSuggestionsAdditionalAction::VIEW_ALL:
207 callback(nil);
204 if ([wrapper category].IsKnownCategory( 208 if ([wrapper category].IsKnownCategory(
205 ntp_snippets::KnownCategories::READING_LIST)) { 209 ntp_snippets::KnownCategories::READING_LIST)) {
206 [self.commandHandler openReadingList]; 210 [self.commandHandler openReadingList];
207 } 211 }
208 break; 212 break;
209 213
210 case ntp_snippets::ContentSuggestionsAdditionalAction::FETCH: { 214 case ntp_snippets::ContentSuggestionsAdditionalAction::FETCH: {
215 std::set<std::string> known_suggestion_ids;
216 for (ContentSuggestionIdentifier* identifier in knownSuggestions) {
217 if (identifier.sectionInfo != sectionInfo)
218 continue;
219 known_suggestion_ids.insert(identifier.IDInSection);
220 }
221
211 __weak ContentSuggestionsMediator* weakSelf = self; 222 __weak ContentSuggestionsMediator* weakSelf = self;
212 ntp_snippets::FetchDoneCallback serviceCallback = base::Bind( 223 ntp_snippets::FetchDoneCallback serviceCallback = base::Bind(
213 &BindWrapper, 224 &BindWrapper,
214 base::BindBlockArc(^void( 225 base::BindBlockArc(^void(
215 ntp_snippets::Status status, 226 ntp_snippets::Status status,
216 const std::vector<ntp_snippets::ContentSuggestion>& suggestions) { 227 const std::vector<ntp_snippets::ContentSuggestion>& suggestions) {
217 [weakSelf didFetchMoreSuggestions:suggestions 228 [weakSelf didFetchMoreSuggestions:suggestions
218 withStatusCode:status 229 withStatusCode:status
219 callback:callback]; 230 callback:callback];
220 })); 231 }));
221 232
222 self.contentService->Fetch([wrapper category], known_suggestion_ids, 233 self.contentService->Fetch([wrapper category], known_suggestion_ids,
223 serviceCallback); 234 serviceCallback);
224 235
225 break; 236 break;
226 } 237 }
227 } 238 }
228 } 239 }
229 240
230 - (void)fetchFaviconAttributesForURL:(const GURL&)URL 241 - (void)fetchFaviconAttributesForItem:(CSCollectionViewItem*)item
231 completion:(void (^)(FaviconAttributes*))completion { 242 completion:(void (^)(FaviconAttributes*))completion {
232 [self.attributesProvider fetchFaviconAttributesForURL:URL 243 ContentSuggestionsSectionInformation* sectionInfo =
244 item.suggestionIdentifier.sectionInfo;
245 GURL url;
246 if (![self isRelatedToContentSuggestionsService:sectionInfo]) {
247 ContentSuggestionsMostVisitedItem* mostVisited =
248 base::mac::ObjCCast<ContentSuggestionsMostVisitedItem>(item);
249 url = mostVisited.URL;
250 } else {
251 ContentSuggestionsItem* suggestionItem =
252 base::mac::ObjCCast<ContentSuggestionsItem>(item);
253 url = suggestionItem.URL;
254 }
255 [self.attributesProvider fetchFaviconAttributesForURL:url
233 completion:completion]; 256 completion:completion];
234 } 257 }
235 258
236 - (void)fetchFaviconImageForSuggestion:(ContentSuggestionIdentifier*)suggestion 259 - (void)fetchFaviconImageForItem:(CSCollectionViewItem*)item
237 completion:(void (^)(UIImage*))completion { 260 completion:(void (^)(UIImage*))completion {
238 if (!completion) 261 ContentSuggestionsSectionInformation* sectionInfo =
262 item.suggestionIdentifier.sectionInfo;
263 if (![self isRelatedToContentSuggestionsService:sectionInfo]) {
239 return; 264 return;
240 265 }
266 ContentSuggestionsItem* suggestionItem =
267 base::mac::ObjCCast<ContentSuggestionsItem>(item);
268 ntp_snippets::Category category =
269 [[self categoryWrapperForSectionInfo:sectionInfo] category];
270 if (!category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) {
271 // TODO(crbug.com/721266): remove this guard once the choice to download the
272 // favicon from the google server is done in the provider.
273 return;
274 }
241 void (^imageCallback)(const gfx::Image&) = ^(const gfx::Image& image) { 275 void (^imageCallback)(const gfx::Image&) = ^(const gfx::Image& image) {
242 if (!image.IsEmpty()) { 276 if (!image.IsEmpty()) {
243 completion([image.ToUIImage() copy]); 277 completion([image.ToUIImage() copy]);
244 } 278 }
245 }; 279 };
246 280
247 ntp_snippets::ContentSuggestion::ID identifier = 281 ntp_snippets::ContentSuggestion::ID identifier =
248 ntp_snippets::ContentSuggestion::ID( 282 ntp_snippets::ContentSuggestion::ID(
249 [[self categoryWrapperForSectionInfo:suggestion.sectionInfo] 283 category, suggestionItem.suggestionIdentifier.IDInSection);
250 category],
251 suggestion.IDInSection);
252 self.contentService->FetchSuggestionFavicon( 284 self.contentService->FetchSuggestionFavicon(
253 identifier, /* minimum_size_in_pixel = */ 1, kDefaultFaviconSize, 285 identifier, /* minimum_size_in_pixel = */ 1, kDefaultFaviconSize,
254 base::BindBlockArc(imageCallback)); 286 base::BindBlockArc(imageCallback));
255 } 287 }
256 288
257 #pragma mark - ContentSuggestionsServiceObserver 289 #pragma mark - ContentSuggestionsServiceObserver
258 290
259 - (void)contentSuggestionsService: 291 - (void)contentSuggestionsService:
260 (ntp_snippets::ContentSuggestionsService*)suggestionsService 292 (ntp_snippets::ContentSuggestionsService*)suggestionsService
261 newSuggestionsInCategory:(ntp_snippets::Category)category { 293 newSuggestionsInCategory:(ntp_snippets::Category)category {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 RecordPageImpression(mostVisited); 371 RecordPageImpression(mostVisited);
340 } 372 }
341 } 373 }
342 374
343 - (void)onIconMadeAvailable:(const GURL&)siteURL { 375 - (void)onIconMadeAvailable:(const GURL&)siteURL {
344 [self.dataSink faviconAvailableForURL:siteURL]; 376 [self.dataSink faviconAvailableForURL:siteURL];
345 } 377 }
346 378
347 #pragma mark - Private 379 #pragma mark - Private
348 380
381 // Converts the |suggestions| from |category| to CSCollectionViewItem and adds
382 // them to the |contentArray| if the category is available.
jif 2017/05/15 11:14:47 remove extra space before "if"
gambard 2017/05/15 12:50:03 Done.
349 - (void)addSuggestions: 383 - (void)addSuggestions:
350 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions 384 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions
351 fromCategory:(ntp_snippets::Category&)category 385 fromCategory:(ntp_snippets::Category&)category
352 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { 386 toItemArray:(NSMutableArray<CSCollectionViewItem*>*)itemArray {
353 if (!ntp_snippets::IsCategoryStatusAvailable( 387 if (!ntp_snippets::IsCategoryStatusAvailable(
354 self.contentService->GetCategoryStatus(category))) { 388 self.contentService->GetCategoryStatus(category))) {
355 return; 389 return;
356 } 390 }
357 391
358 ContentSuggestionsCategoryWrapper* categoryWrapper = 392 ContentSuggestionsCategoryWrapper* categoryWrapper =
359 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; 393 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category];
360 if (!self.sectionInformationByCategory[categoryWrapper]) { 394 if (!self.sectionInformationByCategory[categoryWrapper]) {
361 [self addSectionInformationForCategory:category]; 395 [self addSectionInformationForCategory:category];
362 } 396 }
397 ContentSuggestionsSectionInformation* sectionInfo =
398 self.sectionInformationByCategory[categoryWrapper];
399 for (auto& contentSuggestion : suggestions) {
400 CSCollectionViewItem* suggestion =
401 ConvertSuggestion(contentSuggestion, sectionInfo, category);
363 402
364 for (auto& contentSuggestion : suggestions) { 403 [itemArray addObject:suggestion];
365 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion);
366
367 suggestion.type = TypeForCategory(category);
368
369 suggestion.suggestionIdentifier.sectionInfo =
370 self.sectionInformationByCategory[categoryWrapper];
371
372 if (category.IsKnownCategory(ntp_snippets::KnownCategories::READING_LIST)) {
373 suggestion.availableOffline =
374 contentSuggestion.reading_list_suggestion_extra()->distilled;
375 }
376
377 [contentArray addObject:suggestion];
378 }
379
380 if (suggestions.size() == 0) {
381 ContentSuggestion* suggestion = EmptySuggestion();
382 suggestion.suggestionIdentifier.sectionInfo =
383 self.sectionInformationByCategory[categoryWrapper];
384
385 [contentArray addObject:suggestion];
386 } 404 }
387 } 405 }
388 406
407 // Adds the section information for |category| in
408 // self.sectionInformationByCategory.
389 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { 409 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category {
390 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = 410 base::Optional<ntp_snippets::CategoryInfo> categoryInfo =
391 self.contentService->GetCategoryInfo(category); 411 self.contentService->GetCategoryInfo(category);
392 412
393 ContentSuggestionsSectionInformation* sectionInfo = 413 ContentSuggestionsSectionInformation* sectionInfo =
394 SectionInformationFromCategoryInfo(categoryInfo, category); 414 SectionInformationFromCategoryInfo(categoryInfo, category);
395 415
396 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper 416 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper
397 wrapperWithCategory:category]] = sectionInfo; 417 wrapperWithCategory:category]] = sectionInfo;
398 } 418 }
399 419
420 // Returns a CategoryWrapper acting as a key for this section info.
400 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: 421 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo:
401 (ContentSuggestionsSectionInformation*)sectionInfo { 422 (ContentSuggestionsSectionInformation*)sectionInfo {
402 return [[self.sectionInformationByCategory allKeysForObject:sectionInfo] 423 return [[self.sectionInformationByCategory allKeysForObject:sectionInfo]
403 firstObject]; 424 firstObject];
404 } 425 }
405 426
406 // If the |statusCode| is a success and |suggestions| is not empty, runs the 427 // If the |statusCode| is a success and |suggestions| is not empty, runs the
407 // |callback| with the |suggestions| converted to Objective-C. 428 // |callback| with the |suggestions| converted to Objective-C.
408 - (void)didFetchMoreSuggestions: 429 - (void)didFetchMoreSuggestions:
409 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions 430 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions
410 withStatusCode:(ntp_snippets::Status)statusCode 431 withStatusCode:(ntp_snippets::Status)statusCode
411 callback:(MoreSuggestionsFetched)callback { 432 callback:(MoreSuggestionsFetched)callback {
433 NSMutableArray<CSCollectionViewItem*>* contentSuggestions = nil;
412 if (statusCode.IsSuccess() && !suggestions.empty() && callback) { 434 if (statusCode.IsSuccess() && !suggestions.empty() && callback) {
413 NSMutableArray<ContentSuggestion*>* contentSuggestions = 435 contentSuggestions = [NSMutableArray array];
414 [NSMutableArray array];
415 ntp_snippets::Category category = suggestions[0].id().category(); 436 ntp_snippets::Category category = suggestions[0].id().category();
416 [self addSuggestions:suggestions 437 [self addSuggestions:suggestions
417 fromCategory:category 438 fromCategory:category
418 toArray:contentSuggestions]; 439 toItemArray:contentSuggestions];
419 callback(contentSuggestions);
420 } 440 }
421 } 441 callback(contentSuggestions);
422
423 // Adds all the suggestions from the |contentService| to |suggestions|.
424 - (void)addContentSuggestionsToArray:
425 (NSMutableArray<ContentSuggestion*>*)arrayToFill {
426 std::vector<ntp_snippets::Category> categories =
427 self.contentService->GetCategories();
428
429 for (auto& category : categories) {
430 const std::vector<ntp_snippets::ContentSuggestion>& suggestions =
431 self.contentService->GetSuggestionsForCategory(category);
432 [self addSuggestions:suggestions fromCategory:category toArray:arrayToFill];
433 }
434 }
435
436 // Adds all the suggestions for the |mostVisitedData| to |suggestions|.
437 - (void)addMostVisitedToArray:(NSMutableArray<ContentSuggestion*>*)arrayToFill {
438 if (self.mostVisitedData.empty()) {
439 ContentSuggestion* suggestion = EmptySuggestion();
440 suggestion.suggestionIdentifier.sectionInfo = self.mostVisitedSectionInfo;
441 [arrayToFill addObject:suggestion];
442
443 return;
444 }
445
446 for (const ntp_tiles::NTPTile& tile : self.mostVisitedData) {
447 ContentSuggestion* suggestion = ConvertNTPTile(tile);
448 suggestion.suggestionIdentifier.sectionInfo = self.mostVisitedSectionInfo;
449 [arrayToFill addObject:suggestion];
450 }
451 } 442 }
452 443
453 // Returns whether the |sectionInfo| is associated with a category from the 444 // Returns whether the |sectionInfo| is associated with a category from the
454 // content suggestions service. 445 // content suggestions service.
455 - (BOOL)isRelatedToContentSuggestionsService: 446 - (BOOL)isRelatedToContentSuggestionsService:
456 (ContentSuggestionsSectionInformation*)sectionInfo { 447 (ContentSuggestionsSectionInformation*)sectionInfo {
457 return sectionInfo != self.mostVisitedSectionInfo; 448 return sectionInfo != self.mostVisitedSectionInfo;
458 } 449 }
459 450
460 @end 451 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698