| 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 // Bridge for this class to become an observer of a ContentSuggestionsService. | 136 // Bridge for this class to become an observer of a ContentSuggestionsService. |
| 132 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; | 137 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; |
| 133 } | 138 } |
| 134 | 139 |
| 135 @property(nonatomic, assign) | 140 @property(nonatomic, assign) |
| 136 ntp_snippets::ContentSuggestionsService* contentService; | 141 ntp_snippets::ContentSuggestionsService* contentService; |
| 137 @property(nonatomic, strong, nonnull) | 142 @property(nonatomic, strong, nonnull) |
| 138 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, | 143 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, |
| 139 ContentSuggestionsSectionInformation*>* | 144 ContentSuggestionsSectionInformation*>* |
| 140 sectionInformationByCategory; | 145 sectionInformationByCategory; |
| 146 // FaviconAttributesProvider to fetch the favicon for the suggestions. |
| 147 @property(nonatomic, nullable, strong) |
| 148 FaviconAttributesProvider* attributesProvider; |
| 141 | 149 |
| 142 // Converts the |suggestions| from |category| to ContentSuggestion and adds them | 150 // Converts the |suggestions| from |category| to ContentSuggestion and adds them |
| 143 // to the |contentArray| if the category is available. | 151 // to the |contentArray| if the category is available. |
| 144 - (void)addSuggestions: | 152 - (void)addSuggestions: |
| 145 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions | 153 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions |
| 146 fromCategory:(ntp_snippets::Category&)category | 154 fromCategory:(ntp_snippets::Category&)category |
| 147 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; | 155 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| 148 | 156 |
| 149 // Adds the section information for |category| in | 157 // Adds the section information for |category| in |
| 150 // self.sectionInformationByCategory. | 158 // self.sectionInformationByCategory. |
| 151 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; | 159 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; |
| 152 | 160 |
| 153 // Returns a CategoryWrapper acting as a key for this section info. | 161 // Returns a CategoryWrapper acting as a key for this section info. |
| 154 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: | 162 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: |
| 155 (ContentSuggestionsSectionInformation*)sectionInfo; | 163 (ContentSuggestionsSectionInformation*)sectionInfo; |
| 156 | 164 |
| 157 @end | 165 @end |
| 158 | 166 |
| 159 @implementation ContentSuggestionsMediator | 167 @implementation ContentSuggestionsMediator |
| 160 | 168 |
| 161 @synthesize contentService = _contentService; | 169 @synthesize contentService = _contentService; |
| 162 @synthesize dataSink = _dataSink; | 170 @synthesize dataSink = _dataSink; |
| 163 @synthesize sectionInformationByCategory = _sectionInformationByCategory; | 171 @synthesize sectionInformationByCategory = _sectionInformationByCategory; |
| 172 @synthesize attributesProvider = _attributesProvider; |
| 164 | 173 |
| 165 #pragma mark - Public | 174 #pragma mark - Public |
| 166 | 175 |
| 167 - (instancetype)initWithContentService: | 176 - (instancetype) |
| 168 (ntp_snippets::ContentSuggestionsService*)contentService { | 177 initWithContentService:(ntp_snippets::ContentSuggestionsService*)contentService |
| 178 largeIconService:(favicon::LargeIconService*)largeIconService { |
| 169 self = [super init]; | 179 self = [super init]; |
| 170 if (self) { | 180 if (self) { |
| 171 _suggestionBridge = | 181 _suggestionBridge = |
| 172 base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService); | 182 base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService); |
| 173 _contentService = contentService; | 183 _contentService = contentService; |
| 174 _sectionInformationByCategory = [[NSMutableDictionary alloc] init]; | 184 _sectionInformationByCategory = [[NSMutableDictionary alloc] init]; |
| 185 _attributesProvider = [[FaviconAttributesProvider alloc] |
| 186 initWithFaviconSize:kDefaultFaviconSize |
| 187 minFaviconSize:1 |
| 188 largeIconService:largeIconService]; |
| 175 } | 189 } |
| 176 return self; | 190 return self; |
| 177 } | 191 } |
| 178 | 192 |
| 179 - (void)dismissSuggestion:(ContentSuggestionIdentifier*)suggestionIdentifier { | 193 - (void)dismissSuggestion:(ContentSuggestionIdentifier*)suggestionIdentifier { |
| 180 ContentSuggestionsCategoryWrapper* categoryWrapper = | 194 ContentSuggestionsCategoryWrapper* categoryWrapper = |
| 181 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo]; | 195 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo]; |
| 182 ntp_snippets::ContentSuggestion::ID suggestion_id = | 196 ntp_snippets::ContentSuggestion::ID suggestion_id = |
| 183 ntp_snippets::ContentSuggestion::ID([categoryWrapper category], | 197 ntp_snippets::ContentSuggestion::ID([categoryWrapper category], |
| 184 suggestionIdentifier.IDInSection); | 198 suggestionIdentifier.IDInSection); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 const std::vector<ntp_snippets::ContentSuggestion>& suggestions) { | 255 const std::vector<ntp_snippets::ContentSuggestion>& suggestions) { |
| 242 [weakSelf didFetchMoreSuggestions:suggestions | 256 [weakSelf didFetchMoreSuggestions:suggestions |
| 243 withStatusCode:status | 257 withStatusCode:status |
| 244 callback:callback]; | 258 callback:callback]; |
| 245 })); | 259 })); |
| 246 | 260 |
| 247 self.contentService->Fetch([wrapper category], known_suggestion_ids, | 261 self.contentService->Fetch([wrapper category], known_suggestion_ids, |
| 248 serviceCallback); | 262 serviceCallback); |
| 249 } | 263 } |
| 250 | 264 |
| 265 - (void)fetchFaviconAttributesForURL:(const GURL&)URL |
| 266 completion:(void (^)(FaviconAttributes*))completion { |
| 267 [self.attributesProvider fetchFaviconAttributesForURL:URL |
| 268 completion:completion]; |
| 269 } |
| 270 |
| 251 #pragma mark - ContentSuggestionsServiceObserver | 271 #pragma mark - ContentSuggestionsServiceObserver |
| 252 | 272 |
| 253 - (void)contentSuggestionsService: | 273 - (void)contentSuggestionsService: |
| 254 (ntp_snippets::ContentSuggestionsService*)suggestionsService | 274 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 255 newSuggestionsInCategory:(ntp_snippets::Category)category { | 275 newSuggestionsInCategory:(ntp_snippets::Category)category { |
| 256 ContentSuggestionsCategoryWrapper* wrapper = | 276 ContentSuggestionsCategoryWrapper* wrapper = |
| 257 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; | 277 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; |
| 258 if (!self.sectionInformationByCategory[wrapper]) { | 278 if (!self.sectionInformationByCategory[wrapper]) { |
| 259 [self addSectionInformationForCategory:category]; | 279 [self addSectionInformationForCategory:category]; |
| 260 } | 280 } |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 [NSMutableArray array]; | 403 [NSMutableArray array]; |
| 384 ntp_snippets::Category category = suggestions[0].id().category(); | 404 ntp_snippets::Category category = suggestions[0].id().category(); |
| 385 [self addSuggestions:suggestions | 405 [self addSuggestions:suggestions |
| 386 fromCategory:category | 406 fromCategory:category |
| 387 toArray:contentSuggestions]; | 407 toArray:contentSuggestions]; |
| 388 callback(contentSuggestions); | 408 callback(contentSuggestions); |
| 389 } | 409 } |
| 390 } | 410 } |
| 391 | 411 |
| 392 @end | 412 @end |
| OLD | NEW |