| OLD | NEW |
| 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 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "components/image_fetcher/image_fetcher.h" | 22 #include "components/image_fetcher/image_fetcher.h" |
| 23 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 23 #include "components/ntp_snippets/pref_names.h" | 24 #include "components/ntp_snippets/pref_names.h" |
| 24 #include "components/ntp_snippets/switches.h" | 25 #include "components/ntp_snippets/switches.h" |
| 25 #include "components/prefs/pref_registry_simple.h" | 26 #include "components/prefs/pref_registry_simple.h" |
| 26 #include "components/prefs/pref_service.h" | 27 #include "components/prefs/pref_service.h" |
| 27 #include "components/suggestions/proto/suggestions.pb.h" | 28 #include "components/suggestions/proto/suggestions.pb.h" |
| 29 #include "components/variations/variations_associated_data.h" |
| 28 | 30 |
| 29 using image_fetcher::ImageFetcher; | 31 using image_fetcher::ImageFetcher; |
| 30 using suggestions::ChromeSuggestion; | 32 using suggestions::ChromeSuggestion; |
| 31 using suggestions::SuggestionsProfile; | 33 using suggestions::SuggestionsProfile; |
| 32 using suggestions::SuggestionsService; | 34 using suggestions::SuggestionsService; |
| 33 | 35 |
| 34 namespace ntp_snippets { | 36 namespace ntp_snippets { |
| 35 | 37 |
| 36 namespace { | 38 namespace { |
| 37 | 39 |
| 38 // Number of snippets requested to the server. Consider replacing sparse UMA | 40 // Number of snippets requested to the server. Consider replacing sparse UMA |
| 39 // histograms with COUNTS() if this number increases beyond 50. | 41 // histograms with COUNTS() if this number increases beyond 50. |
| 40 const int kMaxSnippetCount = 10; | 42 const int kMaxSnippetCount = 10; |
| 41 | 43 |
| 42 const int kFetchingIntervalWifiChargingSeconds = 30 * 60; | 44 // Default values for snippets fetching intervals. |
| 43 const int kFetchingIntervalWifiSeconds = 2 * 60 * 60; | 45 const int kDefaultFetchingIntervalWifiChargingSeconds = 30 * 60; |
| 44 const int kFetchingIntervalFallbackSeconds = 24 * 60 * 60; | 46 const int kDefaultFetchingIntervalWifiSeconds = 2 * 60 * 60; |
| 47 const int kDefaultFetchingIntervalFallbackSeconds = 24 * 60 * 60; |
| 48 |
| 49 // Variation parameters than can override the default fetching intervals. |
| 50 const char kFetchingIntervalWifiChargingParamName[] = |
| 51 "fetching_interval_wifi_charging_seconds"; |
| 52 const char kFetchingIntervalWifiParamName[] = |
| 53 "fetching_interval_wifi_seconds"; |
| 54 const char kFetchingIntervalFallbackParamName[] = |
| 55 "fetching_interval_fallback_seconds"; |
| 45 | 56 |
| 46 // These define the times of day during which we will fetch via Wifi (without | 57 // These define the times of day during which we will fetch via Wifi (without |
| 47 // charging) - 6 AM to 10 PM. | 58 // charging) - 6 AM to 10 PM. |
| 48 const int kWifiFetchingHourMin = 6; | 59 const int kWifiFetchingHourMin = 6; |
| 49 const int kWifiFetchingHourMax = 22; | 60 const int kWifiFetchingHourMax = 22; |
| 50 | 61 |
| 51 const int kDefaultExpiryTimeMins = 24 * 60; | 62 const int kDefaultExpiryTimeMins = 24 * 60; |
| 52 | 63 |
| 53 base::TimeDelta GetFetchingInterval(const char* switch_name, | 64 base::TimeDelta GetFetchingInterval(const char* switch_name, |
| 65 const char* param_name, |
| 54 int default_value_seconds) { | 66 int default_value_seconds) { |
| 55 int value_seconds = default_value_seconds; | 67 int value_seconds = default_value_seconds; |
| 68 |
| 69 // The default value can be overridden by a variation parameter. |
| 70 std::string param_value_str = variations::GetVariationParamValue( |
| 71 ntp_snippets::kStudyName, param_name); |
| 72 int param_value_seconds = 0; |
| 73 if (base::StringToInt(param_value_str, ¶m_value_seconds)) |
| 74 value_seconds = param_value_seconds; |
| 75 else |
| 76 LOG(WARNING) << "Invalid value for variation parameter " << param_name; |
| 77 |
| 78 // A value from the command line parameter overrides anything else. |
| 56 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess(); | 79 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess(); |
| 57 if (cmdline.HasSwitch(switch_name)) { | 80 if (cmdline.HasSwitch(switch_name)) { |
| 58 std::string str = cmdline.GetSwitchValueASCII(switch_name); | 81 std::string str = cmdline.GetSwitchValueASCII(switch_name); |
| 59 int switch_value_seconds = 0; | 82 int switch_value_seconds = 0; |
| 60 if (base::StringToInt(str, &switch_value_seconds)) | 83 if (base::StringToInt(str, &switch_value_seconds)) |
| 61 value_seconds = switch_value_seconds; | 84 value_seconds = switch_value_seconds; |
| 62 else | 85 else |
| 63 LOG(WARNING) << "Invalid value for switch " << switch_name; | 86 LOG(WARNING) << "Invalid value for switch " << switch_name; |
| 64 } | 87 } |
| 65 return base::TimeDelta::FromSeconds(value_seconds); | 88 return base::TimeDelta::FromSeconds(value_seconds); |
| 66 } | 89 } |
| 67 | 90 |
| 68 base::TimeDelta GetFetchingIntervalWifiCharging() { | 91 base::TimeDelta GetFetchingIntervalWifiCharging() { |
| 69 return GetFetchingInterval(switches::kFetchingIntervalWifiChargingSeconds, | 92 return GetFetchingInterval(switches::kFetchingIntervalWifiChargingSeconds, |
| 70 kFetchingIntervalWifiChargingSeconds); | 93 kFetchingIntervalWifiChargingParamName, |
| 94 kDefaultFetchingIntervalWifiChargingSeconds); |
| 71 } | 95 } |
| 72 | 96 |
| 73 base::TimeDelta GetFetchingIntervalWifi(const base::Time& now) { | 97 base::TimeDelta GetFetchingIntervalWifi(const base::Time& now) { |
| 74 // Only fetch via Wifi (without charging) during the proper times of day. | 98 // Only fetch via Wifi (without charging) during the proper times of day. |
| 75 base::Time::Exploded exploded; | 99 base::Time::Exploded exploded; |
| 76 now.LocalExplode(&exploded); | 100 now.LocalExplode(&exploded); |
| 77 if (kWifiFetchingHourMin <= exploded.hour && | 101 if (kWifiFetchingHourMin <= exploded.hour && |
| 78 exploded.hour < kWifiFetchingHourMax) { | 102 exploded.hour < kWifiFetchingHourMax) { |
| 79 return GetFetchingInterval(switches::kFetchingIntervalWifiSeconds, | 103 return GetFetchingInterval(switches::kFetchingIntervalWifiSeconds, |
| 80 kFetchingIntervalWifiSeconds); | 104 kFetchingIntervalWifiParamName, |
| 105 kDefaultFetchingIntervalWifiSeconds); |
| 81 } | 106 } |
| 82 return base::TimeDelta(); | 107 return base::TimeDelta(); |
| 83 } | 108 } |
| 84 | 109 |
| 85 base::TimeDelta GetFetchingIntervalFallback() { | 110 base::TimeDelta GetFetchingIntervalFallback() { |
| 86 return GetFetchingInterval(switches::kFetchingIntervalFallbackSeconds, | 111 return GetFetchingInterval(switches::kFetchingIntervalFallbackSeconds, |
| 87 kFetchingIntervalFallbackSeconds); | 112 kFetchingIntervalFallbackParamName, |
| 113 kDefaultFetchingIntervalFallbackSeconds); |
| 88 } | 114 } |
| 89 | 115 |
| 90 base::Time GetRescheduleTime(const base::Time& now) { | 116 base::Time GetRescheduleTime(const base::Time& now) { |
| 91 base::Time::Exploded exploded; | 117 base::Time::Exploded exploded; |
| 92 now.LocalExplode(&exploded); | 118 now.LocalExplode(&exploded); |
| 93 // The scheduling changes at both |kWifiFetchingHourMin| and | 119 // The scheduling changes at both |kWifiFetchingHourMin| and |
| 94 // |kWifiFetchingHourMax|. Find the time of the next one that we'll hit. | 120 // |kWifiFetchingHourMax|. Find the time of the next one that we'll hit. |
| 95 bool next_day = false; | 121 bool next_day = false; |
| 96 if (exploded.hour < kWifiFetchingHourMin) { | 122 if (exploded.hour < kWifiFetchingHourMin) { |
| 97 exploded.hour = kWifiFetchingHourMin; | 123 exploded.hour = kWifiFetchingHourMin; |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 if (snippet->expiry_date() < next_expiry) | 534 if (snippet->expiry_date() < next_expiry) |
| 509 next_expiry = snippet->expiry_date(); | 535 next_expiry = snippet->expiry_date(); |
| 510 } | 536 } |
| 511 DCHECK_GT(next_expiry, expiry); | 537 DCHECK_GT(next_expiry, expiry); |
| 512 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 538 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 513 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 539 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 514 base::Unretained(this))); | 540 base::Unretained(this))); |
| 515 } | 541 } |
| 516 | 542 |
| 517 } // namespace ntp_snippets | 543 } // namespace ntp_snippets |
| OLD | NEW |