summaryrefslogtreecommitdiffstats
path: root/net/proxy/load_state_change_coalescer.h
blob: 642538523cf57cf98b43122f64556a337f0997ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2015 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 NET_PROXY_LOAD_STATE_CHANGE_COALESCER_H_
#define NET_PROXY_LOAD_STATE_CHANGE_COALESCER_H_

#include "base/cancelable_callback.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "net/base/load_states.h"

namespace net {

// A class that coalesces LoadState changes. When a new LoadState is reported,
// it becomes the pending LoadState and is queued for |timeout|. If the timeout
// elapses without another new LoadState, the pending LoadState becomes the
// committed LoadState and the callback is called with that LoadState. If a new
// LoadState is reported before the timeout has elapsed, the pending LoadState
// is discarded and the new LoadState becomes the new pending LoadState, unless
// it's the same as the committed LoadState, in which case no pending LoadState
// is queued.
class LoadStateChangeCoalescer {
 public:
  LoadStateChangeCoalescer(const base::Callback<void(LoadState)>& callback,
                           const base::TimeDelta& timeout,
                           LoadState initial_load_state);
  ~LoadStateChangeCoalescer();

  // Adds a LoadState change to the pipeline. If it isn't coalesced, |callback_|
  // will be called with |load_state| after |timeout_|.
  void LoadStateChanged(LoadState load_state);

 private:
  void SendLoadStateChanged();

  // The callback to call to report a LoadState change.
  const base::Callback<void(LoadState)> callback_;

  // The amount of time for which a LoadState change can be coalesced.
  const base::TimeDelta timeout_;

  base::OneShotTimer<LoadStateChangeCoalescer> timer_;
  LoadState committed_load_state_;
  LoadState pending_load_state_;

  DISALLOW_COPY_AND_ASSIGN(LoadStateChangeCoalescer);
};

}  // namespace net

#endif  // NET_PROXY_LOAD_STATE_CHANGE_COALESCER_H_