diff --git a/browser/BUILD.gn b/browser/BUILD.gn index 8570bf9bee32..3d9db7b046a2 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -23,6 +23,8 @@ source_set("browser_process") { "brave_stats_updater.h", "brave_stats_updater_params.cc", "brave_stats_updater_params.h", + "brave_stats_updater_util.cc", + "brave_stats_updater_util.h", "brave_tab_helpers.cc", "brave_tab_helpers.h", "brave_rewards/donations_dialog.cc", diff --git a/browser/brave_stats_updater.cc b/browser/brave_stats_updater.cc index 9bc4e7d26636..ee2ca2de05a3 100644 --- a/browser/brave_stats_updater.cc +++ b/browser/brave_stats_updater.cc @@ -6,6 +6,7 @@ #include "base/sys_info.h" #include "brave/browser/brave_stats_updater_params.h" +#include "brave/browser/brave_stats_updater_util.h" #include "brave/browser/version_info.h" #include "brave/common/pref_names.h" #include "chrome/browser/browser_process.h" @@ -25,8 +26,9 @@ // Ping the update server shortly after startup (units are seconds). const int kUpdateServerStartupPingDelay = 3; -// Ping the update server once an hour (units are seconds). -const int kUpdateServerPeriodicPingFrequency = 60 * 60; +// Every five minutes, check if we need to ping the update server for +// today (units are seconds). +const int kUpdateServerPeriodicPingFrequency = 5 * 60; namespace { @@ -112,7 +114,7 @@ void BraveStatsUpdater::Start() { server_ping_periodic_timer_->Start( FROM_HERE, base::TimeDelta::FromSeconds(kUpdateServerPeriodicPingFrequency), this, - &BraveStatsUpdater::OnServerPingPeriodicTimerFired); + &BraveStatsUpdater::OnServerPingTimerFired); DCHECK(server_ping_periodic_timer_->IsRunning()); } @@ -155,11 +157,13 @@ void BraveStatsUpdater::OnSimpleLoaderComplete( VLOG(1) << "Brave stats ping, url: " << final_url.spec(); } -void BraveStatsUpdater::OnServerPingStartupTimerFired() { - SendServerPing(); -} +void BraveStatsUpdater::OnServerPingTimerFired() { + // If we already pinged the stats server today, then we're done. + std::string today_ymd = brave::GetDateAsYMD(base::Time::Now()); + std::string last_check_ymd = pref_service_->GetString(kLastCheckYMD); + if (base::CompareCaseInsensitiveASCII(today_ymd, last_check_ymd) == 0) + return; -void BraveStatsUpdater::OnServerPingPeriodicTimerFired() { SendServerPing(); } @@ -170,7 +174,7 @@ void BraveStatsUpdater::OnReferralCheckedForPromoCodeFileChanged() { void BraveStatsUpdater::StartServerPingStartupTimer() { server_ping_startup_timer_->Start( FROM_HERE, base::TimeDelta::FromSeconds(kUpdateServerStartupPingDelay), - this, &BraveStatsUpdater::OnServerPingStartupTimerFired); + this, &BraveStatsUpdater::OnServerPingTimerFired); DCHECK(server_ping_startup_timer_->IsRunning()); } diff --git a/browser/brave_stats_updater.h b/browser/brave_stats_updater.h index 599fd3d98ef9..8d514ca81f1e 100644 --- a/browser/brave_stats_updater.h +++ b/browser/brave_stats_updater.h @@ -53,11 +53,8 @@ class BraveStatsUpdater { std::unique_ptr stats_updater_params, scoped_refptr headers); - // Invoked when server ping startup timer fires. - void OnServerPingStartupTimerFired(); - - // Invoked when server ping periodic timer fires. - void OnServerPingPeriodicTimerFired(); + // Invoked when server ping timer fires. + void OnServerPingTimerFired(); // Invoked when the specified referral preference changes. void OnReferralCheckedForPromoCodeFileChanged(); diff --git a/browser/brave_stats_updater_params.cc b/browser/brave_stats_updater_params.cc index bd9947aa4221..d7f97ee63f81 100644 --- a/browser/brave_stats_updater_params.cc +++ b/browser/brave_stats_updater_params.cc @@ -6,9 +6,9 @@ #include "brave/browser/brave_stats_updater_params.h" -#include "base/strings/stringprintf.h" #include "base/strings/string_util.h" #include "base/time/time.h" +#include "brave/browser/brave_stats_updater_util.h" #include "brave/common/pref_names.h" #include "components/prefs/pref_service.h" @@ -83,15 +83,8 @@ std::string BraveStatsUpdaterParams::BooleanToString(bool bool_value) const { return bool_value ? "true" : "false"; } -std::string BraveStatsUpdaterParams::GetDateAsYMD(const base::Time& time) const { - base::Time::Exploded exploded; - time.LocalExplode(&exploded); - return base::StringPrintf("%d-%02d-%02d", exploded.year, exploded.month, - exploded.day_of_month); -} - std::string BraveStatsUpdaterParams::GetCurrentDateAsYMD() const { - return GetDateAsYMD(g_current_time); + return brave::GetDateAsYMD(g_current_time); } std::string BraveStatsUpdaterParams::GetLastMondayAsYMD() const { @@ -104,7 +97,7 @@ std::string BraveStatsUpdaterParams::GetLastMondayAsYMD() const { now.ToJsTime() - ((exploded.day_of_week - 1) * base::Time::kMillisecondsPerDay)); - return GetDateAsYMD(last_monday); + return brave::GetDateAsYMD(last_monday); } int BraveStatsUpdaterParams::GetCurrentMonth() const { diff --git a/browser/brave_stats_updater_util.cc b/browser/brave_stats_updater_util.cc new file mode 100644 index 000000000000..d0d7682cb341 --- /dev/null +++ b/browser/brave_stats_updater_util.cc @@ -0,0 +1,18 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/brave_stats_updater_util.h" + +#include "base/strings/stringprintf.h" + +namespace brave { + +std::string GetDateAsYMD(const base::Time& time) { + base::Time::Exploded exploded; + time.LocalExplode(&exploded); + return base::StringPrintf("%d-%02d-%02d", exploded.year, exploded.month, + exploded.day_of_month); +} + +} // namespace brave diff --git a/browser/brave_stats_updater_util.h b/browser/brave_stats_updater_util.h new file mode 100644 index 000000000000..bb862824077c --- /dev/null +++ b/browser/brave_stats_updater_util.h @@ -0,0 +1,18 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_BRAVE_STATS_UPDATER_UTIL_H_ +#define BRAVE_BROWSER_BRAVE_STATS_UPDATER_UTIL_H_ + +#include + +#include "base/time/time.h" + +namespace brave { + +std::string GetDateAsYMD(const base::Time& time); + +} // namespace brave + +#endif // BRAVE_BROWSER_BRAVE_STATS_UPDATER_UTIL_H_