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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
// 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.
#include "chrome/browser/devtools/devtools_network_protocol_handler.h"
#include "base/values.h"
#include "chrome/browser/devtools/devtools_network_conditions.h"
#include "chrome/browser/devtools/devtools_network_controller_handle.h"
#include "chrome/browser/devtools/devtools_protocol_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/web_contents.h"
DevToolsNetworkProtocolHandler::DevToolsNetworkProtocolHandler() {
}
DevToolsNetworkProtocolHandler::~DevToolsNetworkProtocolHandler() {
}
base::DictionaryValue* DevToolsNetworkProtocolHandler::HandleCommand(
content::DevToolsAgentHost* agent_host,
base::DictionaryValue* command_dict) {
int id = 0;
std::string method;
const base::DictionaryValue* params = nullptr;
if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, ¶ms))
return nullptr;
namespace network = ::chrome::devtools::Network;
if (method == network::emulateNetworkConditions::kName)
return EmulateNetworkConditions(agent_host, id, params).release();
if (method == network::canEmulateNetworkConditions::kName)
return CanEmulateNetworkConditions(agent_host, id, params).release();
return nullptr;
}
scoped_ptr<base::DictionaryValue>
DevToolsNetworkProtocolHandler::CanEmulateNetworkConditions(
content::DevToolsAgentHost* agent_host,
int command_id,
const base::DictionaryValue* params) {
scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
result->SetBoolean(chrome::devtools::kResult, true);
return DevToolsProtocol::CreateSuccessResponse(command_id, result.Pass());
}
scoped_ptr<base::DictionaryValue>
DevToolsNetworkProtocolHandler::EmulateNetworkConditions(
content::DevToolsAgentHost* agent_host,
int command_id,
const base::DictionaryValue* params) {
namespace names = ::chrome::devtools::Network::emulateNetworkConditions;
bool offline = false;
if (!params || !params->GetBoolean(names::kParamOffline, &offline)) {
return DevToolsProtocol::CreateInvalidParamsResponse(
command_id, names::kParamOffline);
}
double latency = 0.0;
if (!params->GetDouble(names::kParamLatency, &latency)) {
return DevToolsProtocol::CreateInvalidParamsResponse(
command_id, names::kParamLatency);
}
if (latency < 0.0)
latency = 0.0;
double download_throughput = 0.0;
if (!params->GetDouble(names::kParamDownloadThroughput,
&download_throughput)) {
return DevToolsProtocol::CreateInvalidParamsResponse(
command_id, names::kParamDownloadThroughput);
}
if (download_throughput < 0.0)
download_throughput = 0.0;
double upload_throughput = 0.0;
if (!params->GetDouble(names::kParamUploadThroughput, &upload_throughput)) {
return DevToolsProtocol::CreateInvalidParamsResponse(
command_id, names::kParamUploadThroughput);
}
if (upload_throughput < 0.0)
upload_throughput = 0.0;
scoped_ptr<DevToolsNetworkConditions> conditions(
new DevToolsNetworkConditions(
offline, latency, download_throughput, upload_throughput));
UpdateNetworkState(agent_host, conditions.Pass());
return scoped_ptr<base::DictionaryValue>();
}
void DevToolsNetworkProtocolHandler::UpdateNetworkState(
content::DevToolsAgentHost* agent_host,
scoped_ptr<DevToolsNetworkConditions> conditions) {
Profile* profile = Profile::FromBrowserContext(
agent_host->GetBrowserContext());
if (!profile)
return;
profile->GetDevToolsNetworkControllerHandle()->SetNetworkState(
agent_host->GetId(), conditions.Pass());
}
void DevToolsNetworkProtocolHandler::DevToolsAgentStateChanged(
content::DevToolsAgentHost* agent_host,
bool attached) {
scoped_ptr<DevToolsNetworkConditions> conditions;
if (attached)
conditions.reset(new DevToolsNetworkConditions());
UpdateNetworkState(agent_host, conditions.Pass());
}
|