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

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1922463002: [NTP Snippets] Limit the number of snippets to 10 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review Created 4 years, 8 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/ntp_snippets_service.h" 5 #include "components/ntp_snippets/ntp_snippets_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 15 matching lines...) Expand all
26 #include "components/suggestions/proto/suggestions.pb.h" 26 #include "components/suggestions/proto/suggestions.pb.h"
27 27
28 using suggestions::ChromeSuggestion; 28 using suggestions::ChromeSuggestion;
29 using suggestions::SuggestionsProfile; 29 using suggestions::SuggestionsProfile;
30 using suggestions::SuggestionsService; 30 using suggestions::SuggestionsService;
31 31
32 namespace ntp_snippets { 32 namespace ntp_snippets {
33 33
34 namespace { 34 namespace {
35 35
36 const int kMaxSnippetCount = 10;
37
36 const int kFetchingIntervalWifiChargingSeconds = 30 * 60; 38 const int kFetchingIntervalWifiChargingSeconds = 30 * 60;
37 const int kFetchingIntervalWifiSeconds = 2 * 60 * 60; 39 const int kFetchingIntervalWifiSeconds = 2 * 60 * 60;
38 const int kFetchingIntervalFallbackSeconds = 24 * 60 * 60; 40 const int kFetchingIntervalFallbackSeconds = 24 * 60 * 60;
39 41
40 // These define the times of day during which we will fetch via Wifi (without 42 // These define the times of day during which we will fetch via Wifi (without
41 // charging) - 6 AM to 10 PM. 43 // charging) - 6 AM to 10 PM.
42 const int kWifiFetchingHourMin = 6; 44 const int kWifiFetchingHourMin = 6;
43 const int kWifiFetchingHourMax = 22; 45 const int kWifiFetchingHourMax = 22;
44 46
45 const int kDefaultExpiryTimeMins = 24 * 60; 47 const int kDefaultExpiryTimeMins = 24 * 60;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 } 230 }
229 231
230 void NTPSnippetsService::FetchSnippets() { 232 void NTPSnippetsService::FetchSnippets() {
231 FetchSnippetsFromHosts(GetSuggestionsHosts()); 233 FetchSnippetsFromHosts(GetSuggestionsHosts());
232 } 234 }
233 235
234 void NTPSnippetsService::FetchSnippetsFromHosts( 236 void NTPSnippetsService::FetchSnippetsFromHosts(
235 const std::set<std::string>& hosts) { 237 const std::set<std::string>& hosts) {
236 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 238 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
237 switches::kDontRestrict)) { 239 switches::kDontRestrict)) {
238 snippets_fetcher_->FetchSnippets(std::set<std::string>()); 240 snippets_fetcher_->FetchSnippets(std::set<std::string>(), kMaxSnippetCount);
239 return; 241 return;
240 } 242 }
241 if (!hosts.empty()) { 243 if (!hosts.empty()) {
242 snippets_fetcher_->FetchSnippets(hosts); 244 snippets_fetcher_->FetchSnippets(hosts, kMaxSnippetCount);
243 } else { 245 } else {
244 last_fetch_status_ = kStatusMessageEmptyHosts; 246 last_fetch_status_ = kStatusMessageEmptyHosts;
245 LoadingSnippetsFinished(); 247 LoadingSnippetsFinished();
246 } 248 }
247 } 249 }
248 250
249 void NTPSnippetsService::RescheduleFetching() { 251 void NTPSnippetsService::RescheduleFetching() {
250 // The scheduler only exists on Android so far, it's null on other platforms. 252 // The scheduler only exists on Android so far, it's null on other platforms.
251 if (!scheduler_) 253 if (!scheduler_)
252 return; 254 return;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 } 305 }
304 306
305 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { 307 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) {
306 observers_.AddObserver(observer); 308 observers_.AddObserver(observer);
307 } 309 }
308 310
309 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { 311 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) {
310 observers_.RemoveObserver(observer); 312 observers_.RemoveObserver(observer);
311 } 313 }
312 314
315 // static
316 int NTPSnippetsService::GetMaxSnippetCountForTesting() {
317 return kMaxSnippetCount;
318 }
319
313 void NTPSnippetsService::OnSuggestionsChanged( 320 void NTPSnippetsService::OnSuggestionsChanged(
314 const SuggestionsProfile& suggestions) { 321 const SuggestionsProfile& suggestions) {
315 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); 322 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions);
316 if (hosts == GetSnippetHostsFromPrefs()) 323 if (hosts == GetSnippetHostsFromPrefs())
317 return; 324 return;
318 325
319 // Remove existing snippets that aren't in the suggestions anymore. 326 // Remove existing snippets that aren't in the suggestions anymore.
320 snippets_.erase( 327 snippets_.erase(
321 std::remove_if(snippets_.begin(), snippets_.end(), 328 std::remove_if(snippets_.begin(), snippets_.end(),
322 [&hosts](const scoped_ptr<NTPSnippet>& snippet) { 329 [&hosts](const scoped_ptr<NTPSnippet>& snippet) {
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 snippet->publish_date() + 414 snippet->publish_date() +
408 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); 415 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins));
409 } 416 }
410 } 417 }
411 418
412 // Insert the new snippets at the front. 419 // Insert the new snippets at the front.
413 snippets_.insert(snippets_.begin(), 420 snippets_.insert(snippets_.begin(),
414 std::make_move_iterator(new_snippets.begin()), 421 std::make_move_iterator(new_snippets.begin()),
415 std::make_move_iterator(new_snippets.end())); 422 std::make_move_iterator(new_snippets.end()));
416 423
424 // If there are more snippets now than we want to show, drop the extra ones
425 // from the end of the list.
426 if (snippets_.size() > kMaxSnippetCount)
427 snippets_.resize(kMaxSnippetCount);
428
417 return true; 429 return true;
418 } 430 }
419 431
420 void NTPSnippetsService::LoadSnippetsFromPrefs() { 432 void NTPSnippetsService::LoadSnippetsFromPrefs() {
421 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); 433 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets));
422 DCHECK(success) << "Failed to parse snippets from prefs"; 434 DCHECK(success) << "Failed to parse snippets from prefs";
423 435
424 LoadingSnippetsFinished(); 436 LoadingSnippetsFinished();
425 } 437 }
426 438
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 if (snippet->expiry_date() < next_expiry) 509 if (snippet->expiry_date() < next_expiry)
498 next_expiry = snippet->expiry_date(); 510 next_expiry = snippet->expiry_date();
499 } 511 }
500 DCHECK_GT(next_expiry, expiry); 512 DCHECK_GT(next_expiry, expiry);
501 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, 513 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
502 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, 514 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished,
503 base::Unretained(this))); 515 base::Unretained(this)));
504 } 516 }
505 517
506 } // namespace ntp_snippets 518 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698