Chromium Code Reviews| Index: ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm |
| diff --git a/ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm b/ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm |
| index 1a5ea6668cf488d2f83ad741a0e3afc2ce4564d7..14b5abf35c84891799688eff275b9d5c637673f2 100644 |
| --- a/ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm |
| +++ b/ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm |
| @@ -25,6 +25,20 @@ |
| namespace { |
| +// TODO(crbug.com/701275):Once base::BindBlock supports the move semantics, |
|
lpromero
2017/03/14 16:33:08
Add a space after the colon.
gambard
2017/03/15 09:53:36
Done.
|
| +// remove this wrapper. |
| +// Wraps a callback taking a const ref to a callback taking an object. |
| +void BindWrapper( |
| + base::Callback<void(ntp_snippets::Status status_code, |
| + const std::vector<ntp_snippets::ContentSuggestion>& |
| + suggestions)> callback, |
| + ntp_snippets::Status status_code, |
| + std::vector<ntp_snippets::ContentSuggestion> suggestions) { |
| + if (callback) { |
| + callback.Run(status_code, suggestions); |
| + } |
| +} |
| + |
| // Returns the Type for this |category|. |
| ContentSuggestionType TypeForCategory(ntp_snippets::Category category) { |
| // For now, only Article is a relevant type. |
| @@ -112,10 +126,12 @@ ntp_snippets::ContentSuggestion::ID SuggestionIDForSectionID( |
| ContentSuggestionsSectionInformation*>* |
| sectionInformationByCategory; |
| -// Converts the data in |category| to ContentSuggestion and adds them to the |
| -// |contentArray|. |
| -- (void)addContentInCategory:(ntp_snippets::Category&)category |
| - toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| +// Converts the |suggestions| from |category| to ContentSuggestion and adds them |
| +// to the |contentArray|. |
| +- (void)addSuggestions: |
| + (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions |
| + fromCategory:(ntp_snippets::Category&)category |
| + toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| // Adds the section information for |category| in |
| // self.sectionInformationByCategory. |
| @@ -172,7 +188,9 @@ ntp_snippets::ContentSuggestion::ID SuggestionIDForSectionID( |
| [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]]) { |
| [self addSectionInformationForCategory:category]; |
| } |
| - [self addContentInCategory:category toArray:dataHolders]; |
| + const std::vector<ntp_snippets::ContentSuggestion>& suggestions = |
| + self.contentService->GetSuggestionsForCategory(category); |
| + [self addSuggestions:suggestions fromCategory:category toArray:dataHolders]; |
| } |
| return dataHolders; |
| } |
| @@ -181,6 +199,36 @@ ntp_snippets::ContentSuggestion::ID SuggestionIDForSectionID( |
| return self; |
| } |
| +- (void)fetchMoreSuggestionsKnowing: |
| + (NSArray<ContentSuggestionIdentifier*>*)knownSuggestions |
| + fromSectionInfo: |
| + (ContentSuggestionsSectionInformation*)sectionInfo |
| + callback:(MoreSuggestionsFetched)callback { |
| + std::set<std::string> known_suggestion_ids; |
| + for (ContentSuggestionIdentifier* identifier in knownSuggestions) { |
| + if (identifier.sectionInfo != sectionInfo) |
| + continue; |
| + known_suggestion_ids.insert(identifier.IDInSection); |
| + } |
| + |
| + ContentSuggestionsCategoryWrapper* wrapper = |
| + [self categoryWrapperForSectionInfo:sectionInfo]; |
| + |
| + __weak ContentSuggestionsMediator* weakSelf = self; |
| + ntp_snippets::FetchDoneCallback serviceCallback = base::Bind( |
| + &BindWrapper, |
| + base::BindBlockArc(^void( |
| + ntp_snippets::Status status, |
| + const std::vector<ntp_snippets::ContentSuggestion>& suggestions) { |
| + [weakSelf didFetchMoreSuggestions:suggestions |
| + withStatusCode:status |
| + callback:callback]; |
| + })); |
| + |
| + self.contentService->Fetch([wrapper category], known_suggestion_ids, |
| + serviceCallback); |
| +} |
| + |
| #pragma mark - ContentSuggestionsServiceObserver |
| - (void)contentSuggestionsService: |
| @@ -235,15 +283,16 @@ ntp_snippets::ContentSuggestion::ID SuggestionIDForSectionID( |
| #pragma mark - Private |
| -- (void)addContentInCategory:(ntp_snippets::Category&)category |
| - toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { |
| - const std::vector<ntp_snippets::ContentSuggestion>& suggestions = |
| - self.contentService->GetSuggestionsForCategory(category); |
| +- (void)addSuggestions: |
| + (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions |
| + fromCategory:(ntp_snippets::Category&)category |
| + toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { |
| ContentSuggestionsCategoryWrapper* categoryWrapper = |
| [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; |
| for (auto& contentSuggestion : suggestions) { |
| ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); |
| suggestion.type = TypeForCategory(category); |
| + |
| suggestion.suggestionIdentifier.sectionInfo = |
| self.sectionInformationByCategory[categoryWrapper]; |
| @@ -268,4 +317,21 @@ ntp_snippets::ContentSuggestion::ID SuggestionIDForSectionID( |
| firstObject]; |
| } |
| +// If the |statusCode| is a success and |suggestions| is not empty, runs the |
| +// |callback| with the |suggestions| converted to Objective-C. |
| +- (void)didFetchMoreSuggestions: |
| + (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions |
| + withStatusCode:(ntp_snippets::Status)statusCode |
| + callback:(MoreSuggestionsFetched)callback { |
| + if (statusCode.IsSuccess() && !suggestions.empty() && callback) { |
| + NSMutableArray<ContentSuggestion*>* contentSuggestions = |
| + [NSMutableArray array]; |
| + ntp_snippets::Category category = suggestions[0].id().category(); |
| + [self addSuggestions:suggestions |
| + fromCategory:category |
| + toArray:contentSuggestions]; |
| + callback(contentSuggestions); |
| + } |
| +} |
| + |
| @end |