Commit 33770464 authored by Sadrul Habib Chowdhury's avatar Sadrul Habib Chowdhury Committed by Commit Bot

devtools: Add an UberDispatcher in chrome.

Dispatch the commands in Page domain through an UberDispatcher. Follow up
CLs make this change for the other domains (Browser, Target).

This is in preparation for adding WindowManager domain.

BUG=758061

Change-Id: I35e9dcae13ee9f80e4d81054bdfaf2c5292785ad
Reviewed-on: https://chromium-review.googlesource.com/674263
Commit-Queue: Sadrul Chowdhury <sadrul@chromium.org>
Reviewed-by: default avatarPavel Feldman <pfeldman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#504474}
parent 98ee1b09
...@@ -29,6 +29,41 @@ action("devtools_protocol_constants") { ...@@ -29,6 +29,41 @@ action("devtools_protocol_constants") {
args += [ rebase_path(blink_protocol, root_build_dir) ] args += [ rebase_path(blink_protocol, root_build_dir) ]
} }
if (!is_android) {
_inspector_protocol = "//third_party/inspector_protocol"
import("$_inspector_protocol/inspector_protocol.gni")
_protocol_generated = [
"protocol/forward.h",
"protocol/page.cc",
"protocol/page.h",
"protocol/protocol.cc",
"protocol/protocol.h",
]
inspector_protocol_generate("protocol_generated_sources") {
inspector_protocol_dir = _inspector_protocol
visibility = [ ":*" ] # Only targets in this file can depend on this.
deps = [
"//third_party/WebKit/Source/core/inspector:protocol_version",
]
out_dir = target_gen_dir
config_file = "inspector_protocol_config.json"
_blink_protocol_path =
rebase_path("$root_gen_dir/blink/core/inspector/protocol.json",
root_build_dir)
config_values = [ "protocol.path=$_blink_protocol_path" ]
inputs = [
"$root_gen_dir/blink/core/inspector/protocol.json",
"inspector_protocol_config.json",
]
outputs = _protocol_generated
}
}
static_library("devtools") { static_library("devtools") {
# Note: new sources and deps should be generally added in (!is_android) below. # Note: new sources and deps should be generally added in (!is_android) below.
sources = [ sources = [
...@@ -67,6 +102,8 @@ static_library("devtools") { ...@@ -67,6 +102,8 @@ static_library("devtools") {
sources += [ sources += [
"chrome_devtools_manager_delegate.cc", "chrome_devtools_manager_delegate.cc",
"chrome_devtools_manager_delegate.h", "chrome_devtools_manager_delegate.h",
"chrome_devtools_session.cc",
"chrome_devtools_session.h",
"device/adb/adb_client_socket.cc", "device/adb/adb_client_socket.cc",
"device/adb/adb_client_socket.h", "device/adb/adb_client_socket.h",
"device/adb/adb_device_provider.cc", "device/adb/adb_device_provider.cc",
...@@ -130,6 +167,17 @@ static_library("devtools") { ...@@ -130,6 +167,17 @@ static_library("devtools") {
} }
} }
if (!is_android) {
deps += [ ":protocol_generated_sources" ]
sources += [
"protocol/page_handler.cc",
"protocol/page_handler.h",
"protocol_string.cc",
"protocol_string.h",
]
sources += rebase_path(_protocol_generated, ".", target_gen_dir)
}
if (enable_extensions) { if (enable_extensions) {
deps += [ "//chrome/common/extensions/api" ] deps += [ "//chrome/common/extensions/api" ]
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/devtools/chrome_devtools_session.h"
#include "chrome/browser/devtools/device/android_device_manager.h" #include "chrome/browser/devtools/device/android_device_manager.h"
#include "chrome/browser/devtools/device/tcp_device_provider.h" #include "chrome/browser/devtools/device/tcp_device_provider.h"
#include "chrome/browser/devtools/devtools_protocol.h" #include "chrome/browser/devtools/devtools_protocol.h"
...@@ -18,7 +19,6 @@ ...@@ -18,7 +19,6 @@
#include "chrome/browser/extensions/extension_tab_util.h" #include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h"
#include "chrome/browser/ui/browser_navigator.h" #include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h" #include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/browser_window.h"
...@@ -110,15 +110,6 @@ bool GetExtensionInfo(content::WebContents* wc, ...@@ -110,15 +110,6 @@ bool GetExtensionInfo(content::WebContents* wc,
return false; return false;
} }
void ToggleAdBlocking(bool enabled, content::DevToolsAgentHost* agent_host) {
if (content::WebContents* web_contents = agent_host->GetWebContents()) {
if (auto* client =
ChromeSubresourceFilterClient::FromWebContents(web_contents)) {
client->ToggleForceActivationInCurrentWebContents(enabled);
}
}
}
std::string ToString(std::unique_ptr<base::DictionaryValue> value) { std::string ToString(std::unique_ptr<base::DictionaryValue> value) {
std::string json; std::string json;
base::JSONWriter::Write(*value, &json); base::JSONWriter::Write(*value, &json);
...@@ -291,27 +282,6 @@ ChromeDevToolsManagerDelegate::SetWindowBounds(int id, ...@@ -291,27 +282,6 @@ ChromeDevToolsManagerDelegate::SetWindowBounds(int id,
return DevToolsProtocol::CreateSuccessResponse(id, nullptr); return DevToolsProtocol::CreateSuccessResponse(id, nullptr);
} }
std::unique_ptr<base::DictionaryValue>
ChromeDevToolsManagerDelegate::SetAdBlockingEnabled(
content::DevToolsAgentHost* agent_host,
int id,
base::DictionaryValue* params) {
if (!page_enable_)
return DevToolsProtocol::CreateErrorResponse(id, "Page domain is disabled");
bool enabled = false;
params->GetBoolean("enabled", &enabled);
ToggleAdBlocking(enabled, agent_host);
return DevToolsProtocol::CreateSuccessResponse(id, nullptr);
}
void ChromeDevToolsManagerDelegate::TogglePageEnable(
bool enable,
content::DevToolsAgentHost* agent_host) {
page_enable_ = enable;
if (!page_enable_)
ToggleAdBlocking(false /* enable */, agent_host);
}
std::unique_ptr<base::DictionaryValue> std::unique_ptr<base::DictionaryValue>
ChromeDevToolsManagerDelegate::HandleBrowserCommand( ChromeDevToolsManagerDelegate::HandleBrowserCommand(
int id, int id,
...@@ -364,18 +334,6 @@ bool ChromeDevToolsManagerDelegate::HandleCommand( ...@@ -364,18 +334,6 @@ bool ChromeDevToolsManagerDelegate::HandleCommand(
if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, &params)) if (!DevToolsProtocol::ParseCommand(command_dict, &id, &method, &params))
return false; return false;
// Do not actually handle the enable/disable commands, just keep track of the
// enable state.
if (method == chrome::devtools::Page::enable::kName) {
TogglePageEnable(true /* enable */, agent_host);
return false;
}
if (method == chrome::devtools::Page::disable::kName) {
TogglePageEnable(false /* enable */, agent_host);
return false;
}
auto result = HandleBrowserCommand(id, method, params); auto result = HandleBrowserCommand(id, method, params);
if (result) { if (result) {
agent_host->SendProtocolMessageToClient(session_id, agent_host->SendProtocolMessageToClient(session_id,
...@@ -383,14 +341,6 @@ bool ChromeDevToolsManagerDelegate::HandleCommand( ...@@ -383,14 +341,6 @@ bool ChromeDevToolsManagerDelegate::HandleCommand(
return true; return true;
} }
if (method == chrome::devtools::Page::setAdBlockingEnabled::kName) {
result = SetAdBlockingEnabled(agent_host, id, params);
DCHECK(result);
agent_host->SendProtocolMessageToClient(session_id,
ToString(std::move(result)));
return true;
}
if (method == chrome::devtools::Target::setRemoteLocations::kName) { if (method == chrome::devtools::Target::setRemoteLocations::kName) {
result = SetRemoteLocations(agent_host, id, params); result = SetRemoteLocations(agent_host, id, params);
DCHECK(result); DCHECK(result);
...@@ -399,7 +349,10 @@ bool ChromeDevToolsManagerDelegate::HandleCommand( ...@@ -399,7 +349,10 @@ bool ChromeDevToolsManagerDelegate::HandleCommand(
return true; return true;
} }
return false; DCHECK(sessions_.find(session_id) != sessions_.end());
auto response = sessions_[session_id]->dispatcher()->dispatch(
protocol::toProtocolValue(command_dict, 1000));
return response != protocol::DispatchResponse::Status::kFallThrough;
} }
std::string ChromeDevToolsManagerDelegate::GetTargetType( std::string ChromeDevToolsManagerDelegate::GetTargetType(
...@@ -425,6 +378,20 @@ std::string ChromeDevToolsManagerDelegate::GetTargetTitle( ...@@ -425,6 +378,20 @@ std::string ChromeDevToolsManagerDelegate::GetTargetTitle(
return extension_name; return extension_name;
} }
void ChromeDevToolsManagerDelegate::SessionCreated(
content::DevToolsAgentHost* agent_host,
int session_id) {
DCHECK(sessions_.find(session_id) == sessions_.end());
sessions_[session_id] =
std::make_unique<ChromeDevToolsSession>(agent_host, session_id);
}
void ChromeDevToolsManagerDelegate::SessionDestroyed(
content::DevToolsAgentHost* agent_host,
int session_id) {
sessions_.erase(session_id);
}
scoped_refptr<DevToolsAgentHost> scoped_refptr<DevToolsAgentHost>
ChromeDevToolsManagerDelegate::CreateNewTarget(const GURL& url) { ChromeDevToolsManagerDelegate::CreateNewTarget(const GURL& url) {
chrome::NavigateParams params(ProfileManager::GetLastUsedProfile(), chrome::NavigateParams params(ProfileManager::GetLastUsedProfile(),
...@@ -454,8 +421,6 @@ void ChromeDevToolsManagerDelegate::DevToolsAgentHostAttached( ...@@ -454,8 +421,6 @@ void ChromeDevToolsManagerDelegate::DevToolsAgentHostAttached(
void ChromeDevToolsManagerDelegate::DevToolsAgentHostDetached( void ChromeDevToolsManagerDelegate::DevToolsAgentHostDetached(
content::DevToolsAgentHost* agent_host) { content::DevToolsAgentHost* agent_host) {
ToggleAdBlocking(false /* enable */, agent_host);
// This class is created lazily, so it may not know about some attached hosts. // This class is created lazily, so it may not know about some attached hosts.
if (host_data_.find(agent_host) != host_data_.end()) { if (host_data_.find(agent_host) != host_data_.end()) {
host_data_.erase(agent_host); host_data_.erase(agent_host);
......
...@@ -13,10 +13,14 @@ ...@@ -13,10 +13,14 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/macros.h" #include "base/macros.h"
#include "chrome/browser/devtools/device/devtools_device_discovery.h" #include "chrome/browser/devtools/device/devtools_device_discovery.h"
#include "chrome/browser/devtools/protocol/forward.h"
#include "chrome/browser/devtools/protocol/protocol.h"
#include "content/public/browser/devtools_agent_host_observer.h" #include "content/public/browser/devtools_agent_host_observer.h"
#include "content/public/browser/devtools_manager_delegate.h" #include "content/public/browser/devtools_manager_delegate.h"
#include "net/base/host_port_pair.h" #include "net/base/host_port_pair.h"
class ChromeDevToolsSession;
class ChromeDevToolsManagerDelegate : class ChromeDevToolsManagerDelegate :
public content::DevToolsManagerDelegate, public content::DevToolsManagerDelegate,
public content::DevToolsAgentHostObserver { public content::DevToolsAgentHostObserver {
...@@ -39,6 +43,10 @@ class ChromeDevToolsManagerDelegate : ...@@ -39,6 +43,10 @@ class ChromeDevToolsManagerDelegate :
base::DictionaryValue* command_dict) override; base::DictionaryValue* command_dict) override;
std::string GetTargetType(content::WebContents* web_contents) override; std::string GetTargetType(content::WebContents* web_contents) override;
std::string GetTargetTitle(content::WebContents* web_contents) override; std::string GetTargetTitle(content::WebContents* web_contents) override;
void SessionCreated(content::DevToolsAgentHost* agent_host,
int session_id) override;
void SessionDestroyed(content::DevToolsAgentHost* agent_host,
int session_id) override;
scoped_refptr<content::DevToolsAgentHost> CreateNewTarget( scoped_refptr<content::DevToolsAgentHost> CreateNewTarget(
const GURL& url) override; const GURL& url) override;
std::string GetDiscoveryPageHTML() override; std::string GetDiscoveryPageHTML() override;
...@@ -72,22 +80,16 @@ class ChromeDevToolsManagerDelegate : ...@@ -72,22 +80,16 @@ class ChromeDevToolsManagerDelegate :
static std::unique_ptr<base::DictionaryValue> SetWindowBounds( static std::unique_ptr<base::DictionaryValue> SetWindowBounds(
int id, int id,
base::DictionaryValue* params); base::DictionaryValue* params);
std::unique_ptr<base::DictionaryValue> SetAdBlockingEnabled(
content::DevToolsAgentHost* agent_host,
int id,
base::DictionaryValue* params);
void TogglePageEnable(bool enable, content::DevToolsAgentHost* agent_host);
std::map<content::DevToolsAgentHost*, std::unique_ptr<HostData>> host_data_; std::map<content::DevToolsAgentHost*, std::unique_ptr<HostData>> host_data_;
std::map<int, std::unique_ptr<ChromeDevToolsSession>> sessions_;
std::unique_ptr<AndroidDeviceManager> device_manager_; std::unique_ptr<AndroidDeviceManager> device_manager_;
std::unique_ptr<DevToolsDeviceDiscovery> device_discovery_; std::unique_ptr<DevToolsDeviceDiscovery> device_discovery_;
content::DevToolsAgentHost::List remote_agent_hosts_; content::DevToolsAgentHost::List remote_agent_hosts_;
RemoteLocations remote_locations_; RemoteLocations remote_locations_;
bool page_enable_ = false;
DISALLOW_COPY_AND_ASSIGN(ChromeDevToolsManagerDelegate); DISALLOW_COPY_AND_ASSIGN(ChromeDevToolsManagerDelegate);
}; };
......
// Copyright 2017 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/chrome_devtools_session.h"
#include "chrome/browser/devtools/protocol/page_handler.h"
#include "content/public/browser/devtools_agent_host.h"
ChromeDevToolsSession::ChromeDevToolsSession(
content::DevToolsAgentHost* agent_host,
int session_id)
: agent_host_(agent_host),
session_id_(session_id),
dispatcher_(std::make_unique<protocol::UberDispatcher>(this)) {
dispatcher_->setFallThroughForNotFound(true);
if (agent_host->GetWebContents()) {
page_handler_ = std::make_unique<PageHandler>(agent_host->GetWebContents(),
dispatcher_.get());
}
}
ChromeDevToolsSession::~ChromeDevToolsSession() = default;
void ChromeDevToolsSession::sendProtocolResponse(
int call_id,
std::unique_ptr<protocol::Serializable> message) {
agent_host_->SendProtocolMessageToClient(session_id_, message->serialize());
}
void ChromeDevToolsSession::sendProtocolNotification(
std::unique_ptr<protocol::Serializable> message) {
agent_host_->SendProtocolMessageToClient(session_id_, message->serialize());
}
void ChromeDevToolsSession::flushProtocolNotifications() {}
// Copyright 2017 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 CHROME_BROWSER_DEVTOOLS_CHROME_DEVTOOLS_SESSION_H_
#define CHROME_BROWSER_DEVTOOLS_CHROME_DEVTOOLS_SESSION_H_
#include <memory>
#include "chrome/browser/devtools/protocol/forward.h"
#include "chrome/browser/devtools/protocol/protocol.h"
namespace content {
class DevToolsAgentHost;
}
class PageHandler;
class ChromeDevToolsSession : public protocol::FrontendChannel {
public:
ChromeDevToolsSession(content::DevToolsAgentHost* agent_host, int session_id);
~ChromeDevToolsSession() override;
protocol::UberDispatcher* dispatcher() { return dispatcher_.get(); }
private:
// protocol::FrontendChannel:
void sendProtocolResponse(
int call_id,
std::unique_ptr<protocol::Serializable> message) override;
void sendProtocolNotification(
std::unique_ptr<protocol::Serializable> message) override;
void flushProtocolNotifications() override;
content::DevToolsAgentHost* const agent_host_;
const int session_id_;
std::unique_ptr<protocol::UberDispatcher> dispatcher_;
std::unique_ptr<PageHandler> page_handler_;
DISALLOW_COPY_AND_ASSIGN(ChromeDevToolsSession);
};
#endif // CHROME_BROWSER_DEVTOOLS_CHROME_DEVTOOLS_SESSION_H_
{
"use_snake_file_names": true,
"use_title_case_methods": true,
"protocol": {
"package": "chrome/browser/devtools/protocol",
"output": "protocol",
"namespace": [ "protocol" ],
"options": [
{
"domain": "Page",
"include": [ "enable", "disable", "setAdBlockingEnabled" ],
"include_types": [],
"include_events": []
}
]
},
"lib": {
"package": "chrome/browser/devtools/protocol",
"output": "protocol",
"string_header": "chrome/browser/devtools/protocol_string.h"
}
}
// Copyright 2017 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/protocol/page_handler.h"
#include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h"
PageHandler::PageHandler(content::WebContents* web_contents,
protocol::UberDispatcher* dispatcher)
: content::WebContentsObserver(web_contents) {
DCHECK(web_contents);
protocol::Page::Dispatcher::wire(dispatcher, this);
}
PageHandler::~PageHandler() {
ToggleAdBlocking(false /* enabled */);
}
void PageHandler::ToggleAdBlocking(bool enabled) {
if (!web_contents())
return;
if (auto* client =
ChromeSubresourceFilterClient::FromWebContents(web_contents())) {
client->ToggleForceActivationInCurrentWebContents(enabled);
}
}
protocol::Response PageHandler::Enable() {
enabled_ = true;
// Do not mark the command as handled. Let it fall through instead, so that
// the handler in content gets a chance to process the command.
return protocol::Response::FallThrough();
}
protocol::Response PageHandler::Disable() {
enabled_ = false;
ToggleAdBlocking(false /* enable */);
// Do not mark the command as handled. Let it fall through instead, so that
// the handler in content gets a chance to process the command.
return protocol::Response::FallThrough();
}
protocol::Response PageHandler::SetAdBlockingEnabled(bool enabled) {
if (!enabled_)
return protocol::Response::Error("Page domain is disabled.");
ToggleAdBlocking(enabled);
return protocol::Response::OK();
}
// Copyright 2017 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 CHROME_BROWSER_DEVTOOLS_PROTOCOL_PAGE_HANDLER_H_
#define CHROME_BROWSER_DEVTOOLS_PROTOCOL_PAGE_HANDLER_H_
#include "chrome/browser/devtools/protocol/forward.h"
#include "chrome/browser/devtools/protocol/page.h"
#include "content/public/browser/web_contents_observer.h"
namespace content {
class WebContents;
}
class PageHandler : public protocol::Page::Backend,
public content::WebContentsObserver {
public:
PageHandler(content::WebContents* web_contents,
protocol::UberDispatcher* dispatcher);
~PageHandler() override;
void ToggleAdBlocking(bool enabled);
// Page::Backend:
protocol::Response Enable() override;
protocol::Response Disable() override;
protocol::Response SetAdBlockingEnabled(bool enabled) override;
private:
bool enabled_ = false;
DISALLOW_COPY_AND_ASSIGN(PageHandler);
};
#endif // CHROME_BROWSER_DEVTOOLS_PROTOCOL_PAGE_HANDLER_H_
// Copyright 2016 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/protocol_string.h"
#include "base/json/json_reader.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/devtools/protocol/protocol.h"
namespace protocol {
std::unique_ptr<protocol::Value> toProtocolValue(const base::Value* value,
int depth) {
if (!value || !depth)
return nullptr;
if (value->IsType(base::Value::Type::NONE))
return protocol::Value::null();
if (value->IsType(base::Value::Type::BOOLEAN)) {
bool inner;
value->GetAsBoolean(&inner);
return protocol::FundamentalValue::create(inner);
}
if (value->IsType(base::Value::Type::INTEGER)) {
int inner;
value->GetAsInteger(&inner);
return protocol::FundamentalValue::create(inner);
}
if (value->IsType(base::Value::Type::DOUBLE)) {
double inner;
value->GetAsDouble(&inner);
return protocol::FundamentalValue::create(inner);
}
if (value->IsType(base::Value::Type::STRING)) {
std::string inner;
value->GetAsString(&inner);
return protocol::StringValue::create(inner);
}
if (value->IsType(base::Value::Type::LIST)) {
const base::ListValue* list = nullptr;
value->GetAsList(&list);
std::unique_ptr<protocol::ListValue> result = protocol::ListValue::create();
for (size_t i = 0; i < list->GetSize(); i++) {
const base::Value* item = nullptr;
list->Get(i, &item);
std::unique_ptr<protocol::Value> converted =
toProtocolValue(item, depth - 1);
if (converted)
result->pushValue(std::move(converted));
}
return std::move(result);
}
if (value->IsType(base::Value::Type::DICTIONARY)) {
const base::DictionaryValue* dictionary = nullptr;
value->GetAsDictionary(&dictionary);
std::unique_ptr<protocol::DictionaryValue> result =
protocol::DictionaryValue::create();
for (base::DictionaryValue::Iterator it(*dictionary); !it.IsAtEnd();
it.Advance()) {
std::unique_ptr<protocol::Value> converted =
toProtocolValue(&it.value(), depth - 1);
if (converted)
result->setValue(it.key(), std::move(converted));
}
return std::move(result);
}
return nullptr;
}
std::unique_ptr<base::Value> toBaseValue(protocol::Value* value, int depth) {
if (!value || !depth)
return nullptr;
if (value->type() == protocol::Value::TypeNull)
return base::MakeUnique<base::Value>();
if (value->type() == protocol::Value::TypeBoolean) {
bool inner;
value->asBoolean(&inner);
return base::WrapUnique(new base::Value(inner));
}
if (value->type() == protocol::Value::TypeInteger) {
int inner;
value->asInteger(&inner);
return base::WrapUnique(new base::Value(inner));
}
if (value->type() == protocol::Value::TypeDouble) {
double inner;
value->asDouble(&inner);
return base::WrapUnique(new base::Value(inner));
}
if (value->type() == protocol::Value::TypeString) {
std::string inner;
value->asString(&inner);
return base::WrapUnique(new base::Value(inner));
}
if (value->type() == protocol::Value::TypeArray) {
protocol::ListValue* list = protocol::ListValue::cast(value);
std::unique_ptr<base::ListValue> result(new base::ListValue());
for (size_t i = 0; i < list->size(); i++) {
std::unique_ptr<base::Value> converted =
toBaseValue(list->at(i), depth - 1);
if (converted)
result->Append(std::move(converted));
}
return std::move(result);
}
if (value->type() == protocol::Value::TypeObject) {
protocol::DictionaryValue* dict = protocol::DictionaryValue::cast(value);
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
for (size_t i = 0; i < dict->size(); i++) {
protocol::DictionaryValue::Entry entry = dict->at(i);
std::unique_ptr<base::Value> converted =
toBaseValue(entry.second, depth - 1);
if (converted)
result->SetWithoutPathExpansion(entry.first, std::move(converted));
}
return std::move(result);
}
return nullptr;
}
// static
std::unique_ptr<protocol::Value> StringUtil::parseJSON(
const std::string& json) {
std::unique_ptr<base::Value> value = base::JSONReader::Read(json);
return toProtocolValue(value.get(), 1000);
}
StringBuilder::StringBuilder() {}
StringBuilder::~StringBuilder() {}
void StringBuilder::append(const std::string& s) {
string_ += s;
}
void StringBuilder::append(char c) {
string_ += c;
}
void StringBuilder::append(const char* characters, size_t length) {
string_.append(characters, length);
}
// static
void StringUtil::builderAppendQuotedString(StringBuilder& builder,
const String& str) {
builder.append('"');
base::string16 str16 = base::UTF8ToUTF16(str);
escapeWideStringForJSON(reinterpret_cast<const uint16_t*>(&str16[0]),
str16.length(), &builder);
builder.append('"');
}
std::string StringBuilder::toString() {
return string_;
}
void StringBuilder::reserveCapacity(size_t capacity) {
string_.reserve(capacity);
}
} // namespace protocol
// Copyright 2017 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 CHROME_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_
#define CHROME_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_
#include <memory>
#include <string>
#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/string_number_conversions.h"
namespace base {
class Value;
}
namespace protocol {
class Value;
using String = std::string;
class StringBuilder {
public:
StringBuilder();
~StringBuilder();
void append(const String&);
void append(char);
void append(const char*, size_t);
String toString();
void reserveCapacity(size_t);
private:
std::string string_;
};
class StringUtil {
public:
static String substring(const String& s, unsigned pos, unsigned len) {
return s.substr(pos, len);
}
static String fromInteger(int number) { return base::IntToString(number); }
static String fromDouble(double number) {
String s = base::DoubleToString(number);
if (!s.empty() && s[0] == '.')
s = "0" + s;
return s;
}
static double toDouble(const char* s, size_t len, bool* ok) {
double v = 0.0;
*ok = base::StringToDouble(std::string(s, len), &v);
return *ok ? v : 0.0;
}
static size_t find(const String& s, const char* needle) {
return s.find(needle);
}
static size_t find(const String& s, const String& needle) {
return s.find(needle);
}
static const size_t kNotFound = static_cast<size_t>(-1);
static void builderAppend(StringBuilder& builder, const String& s) {
builder.append(s);
}
static void builderAppend(StringBuilder& builder, char c) {
builder.append(c);
}
static void builderAppend(StringBuilder& builder, const char* s, size_t len) {
builder.append(s, len);
}
static void builderAppendQuotedString(StringBuilder& builder,
const String& str);
static void builderReserve(StringBuilder& builder, unsigned capacity) {
builder.reserveCapacity(capacity);
}
static String builderToString(StringBuilder& builder) {
return builder.toString();
}
static std::unique_ptr<protocol::Value> parseJSON(const String&);
};
std::unique_ptr<protocol::Value> toProtocolValue(const base::Value* value,
int depth);
std::unique_ptr<base::Value> toBaseValue(protocol::Value* value, int depth);
} // namespace protocol
#endif // CHROME_BROWSER_DEVTOOLS_PROTOCOL_STRING_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment