Chromium Code Reviews| Index: components/password_manager/core/browser/affiliation_fetch_throttler.h |
| diff --git a/components/password_manager/core/browser/affiliation_fetch_throttler.h b/components/password_manager/core/browser/affiliation_fetch_throttler.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f90511c33de5001f8e8c9482ef5e4f398ee06a68 |
| --- /dev/null |
| +++ b/components/password_manager/core/browser/affiliation_fetch_throttler.h |
| @@ -0,0 +1,95 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCH_THROTTLER_H_ |
| +#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCH_THROTTLER_H_ |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "net/base/backoff_entry.h" |
|
mmenke
2015/01/14 19:25:07
Think we can forward declare this?
engedy
2015/01/15 19:16:42
Done.
|
| +#include "net/base/network_change_notifier.h" |
| + |
| +namespace base { |
| +class TickClock; |
| +class SingleThreadTaskRunner; |
| +} |
| + |
| +namespace password_manager { |
| + |
| +// A delegate to receive a callback once it is okay to issue a network request. |
|
mmenke
2015/01/14 19:25:07
This needs clearer documentation, since it's the f
engedy
2015/01/15 19:16:42
I have moved the delegate to its own file.
|
| +class AffiliationFetchThrottlerDelegate { |
|
mmenke
2015/01/14 19:25:07
option: Believe it's currently recommended these
engedy
2015/01/15 19:16:42
Done.
|
| + public: |
| + // Will be called once the throttling policy allows issuing a network request, |
| + // provided there has been at least one SignalNetworkRequestNeeded() call |
| + // since the last request. The implementor should return whether a request was |
|
mmenke
2015/01/14 19:25:07
nit: implementor -> implemention
Should avoid am
engedy
2015/01/15 19:16:42
Done.
|
| + // actually initiated, and if so, should call InformOfNetworkRequestComplete() |
|
mmenke
2015/01/14 19:25:07
nit: should -> must (Maybe I've been reading too
engedy
2015/01/15 19:16:42
Done.
|
| + // when the request completes. |
| + virtual bool OnCanSendNetworkRequest() = 0; |
| + |
| + protected: |
| + virtual ~AffiliationFetchThrottlerDelegate() {} |
| +}; |
| + |
| +// Implements the throttling logic that AffiliationBackend will use when it |
| +// needs to issue requests to fetch affiliation information over the network. |
| +// |
| +// Essentially, the logic implements exponential backoff on network and server |
| +// errors, plus that no requests will be issued in the first place while there |
| +// is known to be no network connectivity. |
| +class AffiliationFetchThrottler |
| + : public net::NetworkChangeNotifier::ConnectionTypeObserver { |
|
mmenke
2015/01/14 19:25:07
I wonder about whether we really should be using N
engedy
2015/01/15 19:16:42
Yes, even as a signal that is known to produce fal
|
| + public: |
| + // Creates an instance that will use |tick_clock| as its tick source, and will |
| + // post to |task_runner| to call the |delegate|'s OnSendNetworkRequest(). |
| + AffiliationFetchThrottler( |
| + AffiliationFetchThrottlerDelegate* delegate, |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| + scoped_ptr<base::TickClock> tick_clock); |
| + ~AffiliationFetchThrottler() override; |
| + |
| + // Signals to the throttling logic that a network request is needed, and that |
| + // OnCanSendNetworkRequest() should be called as soon as the request can be |
| + // sent. Calls to this method will be ignored while a request is in flight. |
| + void SignalNetworkRequestNeeded(); |
| + |
| + // Informs the back-off logic that a network request has been completed, with |
| + // either |success| or not. |
| + void InformOfNetworkRequestComplete(bool success); |
| + |
| + protected: |
| + enum State { |
| + IDLE, |
| + FETCH_NEEDED, |
| + FETCH_IN_FLIGHT |
| + }; |
| + |
| + // Ensures that OnBackoffDelayExpiredCallback() is scheduled to be called back |
| + // once the |exponential_backoff_| delay expires. |
| + void EnsureCallbackIsScheduled(); |
| + |
| + // Called back when the |exponential_backoff_| delay expires. |
| + void OnBackoffDelayExpiredCallback(); |
| + |
| + // net::NetworkChangeNotifier::ConnectionTypeObserver: |
| + virtual void OnConnectionTypeChanged( |
|
mmenke
2015/01/14 19:25:07
nit: --virtual
engedy
2015/01/15 19:16:42
Done.
|
| + net::NetworkChangeNotifier::ConnectionType type) override; |
| + |
| + private: |
| + AffiliationFetchThrottlerDelegate* delegate_; |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + |
| + State state_; |
| + bool has_network_connectivity_; |
| + bool is_fetch_scheduled_; |
| + scoped_ptr<base::TickClock> tick_clock_; |
| + scoped_ptr<net::BackoffEntry> exponential_backoff_; |
|
mmenke
2015/01/14 19:25:07
Need to include scoped_ptr.h
engedy
2015/01/15 19:16:42
Done.
|
| + base::WeakPtrFactory<AffiliationFetchThrottler> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AffiliationFetchThrottler); |
| +}; |
| + |
| +} // namespace password_manager |
| + |
| +#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_FETCH_THROTTLER_H_ |