| OLD | NEW |
| 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> |
| 11 | 11 |
| 12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "components/ntp_snippets/category.h" | 16 #include "components/ntp_snippets/category.h" |
| 17 #include "components/ntp_snippets/category_info.h" | 17 #include "components/ntp_snippets/category_info.h" |
| 18 #include "components/ntp_snippets/content_suggestion.h" | 18 #include "components/ntp_snippets/content_suggestion.h" |
| 19 #include "components/ntp_snippets/features.h" | 19 #include "components/ntp_snippets/features.h" |
| 20 #include "components/ntp_snippets/pref_names.h" | 20 #include "components/ntp_snippets/pref_names.h" |
| 21 #include "components/ntp_snippets/pref_util.h" | 21 #include "components/ntp_snippets/pref_util.h" |
| 22 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
| 23 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 24 #include "components/sessions/core/session_types.h" | 24 #include "components/sessions/core/session_types.h" |
| 25 #include "components/strings/grit/components_strings.h" | 25 #include "components/strings/grit/components_strings.h" |
| 26 #include "components/sync_sessions/synced_session.h" | 26 #include "components/sync_sessions/synced_session.h" |
| 27 #include "components/variations/variations_associated_data.h" | |
| 28 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
| 29 #include "ui/gfx/image/image.h" | 28 #include "ui/gfx/image/image.h" |
| 30 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 31 | 30 |
| 32 using base::Time; | 31 using base::Time; |
| 33 using base::TimeDelta; | 32 using base::TimeDelta; |
| 34 using sessions::SerializedNavigationEntry; | 33 using sessions::SerializedNavigationEntry; |
| 35 using sessions::SessionTab; | 34 using sessions::SessionTab; |
| 36 using sessions::SessionWindow; | 35 using sessions::SessionWindow; |
| 37 using sync_sessions::SyncedSessionWindow; | 36 using sync_sessions::SyncedSessionWindow; |
| 38 using sync_sessions::SyncedSession; | 37 using sync_sessions::SyncedSession; |
| 39 | 38 |
| 40 using DismissedFilter = base::Callback<bool(const std::string& id)>; | 39 using DismissedFilter = base::Callback<bool(const std::string& id)>; |
| 41 | 40 |
| 42 namespace ntp_snippets { | 41 namespace ntp_snippets { |
| 43 namespace { | 42 namespace { |
| 44 | 43 |
| 45 const int kMaxForeignTabsTotal = 10; | 44 constexpr base::FeatureParam<int> kMaxForeignTabsTotalParam{ |
| 46 const int kMaxForeignTabsPerDevice = 3; | 45 &kForeignSessionsSuggestionsFeature, "max_foreign_tabs_total", 10}; |
| 47 const int kMaxForeignTabAgeInMinutes = 180; | 46 constexpr base::FeatureParam<int> kMaxForeignTabsPerDeviceParam{ |
| 48 | 47 &kForeignSessionsSuggestionsFeature, "max_foreign_tabs_per_device", 3}; |
| 49 const char* kMaxForeignTabsTotalParamName = "max_foreign_tabs_total"; | 48 constexpr base::FeatureParam<int> kMaxForeignTabAgeInMinutesParam{ |
| 50 const char* kMaxForeignTabsPerDeviceParamName = "max_foreign_tabs_per_device"; | 49 &kForeignSessionsSuggestionsFeature, "max_foreign_tabs_age_in_minutes", |
| 51 const char* kMaxForeignTabAgeInMinutesParamName = | 50 180}; |
| 52 "max_foreign_tabs_age_in_minutes"; | |
| 53 | |
| 54 int GetMaxForeignTabsTotal() { | |
| 55 return variations::GetVariationParamByFeatureAsInt( | |
| 56 ntp_snippets::kForeignSessionsSuggestionsFeature, | |
| 57 kMaxForeignTabsTotalParamName, kMaxForeignTabsTotal); | |
| 58 } | |
| 59 | |
| 60 int GetMaxForeignTabsPerDevice() { | |
| 61 return variations::GetVariationParamByFeatureAsInt( | |
| 62 ntp_snippets::kForeignSessionsSuggestionsFeature, | |
| 63 kMaxForeignTabsPerDeviceParamName, kMaxForeignTabsPerDevice); | |
| 64 } | |
| 65 | 51 |
| 66 TimeDelta GetMaxForeignTabAge() { | 52 TimeDelta GetMaxForeignTabAge() { |
| 67 return TimeDelta::FromMinutes(variations::GetVariationParamByFeatureAsInt( | 53 return TimeDelta::FromMinutes(kMaxForeignTabAgeInMinutesParam.Get()); |
| 68 ntp_snippets::kForeignSessionsSuggestionsFeature, | |
| 69 kMaxForeignTabAgeInMinutesParamName, kMaxForeignTabAgeInMinutes)); | |
| 70 } | 54 } |
| 71 | 55 |
| 72 // This filter does two things. Most importantly it lets through only ids that | 56 // This filter does two things. Most importantly it lets through only ids that |
| 73 // have not been dismissed. The other responsibility this class has is it tracks | 57 // have not been dismissed. The other responsibility this class has is it tracks |
| 74 // all of the ids that fly past it, and it will save the intersection of | 58 // all of the ids that fly past it, and it will save the intersection of |
| 75 // initially dismissed ids, and seen ids. This will aggressively prune any | 59 // initially dismissed ids, and seen ids. This will aggressively prune any |
| 76 // dismissal that is not currently blocking a recent tab. | 60 // dismissal that is not currently blocking a recent tab. |
| 77 class PrefsPruningDismissedItemFilter { | 61 class PrefsPruningDismissedItemFilter { |
| 78 public: | 62 public: |
| 79 explicit PrefsPruningDismissedItemFilter(PrefService* pref_service) | 63 explicit PrefsPruningDismissedItemFilter(PrefService* pref_service) |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 category_status_); | 297 category_status_); |
| 314 } | 298 } |
| 315 | 299 |
| 316 // observer()->OnNewSuggestions must be called even when we have no | 300 // observer()->OnNewSuggestions must be called even when we have no |
| 317 // suggestions to remove previous suggestions that are now filtered out. | 301 // suggestions to remove previous suggestions that are now filtered out. |
| 318 observer()->OnNewSuggestions(this, provided_category_, BuildSuggestions()); | 302 observer()->OnNewSuggestions(this, provided_category_, BuildSuggestions()); |
| 319 } | 303 } |
| 320 | 304 |
| 321 std::vector<ContentSuggestion> | 305 std::vector<ContentSuggestion> |
| 322 ForeignSessionsSuggestionsProvider::BuildSuggestions() { | 306 ForeignSessionsSuggestionsProvider::BuildSuggestions() { |
| 323 const int max_foreign_tabs_total = GetMaxForeignTabsTotal(); | 307 const int max_foreign_tabs_total = kMaxForeignTabsTotalParam.Get(); |
| 324 const int max_foreign_tabs_per_device = GetMaxForeignTabsPerDevice(); | 308 const int max_foreign_tabs_per_device = kMaxForeignTabsPerDeviceParam.Get(); |
| 325 | 309 |
| 326 PrefsPruningDismissedItemFilter filter(pref_service_); | 310 PrefsPruningDismissedItemFilter filter(pref_service_); |
| 327 std::vector<SessionData> suggestion_candidates = | 311 std::vector<SessionData> suggestion_candidates = |
| 328 GetSuggestionCandidates(filter.ToCallback()); | 312 GetSuggestionCandidates(filter.ToCallback()); |
| 329 // This sorts by recency so that we keep the most recent entries and they | 313 // This sorts by recency so that we keep the most recent entries and they |
| 330 // appear as suggestions in reverse chronological order. | 314 // appear as suggestions in reverse chronological order. |
| 331 std::sort(suggestion_candidates.begin(), suggestion_candidates.end()); | 315 std::sort(suggestion_candidates.begin(), suggestion_candidates.end()); |
| 332 | 316 |
| 333 std::vector<ContentSuggestion> suggestions; | 317 std::vector<ContentSuggestion> suggestions; |
| 334 std::set<std::string> included_urls; | 318 std::set<std::string> included_urls; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 data.navigation->virtual_url().spec(), | 382 data.navigation->virtual_url().spec(), |
| 399 data.navigation->virtual_url()); | 383 data.navigation->virtual_url()); |
| 400 suggestion.set_title(data.navigation->title()); | 384 suggestion.set_title(data.navigation->title()); |
| 401 suggestion.set_publish_date(data.tab->timestamp); | 385 suggestion.set_publish_date(data.tab->timestamp); |
| 402 suggestion.set_publisher_name( | 386 suggestion.set_publisher_name( |
| 403 base::UTF8ToUTF16(data.navigation->virtual_url().host())); | 387 base::UTF8ToUTF16(data.navigation->virtual_url().host())); |
| 404 return suggestion; | 388 return suggestion; |
| 405 } | 389 } |
| 406 | 390 |
| 407 } // namespace ntp_snippets | 391 } // namespace ntp_snippets |
| OLD | NEW |