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/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 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra pper.h" | 15 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra pper.h" |
| 15 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid ge_observer.h" | 16 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid ge_observer.h" |
| 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" | 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
| 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h" | 18 #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" | 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h" |
| 19 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion _identifier.h" | 20 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion _identifier.h" |
| 20 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion s_section_information.h" | 21 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion s_section_information.h" |
| 22 #import "ios/chrome/browser/ui/favicon/favicon_attributes_provider.h" | |
| 21 #include "ios/chrome/grit/ios_strings.h" | 23 #include "ios/chrome/grit/ios_strings.h" |
| 22 #include "ui/base/l10n/l10n_util_mac.h" | 24 #include "ui/base/l10n/l10n_util_mac.h" |
| 23 #include "ui/gfx/image/image.h" | 25 #include "ui/gfx/image/image.h" |
| 24 | 26 |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) | 27 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 26 #error "This file requires ARC support." | 28 #error "This file requires ARC support." |
| 27 #endif | 29 #endif |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 33 // Size of the favicon returned by the provider. | |
| 34 const CGFloat kDefaultFaviconSize = 16; | |
| 35 | |
| 31 // TODO(crbug.com/701275): Once base::BindBlock supports the move semantics, | 36 // TODO(crbug.com/701275): Once base::BindBlock supports the move semantics, |
| 32 // remove this wrapper. | 37 // remove this wrapper. |
| 33 // Wraps a callback taking a const ref to a callback taking an object. | 38 // Wraps a callback taking a const ref to a callback taking an object. |
| 34 void BindWrapper( | 39 void BindWrapper( |
| 35 base::Callback<void(ntp_snippets::Status status_code, | 40 base::Callback<void(ntp_snippets::Status status_code, |
| 36 const std::vector<ntp_snippets::ContentSuggestion>& | 41 const std::vector<ntp_snippets::ContentSuggestion>& |
| 37 suggestions)> callback, | 42 suggestions)> callback, |
| 38 ntp_snippets::Status status_code, | 43 ntp_snippets::Status status_code, |
| 39 std::vector<ntp_snippets::ContentSuggestion> suggestions) { | 44 std::vector<ntp_snippets::ContentSuggestion> suggestions) { |
| 40 if (callback) { | 45 if (callback) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 // Bridge for this class to become an observer of a ContentSuggestionsService. | 130 // Bridge for this class to become an observer of a ContentSuggestionsService. |
| 126 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; | 131 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; |
| 127 } | 132 } |
| 128 | 133 |
| 129 @property(nonatomic, assign) | 134 @property(nonatomic, assign) |
| 130 ntp_snippets::ContentSuggestionsService* contentService; | 135 ntp_snippets::ContentSuggestionsService* contentService; |
| 131 @property(nonatomic, strong, nonnull) | 136 @property(nonatomic, strong, nonnull) |
| 132 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, | 137 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, |
| 133 ContentSuggestionsSectionInformation*>* | 138 ContentSuggestionsSectionInformation*>* |
| 134 sectionInformationByCategory; | 139 sectionInformationByCategory; |
| 140 // FaviconAttributesProvider to fetch the favicon for the suggestions. | |
| 141 @property(nonatomic, nullable, strong) | |
|
lpromero
2017/03/31 08:30:26
Optional nit: Could be nonnull.
gambard
2017/04/03 12:34:34
Yes but I think it is ok as is.
| |
| 142 FaviconAttributesProvider* attributesProvider; | |
| 135 | 143 |
| 136 // Converts the |suggestions| from |category| to ContentSuggestion and adds them | 144 // Converts the |suggestions| from |category| to ContentSuggestion and adds them |
| 137 // to the |contentArray| if the category is available. | 145 // to the |contentArray| if the category is available. |
| 138 - (void)addSuggestions: | 146 - (void)addSuggestions: |
| 139 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions | 147 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions |
| 140 fromCategory:(ntp_snippets::Category&)category | 148 fromCategory:(ntp_snippets::Category&)category |
| 141 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; | 149 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| 142 | 150 |
| 143 // Adds the section information for |category| in | 151 // Adds the section information for |category| in |
| 144 // self.sectionInformationByCategory. | 152 // self.sectionInformationByCategory. |
| 145 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; | 153 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; |
| 146 | 154 |
| 147 // Returns a CategoryWrapper acting as a key for this section info. | 155 // Returns a CategoryWrapper acting as a key for this section info. |
| 148 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: | 156 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: |
| 149 (ContentSuggestionsSectionInformation*)sectionInfo; | 157 (ContentSuggestionsSectionInformation*)sectionInfo; |
| 150 | 158 |
| 151 @end | 159 @end |
| 152 | 160 |
| 153 @implementation ContentSuggestionsMediator | 161 @implementation ContentSuggestionsMediator |
| 154 | 162 |
| 155 @synthesize contentService = _contentService; | 163 @synthesize contentService = _contentService; |
| 156 @synthesize dataSink = _dataSink; | 164 @synthesize dataSink = _dataSink; |
| 157 @synthesize sectionInformationByCategory = _sectionInformationByCategory; | 165 @synthesize sectionInformationByCategory = _sectionInformationByCategory; |
| 166 @synthesize attributesProvider = _attributesProvider; | |
| 158 | 167 |
| 159 #pragma mark - Public | 168 #pragma mark - Public |
| 160 | 169 |
| 161 - (instancetype)initWithContentService: | 170 - (instancetype) |
| 162 (ntp_snippets::ContentSuggestionsService*)contentService { | 171 initWithContentService:(ntp_snippets::ContentSuggestionsService*)contentService |
| 172 largeIconService:(favicon::LargeIconService*)largeIconService { | |
| 163 self = [super init]; | 173 self = [super init]; |
| 164 if (self) { | 174 if (self) { |
| 165 _suggestionBridge = | 175 _suggestionBridge = |
| 166 base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService); | 176 base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService); |
| 167 _contentService = contentService; | 177 _contentService = contentService; |
| 168 _sectionInformationByCategory = [[NSMutableDictionary alloc] init]; | 178 _sectionInformationByCategory = [[NSMutableDictionary alloc] init]; |
| 179 _attributesProvider = [[FaviconAttributesProvider alloc] | |
| 180 initWithFaviconSize:kDefaultFaviconSize | |
| 181 minFaviconSize:1 | |
| 182 largeIconService:largeIconService]; | |
| 169 } | 183 } |
| 170 return self; | 184 return self; |
| 171 } | 185 } |
| 172 | 186 |
| 173 - (void)dismissSuggestion:(ContentSuggestionIdentifier*)suggestionIdentifier { | 187 - (void)dismissSuggestion:(ContentSuggestionIdentifier*)suggestionIdentifier { |
| 174 ContentSuggestionsCategoryWrapper* categoryWrapper = | 188 ContentSuggestionsCategoryWrapper* categoryWrapper = |
| 175 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo]; | 189 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo]; |
| 176 ntp_snippets::ContentSuggestion::ID suggestion_id = | 190 ntp_snippets::ContentSuggestion::ID suggestion_id = |
| 177 ntp_snippets::ContentSuggestion::ID([categoryWrapper category], | 191 ntp_snippets::ContentSuggestion::ID([categoryWrapper category], |
| 178 suggestionIdentifier.IDInSection); | 192 suggestionIdentifier.IDInSection); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 const std::vector<ntp_snippets::ContentSuggestion>& suggestions) { | 249 const std::vector<ntp_snippets::ContentSuggestion>& suggestions) { |
| 236 [weakSelf didFetchMoreSuggestions:suggestions | 250 [weakSelf didFetchMoreSuggestions:suggestions |
| 237 withStatusCode:status | 251 withStatusCode:status |
| 238 callback:callback]; | 252 callback:callback]; |
| 239 })); | 253 })); |
| 240 | 254 |
| 241 self.contentService->Fetch([wrapper category], known_suggestion_ids, | 255 self.contentService->Fetch([wrapper category], known_suggestion_ids, |
| 242 serviceCallback); | 256 serviceCallback); |
| 243 } | 257 } |
| 244 | 258 |
| 259 - (void)fetchFaviconAttributesForURL:(const GURL&)URL | |
| 260 completion:(void (^)(FaviconAttributes*))completion { | |
| 261 [self.attributesProvider fetchFaviconAttributesForURL:URL | |
| 262 completion:completion]; | |
| 263 } | |
| 264 | |
| 245 #pragma mark - ContentSuggestionsServiceObserver | 265 #pragma mark - ContentSuggestionsServiceObserver |
| 246 | 266 |
| 247 - (void)contentSuggestionsService: | 267 - (void)contentSuggestionsService: |
| 248 (ntp_snippets::ContentSuggestionsService*)suggestionsService | 268 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 249 newSuggestionsInCategory:(ntp_snippets::Category)category { | 269 newSuggestionsInCategory:(ntp_snippets::Category)category { |
| 250 ContentSuggestionsCategoryWrapper* wrapper = | 270 ContentSuggestionsCategoryWrapper* wrapper = |
| 251 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; | 271 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; |
| 252 if (!self.sectionInformationByCategory[wrapper]) { | 272 if (!self.sectionInformationByCategory[wrapper]) { |
| 253 [self addSectionInformationForCategory:category]; | 273 [self addSectionInformationForCategory:category]; |
| 254 } | 274 } |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 377 [NSMutableArray array]; | 397 [NSMutableArray array]; |
| 378 ntp_snippets::Category category = suggestions[0].id().category(); | 398 ntp_snippets::Category category = suggestions[0].id().category(); |
| 379 [self addSuggestions:suggestions | 399 [self addSuggestions:suggestions |
| 380 fromCategory:category | 400 fromCategory:category |
| 381 toArray:contentSuggestions]; | 401 toArray:contentSuggestions]; |
| 382 callback(contentSuggestions); | 402 callback(contentSuggestions); |
| 383 } | 403 } |
| 384 } | 404 } |
| 385 | 405 |
| 386 @end | 406 @end |
| OLD | NEW |