Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: components/ntp_snippets/sessions/foreign_sessions_suggestions_provider.cc

Issue 2377663002: [NTP Snippets] Introduce ContentSuggestion::ID (Closed)
Patch Set: rebase Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 #include "components/ntp_snippets/sessions/foreign_sessions_suggestions_provider .h" 5 #include "components/ntp_snippets/sessions/foreign_sessions_suggestions_provider .h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <tuple> 9 #include <tuple>
10 #include <utility> 10 #include <utility>
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 Category category) { 118 Category category) {
119 DCHECK_EQ(category, provided_category_); 119 DCHECK_EQ(category, provided_category_);
120 return CategoryInfo(l10n_util::GetStringUTF16( 120 return CategoryInfo(l10n_util::GetStringUTF16(
121 IDS_NTP_FOREIGN_SESSIONS_SUGGESTIONS_SECTION_HEADER), 121 IDS_NTP_FOREIGN_SESSIONS_SUGGESTIONS_SECTION_HEADER),
122 ContentSuggestionsCardLayout::MINIMAL_CARD, 122 ContentSuggestionsCardLayout::MINIMAL_CARD,
123 /*has_more_button=*/true, 123 /*has_more_button=*/true,
124 /*show_if_empty=*/false); 124 /*show_if_empty=*/false);
125 } 125 }
126 126
127 void ForeignSessionsSuggestionsProvider::DismissSuggestion( 127 void ForeignSessionsSuggestionsProvider::DismissSuggestion(
128 const std::string& suggestion_id) { 128 const ContentSuggestion::ID& suggestion_id) {
129 // TODO(skym): Right now this continuously grows, without clearing out old and 129 // TODO(skym): Right now this continuously grows, without clearing out old and
130 // irrelevant entries. Could either use a timestamp and expire after a 130 // irrelevant entries. Could either use a timestamp and expire after a
131 // threshold, or compare with current foreign tabs and remove anything that 131 // threshold, or compare with current foreign tabs and remove anything that
132 // isn't actively blockign a foreign_sessions tab. 132 // isn't actively blockign a foreign_sessions tab.
133 std::set<std::string> dismissed_ids = prefs::ReadDismissedIDsFromPrefs( 133 std::set<std::string> dismissed_ids = prefs::ReadDismissedIDsFromPrefs(
134 *pref_service_, prefs::kDismissedForeignSessionsSuggestions); 134 *pref_service_, prefs::kDismissedForeignSessionsSuggestions);
135 dismissed_ids.insert(suggestion_id); 135 dismissed_ids.insert(suggestion_id.id_within_category());
136 prefs::StoreDismissedIDsToPrefs(pref_service_, 136 prefs::StoreDismissedIDsToPrefs(pref_service_,
137 prefs::kDismissedForeignSessionsSuggestions, 137 prefs::kDismissedForeignSessionsSuggestions,
138 dismissed_ids); 138 dismissed_ids);
139 } 139 }
140 140
141 void ForeignSessionsSuggestionsProvider::FetchSuggestionImage( 141 void ForeignSessionsSuggestionsProvider::FetchSuggestionImage(
142 const std::string& suggestion_id, 142 const ContentSuggestion::ID& suggestion_id,
143 const ImageFetchedCallback& callback) { 143 const ImageFetchedCallback& callback) {
144 base::ThreadTaskRunnerHandle::Get()->PostTask( 144 base::ThreadTaskRunnerHandle::Get()->PostTask(
145 FROM_HERE, base::Bind(callback, gfx::Image())); 145 FROM_HERE, base::Bind(callback, gfx::Image()));
146 } 146 }
147 147
148 void ForeignSessionsSuggestionsProvider::ClearHistory( 148 void ForeignSessionsSuggestionsProvider::ClearHistory(
149 base::Time begin, 149 base::Time begin,
150 base::Time end, 150 base::Time end,
151 const base::Callback<bool(const GURL& url)>& filter) { 151 const base::Callback<bool(const GURL& url)>& filter) {
152 std::set<std::string> dismissed_ids = prefs::ReadDismissedIDsFromPrefs( 152 std::set<std::string> dismissed_ids = prefs::ReadDismissedIDsFromPrefs(
153 *pref_service_, prefs::kDismissedForeignSessionsSuggestions); 153 *pref_service_, prefs::kDismissedForeignSessionsSuggestions);
154 for (auto iter = dismissed_ids.begin(); iter != dismissed_ids.end();) { 154 for (auto iter = dismissed_ids.begin(); iter != dismissed_ids.end();) {
155 if (filter.Run(GURL(base::StringPiece(*iter)))) { 155 if (filter.Run(GURL(*iter))) {
156 iter = dismissed_ids.erase(iter); 156 iter = dismissed_ids.erase(iter);
157 } else { 157 } else {
158 ++iter; 158 ++iter;
159 } 159 }
160 } 160 }
161 prefs::StoreDismissedIDsToPrefs(pref_service_, 161 prefs::StoreDismissedIDsToPrefs(pref_service_,
162 prefs::kDismissedForeignSessionsSuggestions, 162 prefs::kDismissedForeignSessionsSuggestions,
163 dismissed_ids); 163 dismissed_ids);
164 } 164 }
165 165
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 270
271 for (const SyncedSession* session : foreign_sessions) { 271 for (const SyncedSession* session : foreign_sessions) {
272 for (const std::pair<const SessionID::id_type, 272 for (const std::pair<const SessionID::id_type,
273 std::unique_ptr<sessions::SessionWindow>>& key_value : 273 std::unique_ptr<sessions::SessionWindow>>& key_value :
274 session->windows) { 274 session->windows) {
275 for (const std::unique_ptr<SessionTab>& tab : key_value.second->tabs) { 275 for (const std::unique_ptr<SessionTab>& tab : key_value.second->tabs) {
276 if (tab->navigations.empty()) 276 if (tab->navigations.empty())
277 continue; 277 continue;
278 278
279 const SerializedNavigationEntry& navigation = tab->navigations.back(); 279 const SerializedNavigationEntry& navigation = tab->navigations.back();
280 const std::string unique_id = 280 const std::string id = navigation.virtual_url().spec();
281 MakeUniqueID(provided_category_, navigation.virtual_url().spec());
282 // TODO(skym): Filter out internal pages. Tabs that contain only 281 // TODO(skym): Filter out internal pages. Tabs that contain only
283 // non-syncable content should never reach the local client, but 282 // non-syncable content should never reach the local client, but
284 // sometimes the most recent navigation may be internal while one 283 // sometimes the most recent navigation may be internal while one
285 // of the previous ones was more valid. 284 // of the previous ones was more valid.
286 if (dismissed_ids.find(unique_id) == dismissed_ids.end() && 285 if (dismissed_ids.find(id) == dismissed_ids.end() &&
287 (base::Time::Now() - tab->timestamp) < max_foreign_tab_age) { 286 (base::Time::Now() - tab->timestamp) < max_foreign_tab_age) {
288 suggestion_candidates.push_back( 287 suggestion_candidates.push_back(
289 SessionData{session, tab.get(), &navigation}); 288 SessionData{session, tab.get(), &navigation});
290 } 289 }
291 } 290 }
292 } 291 }
293 } 292 }
294 return suggestion_candidates; 293 return suggestion_candidates;
295 } 294 }
296 295
297 ContentSuggestion ForeignSessionsSuggestionsProvider::BuildSuggestion( 296 ContentSuggestion ForeignSessionsSuggestionsProvider::BuildSuggestion(
298 const SessionData& data) { 297 const SessionData& data) {
299 ContentSuggestion suggestion( 298 ContentSuggestion suggestion(provided_category_,
300 MakeUniqueID(provided_category_, data.navigation->virtual_url().spec()), 299 data.navigation->virtual_url().spec(),
301 data.navigation->virtual_url()); 300 data.navigation->virtual_url());
302 suggestion.set_title(data.navigation->title()); 301 suggestion.set_title(data.navigation->title());
303 suggestion.set_publish_date(data.tab->timestamp); 302 suggestion.set_publish_date(data.tab->timestamp);
304 // TODO(skym): It's unclear if this simple approach is sufficient for 303 // TODO(skym): It's unclear if this simple approach is sufficient for
305 // right-to-left languages. 304 // right-to-left languages.
306 // This field is sandwiched between the url's favicon, which is on the left, 305 // This field is sandwiched between the url's favicon, which is on the left,
307 // and the |publish_date|, which is to the right. The domain should always 306 // and the |publish_date|, which is to the right. The domain should always
308 // appear next to the favicon. 307 // appear next to the favicon.
309 suggestion.set_publisher_name( 308 suggestion.set_publisher_name(
310 base::UTF8ToUTF16(data.navigation->virtual_url().host() + " - " + 309 base::UTF8ToUTF16(data.navigation->virtual_url().host() + " - " +
311 data.session->session_name)); 310 data.session->session_name));
312 return suggestion; 311 return suggestion;
313 } 312 }
314 313
315 } // namespace ntp_snippets 314 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698