Chromium Code Reviews| 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/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_id.h" | 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_id.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_item.h" | 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_item.h" |
| 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i nformation.h" | 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i nformation.h" |
| 20 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
| 21 | 22 |
| 22 #if !defined(__has_feature) || !__has_feature(objc_arc) | 23 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 23 #error "This file requires ARC support." | 24 #error "This file requires ARC support." |
| 24 #endif | 25 #endif |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); | 77 sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); |
| 77 if (categoryInfo->show_if_empty()) { | 78 if (categoryInfo->show_if_empty()) { |
| 78 // TODO(crbug.com/686728): Creates an item to display information when the | 79 // TODO(crbug.com/686728): Creates an item to display information when the |
| 79 // section is empty. | 80 // section is empty. |
| 80 } | 81 } |
| 81 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); | 82 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); |
| 82 } | 83 } |
| 83 return sectionInfo; | 84 return sectionInfo; |
| 84 } | 85 } |
| 85 | 86 |
| 87 // Returns a ntp_snippets::ID based on a Objective-C Category and the ID in the | |
| 88 // category. | |
| 89 ntp_snippets::ContentSuggestion::ID SuggestionIDForSectionID( | |
|
marq (ping after 24h)
2017/02/21 11:27:08
Should this be a method on ContentSuggestionsCateg
gambard
2017/02/21 15:07:53
Maybe. As they are only used in this file, I am no
| |
| 90 ContentSuggestionsCategoryWrapper* category, | |
| 91 const std::string& id_in_category) { | |
| 92 return ntp_snippets::ContentSuggestion::ID(category.category, id_in_category); | |
| 93 } | |
| 94 | |
| 86 } // namespace | 95 } // namespace |
| 87 | 96 |
| 88 @interface ContentSuggestionsMediator ()<ContentSuggestionsServiceObserver> { | 97 @interface ContentSuggestionsMediator ()<ContentSuggestionsImageFetcher, |
| 98 ContentSuggestionsServiceObserver> { | |
| 89 // Bridge for this class to become an observer of a ContentSuggestionsService. | 99 // Bridge for this class to become an observer of a ContentSuggestionsService. |
| 90 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; | 100 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; |
| 91 } | 101 } |
| 92 | 102 |
| 93 @property(nonatomic, assign) | 103 @property(nonatomic, assign) |
| 94 ntp_snippets::ContentSuggestionsService* contentService; | 104 ntp_snippets::ContentSuggestionsService* contentService; |
| 95 @property(nonatomic, strong, nonnull) | 105 @property(nonatomic, strong, nonnull) |
| 96 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, | 106 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, |
| 97 ContentSuggestionsSectionInformation*>* | 107 ContentSuggestionsSectionInformation*>* |
| 98 sectionInformationByCategory; | 108 sectionInformationByCategory; |
| 99 | 109 |
| 100 // Converts the data in |category| to ContentSuggestion and adds them to the | 110 // Converts the data in |category| to ContentSuggestion and adds them to the |
| 101 // |contentArray|. | 111 // |contentArray|. |
| 102 - (void)addContentInCategory:(ntp_snippets::Category&)category | 112 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 103 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; | 113 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| 104 | 114 |
| 105 // Adds the section information for |category| in | 115 // Adds the section information for |category| in |
| 106 // self.sectionInformationByCategory. | 116 // self.sectionInformationByCategory. |
| 107 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; | 117 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; |
| 108 | 118 |
| 119 // Returns a CategoryWrapper acting as a key for this section info. | |
| 120 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: | |
| 121 (ContentSuggestionsSectionInformation*)sectionInfo; | |
| 122 | |
| 109 @end | 123 @end |
| 110 | 124 |
| 111 @implementation ContentSuggestionsMediator | 125 @implementation ContentSuggestionsMediator |
| 112 | 126 |
| 113 @synthesize contentService = _contentService; | 127 @synthesize contentService = _contentService; |
| 114 @synthesize dataSink = _dataSink; | 128 @synthesize dataSink = _dataSink; |
| 115 @synthesize sectionInformationByCategory = _sectionInformationByCategory; | 129 @synthesize sectionInformationByCategory = _sectionInformationByCategory; |
| 116 | 130 |
| 117 - (instancetype)initWithContentService: | 131 - (instancetype)initWithContentService: |
| 118 (ntp_snippets::ContentSuggestionsService*)contentService { | 132 (ntp_snippets::ContentSuggestionsService*)contentService { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 139 } | 153 } |
| 140 if (!self.sectionInformationByCategory[ | 154 if (!self.sectionInformationByCategory[ |
| 141 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]]) { | 155 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]]) { |
| 142 [self addSectionInformationForCategory:category]; | 156 [self addSectionInformationForCategory:category]; |
| 143 } | 157 } |
| 144 [self addContentInCategory:category toArray:dataHolders]; | 158 [self addContentInCategory:category toArray:dataHolders]; |
| 145 } | 159 } |
| 146 return dataHolders; | 160 return dataHolders; |
| 147 } | 161 } |
| 148 | 162 |
| 163 - (id<ContentSuggestionsImageFetcher>)imageFetcher { | |
| 164 return self; | |
| 165 } | |
| 166 | |
| 149 #pragma mark - ContentSuggestionsServiceObserver | 167 #pragma mark - ContentSuggestionsServiceObserver |
| 150 | 168 |
| 151 - (void)contentSuggestionsService: | 169 - (void)contentSuggestionsService: |
| 152 (ntp_snippets::ContentSuggestionsService*)suggestionsService | 170 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 153 newSuggestionsInCategory:(ntp_snippets::Category)category { | 171 newSuggestionsInCategory:(ntp_snippets::Category)category { |
| 154 [self.dataSink dataAvailable]; | 172 [self.dataSink dataAvailable]; |
| 155 } | 173 } |
| 156 | 174 |
| 157 - (void)contentSuggestionsService: | 175 - (void)contentSuggestionsService: |
| 158 (ntp_snippets::ContentSuggestionsService*)suggestionsService | 176 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 171 - (void)contentSuggestionsServiceFullRefreshRequired: | 189 - (void)contentSuggestionsServiceFullRefreshRequired: |
| 172 (ntp_snippets::ContentSuggestionsService*)suggestionsService { | 190 (ntp_snippets::ContentSuggestionsService*)suggestionsService { |
| 173 // Update dataSink. | 191 // Update dataSink. |
| 174 } | 192 } |
| 175 | 193 |
| 176 - (void)contentSuggestionsServiceShutdown: | 194 - (void)contentSuggestionsServiceShutdown: |
| 177 (ntp_snippets::ContentSuggestionsService*)suggestionsService { | 195 (ntp_snippets::ContentSuggestionsService*)suggestionsService { |
| 178 // Update dataSink. | 196 // Update dataSink. |
| 179 } | 197 } |
| 180 | 198 |
| 199 #pragma mark - ContentSuggestionsImageFetcher | |
| 200 | |
| 201 - (void)fetchImageForSuggestion:(ContentSuggestionID*)suggestionID | |
| 202 callback:(void (^)(const gfx::Image&))callback { | |
| 203 self.contentService->FetchSuggestionImage( | |
| 204 SuggestionIDForSectionID( | |
| 205 [self categoryWrapperForSectionInfo:suggestionID.sectionInfo], | |
| 206 suggestionID.IDInSection), | |
| 207 base::BindBlockArc(callback)); | |
| 208 } | |
| 209 | |
| 181 #pragma mark - Private | 210 #pragma mark - Private |
| 182 | 211 |
| 183 - (void)addContentInCategory:(ntp_snippets::Category&)category | 212 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 184 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { | 213 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { |
| 185 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = | 214 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = |
| 186 self.contentService->GetSuggestionsForCategory(category); | 215 self.contentService->GetSuggestionsForCategory(category); |
| 187 ContentSuggestionsCategoryWrapper* categoryWrapper = | 216 ContentSuggestionsCategoryWrapper* categoryWrapper = |
| 188 [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; | 217 [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; |
| 189 for (auto& contentSuggestion : suggestions) { | 218 for (auto& contentSuggestion : suggestions) { |
| 190 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); | 219 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); |
| 191 suggestion.type = TypeForCategory(category); | 220 suggestion.type = TypeForCategory(category); |
| 192 suggestion.suggestionID.sectionInfo = | 221 suggestion.suggestionID.sectionInfo = |
| 193 self.sectionInformationByCategory[categoryWrapper]; | 222 self.sectionInformationByCategory[categoryWrapper]; |
| 194 | 223 |
| 195 // TODO(crbug.com/686728): fetch the image. | |
| 196 | |
| 197 [contentArray addObject:suggestion]; | 224 [contentArray addObject:suggestion]; |
| 198 } | 225 } |
| 199 } | 226 } |
| 200 | 227 |
| 201 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { | 228 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { |
| 202 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = | 229 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = |
| 203 self.contentService->GetCategoryInfo(category); | 230 self.contentService->GetCategoryInfo(category); |
| 204 | 231 |
| 205 ContentSuggestionsSectionInformation* sectionInfo = | 232 ContentSuggestionsSectionInformation* sectionInfo = |
| 206 SectionInformationFromCategoryInfo(categoryInfo, category); | 233 SectionInformationFromCategoryInfo(categoryInfo, category); |
| 207 | 234 |
| 208 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper | 235 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper |
| 209 wrapperWithCategory:category]] = sectionInfo; | 236 wrapperWithCategory:category]] = sectionInfo; |
| 210 } | 237 } |
| 211 | 238 |
| 239 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: | |
| 240 (ContentSuggestionsSectionInformation*)sectionInfo { | |
| 241 return [[self.sectionInformationByCategory allKeysForObject:sectionInfo] | |
| 242 firstObject]; | |
| 243 } | |
| 244 | |
| 212 @end | 245 @end |
| OLD | NEW |