2015-09-26 18:19:27 +00:00
|
|
|
// 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 BROWSER_DEVTOOLS_NETWORK_INTERCEPTOR_H_
|
|
|
|
#define BROWSER_DEVTOOLS_NETWORK_INTERCEPTOR_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base/macros.h"
|
|
|
|
#include "base/timer/timer.h"
|
|
|
|
|
|
|
|
namespace base {
|
|
|
|
class TimeDelta;
|
|
|
|
class TimeTicks;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
|
|
|
class DevToolsNetworkConditions;
|
|
|
|
class DevToolsNetworkTransaction;
|
|
|
|
|
|
|
|
class DevToolsNetworkInterceptor {
|
|
|
|
public:
|
2016-03-13 21:05:22 +00:00
|
|
|
using ThrottleCallback = base::Callback<void(int, int64_t)>;
|
|
|
|
|
2015-09-26 18:19:27 +00:00
|
|
|
DevToolsNetworkInterceptor();
|
|
|
|
virtual ~DevToolsNetworkInterceptor();
|
|
|
|
|
|
|
|
base::WeakPtr<DevToolsNetworkInterceptor> GetWeakPtr();
|
|
|
|
|
|
|
|
// Applies network emulation configuration.
|
2016-05-23 01:59:07 +00:00
|
|
|
void UpdateConditions(std::unique_ptr<DevToolsNetworkConditions> conditions);
|
2015-09-26 18:19:27 +00:00
|
|
|
|
2016-03-13 21:05:22 +00:00
|
|
|
// Throttles with |is_upload == true| always succeed, even in offline mode.
|
|
|
|
int StartThrottle(int result,
|
|
|
|
int64_t bytes,
|
|
|
|
base::TimeTicks send_end,
|
|
|
|
bool start,
|
|
|
|
bool is_upload,
|
|
|
|
const ThrottleCallback& callback);
|
|
|
|
void StopThrottle(const ThrottleCallback& callback);
|
2015-09-26 18:19:27 +00:00
|
|
|
|
2016-03-13 21:05:22 +00:00
|
|
|
bool IsOffline();
|
2015-09-26 18:19:27 +00:00
|
|
|
|
|
|
|
private:
|
2016-03-13 21:05:22 +00:00
|
|
|
struct ThrottleRecord {
|
|
|
|
public:
|
|
|
|
ThrottleRecord();
|
|
|
|
ThrottleRecord(const ThrottleRecord& other);
|
|
|
|
~ThrottleRecord();
|
|
|
|
|
|
|
|
int result;
|
|
|
|
int64_t bytes;
|
|
|
|
int64_t send_end;
|
|
|
|
bool is_upload;
|
|
|
|
ThrottleCallback callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
using ThrottleRecords = std::vector<ThrottleRecord>;
|
|
|
|
|
|
|
|
void FinishRecords(ThrottleRecords* records, bool offline);
|
|
|
|
|
|
|
|
uint64_t UpdateThrottledRecords(base::TimeTicks now,
|
|
|
|
ThrottleRecords* records,
|
|
|
|
uint64_t last_tick,
|
|
|
|
base::TimeDelta tick_length);
|
|
|
|
void UpdateThrottled(base::TimeTicks now);
|
|
|
|
void UpdateSuspended(base::TimeTicks now);
|
|
|
|
|
|
|
|
void CollectFinished(ThrottleRecords* records, ThrottleRecords* finished);
|
2015-09-26 18:19:27 +00:00
|
|
|
void OnTimer();
|
|
|
|
|
2016-03-13 21:05:22 +00:00
|
|
|
base::TimeTicks CalculateDesiredTime(const ThrottleRecords& records,
|
|
|
|
uint64_t last_tick,
|
|
|
|
base::TimeDelta tick_length);
|
|
|
|
void ArmTimer(base::TimeTicks now);
|
2015-09-26 18:19:27 +00:00
|
|
|
|
2016-03-13 21:05:22 +00:00
|
|
|
void RemoveRecord(ThrottleRecords* records, const ThrottleCallback& callback);
|
|
|
|
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<DevToolsNetworkConditions> conditions_;
|
2015-09-26 18:19:27 +00:00
|
|
|
|
2016-03-13 21:05:22 +00:00
|
|
|
// Throttables suspended for a "latency" period.
|
|
|
|
ThrottleRecords suspended_;
|
2015-09-26 18:19:27 +00:00
|
|
|
|
2016-03-13 21:05:22 +00:00
|
|
|
// Throttables waiting for certain amount of transfer to be "accounted".
|
|
|
|
ThrottleRecords download_;
|
|
|
|
ThrottleRecords upload_;
|
2015-09-26 18:19:27 +00:00
|
|
|
|
2015-12-07 11:55:01 +00:00
|
|
|
base::OneShotTimer timer_;
|
2015-09-26 18:19:27 +00:00
|
|
|
base::TimeTicks offset_;
|
2016-03-13 21:05:22 +00:00
|
|
|
base::TimeDelta download_tick_length_;
|
|
|
|
base::TimeDelta upload_tick_length_;
|
2015-09-26 18:19:27 +00:00
|
|
|
base::TimeDelta latency_length_;
|
2016-03-13 21:05:22 +00:00
|
|
|
uint64_t download_last_tick_;
|
|
|
|
uint64_t upload_last_tick_;
|
2015-09-26 18:19:27 +00:00
|
|
|
|
|
|
|
base::WeakPtrFactory<DevToolsNetworkInterceptor> weak_ptr_factory_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkInterceptor);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace brightray
|
|
|
|
|
|
|
|
#endif // BROWSER_DEVTOOLS_NETWORK_INTERCEPTOR_H_
|