From a3b40108e497f074ef7e874fb3c7acd70e79837b Mon Sep 17 00:00:00 2001 From: Sergey Yershov Date: Tue, 19 Jul 2016 16:04:37 +0300 Subject: [PATCH] [downloader] Mandatory disable cellular network downloading after one hour --- storage/downloading_policy.cpp | 21 +++++++++++++++++---- storage/downloading_policy.hpp | 23 +++++++++++++++-------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/storage/downloading_policy.cpp b/storage/downloading_policy.cpp index c5175ed82f..758f152af8 100644 --- a/storage/downloading_policy.cpp +++ b/storage/downloading_policy.cpp @@ -2,7 +2,21 @@ #include "platform/platform.hpp" -bool StorageDownloadingPolicy::IsDownloadingAllowed() const +void StorageDownloadingPolicy::EnableCellularDownload(bool enabled) +{ + m_cellularDownloadEnabled = enabled; + m_disableCellularTime = steady_clock::now() + hours(1); +} + +bool StorageDownloadingPolicy::IsCellularDownloadEnabled() +{ + if (m_cellularDownloadEnabled && steady_clock::now() > m_disableCellularTime) + m_cellularDownloadEnabled = false; + + return m_cellularDownloadEnabled; +} + +bool StorageDownloadingPolicy::IsDownloadingAllowed() { return !(GetPlatform().ConnectionStatus() == Platform::EConnectionType::CONNECTION_WWAN && !IsCellularDownloadEnabled()); @@ -14,8 +28,7 @@ void StorageDownloadingPolicy::ScheduleRetry(storage::TCountriesSet const & fail if (IsDownloadingAllowed() && !failedCountries.empty() && m_autoRetryCounter > 0) { m_downloadRetryFailed = false; - auto action = [this, func, failedCountries] - { + auto action = [this, func, failedCountries] { --m_autoRetryCounter; func(failedCountries); }; @@ -23,7 +36,7 @@ void StorageDownloadingPolicy::ScheduleRetry(storage::TCountriesSet const & fail } else { - if(!failedCountries.empty()) + if (!failedCountries.empty()) m_downloadRetryFailed = true; m_autoRetryCounter = kAutoRetryCounterMax; } diff --git a/storage/downloading_policy.hpp b/storage/downloading_policy.hpp index 5f9df010c1..78fd6119fa 100644 --- a/storage/downloading_policy.hpp +++ b/storage/downloading_policy.hpp @@ -11,7 +11,7 @@ class DownloadingPolicy { public: using TProcessFunc = function; - virtual bool IsDownloadingAllowed() const { return true; } + virtual bool IsDownloadingAllowed() { return true; } virtual void ScheduleRetry(storage::TCountriesSet const &, TProcessFunc const &) {} }; @@ -22,14 +22,21 @@ class StorageDownloadingPolicy : public DownloadingPolicy static size_t constexpr kAutoRetryCounterMax = 3; size_t m_autoRetryCounter = kAutoRetryCounterMax; my::DeferredTask m_autoRetryWorker; - + + time_point m_disableCellularTime; + public: StorageDownloadingPolicy() : m_autoRetryWorker(seconds(20)) {} - - inline void EnableCellularDownload(bool value) { m_cellularDownloadEnabled = value; } - inline bool IsCellularDownloadEnabled() const { return m_cellularDownloadEnabled; } - inline bool IsAutoRetryDownloadFailed() const { return m_downloadRetryFailed || m_autoRetryCounter == 0; } + void EnableCellularDownload(bool enabled); + bool IsCellularDownloadEnabled(); - bool IsDownloadingAllowed() const override; - void ScheduleRetry(storage::TCountriesSet const & failedCountries, TProcessFunc const & func) override; + inline bool IsAutoRetryDownloadFailed() const + { + return m_downloadRetryFailed || m_autoRetryCounter == 0; + } + + // DownloadingPolicy overrides: + bool IsDownloadingAllowed() override; + void ScheduleRetry(storage::TCountriesSet const & failedCountries, + TProcessFunc const & func) override; };