1 // Copyright 2014 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 CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_INTERCEPTOR_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_INTERCEPTOR_H_
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/timer/timer.h"
17 class DevToolsNetworkConditions
;
18 class DevToolsNetworkTransaction
;
25 // DevToolsNetworkInterceptor emulates network conditions for transactions with
26 // specific client id.
27 class DevToolsNetworkInterceptor
{
30 DevToolsNetworkInterceptor();
31 virtual ~DevToolsNetworkInterceptor();
33 base::WeakPtr
<DevToolsNetworkInterceptor
> GetWeakPtr();
35 // Applies network emulation configuration.
36 void UpdateConditions(scoped_ptr
<DevToolsNetworkConditions
> conditions
);
38 void AddTransaction(DevToolsNetworkTransaction
* transaction
);
39 void RemoveTransaction(DevToolsNetworkTransaction
* transaction
);
41 bool ShouldFail(const DevToolsNetworkTransaction
* transaction
);
42 bool ShouldThrottle(const DevToolsNetworkTransaction
* transaction
);
43 void ThrottleTransaction(DevToolsNetworkTransaction
* transaction
, bool start
);
45 const DevToolsNetworkConditions
* conditions() const {
46 return conditions_
.get();
50 scoped_ptr
<DevToolsNetworkConditions
> conditions_
;
52 void UpdateThrottledTransactions(base::TimeTicks now
);
53 void UpdateSuspendedTransactions(base::TimeTicks now
);
54 void ArmTimer(base::TimeTicks now
);
57 void FireThrottledCallback(DevToolsNetworkTransaction
* transaction
);
59 typedef std::set
<DevToolsNetworkTransaction
*> Transactions
;
60 Transactions transactions_
;
62 // Transactions suspended for a "latency" period.
63 typedef std::pair
<DevToolsNetworkTransaction
*, int64_t> SuspendedTransaction
;
64 typedef std::vector
<SuspendedTransaction
> SuspendedTransactions
;
65 SuspendedTransactions suspended_transactions_
;
67 // Transactions waiting certain amount of transfer to be "accounted".
68 std::vector
<DevToolsNetworkTransaction
*> throttled_transactions_
;
70 base::OneShotTimer
<DevToolsNetworkInterceptor
> timer_
;
71 base::TimeTicks offset_
;
72 base::TimeDelta tick_length_
;
73 base::TimeDelta latency_length_
;
76 base::WeakPtrFactory
<DevToolsNetworkInterceptor
> weak_ptr_factory_
;
78 DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkInterceptor
);
81 #endif // CHROME_BROWSER_DEVTOOLS_DEVTOOLS_NETWORK_INTERCEPTOR_H_