From 5323241303055424caca721c48306aae8d10df35 Mon Sep 17 00:00:00 2001 From: vkuzkokov Date: Tue, 11 Nov 2014 04:55:54 -0800 Subject: [PATCH] [DevTools] Added browser protocol to handler generator BUG=405566 Review URL: https://codereview.chromium.org/658163003 Cr-Commit-Position: refs/heads/master@{#303632} --- content/browser/devtools/BUILD.gn | 8 +-- content/browser/devtools/browser_protocol.json | 8 ++- content/browser/devtools/devtools.gyp | 5 +- .../browser/devtools/devtools_http_handler_impl.cc | 11 ++-- .../devtools_protocol_handler_generator.py | 22 +++---- .../devtools/protocol/system_info_handler.cc | 25 ++++++++ .../devtools/protocol/system_info_handler.h | 31 ++++++++++ .../devtools/{ => protocol}/tethering_handler.cc | 68 +++++++--------------- .../devtools/{ => protocol}/tethering_handler.h | 36 +++++++----- content/content_browser.gypi | 6 +- 10 files changed, 134 insertions(+), 86 deletions(-) create mode 100644 content/browser/devtools/protocol/system_info_handler.cc create mode 100644 content/browser/devtools/protocol/system_info_handler.h rename content/browser/devtools/{ => protocol}/tethering_handler.cc (83%) rename content/browser/devtools/{ => protocol}/tethering_handler.h (56%) diff --git a/content/browser/devtools/BUILD.gn b/content/browser/devtools/BUILD.gn index 75777316dcdd..19a4003bd7c0 100644 --- a/content/browser/devtools/BUILD.gn +++ b/content/browser/devtools/BUILD.gn @@ -64,16 +64,16 @@ action("gen_devtools_protocol_handler") { "devtools_protocol_handler_generator.py" blink_protocol = "//third_party/WebKit/Source/devtools/protocol.json" - inputs = [ blink_protocol ] + browser_protocol = "browser_protocol.json" + inputs = [ blink_protocol, browser_protocol ] outputs = [ "$target_gen_dir/protocol/devtools_protocol_handler_impl.cc", "$target_gen_dir/protocol/devtools_protocol_handler_impl.h", ] - args = [ - rebase_path(blink_protocol, root_build_dir), - ] + rebase_path(outputs, root_build_dir) + args = rebase_path(inputs, root_build_dir) + + rebase_path(outputs, root_build_dir) } source_set("devtools_protocol_constants") { diff --git a/content/browser/devtools/browser_protocol.json b/content/browser/devtools/browser_protocol.json index f4b67407d2e8..53ad99cd4465 100644 --- a/content/browser/devtools/browser_protocol.json +++ b/content/browser/devtools/browser_protocol.json @@ -55,17 +55,19 @@ "commands": [ { "name": "bind", + "async": true, "description": "Request browser port binding.", "parameters": [ - { "name": "port", "type": "number", "description": "Port number to bind." } + { "name": "port", "type": "integer", "description": "Port number to bind." } ], "handlers": ["browser"] }, { "name": "unbind", + "async": true, "description": "Request browser port unbinding.", "parameters": [ - { "name": "port", "type": "number", "description": "Port number to unbind." } + { "name": "port", "type": "integer", "description": "Port number to unbind." } ], "handlers": ["browser"] } @@ -75,7 +77,7 @@ "name": "accepted", "description": "Informs that port was successfully bound and got a specified connection id.", "parameters": [ - {"name": "port", "type": "number", "description": "Port number that was successfully bound." }, + {"name": "port", "type": "integer", "description": "Port number that was successfully bound." }, {"name": "connectionId", "type": "string", "description": "Connection id to be used." } ], "handlers": ["browser"] diff --git a/content/browser/devtools/devtools.gyp b/content/browser/devtools/devtools.gyp index a67eca87890a..8957d721e9cb 100644 --- a/content/browser/devtools/devtools.gyp +++ b/content/browser/devtools/devtools.gyp @@ -12,12 +12,14 @@ 'action_name': 'devtools_protocol_handler', 'variables': { 'blink_protocol': '../../../third_party/WebKit/Source/devtools/protocol.json', + 'browser_protocol': 'browser_protocol.json', 'generator': 'protocol/devtools_protocol_handler_generator.py', 'output_cc': '<(SHARED_INTERMEDIATE_DIR)/content/browser/devtools/protocol/devtools_protocol_handler_impl.cc', 'output_h': '<(SHARED_INTERMEDIATE_DIR)/content/browser/devtools/protocol/devtools_protocol_handler_impl.h', }, 'inputs': [ '<(blink_protocol)', + '<(browser_protocol)', '<(generator)', ], 'outputs': [ @@ -28,10 +30,11 @@ 'python', '<(generator)', '<(blink_protocol)', + '<(browser_protocol)', '<(output_cc)', '<(output_h)', ], - 'message': 'Generating DevTools protocol browser-side handlers from <(blink_protocol)' + 'message': 'Generating DevTools protocol browser-side handlers from <(blink_protocol) and <(browser_protocol)' }, ], 'direct_dependent_settings': { diff --git a/content/browser/devtools/devtools_http_handler_impl.cc b/content/browser/devtools/devtools_http_handler_impl.cc index e7b7fb9b99e6..6ee6eaafe958 100644 --- a/content/browser/devtools/devtools_http_handler_impl.cc +++ b/content/browser/devtools/devtools_http_handler_impl.cc @@ -22,8 +22,8 @@ #include "content/browser/devtools/devtools_protocol_constants.h" #include "content/browser/devtools/devtools_system_info_handler.h" #include "content/browser/devtools/protocol/devtools_protocol_handler_impl.h" +#include "content/browser/devtools/protocol/tethering_handler.h" #include "content/browser/devtools/protocol/tracing_handler.h" -#include "content/browser/devtools/tethering_handler.h" #include "content/common/devtools_messages.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/devtools_agent_host.h" @@ -156,15 +156,19 @@ class DevToolsHttpHandlerImpl::BrowserTarget { public: BrowserTarget(base::MessageLoop* message_loop, net::HttpServer* server, + DevToolsHttpHandlerDelegate* delegate, int connection_id) : message_loop_(message_loop), server_(server), connection_id_(connection_id), + tethering_handler_(new devtools::tethering::TetheringHandler( + delegate, message_loop->message_loop_proxy())), tracing_handler_(new devtools::tracing::TracingHandler( devtools::tracing::TracingHandler::Browser)), protocol_handler_(new DevToolsProtocolHandlerImpl()) { protocol_handler_->SetNotifier( base::Bind(&BrowserTarget::Respond, base::Unretained(this))); + protocol_handler_->SetTetheringHandler(tethering_handler_.get()); protocol_handler_->SetTracingHandler(tracing_handler_.get()); } @@ -220,6 +224,7 @@ class DevToolsHttpHandlerImpl::BrowserTarget { base::MessageLoop* const message_loop_; net::HttpServer* const server_; const int connection_id_; + scoped_ptr tethering_handler_; scoped_ptr tracing_handler_; scoped_ptr protocol_handler_; std::vector handlers_; @@ -701,9 +706,7 @@ void DevToolsHttpHandlerImpl::OnWebSocketRequestUI( size_t browser_pos = request.path.find(browser_prefix); if (browser_pos == 0) { BrowserTarget* browser_target = new BrowserTarget( - thread_->message_loop(), server_.get(), connection_id); - browser_target->RegisterHandler( - new TetheringHandler(delegate_.get(), thread_->message_loop_proxy())); + thread_->message_loop(), server_.get(), delegate_.get(), connection_id); browser_target->RegisterHandler( new DevToolsSystemInfoHandler()); browser_targets_[connection_id] = browser_target; diff --git a/content/browser/devtools/protocol/devtools_protocol_handler_generator.py b/content/browser/devtools/protocol/devtools_protocol_handler_generator.py index 3d3393f68f83..9138e2605e16 100755 --- a/content/browser/devtools/protocol/devtools_protocol_handler_generator.py +++ b/content/browser/devtools/protocol/devtools_protocol_handler_generator.py @@ -7,9 +7,10 @@ import sys import string import json -input_json_path = sys.argv[1] -output_cc_path = sys.argv[2] -output_h_path = sys.argv[3] +blink_protocol_path = sys.argv[1] +browser_protocol_path = sys.argv[2] +output_cc_path = sys.argv[3] +output_h_path = sys.argv[4] header = """\ // Copyright 2014 The Chromium Authors. All rights reserved. @@ -19,7 +20,8 @@ header = """\ // THIS FILE IS AUTOGENERATED. DO NOT EDIT. // Generated by // content/public/browser/devtools_protocol_handler_generator.py from -// third_party/WebKit/Source/devtools/protocol.json +// third_party/WebKit/Source/devtools/protocol.json and +// content/browser/devtools/browser_protocol.json """ template_h = string.Template(header + """\ @@ -427,13 +429,16 @@ def Uncamelcase(s): return result types = {} -json_api = json.loads(open(input_json_path, "r").read()) +blink_protocol = json.loads(open(blink_protocol_path, "r").read()) +browser_protocol = json.loads(open(browser_protocol_path, "r").read()) type_decls = [] type_impls = [] handler_methods = [] handler_method_impls = [] -for json_domain in json_api["domains"]: +all_domains = blink_protocol["domains"] + browser_protocol["domains"] + +for json_domain in all_domains: if "types" in json_domain: for json_type in json_domain["types"]: types["%s.%s" % (json_domain["domain"], json_type["id"])] = json_type @@ -589,7 +594,7 @@ fields = [] includes = [] fields_init = [] -for json_domain in json_api["domains"]: +for json_domain in all_domains: domain_map = {} domain_map["Domain"] = json_domain["domain"] domain_map["domain"] = Uncamelcase(json_domain["domain"]) @@ -619,7 +624,6 @@ for json_domain in json_api["domains"]: param_map = command_map.copy() param_map["proto_param"] = json_param["name"] param_map["param"] = Uncamelcase(json_param["name"]) - ResolveType(json_param, param_map) if len(prep) == 0: prep.append(params_prep) @@ -659,7 +663,6 @@ for json_domain in json_api["domains"]: param_map = command_map.copy() param_map["proto_param"] = json_param["name"] param_map["param"] = Uncamelcase(json_param["name"]) - if json_param.get("optional"): # TODO(vkuzkokov) Implement Optional for value types. raise Exception("Optional return values are not implemented") @@ -667,7 +670,6 @@ for json_domain in json_api["domains"]: prep.append(tmpl_prep_output.substitute(param_map)) args.append(param_map["arg_out"]) wrap.append(tmpl_wrap.substitute(param_map)) - args_str = "" if len(args) > 0: args_str = "\n " + ",\n ".join(args) diff --git a/content/browser/devtools/protocol/system_info_handler.cc b/content/browser/devtools/protocol/system_info_handler.cc new file mode 100644 index 000000000000..2a0366e95900 --- /dev/null +++ b/content/browser/devtools/protocol/system_info_handler.cc @@ -0,0 +1,25 @@ +// 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 "content/browser/devtools/protocol/system_info_handler.h" + +namespace content { +namespace devtools { +namespace system_info { + +typedef DevToolsProtocolClient::Response Response; + +SystemInfoHandler::SystemInfoHandler() { +} + +SystemInfoHandler::~SystemInfoHandler() { +} + +Response SystemInfoHandler::GetInfo(scoped_refptr* info) { + return Response::FallThrough(); +} + +} // namespace system_info +} // namespace devtools +} // namespace content diff --git a/content/browser/devtools/protocol/system_info_handler.h b/content/browser/devtools/protocol/system_info_handler.h new file mode 100644 index 000000000000..57812752fabf --- /dev/null +++ b/content/browser/devtools/protocol/system_info_handler.h @@ -0,0 +1,31 @@ +// 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. + +#ifndef CONTENT_BROWSER_DEVTOOLS_PROTOCOL_SYSTEM_INFO_HANDLER_H_ +#define CONTENT_BROWSER_DEVTOOLS_PROTOCOL_SYSTEM_INFO_HANDLER_H_ + +#include "content/browser/devtools/protocol/devtools_protocol_handler_impl.h" + +namespace content { +namespace devtools { +namespace system_info { + +class SystemInfoHandler { + public: + typedef DevToolsProtocolClient::Response Response; + + SystemInfoHandler(); + virtual ~SystemInfoHandler(); + + Response GetInfo(scoped_refptr* info); + + private: + DISALLOW_COPY_AND_ASSIGN(SystemInfoHandler); +}; + +} // namespace system_info +} // namespace devtools +} // namespace content + +#endif // CONTENT_BROWSER_DEVTOOLS_PROTOCOL_SYSTEM_INFO_HANDLER_H_ diff --git a/content/browser/devtools/tethering_handler.cc b/content/browser/devtools/protocol/tethering_handler.cc similarity index 83% rename from content/browser/devtools/tethering_handler.cc rename to content/browser/devtools/protocol/tethering_handler.cc index 5f039117ff42..3706a2d69caf 100644 --- a/content/browser/devtools/tethering_handler.cc +++ b/content/browser/devtools/protocol/tethering_handler.cc @@ -1,26 +1,20 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// 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 "content/browser/devtools/tethering_handler.h" +#include "content/browser/devtools/protocol/tethering_handler.h" -#include "base/bind.h" -#include "base/callback.h" -#include "base/stl_util.h" -#include "base/values.h" -#include "content/browser/devtools/devtools_http_handler_impl.h" -#include "content/browser/devtools/devtools_protocol_constants.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/devtools_http_handler_delegate.h" #include "net/base/io_buffer.h" -#include "net/base/ip_endpoint.h" #include "net/base/net_errors.h" -#include "net/base/net_log.h" #include "net/socket/server_socket.h" #include "net/socket/stream_socket.h" #include "net/socket/tcp_server_socket.h" namespace content { +namespace devtools { +namespace tethering { namespace { @@ -159,17 +153,6 @@ class SocketPump { bool pending_destruction_; }; -static int GetPort(scoped_refptr command, - const std::string& paramName) { - base::DictionaryValue* params = command->params(); - int port = 0; - if (!params || - !params->GetInteger(paramName, &port) || - port < kMinTetheringPort || port > kMaxTetheringPort) - return 0; - return port; -} - class BoundSocket { public: typedef base::Callback AcceptedCallback; @@ -351,12 +334,6 @@ TetheringHandler::TetheringHandler( message_loop_proxy_(message_loop_proxy), is_active_(false), weak_factory_(this) { - RegisterCommandHandler(devtools::Tethering::bind::kName, - base::Bind(&TetheringHandler::OnBind, - base::Unretained(this))); - RegisterCommandHandler(devtools::Tethering::unbind::kName, - base::Bind(&TetheringHandler::OnUnbind, - base::Unretained(this))); } TetheringHandler::~TetheringHandler() { @@ -366,11 +343,13 @@ TetheringHandler::~TetheringHandler() { } } +void TetheringHandler::SetClient(scoped_ptr client) { + client_.swap(client); +} + void TetheringHandler::Accepted(int port, const std::string& name) { - base::DictionaryValue* params = new base::DictionaryValue(); - params->SetInteger(devtools::Tethering::accepted::kParamPort, port); - params->SetString(devtools::Tethering::accepted::kParamConnectionId, name); - SendNotification(devtools::Tethering::accepted::kName, params); + client_->Accepted(AcceptedParams::Create()->set_port(port) + ->set_connection_id(name)); } bool TetheringHandler::Activate() { @@ -383,12 +362,10 @@ bool TetheringHandler::Activate() { return true; } -scoped_refptr -TetheringHandler::OnBind(scoped_refptr command) { - const std::string& portParamName = devtools::Tethering::bind::kParamPort; - int port = GetPort(command, portParamName); - if (port == 0) - return command->InvalidParamResponse(portParamName); +scoped_refptr TetheringHandler::Bind( + int port, scoped_refptr command) { + if (port < kMinTetheringPort || port > kMaxTetheringPort) + return command->InvalidParamResponse("port"); if (!Activate()) { return command->ServerErrorResponse( @@ -402,13 +379,8 @@ TetheringHandler::OnBind(scoped_refptr command) { return command->AsyncResponsePromise(); } -scoped_refptr -TetheringHandler::OnUnbind(scoped_refptr command) { - const std::string& portParamName = devtools::Tethering::unbind::kParamPort; - int port = GetPort(command, portParamName); - if (port == 0) - return command->InvalidParamResponse(portParamName); - +scoped_refptr TetheringHandler::Unbind( + int port, scoped_refptr command) { if (!Activate()) { return command->ServerErrorResponse( "Tethering is used by another connection"); @@ -423,18 +395,20 @@ TetheringHandler::OnUnbind(scoped_refptr command) { void TetheringHandler::SendBindSuccess( scoped_refptr command) { - SendAsyncResponse(command->SuccessResponse(nullptr)); + client_->SendBindResponse(command, BindResponse::Create()); } void TetheringHandler::SendUnbindSuccess( scoped_refptr command) { - SendAsyncResponse(command->SuccessResponse(nullptr)); + client_->SendUnbindResponse(command, UnbindResponse::Create()); } void TetheringHandler::SendInternalError( scoped_refptr command, const std::string& message) { - SendAsyncResponse(command->InternalErrorResponse(message)); + client_->SendInternalErrorResponse(command, message); } +} // namespace tethering +} // namespace devtools } // namespace content diff --git a/content/browser/devtools/tethering_handler.h b/content/browser/devtools/protocol/tethering_handler.h similarity index 56% rename from content/browser/devtools/tethering_handler.h rename to content/browser/devtools/protocol/tethering_handler.h index d6e56b592fd7..a1b808217258 100644 --- a/content/browser/devtools/tethering_handler.h +++ b/content/browser/devtools/protocol/tethering_handler.h @@ -1,26 +1,34 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// 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. -#ifndef CONTENT_BROWSER_DEVTOOLS_TETHERING_HANDLER_H_ -#define CONTENT_BROWSER_DEVTOOLS_TETHERING_HANDLER_H_ +#ifndef CONTENT_BROWSER_DEVTOOLS_PROTOCOL_TETHERING_HANDLER_H_ +#define CONTENT_BROWSER_DEVTOOLS_PROTOCOL_TETHERING_HANDLER_H_ -#include - -#include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" -#include "content/browser/devtools/devtools_protocol.h" +#include "base/message_loop/message_loop_proxy.h" +#include "content/browser/devtools/protocol/devtools_protocol_handler_impl.h" namespace content { class DevToolsHttpHandlerDelegate; +namespace devtools { +namespace tethering { + // This class implements reversed tethering handler. -class TetheringHandler : public DevToolsProtocol::Handler { +class TetheringHandler { public: TetheringHandler(DevToolsHttpHandlerDelegate* delegate, scoped_refptr message_loop_proxy); - ~TetheringHandler() override; + ~TetheringHandler(); + + void SetClient(scoped_ptr client); + + scoped_refptr Bind( + int port, scoped_refptr command); + scoped_refptr Unbind( + int port, scoped_refptr command); private: class TetheringImpl; @@ -28,16 +36,12 @@ class TetheringHandler : public DevToolsProtocol::Handler { void Accepted(int port, const std::string& name); bool Activate(); - scoped_refptr OnBind( - scoped_refptr command); - scoped_refptr OnUnbind( - scoped_refptr command); - void SendBindSuccess(scoped_refptr command); void SendUnbindSuccess(scoped_refptr command); void SendInternalError(scoped_refptr command, const std::string& message); + scoped_ptr client_; DevToolsHttpHandlerDelegate* delegate_; scoped_refptr message_loop_proxy_; bool is_active_; @@ -48,6 +52,8 @@ class TetheringHandler : public DevToolsProtocol::Handler { DISALLOW_COPY_AND_ASSIGN(TetheringHandler); }; +} // namespace tethering +} // namespace devtools } // namespace content -#endif // CONTENT_BROWSER_DEVTOOLS_TETHERING_HANDLER_H_ +#endif // CONTENT_BROWSER_DEVTOOLS_PROTOCOL_TETHERING_HANDLER_H_ diff --git a/content/content_browser.gypi b/content/content_browser.gypi index 9a7f4203c1ce..5645a74408d6 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -459,6 +459,10 @@ 'browser/devtools/protocol/page_handler.h', 'browser/devtools/protocol/power_handler.cc', 'browser/devtools/protocol/power_handler.h', + 'browser/devtools/protocol/system_info_handler.cc', + 'browser/devtools/protocol/system_info_handler.h', + 'browser/devtools/protocol/tethering_handler.cc', + 'browser/devtools/protocol/tethering_handler.h', 'browser/devtools/protocol/tracing_handler.cc', 'browser/devtools/protocol/tracing_handler.h', 'browser/devtools/protocol/usage_and_quota_query.cc', @@ -467,8 +471,6 @@ 'browser/devtools/protocol/worker_handler.h', 'browser/devtools/render_view_devtools_agent_host.cc', 'browser/devtools/render_view_devtools_agent_host.h', - 'browser/devtools/tethering_handler.h', - 'browser/devtools/tethering_handler.cc', 'browser/device_monitor_mac.h', 'browser/device_monitor_mac.mm', 'browser/device_monitor_udev.cc', -- 2.11.4.GIT