OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "net/base/load_state_change_coalescer.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 | |
10 namespace net { | |
11 | |
12 LoadStateChangeCoalescer::LoadStateChangeCoalescer( | |
13 const base::Callback<void(LoadState)>& callback, | |
14 const base::TimeDelta& timeout, | |
15 LoadState initial_load_state) | |
16 : callback_(callback), | |
17 timeout_(timeout), | |
18 committed_load_state_(initial_load_state), | |
19 pending_load_state_(initial_load_state) { | |
20 } | |
21 | |
22 void LoadStateChangeCoalescer::LoadStateChanged(LoadState load_state) { | |
23 if (load_state == committed_load_state_) { | |
24 pending_load_state_change_.Cancel(); | |
eroman
2015/03/06 05:13:01
Personally I think a OneShotTimer would be easier
Sam McNally
2015/03/06 09:33:02
Done.
| |
25 return; | |
26 } | |
27 pending_load_state_ = load_state; | |
28 pending_load_state_change_.Reset(base::Bind( | |
eroman
2015/03/06 05:13:01
Won't this mean that If the load state is continuo
Sam McNally
2015/03/06 09:33:02
If we use a maximum delay, a continuously changing
| |
29 &LoadStateChangeCoalescer::SendLoadStateChanged, base::Unretained(this))); | |
30 base::MessageLoop::current()->PostDelayedTask( | |
31 FROM_HERE, pending_load_state_change_.callback(), timeout_); | |
32 } | |
33 | |
34 LoadStateChangeCoalescer::~LoadStateChangeCoalescer() = default; | |
35 | |
36 void LoadStateChangeCoalescer::SendLoadStateChanged() { | |
37 callback_.Run(pending_load_state_); | |
38 pending_load_state_change_.Cancel(); | |
39 committed_load_state_ = pending_load_state_; | |
40 } | |
41 | |
42 } // namespace net | |
OLD | NEW |