2015-09-26 18:19:27 +00:00
|
|
|
// Copyright (c) 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-CHROMIUM file.
|
|
|
|
|
2017-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/net/devtools_network_protocol_handler.h"
|
2015-09-26 18:19:27 +00:00
|
|
|
|
2017-05-18 22:06:57 +00:00
|
|
|
#include <string>
|
2017-08-12 17:27:48 +00:00
|
|
|
#include <utility>
|
2017-05-18 22:06:57 +00:00
|
|
|
|
2017-08-12 17:27:48 +00:00
|
|
|
#include "base/memory/ptr_util.h"
|
2017-05-18 22:29:22 +00:00
|
|
|
#include "base/strings/stringprintf.h"
|
2017-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/browser_context.h"
|
|
|
|
#include "brightray/browser/net/devtools_network_conditions.h"
|
|
|
|
#include "brightray/browser/net/devtools_network_controller.h"
|
2015-09-26 18:19:27 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "content/public/browser/devtools_agent_host.h"
|
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
namespace params {
|
|
|
|
|
|
|
|
const char kDownloadThroughput[] = "downloadThroughput";
|
|
|
|
const char kLatency[] = "latency";
|
|
|
|
const char kOffline[] = "offline";
|
|
|
|
const char kUploadThroughput[] = "uploadThroughput";
|
|
|
|
const char kResult[] = "result";
|
|
|
|
const char kErrorCode[] = "code";
|
|
|
|
const char kErrorMessage[] = "message";
|
|
|
|
|
|
|
|
} // namespace params
|
|
|
|
|
|
|
|
const char kEmulateNetworkConditions[] = "Network.emulateNetworkConditions";
|
2017-03-23 22:47:30 +00:00
|
|
|
const char kCanEmulateNetworkConditions[] =
|
|
|
|
"Network.canEmulateNetworkConditions";
|
2015-09-26 18:19:27 +00:00
|
|
|
const char kId[] = "id";
|
|
|
|
const char kMethod[] = "method";
|
|
|
|
const char kParams[] = "params";
|
|
|
|
const char kError[] = "error";
|
|
|
|
// JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
|
|
|
|
const int kErrorInvalidParams = -32602;
|
|
|
|
|
|
|
|
bool ParseCommand(const base::DictionaryValue* command,
|
|
|
|
int* id,
|
|
|
|
std::string* method,
|
|
|
|
const base::DictionaryValue** params) {
|
|
|
|
if (!command)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!command->GetInteger(kId, id) || *id < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!command->GetString(kMethod, method))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!command->GetDictionary(kParams, params))
|
|
|
|
*params = nullptr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue>
|
|
|
|
CreateSuccessResponse(int id, std::unique_ptr<base::DictionaryValue> result) {
|
2017-08-12 17:27:48 +00:00
|
|
|
auto response = base::MakeUnique<base::DictionaryValue>();
|
2015-09-26 18:19:27 +00:00
|
|
|
response->SetInteger(kId, id);
|
2017-08-12 17:27:48 +00:00
|
|
|
response->Set(params::kResult, std::move(result));
|
2016-03-08 11:59:29 +00:00
|
|
|
return response;
|
2015-09-26 18:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue>
|
2015-09-26 18:19:27 +00:00
|
|
|
CreateFailureResponse(int id, const std::string& param) {
|
2017-08-12 17:27:48 +00:00
|
|
|
auto response = base::MakeUnique<base::DictionaryValue>();
|
|
|
|
auto error_object = base::MakeUnique<base::DictionaryValue>();
|
2015-09-26 18:19:27 +00:00
|
|
|
error_object->SetInteger(params::kErrorCode, kErrorInvalidParams);
|
|
|
|
error_object->SetString(params::kErrorMessage,
|
|
|
|
base::StringPrintf("Missing or Invalid '%s' parameter", param.c_str()));
|
2018-02-14 08:21:46 +00:00
|
|
|
response->Set(kError, std::move(error_object));
|
2016-03-08 11:59:29 +00:00
|
|
|
return response;
|
2015-09-26 18:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
DevToolsNetworkProtocolHandler::DevToolsNetworkProtocolHandler() {
|
|
|
|
}
|
|
|
|
|
|
|
|
DevToolsNetworkProtocolHandler::~DevToolsNetworkProtocolHandler() {
|
|
|
|
}
|
|
|
|
|
|
|
|
base::DictionaryValue* DevToolsNetworkProtocolHandler::HandleCommand(
|
|
|
|
content::DevToolsAgentHost* agent_host,
|
|
|
|
base::DictionaryValue* command) {
|
|
|
|
int id = 0;
|
|
|
|
std::string method;
|
|
|
|
const base::DictionaryValue* params = nullptr;
|
|
|
|
|
|
|
|
if (!ParseCommand(command, &id, &method, ¶ms))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (method == kEmulateNetworkConditions)
|
|
|
|
return EmulateNetworkConditions(agent_host, id, params).release();
|
|
|
|
|
|
|
|
if (method == kCanEmulateNetworkConditions)
|
|
|
|
return CanEmulateNetworkConditions(agent_host, id, params).release();
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-13 21:05:22 +00:00
|
|
|
void DevToolsNetworkProtocolHandler::DevToolsAgentStateChanged(
|
|
|
|
content::DevToolsAgentHost* agent_host,
|
|
|
|
bool attached) {
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<DevToolsNetworkConditions> conditions;
|
2016-03-13 21:05:22 +00:00
|
|
|
if (attached)
|
|
|
|
conditions.reset(new DevToolsNetworkConditions(false));
|
|
|
|
UpdateNetworkState(agent_host, std::move(conditions));
|
|
|
|
}
|
|
|
|
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue>
|
2015-09-26 18:19:27 +00:00
|
|
|
DevToolsNetworkProtocolHandler::CanEmulateNetworkConditions(
|
|
|
|
content::DevToolsAgentHost* agent_host,
|
|
|
|
int id,
|
|
|
|
const base::DictionaryValue* params) {
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue);
|
2015-09-26 18:19:27 +00:00
|
|
|
result->SetBoolean(params::kResult, true);
|
2016-03-08 11:59:29 +00:00
|
|
|
return CreateSuccessResponse(id, std::move(result));
|
2015-09-26 18:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue>
|
2015-09-26 18:19:27 +00:00
|
|
|
DevToolsNetworkProtocolHandler::EmulateNetworkConditions(
|
|
|
|
content::DevToolsAgentHost* agent_host,
|
|
|
|
int id,
|
|
|
|
const base::DictionaryValue* params) {
|
|
|
|
bool offline = false;
|
|
|
|
if (!params || !params->GetBoolean(params::kOffline, &offline))
|
|
|
|
return CreateFailureResponse(id, params::kOffline);
|
|
|
|
|
|
|
|
double latency = 0.0;
|
|
|
|
if (!params->GetDouble(params::kLatency, &latency))
|
|
|
|
return CreateFailureResponse(id, params::kLatency);
|
|
|
|
if (latency < 0.0)
|
|
|
|
latency = 0.0;
|
|
|
|
|
|
|
|
double download_throughput = 0.0;
|
|
|
|
if (!params->GetDouble(params::kDownloadThroughput, &download_throughput))
|
|
|
|
return CreateFailureResponse(id, params::kDownloadThroughput);
|
|
|
|
if (download_throughput < 0.0)
|
|
|
|
download_throughput = 0.0;
|
|
|
|
|
|
|
|
double upload_throughput = 0.0;
|
|
|
|
if (!params->GetDouble(params::kUploadThroughput, &upload_throughput))
|
|
|
|
return CreateFailureResponse(id, params::kUploadThroughput);
|
|
|
|
if (upload_throughput < 0.0)
|
|
|
|
upload_throughput = 0.0;
|
|
|
|
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<DevToolsNetworkConditions> conditions(
|
2015-09-26 18:19:27 +00:00
|
|
|
new DevToolsNetworkConditions(offline,
|
|
|
|
latency,
|
|
|
|
download_throughput,
|
|
|
|
upload_throughput));
|
2016-03-08 11:59:29 +00:00
|
|
|
UpdateNetworkState(agent_host, std::move(conditions));
|
2016-05-23 01:59:07 +00:00
|
|
|
return std::unique_ptr<base::DictionaryValue>();
|
2015-09-26 18:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DevToolsNetworkProtocolHandler::UpdateNetworkState(
|
|
|
|
content::DevToolsAgentHost* agent_host,
|
2016-05-23 01:59:07 +00:00
|
|
|
std::unique_ptr<DevToolsNetworkConditions> conditions) {
|
2015-09-26 18:19:27 +00:00
|
|
|
auto browser_context =
|
|
|
|
static_cast<brightray::BrowserContext*>(agent_host->GetBrowserContext());
|
2016-04-14 01:29:14 +00:00
|
|
|
browser_context->network_controller_handle()->SetNetworkState(
|
|
|
|
agent_host->GetId(), std::move(conditions));
|
2015-09-26 18:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace brightray
|