| 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/mediator_util.h" | 5 #import "ios/chrome/browser/content_suggestions/mediator_util.h" |
| 6 | 6 |
| 7 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
| 8 #include "components/ntp_snippets/category.h" | 8 #include "components/ntp_snippets/category.h" |
| 9 #include "components/ntp_tiles/metrics.h" | 9 #include "components/ntp_tiles/metrics.h" |
| 10 #include "components/rappor/rappor_service_impl.h" | 10 #include "components/rappor/rappor_service_impl.h" |
| 11 #include "ios/chrome/browser/application_context.h" | 11 #include "ios/chrome/browser/application_context.h" |
| 12 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra
pper.h" | 12 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra
pper.h" |
| 13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 14 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_ite
m.h" |
| 15 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_mos
t_visited_item.h" |
| 16 #import "ios/chrome/browser/ui/content_suggestions/cells/suggested_content.h" |
| 13 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion
_identifier.h" | 17 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion
_identifier.h" |
| 14 #include "ios/chrome/grit/ios_strings.h" | 18 #include "ios/chrome/grit/ios_strings.h" |
| 15 #include "ui/base/l10n/l10n_util_mac.h" | 19 #include "ui/base/l10n/l10n_util_mac.h" |
| 16 | 20 |
| 17 #if !defined(__has_feature) || !__has_feature(objc_arc) | 21 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 18 #error "This file requires ARC support." | 22 #error "This file requires ARC support." |
| 19 #endif | 23 #endif |
| 20 | 24 |
| 21 void BindWrapper( | 25 void BindWrapper( |
| 22 base::Callback<void(ntp_snippets::Status status_code, | 26 base::Callback<void(ntp_snippets::Status status_code, |
| 23 const std::vector<ntp_snippets::ContentSuggestion>& | 27 const std::vector<ntp_snippets::ContentSuggestion>& |
| 24 suggestions)> callback, | 28 suggestions)> callback, |
| 25 ntp_snippets::Status status_code, | 29 ntp_snippets::Status status_code, |
| 26 std::vector<ntp_snippets::ContentSuggestion> suggestions) { | 30 std::vector<ntp_snippets::ContentSuggestion> suggestions) { |
| 27 if (callback) { | 31 if (callback) { |
| 28 callback.Run(status_code, suggestions); | 32 callback.Run(status_code, suggestions); |
| 29 } | 33 } |
| 30 } | 34 } |
| 31 | 35 |
| 32 ContentSuggestionType TypeForCategory(ntp_snippets::Category category) { | |
| 33 if (category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) | |
| 34 return ContentSuggestionTypeArticle; | |
| 35 if (category.IsKnownCategory(ntp_snippets::KnownCategories::READING_LIST)) | |
| 36 return ContentSuggestionTypeReadingList; | |
| 37 | |
| 38 return ContentSuggestionTypeEmpty; | |
| 39 } | |
| 40 | |
| 41 ContentSuggestionsSectionID SectionIDForCategory( | 36 ContentSuggestionsSectionID SectionIDForCategory( |
| 42 ntp_snippets::Category category) { | 37 ntp_snippets::Category category) { |
| 43 if (category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) | 38 if (category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) |
| 44 return ContentSuggestionsSectionArticles; | 39 return ContentSuggestionsSectionArticles; |
| 45 if (category.IsKnownCategory(ntp_snippets::KnownCategories::READING_LIST)) | 40 if (category.IsKnownCategory(ntp_snippets::KnownCategories::READING_LIST)) |
| 46 return ContentSuggestionsSectionReadingList; | 41 return ContentSuggestionsSectionReadingList; |
| 47 | 42 |
| 48 return ContentSuggestionsSectionUnknown; | 43 return ContentSuggestionsSectionUnknown; |
| 49 } | 44 } |
| 50 | 45 |
| 51 ContentSuggestionsSectionLayout SectionLayoutForLayout( | 46 CollectionViewItem<SuggestedContent>* ConvertSuggestion( |
| 52 ntp_snippets::ContentSuggestionsCardLayout layout) { | 47 const ntp_snippets::ContentSuggestion& contentSuggestion, |
| 53 // For now, only cards are relevant. | 48 ContentSuggestionsSectionInformation* sectionInfo, |
| 54 return ContentSuggestionsSectionLayoutCard; | 49 ntp_snippets::Category category) { |
| 55 } | 50 ContentSuggestionsItem* suggestion = [[ContentSuggestionsItem alloc] |
| 56 | 51 initWithType:0 |
| 57 ContentSuggestion* ConvertContentSuggestion( | 52 title:base::SysUTF16ToNSString(contentSuggestion.title()) |
| 58 const ntp_snippets::ContentSuggestion& contentSuggestion) { | 53 subtitle:base::SysUTF16ToNSString(contentSuggestion.snippet_text()) |
| 59 ContentSuggestion* suggestion = [[ContentSuggestion alloc] init]; | 54 url:contentSuggestion.url()]; |
| 60 | |
| 61 suggestion.title = base::SysUTF16ToNSString(contentSuggestion.title()); | |
| 62 suggestion.text = base::SysUTF16ToNSString(contentSuggestion.snippet_text()); | |
| 63 suggestion.url = contentSuggestion.url(); | |
| 64 | 55 |
| 65 suggestion.publisher = | 56 suggestion.publisher = |
| 66 base::SysUTF16ToNSString(contentSuggestion.publisher_name()); | 57 base::SysUTF16ToNSString(contentSuggestion.publisher_name()); |
| 67 suggestion.publishDate = contentSuggestion.publish_date(); | 58 suggestion.publishDate = contentSuggestion.publish_date(); |
| 68 | 59 |
| 69 suggestion.suggestionIdentifier = [[ContentSuggestionIdentifier alloc] init]; | 60 suggestion.suggestionIdentifier = [[ContentSuggestionIdentifier alloc] init]; |
| 70 suggestion.suggestionIdentifier.IDInSection = | 61 suggestion.suggestionIdentifier.IDInSection = |
| 71 contentSuggestion.id().id_within_category(); | 62 contentSuggestion.id().id_within_category(); |
| 63 suggestion.suggestionIdentifier.sectionInfo = sectionInfo; |
| 64 |
| 65 if (category.IsKnownCategory(ntp_snippets::KnownCategories::READING_LIST)) { |
| 66 suggestion.availableOffline = |
| 67 contentSuggestion.reading_list_suggestion_extra()->distilled; |
| 68 } |
| 69 if (category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) { |
| 70 suggestion.hasImage = YES; |
| 71 } |
| 72 | 72 |
| 73 return suggestion; | 73 return suggestion; |
| 74 } | 74 } |
| 75 | 75 |
| 76 ContentSuggestionsSectionInformation* SectionInformationFromCategoryInfo( | 76 ContentSuggestionsSectionInformation* SectionInformationFromCategoryInfo( |
| 77 const base::Optional<ntp_snippets::CategoryInfo>& categoryInfo, | 77 const base::Optional<ntp_snippets::CategoryInfo>& categoryInfo, |
| 78 const ntp_snippets::Category& category) { | 78 const ntp_snippets::Category& category) { |
| 79 ContentSuggestionsSectionInformation* sectionInfo = | 79 ContentSuggestionsSectionInformation* sectionInfo = |
| 80 [[ContentSuggestionsSectionInformation alloc] | 80 [[ContentSuggestionsSectionInformation alloc] |
| 81 initWithSectionID:SectionIDForCategory(category)]; | 81 initWithSectionID:SectionIDForCategory(category)]; |
| 82 if (categoryInfo) { | 82 if (categoryInfo) { |
| 83 sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); | 83 sectionInfo.layout = ContentSuggestionsSectionLayoutCard; |
| 84 sectionInfo.showIfEmpty = categoryInfo->show_if_empty(); | 84 sectionInfo.showIfEmpty = categoryInfo->show_if_empty(); |
| 85 sectionInfo.emptyText = | 85 sectionInfo.emptyText = |
| 86 base::SysUTF16ToNSString(categoryInfo->no_suggestions_message()); | 86 base::SysUTF16ToNSString(categoryInfo->no_suggestions_message()); |
| 87 if (categoryInfo->additional_action() != | 87 if (categoryInfo->additional_action() != |
| 88 ntp_snippets::ContentSuggestionsAdditionalAction::NONE) { | 88 ntp_snippets::ContentSuggestionsAdditionalAction::NONE) { |
| 89 sectionInfo.footerTitle = | 89 sectionInfo.footerTitle = |
| 90 l10n_util::GetNSString(IDS_IOS_CONTENT_SUGGESTIONS_FOOTER_TITLE); | 90 l10n_util::GetNSString(IDS_IOS_CONTENT_SUGGESTIONS_FOOTER_TITLE); |
| 91 } | 91 } |
| 92 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); | 92 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); |
| 93 } | 93 } |
| 94 return sectionInfo; | 94 return sectionInfo; |
| 95 } | 95 } |
| 96 | 96 |
| 97 ntp_snippets::ContentSuggestion::ID SuggestionIDForSectionID( | 97 ntp_snippets::ContentSuggestion::ID SuggestionIDForSectionID( |
| 98 ContentSuggestionsCategoryWrapper* category, | 98 ContentSuggestionsCategoryWrapper* category, |
| 99 const std::string& id_in_category) { | 99 const std::string& id_in_category) { |
| 100 return ntp_snippets::ContentSuggestion::ID(category.category, id_in_category); | 100 return ntp_snippets::ContentSuggestion::ID(category.category, id_in_category); |
| 101 } | 101 } |
| 102 | 102 |
| 103 ContentSuggestion* EmptySuggestion() { | |
| 104 ContentSuggestion* suggestion = [[ContentSuggestion alloc] init]; | |
| 105 suggestion.type = ContentSuggestionTypeEmpty; | |
| 106 suggestion.suggestionIdentifier = [[ContentSuggestionIdentifier alloc] init]; | |
| 107 | |
| 108 return suggestion; | |
| 109 } | |
| 110 | |
| 111 ContentSuggestionsSectionInformation* MostVisitedSectionInformation() { | 103 ContentSuggestionsSectionInformation* MostVisitedSectionInformation() { |
| 112 ContentSuggestionsSectionInformation* sectionInfo = | 104 ContentSuggestionsSectionInformation* sectionInfo = |
| 113 [[ContentSuggestionsSectionInformation alloc] | 105 [[ContentSuggestionsSectionInformation alloc] |
| 114 initWithSectionID:ContentSuggestionsSectionMostVisited]; | 106 initWithSectionID:ContentSuggestionsSectionMostVisited]; |
| 115 sectionInfo.title = nil; | 107 sectionInfo.title = nil; |
| 116 sectionInfo.footerTitle = nil; | 108 sectionInfo.footerTitle = nil; |
| 117 sectionInfo.showIfEmpty = NO; | 109 sectionInfo.showIfEmpty = NO; |
| 118 sectionInfo.layout = ContentSuggestionsSectionLayoutCustom; | 110 sectionInfo.layout = ContentSuggestionsSectionLayoutCustom; |
| 119 | 111 |
| 120 return sectionInfo; | 112 return sectionInfo; |
| 121 } | 113 } |
| 122 | 114 |
| 123 void RecordPageImpression(const ntp_tiles::NTPTilesVector& mostVisited) { | 115 void RecordPageImpression(const ntp_tiles::NTPTilesVector& mostVisited) { |
| 124 std::vector<ntp_tiles::metrics::TileImpression> tiles; | 116 std::vector<ntp_tiles::metrics::TileImpression> tiles; |
| 125 for (const ntp_tiles::NTPTile& ntpTile : mostVisited) { | 117 for (const ntp_tiles::NTPTile& ntpTile : mostVisited) { |
| 126 tiles.emplace_back(ntpTile.source, ntp_tiles::UNKNOWN_TILE_TYPE, | 118 tiles.emplace_back(ntpTile.source, ntp_tiles::UNKNOWN_TILE_TYPE, |
| 127 ntpTile.url); | 119 ntpTile.url); |
| 128 } | 120 } |
| 129 ntp_tiles::metrics::RecordPageImpression( | 121 ntp_tiles::metrics::RecordPageImpression( |
| 130 tiles, GetApplicationContext()->GetRapporServiceImpl()); | 122 tiles, GetApplicationContext()->GetRapporServiceImpl()); |
| 131 } | 123 } |
| 132 | 124 |
| 133 ContentSuggestion* ConvertNTPTile(const ntp_tiles::NTPTile& tile) { | 125 CollectionViewItem<SuggestedContent>* ConvertNTPTile( |
| 134 ContentSuggestion* suggestion = [[ContentSuggestion alloc] init]; | 126 const ntp_tiles::NTPTile& tile, |
| 127 ContentSuggestionsSectionInformation* sectionInfo) { |
| 128 ContentSuggestionsMostVisitedItem* suggestion = |
| 129 [[ContentSuggestionsMostVisitedItem alloc] initWithType:0]; |
| 135 | 130 |
| 136 suggestion.title = base::SysUTF16ToNSString(tile.title); | 131 suggestion.title = base::SysUTF16ToNSString(tile.title); |
| 137 suggestion.url = tile.url; | 132 suggestion.URL = tile.url; |
| 138 suggestion.type = ContentSuggestionTypeMostVisited; | |
| 139 | 133 |
| 140 suggestion.suggestionIdentifier = [[ContentSuggestionIdentifier alloc] init]; | 134 suggestion.suggestionIdentifier = [[ContentSuggestionIdentifier alloc] init]; |
| 141 suggestion.suggestionIdentifier.IDInSection = tile.url.spec(); | 135 suggestion.suggestionIdentifier.IDInSection = tile.url.spec(); |
| 136 suggestion.suggestionIdentifier.sectionInfo = sectionInfo; |
| 142 | 137 |
| 143 return suggestion; | 138 return suggestion; |
| 144 } | 139 } |
| OLD | NEW |