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

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

Issue 2746373004: Add FetchMore to Content Suggestions Mediator (Closed)
Patch Set: Address comment 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
« no previous file with comments | « no previous file | ios/chrome/browser/ui/content_suggestions/content_suggestions_data_source.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/optional.h" 9 #include "base/optional.h"
10 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
11 #include "components/ntp_snippets/category.h" 11 #include "components/ntp_snippets/category.h"
12 #include "components/ntp_snippets/content_suggestion.h" 12 #include "components/ntp_snippets/content_suggestion.h"
13 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra pper.h" 13 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra pper.h"
14 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid ge_observer.h" 14 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid ge_observer.h"
15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h"
16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_identifier .h" 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_identifier .h"
17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h" 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h"
18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h" 18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h"
19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i nformation.h" 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i nformation.h"
20 #include "ui/gfx/image/image.h" 20 #include "ui/gfx/image/image.h"
21 21
22 #if !defined(__has_feature) || !__has_feature(objc_arc) 22 #if !defined(__has_feature) || !__has_feature(objc_arc)
23 #error "This file requires ARC support." 23 #error "This file requires ARC support."
24 #endif 24 #endif
25 25
26 namespace { 26 namespace {
27 27
28 // TODO(crbug.com/701275): Once base::BindBlock supports the move semantics,
29 // remove this wrapper.
30 // Wraps a callback taking a const ref to a callback taking an object.
31 void BindWrapper(
32 base::Callback<void(ntp_snippets::Status status_code,
33 const std::vector<ntp_snippets::ContentSuggestion>&
34 suggestions)> callback,
35 ntp_snippets::Status status_code,
36 std::vector<ntp_snippets::ContentSuggestion> suggestions) {
37 if (callback) {
38 callback.Run(status_code, suggestions);
39 }
40 }
41
28 // Returns the Type for this |category|. 42 // Returns the Type for this |category|.
29 ContentSuggestionType TypeForCategory(ntp_snippets::Category category) { 43 ContentSuggestionType TypeForCategory(ntp_snippets::Category category) {
30 // For now, only Article is a relevant type. 44 // For now, only Article is a relevant type.
31 return ContentSuggestionTypeArticle; 45 return ContentSuggestionTypeArticle;
32 } 46 }
33 47
34 // Returns the section ID for this |category|. 48 // Returns the section ID for this |category|.
35 ContentSuggestionsSectionID SectionIDForCategory( 49 ContentSuggestionsSectionID SectionIDForCategory(
36 ntp_snippets::Category category) { 50 ntp_snippets::Category category) {
37 if (category.IsKnownCategory(ntp_snippets::KnownCategories::BOOKMARKS)) 51 if (category.IsKnownCategory(ntp_snippets::KnownCategories::BOOKMARKS))
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; 119 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge;
106 } 120 }
107 121
108 @property(nonatomic, assign) 122 @property(nonatomic, assign)
109 ntp_snippets::ContentSuggestionsService* contentService; 123 ntp_snippets::ContentSuggestionsService* contentService;
110 @property(nonatomic, strong, nonnull) 124 @property(nonatomic, strong, nonnull)
111 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, 125 NSMutableDictionary<ContentSuggestionsCategoryWrapper*,
112 ContentSuggestionsSectionInformation*>* 126 ContentSuggestionsSectionInformation*>*
113 sectionInformationByCategory; 127 sectionInformationByCategory;
114 128
115 // Converts the data in |category| to ContentSuggestion and adds them to the 129 // Converts the |suggestions| from |category| to ContentSuggestion and adds them
116 // |contentArray| if the category is available. 130 // to the |contentArray| if the category is available.
117 - (void)addContentInCategory:(ntp_snippets::Category&)category 131 - (void)addSuggestions:
118 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; 132 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions
133 fromCategory:(ntp_snippets::Category&)category
134 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray;
119 135
120 // Adds the section information for |category| in 136 // Adds the section information for |category| in
121 // self.sectionInformationByCategory. 137 // self.sectionInformationByCategory.
122 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; 138 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category;
123 139
124 // Returns a CategoryWrapper acting as a key for this section info. 140 // Returns a CategoryWrapper acting as a key for this section info.
125 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: 141 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo:
126 (ContentSuggestionsSectionInformation*)sectionInfo; 142 (ContentSuggestionsSectionInformation*)sectionInfo;
127 143
128 @end 144 @end
(...skipping 28 matching lines...) Expand all
157 self.contentService->DismissSuggestion(suggestion_id); 173 self.contentService->DismissSuggestion(suggestion_id);
158 } 174 }
159 175
160 #pragma mark - ContentSuggestionsDataSource 176 #pragma mark - ContentSuggestionsDataSource
161 177
162 - (NSArray<ContentSuggestion*>*)allSuggestions { 178 - (NSArray<ContentSuggestion*>*)allSuggestions {
163 std::vector<ntp_snippets::Category> categories = 179 std::vector<ntp_snippets::Category> categories =
164 self.contentService->GetCategories(); 180 self.contentService->GetCategories();
165 NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array]; 181 NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array];
166 for (auto& category : categories) { 182 for (auto& category : categories) {
167 [self addContentInCategory:category toArray:dataHolders]; 183 const std::vector<ntp_snippets::ContentSuggestion>& suggestions =
184 self.contentService->GetSuggestionsForCategory(category);
185 [self addSuggestions:suggestions fromCategory:category toArray:dataHolders];
168 } 186 }
169 return dataHolders; 187 return dataHolders;
170 } 188 }
171 189
172 - (NSArray<ContentSuggestion*>*)suggestionsForSection: 190 - (NSArray<ContentSuggestion*>*)suggestionsForSection:
173 (ContentSuggestionsSectionInformation*)sectionInfo { 191 (ContentSuggestionsSectionInformation*)sectionInfo {
174 ntp_snippets::Category category = 192 ntp_snippets::Category category =
175 [[self categoryWrapperForSectionInfo:sectionInfo] category]; 193 [[self categoryWrapperForSectionInfo:sectionInfo] category];
176 194
177 NSMutableArray* suggestions = [NSMutableArray array]; 195 NSMutableArray* convertedSuggestions = [NSMutableArray array];
178 [self addContentInCategory:category toArray:suggestions]; 196 const std::vector<ntp_snippets::ContentSuggestion>& suggestions =
179 return suggestions; 197 self.contentService->GetSuggestionsForCategory(category);
198 [self addSuggestions:suggestions
199 fromCategory:category
200 toArray:convertedSuggestions];
201 return convertedSuggestions;
180 } 202 }
181 203
182 - (id<ContentSuggestionsImageFetcher>)imageFetcher { 204 - (id<ContentSuggestionsImageFetcher>)imageFetcher {
183 return self; 205 return self;
184 } 206 }
185 207
208 - (void)fetchMoreSuggestionsKnowing:
209 (NSArray<ContentSuggestionIdentifier*>*)knownSuggestions
210 fromSectionInfo:
211 (ContentSuggestionsSectionInformation*)sectionInfo
212 callback:(MoreSuggestionsFetched)callback {
213 std::set<std::string> known_suggestion_ids;
214 for (ContentSuggestionIdentifier* identifier in knownSuggestions) {
215 if (identifier.sectionInfo != sectionInfo)
216 continue;
217 known_suggestion_ids.insert(identifier.IDInSection);
218 }
219
220 ContentSuggestionsCategoryWrapper* wrapper =
221 [self categoryWrapperForSectionInfo:sectionInfo];
222
223 __weak ContentSuggestionsMediator* weakSelf = self;
224 ntp_snippets::FetchDoneCallback serviceCallback = base::Bind(
225 &BindWrapper,
226 base::BindBlockArc(^void(
227 ntp_snippets::Status status,
228 const std::vector<ntp_snippets::ContentSuggestion>& suggestions) {
229 [weakSelf didFetchMoreSuggestions:suggestions
230 withStatusCode:status
231 callback:callback];
232 }));
233
234 self.contentService->Fetch([wrapper category], known_suggestion_ids,
235 serviceCallback);
236 }
237
186 #pragma mark - ContentSuggestionsServiceObserver 238 #pragma mark - ContentSuggestionsServiceObserver
187 239
188 - (void)contentSuggestionsService: 240 - (void)contentSuggestionsService:
189 (ntp_snippets::ContentSuggestionsService*)suggestionsService 241 (ntp_snippets::ContentSuggestionsService*)suggestionsService
190 newSuggestionsInCategory:(ntp_snippets::Category)category { 242 newSuggestionsInCategory:(ntp_snippets::Category)category {
191 ContentSuggestionsCategoryWrapper* wrapper = 243 ContentSuggestionsCategoryWrapper* wrapper =
192 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; 244 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category];
193 if (!self.sectionInformationByCategory[wrapper]) { 245 if (!self.sectionInformationByCategory[wrapper]) {
194 [self addSectionInformationForCategory:category]; 246 [self addSectionInformationForCategory:category];
195 } 247 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 callback:(void (^)(const gfx::Image&))callback { 297 callback:(void (^)(const gfx::Image&))callback {
246 self.contentService->FetchSuggestionImage( 298 self.contentService->FetchSuggestionImage(
247 SuggestionIDForSectionID( 299 SuggestionIDForSectionID(
248 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo], 300 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo],
249 suggestionIdentifier.IDInSection), 301 suggestionIdentifier.IDInSection),
250 base::BindBlockArc(callback)); 302 base::BindBlockArc(callback));
251 } 303 }
252 304
253 #pragma mark - Private 305 #pragma mark - Private
254 306
255 - (void)addContentInCategory:(ntp_snippets::Category&)category 307 - (void)addSuggestions:
256 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { 308 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions
309 fromCategory:(ntp_snippets::Category&)category
310 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray {
257 if (self.contentService->GetCategoryStatus(category) != 311 if (self.contentService->GetCategoryStatus(category) !=
258 ntp_snippets::CategoryStatus::AVAILABLE) { 312 ntp_snippets::CategoryStatus::AVAILABLE) {
259 return; 313 return;
260 } 314 }
315
261 ContentSuggestionsCategoryWrapper* categoryWrapper = 316 ContentSuggestionsCategoryWrapper* categoryWrapper =
262 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; 317 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category];
263 if (!self.sectionInformationByCategory[categoryWrapper]) { 318 if (!self.sectionInformationByCategory[categoryWrapper]) {
264 [self addSectionInformationForCategory:category]; 319 [self addSectionInformationForCategory:category];
265 } 320 }
266 321
267 const std::vector<ntp_snippets::ContentSuggestion>& suggestions =
268 self.contentService->GetSuggestionsForCategory(category);
269
270 for (auto& contentSuggestion : suggestions) { 322 for (auto& contentSuggestion : suggestions) {
271 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); 323 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion);
272 324
273 suggestion.type = TypeForCategory(category); 325 suggestion.type = TypeForCategory(category);
326
274 suggestion.suggestionIdentifier.sectionInfo = 327 suggestion.suggestionIdentifier.sectionInfo =
275 self.sectionInformationByCategory[categoryWrapper]; 328 self.sectionInformationByCategory[categoryWrapper];
276 329
277 [contentArray addObject:suggestion]; 330 [contentArray addObject:suggestion];
278 } 331 }
279 } 332 }
280 333
281 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { 334 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category {
282 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = 335 base::Optional<ntp_snippets::CategoryInfo> categoryInfo =
283 self.contentService->GetCategoryInfo(category); 336 self.contentService->GetCategoryInfo(category);
284 337
285 ContentSuggestionsSectionInformation* sectionInfo = 338 ContentSuggestionsSectionInformation* sectionInfo =
286 SectionInformationFromCategoryInfo(categoryInfo, category); 339 SectionInformationFromCategoryInfo(categoryInfo, category);
287 340
288 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper 341 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper
289 wrapperWithCategory:category]] = sectionInfo; 342 wrapperWithCategory:category]] = sectionInfo;
290 } 343 }
291 344
292 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: 345 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo:
293 (ContentSuggestionsSectionInformation*)sectionInfo { 346 (ContentSuggestionsSectionInformation*)sectionInfo {
294 return [[self.sectionInformationByCategory allKeysForObject:sectionInfo] 347 return [[self.sectionInformationByCategory allKeysForObject:sectionInfo]
295 firstObject]; 348 firstObject];
296 } 349 }
297 350
351 // If the |statusCode| is a success and |suggestions| is not empty, runs the
352 // |callback| with the |suggestions| converted to Objective-C.
353 - (void)didFetchMoreSuggestions:
354 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions
355 withStatusCode:(ntp_snippets::Status)statusCode
356 callback:(MoreSuggestionsFetched)callback {
357 if (statusCode.IsSuccess() && !suggestions.empty() && callback) {
358 NSMutableArray<ContentSuggestion*>* contentSuggestions =
359 [NSMutableArray array];
360 ntp_snippets::Category category = suggestions[0].id().category();
361 [self addSuggestions:suggestions
362 fromCategory:category
363 toArray:contentSuggestions];
364 callback(contentSuggestions);
365 }
366 }
367
298 @end 368 @end
OLDNEW
« no previous file with comments | « no previous file | ios/chrome/browser/ui/content_suggestions/content_suggestions_data_source.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698