1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_PROXY_LOAD_STATE_CHANGE_COALESCER_H_
6 #define NET_PROXY_LOAD_STATE_CHANGE_COALESCER_H_
8 #include "base/cancelable_callback.h"
9 #include "base/time/time.h"
10 #include "base/timer/timer.h"
11 #include "net/base/load_states.h"
15 // A class that coalesces LoadState changes. When a new LoadState is reported,
16 // it becomes the pending LoadState and is queued for |timeout|. If the timeout
17 // elapses without another new LoadState, the pending LoadState becomes the
18 // committed LoadState and the callback is called with that LoadState. If a new
19 // LoadState is reported before the timeout has elapsed, the pending LoadState
20 // is discarded and the new LoadState becomes the new pending LoadState, unless
21 // it's the same as the committed LoadState, in which case no pending LoadState
23 class LoadStateChangeCoalescer
{
25 LoadStateChangeCoalescer(const base::Callback
<void(LoadState
)>& callback
,
26 const base::TimeDelta
& timeout
,
27 LoadState initial_load_state
);
28 ~LoadStateChangeCoalescer();
30 // Adds a LoadState change to the pipeline. If it isn't coalesced, |callback_|
31 // will be called with |load_state| after |timeout_|.
32 void LoadStateChanged(LoadState load_state
);
35 void SendLoadStateChanged();
37 // The callback to call to report a LoadState change.
38 const base::Callback
<void(LoadState
)> callback_
;
40 // The amount of time for which a LoadState change can be coalesced.
41 const base::TimeDelta timeout_
;
43 base::OneShotTimer
<LoadStateChangeCoalescer
> timer_
;
44 LoadState committed_load_state_
;
45 LoadState pending_load_state_
;
47 DISALLOW_COPY_AND_ASSIGN(LoadStateChangeCoalescer
);
52 #endif // NET_PROXY_LOAD_STATE_CHANGE_COALESCER_H_