OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/ntp_snippets/physical_web_pages/physical_web_page_suggestio ns_provider.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/strings/string_number_conversions.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 | |
12 namespace ntp_snippets { | |
13 | |
14 namespace { | |
15 | |
16 const int kMaxSuggestionsCount = 10; | |
17 | |
18 } // namespace | |
19 | |
20 PhysicalWebPageSuggestionsProvider::PhysicalWebPageSuggestionsProvider( | |
21 ContentSuggestionsProvider::Observer* observer, | |
22 CategoryFactory* category_factory) | |
23 : ContentSuggestionsProvider(observer, category_factory), | |
24 category_status_(CategoryStatus::AVAILABLE_LOADING), | |
25 provided_category_( | |
26 category_factory->FromKnownCategory(KnownCategories::PHYSICAL_WEB_PAGE S)) { | |
27 | |
28 // TODO(vitaliii): remove when Physical Web service C++ interface is provided. | |
29 std::vector<UrlInfo> urls; | |
30 UrlInfo url_info; | |
31 url_info.title = "Mockup PW suggestion"; | |
32 url_info.description = "This is a hardcoded PW suggestion."; | |
33 url_info.scan_time = base::Time::Now(); | |
34 url_info.site_url = GURL("https://google.com"); | |
35 urls.push_back(url_info); | |
36 | |
37 url_info.title = "Another mockup PW suggestion"; | |
38 url_info.description = "This is another hardcoded PW suggestion."; | |
39 urls.push_back(url_info); | |
40 | |
41 onDisplayableUrlsChanged(urls); | |
42 } | |
43 | |
44 PhysicalWebPageSuggestionsProvider::~PhysicalWebPageSuggestionsProvider() { | |
45 } | |
46 | |
47 //////////////////////////////////////////////////////////////////////////////// | |
48 // Private methods | |
49 | |
50 std::vector<Category> PhysicalWebPageSuggestionsProvider::GetProvidedCategories( ) { | |
tschumann
2016/08/09 15:24:59
line too long. clang-format (go/clang-format) sugg
vitaliii
2016/08/10 14:18:41
Done.
| |
51 return std::vector<Category>({provided_category_}); | |
52 } | |
53 | |
54 CategoryStatus PhysicalWebPageSuggestionsProvider::GetCategoryStatus( | |
55 Category category) { | |
56 return category_status_; | |
57 } | |
58 | |
59 void PhysicalWebPageSuggestionsProvider::DismissSuggestion( | |
60 const std::string& suggestion_id) { | |
61 // TODO(vitaliii): Implement this and then | |
62 // ClearDismissedSuggestionsForDebugging. | |
63 } | |
64 | |
65 void PhysicalWebPageSuggestionsProvider::FetchSuggestionImage( | |
66 const std::string& suggestion_id, | |
67 const ImageFetchedCallback& callback) { | |
68 // TODO(vitaliii): Implement. | |
69 } | |
70 | |
71 void PhysicalWebPageSuggestionsProvider::ClearCachedSuggestionsForDebugging() { | |
72 // Ignored. | |
73 } | |
74 | |
75 void PhysicalWebPageSuggestionsProvider::ClearDismissedSuggestionsForDebugging() { | |
tschumann
2016/08/09 15:24:59
line > 80 columns.
clangformat (go/clangformat) su
vitaliii
2016/08/10 14:18:41
Done.
| |
76 // TODO(vitaliii): Implement when dismissed suggestions are supported. | |
77 } | |
78 | |
79 void PhysicalWebPageSuggestionsProvider::onDisplayableUrlsChanged( | |
80 const std::vector<UrlInfo>& urls) { | |
81 NotifyStatusChanged(CategoryStatus::AVAILABLE); | |
82 std::vector<ContentSuggestion> suggestions; | |
83 | |
84 for (int i = 0; i < static_cast<int>(urls.size()) && i < kMaxSuggestionsCount; | |
85 ++i) { | |
Marc Treib
2016/08/09 12:45:17
range-based for ftw!
for (const UrlInfo& url_info
vitaliii
2016/08/10 14:18:41
Done.
| |
86 const UrlInfo& url_info = urls[i]; | |
87 ContentSuggestion suggestion( | |
88 MakeUniqueID(provided_category_, url_info.site_url.spec()), | |
89 url_info.site_url); | |
90 | |
91 suggestion.set_title(base::UTF8ToUTF16(url_info.title)); | |
92 suggestion.set_snippet_text(base::UTF8ToUTF16(url_info.description)); | |
93 suggestion.set_publish_date(url_info.scan_time); | |
94 suggestion.set_publisher_name(base::UTF8ToUTF16(url_info.site_url.host())); | |
95 suggestions.emplace_back(std::move(suggestion)); | |
96 } | |
97 | |
98 observer()->OnNewSuggestions(this, provided_category_, | |
99 std::move(suggestions)); | |
tschumann
2016/08/09 15:24:59
indentation.
vitaliii
2016/08/10 14:18:41
Done.
| |
100 } | |
101 | |
102 void PhysicalWebPageSuggestionsProvider::NotifyStatusChanged( | |
103 CategoryStatus new_status) { | |
104 if (category_status_ == new_status) | |
105 return; | |
106 category_status_ = new_status; | |
107 | |
tschumann
2016/08/09 15:24:59
drop the blank line? In general, i'd try to minimi
vitaliii
2016/08/10 14:18:41
Done.
| |
108 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | |
109 } | |
110 | |
111 } // namespace ntp_snippets | |
OLD | NEW |