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 #include "chrome/browser/devtools/devtools_network_protocol_handler.h"
7 #include "base/values.h"
8 #include "chrome/browser/devtools/devtools_network_conditions.h"
9 #include "chrome/browser/devtools/devtools_network_controller.h"
10 #include "chrome/browser/devtools/devtools_protocol_constants.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "content/public/browser/devtools_agent_host.h"
13 #include "content/public/browser/web_contents.h"
15 DevToolsNetworkProtocolHandler::DevToolsNetworkProtocolHandler() {
18 DevToolsNetworkProtocolHandler::~DevToolsNetworkProtocolHandler() {
21 base::DictionaryValue
* DevToolsNetworkProtocolHandler::HandleCommand(
22 content::DevToolsAgentHost
* agent_host
,
23 base::DictionaryValue
* command_dict
) {
26 const base::DictionaryValue
* params
= nullptr;
27 if (!DevToolsProtocol::ParseCommand(command_dict
, &id
, &method
, ¶ms
))
30 namespace network
= ::chrome::devtools::Network
;
32 if (method
== network::emulateNetworkConditions::kName
)
33 return EmulateNetworkConditions(agent_host
, id
, params
).release();
35 if (method
== network::canEmulateNetworkConditions::kName
)
36 return CanEmulateNetworkConditions(agent_host
, id
, params
).release();
41 scoped_ptr
<base::DictionaryValue
>
42 DevToolsNetworkProtocolHandler::CanEmulateNetworkConditions(
43 content::DevToolsAgentHost
* agent_host
,
45 const base::DictionaryValue
* params
) {
46 scoped_ptr
<base::DictionaryValue
> result(new base::DictionaryValue());
47 result
->SetBoolean(chrome::devtools::kResult
, true);
48 return DevToolsProtocol::CreateSuccessResponse(command_id
, result
.Pass());
51 scoped_ptr
<base::DictionaryValue
>
52 DevToolsNetworkProtocolHandler::EmulateNetworkConditions(
53 content::DevToolsAgentHost
* agent_host
,
55 const base::DictionaryValue
* params
) {
56 namespace names
= ::chrome::devtools::Network::emulateNetworkConditions
;
59 if (!params
|| !params
->GetBoolean(names::kParamOffline
, &offline
)) {
60 return DevToolsProtocol::CreateInvalidParamsResponse(
61 command_id
, names::kParamOffline
);
64 if (!params
->GetDouble(names::kParamLatency
, &latency
)) {
65 return DevToolsProtocol::CreateInvalidParamsResponse(
66 command_id
, names::kParamLatency
);
71 double download_throughput
= 0.0;
72 if (!params
->GetDouble(names::kParamDownloadThroughput
,
73 &download_throughput
)) {
74 return DevToolsProtocol::CreateInvalidParamsResponse(
75 command_id
, names::kParamDownloadThroughput
);
77 if (download_throughput
< 0.0)
78 download_throughput
= 0.0;
80 double upload_throughput
= 0.0;
81 if (!params
->GetDouble(names::kParamUploadThroughput
, &upload_throughput
)) {
82 return DevToolsProtocol::CreateInvalidParamsResponse(
83 command_id
, names::kParamUploadThroughput
);
85 if (upload_throughput
< 0.0)
86 upload_throughput
= 0.0;
88 scoped_ptr
<DevToolsNetworkConditions
> conditions(
89 new DevToolsNetworkConditions(
90 offline
, latency
, download_throughput
, upload_throughput
));
92 UpdateNetworkState(agent_host
, conditions
.Pass());
93 return scoped_ptr
<base::DictionaryValue
>();
96 void DevToolsNetworkProtocolHandler::UpdateNetworkState(
97 content::DevToolsAgentHost
* agent_host
,
98 scoped_ptr
<DevToolsNetworkConditions
> conditions
) {
99 Profile
* profile
= Profile::FromBrowserContext(
100 agent_host
->GetBrowserContext());
103 profile
->GetDevToolsNetworkController()->SetNetworkState(
104 agent_host
->GetId(), conditions
.Pass());
107 void DevToolsNetworkProtocolHandler::DevToolsAgentStateChanged(
108 content::DevToolsAgentHost
* agent_host
,
110 scoped_ptr
<DevToolsNetworkConditions
> conditions
;
112 conditions
.reset(new DevToolsNetworkConditions());
113 UpdateNetworkState(agent_host
, conditions
.Pass());