| 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" | |
| 11 #include "components/favicon/core/large_icon_service.h" | 10 #include "components/favicon/core/large_icon_service.h" |
| 12 #include "components/ntp_snippets/category.h" | 11 #include "components/ntp_snippets/category.h" |
| 13 #include "components/ntp_snippets/category_info.h" | 12 #include "components/ntp_snippets/category_info.h" |
| 14 #include "components/ntp_snippets/content_suggestion.h" | 13 #include "components/ntp_snippets/content_suggestion.h" |
| 15 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra
pper.h" | 14 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra
pper.h" |
| 16 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid
ge_observer.h" | 15 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid
ge_observer.h" |
| 16 #import "ios/chrome/browser/content_suggestions/mediator_util.h" |
| 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" | 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
| 18 #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" |
| 19 #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" |
| 20 #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" |
| 21 #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" | 22 #import "ios/chrome/browser/ui/favicon/favicon_attributes_provider.h" |
| 23 #include "ios/chrome/grit/ios_strings.h" | |
| 24 #include "ui/base/l10n/l10n_util_mac.h" | |
| 25 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
| 26 | 24 |
| 27 #if !defined(__has_feature) || !__has_feature(objc_arc) | 25 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 28 #error "This file requires ARC support." | 26 #error "This file requires ARC support." |
| 29 #endif | 27 #endif |
| 30 | 28 |
| 31 namespace { | 29 namespace { |
| 32 | 30 |
| 33 // Size of the favicon returned by the provider. | 31 // Size of the favicon returned by the provider. |
| 34 const CGFloat kDefaultFaviconSize = 16; | 32 const CGFloat kDefaultFaviconSize = 16; |
| 35 | 33 |
| 36 // TODO(crbug.com/701275): Once base::BindBlock supports the move semantics, | |
| 37 // remove this wrapper. | |
| 38 // Wraps a callback taking a const ref to a callback taking an object. | |
| 39 void BindWrapper( | |
| 40 base::Callback<void(ntp_snippets::Status status_code, | |
| 41 const std::vector<ntp_snippets::ContentSuggestion>& | |
| 42 suggestions)> callback, | |
| 43 ntp_snippets::Status status_code, | |
| 44 std::vector<ntp_snippets::ContentSuggestion> suggestions) { | |
| 45 if (callback) { | |
| 46 callback.Run(status_code, suggestions); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 // Returns the Type for this |category|. | |
| 51 ContentSuggestionType TypeForCategory(ntp_snippets::Category category) { | |
| 52 if (category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) | |
| 53 return ContentSuggestionTypeArticle; | |
| 54 if (category.IsKnownCategory(ntp_snippets::KnownCategories::READING_LIST)) | |
| 55 return ContentSuggestionTypeReadingList; | |
| 56 | |
| 57 return ContentSuggestionTypeEmpty; | |
| 58 } | |
| 59 | |
| 60 // Returns the section ID for this |category|. | |
| 61 ContentSuggestionsSectionID SectionIDForCategory( | |
| 62 ntp_snippets::Category category) { | |
| 63 if (category.IsKnownCategory(ntp_snippets::KnownCategories::BOOKMARKS)) | |
| 64 return ContentSuggestionsSectionBookmarks; | |
| 65 if (category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) | |
| 66 return ContentSuggestionsSectionArticles; | |
| 67 if (category.IsKnownCategory(ntp_snippets::KnownCategories::READING_LIST)) | |
| 68 return ContentSuggestionsSectionReadingList; | |
| 69 | |
| 70 return ContentSuggestionsSectionUnknown; | |
| 71 } | |
| 72 | |
| 73 // Returns the section layout corresponding to the category |layout|. | |
| 74 ContentSuggestionsSectionLayout SectionLayoutForLayout( | |
| 75 ntp_snippets::ContentSuggestionsCardLayout layout) { | |
| 76 // For now, only cards are relevant. | |
| 77 return ContentSuggestionsSectionLayoutCard; | |
| 78 } | |
| 79 | |
| 80 // Converts a ntp_snippets::ContentSuggestion to an Objective-C | |
| 81 // ContentSuggestion. | |
| 82 ContentSuggestion* ConvertContentSuggestion( | |
| 83 const ntp_snippets::ContentSuggestion& contentSuggestion) { | |
| 84 ContentSuggestion* suggestion = [[ContentSuggestion alloc] init]; | |
| 85 | |
| 86 suggestion.title = base::SysUTF16ToNSString(contentSuggestion.title()); | |
| 87 suggestion.text = base::SysUTF16ToNSString(contentSuggestion.snippet_text()); | |
| 88 suggestion.url = contentSuggestion.url(); | |
| 89 | |
| 90 suggestion.publisher = | |
| 91 base::SysUTF16ToNSString(contentSuggestion.publisher_name()); | |
| 92 suggestion.publishDate = contentSuggestion.publish_date(); | |
| 93 | |
| 94 suggestion.suggestionIdentifier = [[ContentSuggestionIdentifier alloc] init]; | |
| 95 suggestion.suggestionIdentifier.IDInSection = | |
| 96 contentSuggestion.id().id_within_category(); | |
| 97 | |
| 98 return suggestion; | |
| 99 } | |
| 100 | |
| 101 // Returns a SectionInformation for a |category|, filled with the | |
| 102 // |categoryInfo|. | |
| 103 ContentSuggestionsSectionInformation* SectionInformationFromCategoryInfo( | |
| 104 const base::Optional<ntp_snippets::CategoryInfo>& categoryInfo, | |
| 105 const ntp_snippets::Category& category) { | |
| 106 ContentSuggestionsSectionInformation* sectionInfo = | |
| 107 [[ContentSuggestionsSectionInformation alloc] | |
| 108 initWithSectionID:SectionIDForCategory(category)]; | |
| 109 if (categoryInfo) { | |
| 110 sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); | |
| 111 sectionInfo.showIfEmpty = categoryInfo->show_if_empty(); | |
| 112 sectionInfo.emptyText = | |
| 113 base::SysUTF16ToNSString(categoryInfo->no_suggestions_message()); | |
| 114 if (categoryInfo->additional_action() != | |
| 115 ntp_snippets::ContentSuggestionsAdditionalAction::NONE) { | |
| 116 sectionInfo.footerTitle = | |
| 117 l10n_util::GetNSString(IDS_IOS_CONTENT_SUGGESTIONS_FOOTER_TITLE); | |
| 118 } | |
| 119 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); | |
| 120 } | |
| 121 return sectionInfo; | |
| 122 } | |
| 123 | |
| 124 // Returns a ntp_snippets::ID based on a Objective-C Category and the ID in the | |
| 125 // category. | |
| 126 ntp_snippets::ContentSuggestion::ID SuggestionIDForSectionID( | |
| 127 ContentSuggestionsCategoryWrapper* category, | |
| 128 const std::string& id_in_category) { | |
| 129 return ntp_snippets::ContentSuggestion::ID(category.category, id_in_category); | |
| 130 } | |
| 131 | |
| 132 } // namespace | 34 } // namespace |
| 133 | 35 |
| 134 @interface ContentSuggestionsMediator ()<ContentSuggestionsImageFetcher, | 36 @interface ContentSuggestionsMediator ()<ContentSuggestionsImageFetcher, |
| 135 ContentSuggestionsServiceObserver> { | 37 ContentSuggestionsServiceObserver> { |
| 136 // Bridge for this class to become an observer of a ContentSuggestionsService. | 38 // Bridge for this class to become an observer of a ContentSuggestionsService. |
| 137 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; | 39 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; |
| 138 } | 40 } |
| 139 | 41 |
| 140 @property(nonatomic, assign) | 42 @property(nonatomic, assign) |
| 141 ntp_snippets::ContentSuggestionsService* contentService; | 43 ntp_snippets::ContentSuggestionsService* contentService; |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 [NSMutableArray array]; | 311 [NSMutableArray array]; |
| 410 ntp_snippets::Category category = suggestions[0].id().category(); | 312 ntp_snippets::Category category = suggestions[0].id().category(); |
| 411 [self addSuggestions:suggestions | 313 [self addSuggestions:suggestions |
| 412 fromCategory:category | 314 fromCategory:category |
| 413 toArray:contentSuggestions]; | 315 toArray:contentSuggestions]; |
| 414 callback(contentSuggestions); | 316 callback(contentSuggestions); |
| 415 } | 317 } |
| 416 } | 318 } |
| 417 | 319 |
| 418 @end | 320 @end |
| OLD | NEW |