chromium/components/affiliations/core/browser/affiliation_fetch_throttler.h

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AFFILIATIONS_CORE_BROWSER_AFFILIATION_FETCH_THROTTLER_H_
#define COMPONENTS_AFFILIATIONS_CORE_BROWSER_AFFILIATION_FETCH_THROTTLER_H_

#include <stdint.h>

#include <memory>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "net/base/backoff_entry.h"
#include "services/network/public/cpp/network_connection_tracker.h"

namespace base {
class TickClock;
class SequencedTaskRunner;
}  // namespace base

namespace affiliations {

class AffiliationFetchThrottlerDelegate;

// Implements the throttling logic that the AffiliationBackend will use when it
// needs to issue requests over the network to fetch affiliation information.
//
// This class manages only the scheduling of the requests. It is up to the
// consumer (the AffiliationBackend) to actually assemble and send the requests,
// to report back about their success or failure, and to retry them if desired.
// The process goes like this:
//   1.) The consumer calls SignalNetworkRequestNeeded().
//   2.) Once appropriate, OnCanSendNetworkRequest() is called on the delegate.
//   3.) The consumer sends the request, and waits until it completes.
//   4.) The consumer calls InformOfNetworkRequestComplete().
// Note that only a single request at a time is supported.
//
// If the request fails in Step 3, the consumer should not automatically retry
// it. Instead it should always proceed to Step 4, and then -- if retrying the
// request is desired -- proceed immediately to Step 1. That is, it should act
// as if another request was needed right away.
//
// Essentially, this class implements exponential backoff in case of network and
// server errors with the additional constraint that no requests will be issued
// in the first place while there is known to be no network connectivity. This
// prevents the exponential backoff delay from growing huge during long offline
// periods, so that requests will not be held back for too long after
// connectivity is restored.
class AffiliationFetchThrottler
    : public network::NetworkConnectionTracker::NetworkConnectionObserver {};

}  // namespace affiliations

#endif  // COMPONENTS_AFFILIATIONS_CORE_BROWSER_AFFILIATION_FETCH_THROTTLER_H_