Commit 0b03c606 authored by Robert Ogden's avatar Robert Ogden Committed by Commit Bot

Add a repeating probe functionality to AvailabilityProber

This will be used for litepages in a follow up CL.

Bug: 971918
Change-Id: I7faf3f8e0bc51fa1fff8235226f5832876c7492a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1730767Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Commit-Queue: Robert Ogden <robertogden@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683276}
parent 6828a386
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "chrome/browser/availability/availability_prober.h" #include "chrome/browser/availability/availability_prober.h"
#include <math.h> #include <math.h>
#include <cmath> #include <cmath>
#include "base/base64.h" #include "base/base64.h"
...@@ -347,6 +348,19 @@ void AvailabilityProber::SendNowIfInactive(bool send_only_in_foreground) { ...@@ -347,6 +348,19 @@ void AvailabilityProber::SendNowIfInactive(bool send_only_in_foreground) {
CreateAndStartURLLoader(); CreateAndStartURLLoader();
} }
void AvailabilityProber::RepeatedlyProbe(base::TimeDelta interval,
bool send_only_in_foreground) {
repeating_timer_ = std::make_unique<base::RepeatingTimer>(tick_clock_);
// base::Unretained is safe here because |repeating_timer_| is owned by
// |this|.
repeating_timer_->Start(
FROM_HERE, interval,
base::BindRepeating(&AvailabilityProber::SendNowIfInactive,
base::Unretained(this), send_only_in_foreground));
SendNowIfInactive(send_only_in_foreground);
}
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
void AvailabilityProber::OnApplicationStateChange( void AvailabilityProber::OnApplicationStateChange(
base::android::ApplicationState new_state) { base::android::ApplicationState new_state) {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROME_BROWSER_AVAILABILITY_AVAILABILITY_PROBER_H_ #define CHROME_BROWSER_AVAILABILITY_AVAILABILITY_PROBER_H_
#include <stdint.h> #include <stdint.h>
#include <memory> #include <memory>
#include <string> #include <string>
...@@ -156,6 +157,12 @@ class AvailabilityProber ...@@ -156,6 +157,12 @@ class AvailabilityProber
// is in the foreground (work on Android only). // is in the foreground (work on Android only).
void SendNowIfInactive(bool send_only_in_foreground); void SendNowIfInactive(bool send_only_in_foreground);
// Calls |SendNowIfInactive| immediately with |send_only_in_foreground| and
// repeats every |interval|. Calling this multiple times with different
// |interval| will change the interval to the most recently provided value.
// Only stops when |this| is deleted.
void RepeatedlyProbe(base::TimeDelta interval, bool send_only_in_foreground);
// Returns the successfulness of the last probe, if there was one. If the last // Returns the successfulness of the last probe, if there was one. If the last
// probe status was cached and needs to be revalidated, this may activate the // probe status was cached and needs to be revalidated, this may activate the
// prober. // prober.
...@@ -254,6 +261,9 @@ class AvailabilityProber ...@@ -254,6 +261,9 @@ class AvailabilityProber
// If a probe is being attempted, this will be running until the TTL. // If a probe is being attempted, this will be running until the TTL.
std::unique_ptr<base::OneShotTimer> timeout_timer_; std::unique_ptr<base::OneShotTimer> timeout_timer_;
// If we are repeatedly probing, this will be running.
std::unique_ptr<base::RepeatingTimer> repeating_timer_;
// Caches past probe results in a mapping of one tuple to another: // Caches past probe results in a mapping of one tuple to another:
// (network_id, url_) -> (last_probe_status, last_modification_time). // (network_id, url_) -> (last_probe_status, last_modification_time).
// No more than |max_cache_entries_| will be kept in this dictionary. // No more than |max_cache_entries_| will be kept in this dictionary.
......
...@@ -851,3 +851,24 @@ TEST_F(AvailabilityProberTest, CacheEntryAge) { ...@@ -851,3 +851,24 @@ TEST_F(AvailabilityProberTest, CacheEntryAge) {
histogram_tester.ExpectTotalCount( histogram_tester.ExpectTotalCount(
"Availability.Prober.CacheEntryAge.Litepages", 2); "Availability.Prober.CacheEntryAge.Litepages", 2);
} }
TEST_F(AvailabilityProberTest, Repeating) {
base::HistogramTester histogram_tester;
std::unique_ptr<AvailabilityProber> prober = NewProber();
EXPECT_EQ(prober->LastProbeWasSuccessful(), base::nullopt);
prober->RepeatedlyProbe(base::TimeDelta::FromSeconds(1), false);
VerifyRequest();
MakeResponseAndWait(net::HTTP_OK, net::OK);
EXPECT_TRUE(prober->LastProbeWasSuccessful().value());
EXPECT_FALSE(prober->is_active());
FastForward(base::TimeDelta::FromSeconds(1));
EXPECT_TRUE(prober->is_active());
VerifyRequest();
MakeResponseAndWait(net::HTTP_OK, net::OK);
EXPECT_TRUE(prober->LastProbeWasSuccessful().value());
EXPECT_FALSE(prober->is_active());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment