[cros new GAIA] Update GAIA card size
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_network_protocol_handler.cc
blob1e930659c853823ee5ffe0c17457d49725de3bd9
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) {
24 int id = 0;
25 std::string method;
26 const base::DictionaryValue* params = nullptr;
27 if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, &params))
28 return nullptr;
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();
38 return nullptr;
41 scoped_ptr<base::DictionaryValue>
42 DevToolsNetworkProtocolHandler::CanEmulateNetworkConditions(
43 content::DevToolsAgentHost* agent_host,
44 int command_id,
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,
54 int command_id,
55 const base::DictionaryValue* params) {
56 namespace names = ::chrome::devtools::Network::emulateNetworkConditions;
58 bool offline = false;
59 if (!params || !params->GetBoolean(names::kParamOffline, &offline)) {
60 return DevToolsProtocol::CreateInvalidParamsResponse(
61 command_id, names::kParamOffline);
63 double latency = 0.0;
64 if (!params->GetDouble(names::kParamLatency, &latency)) {
65 return DevToolsProtocol::CreateInvalidParamsResponse(
66 command_id, names::kParamLatency);
68 if (latency < 0.0)
69 latency = 0.0;
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());
101 if (!profile)
102 return;
103 profile->GetDevToolsNetworkController()->SetNetworkState(
104 agent_host->GetId(), conditions.Pass());
107 void DevToolsNetworkProtocolHandler::DevToolsAgentStateChanged(
108 content::DevToolsAgentHost* agent_host,
109 bool attached) {
110 scoped_ptr<DevToolsNetworkConditions> conditions;
111 if (attached)
112 conditions.reset(new DevToolsNetworkConditions());
113 UpdateNetworkState(agent_host, conditions.Pass());