Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1403)

Unified Diff: sky/tools/debugger/debugger.cc

Issue 930903002: Switch skydb to services/http_server. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sky/tools/debugger/BUILD.gn ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/tools/debugger/debugger.cc
diff --git a/sky/tools/debugger/debugger.cc b/sky/tools/debugger/debugger.cc
index 5089acf6b48541a8a9d1b73248e50164e9f68347..c189cfbd8651b6afc450d9bf526a65f847fbc587 100644
--- a/sky/tools/debugger/debugger.cc
+++ b/sky/tools/debugger/debugger.cc
@@ -9,56 +9,67 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "mojo/application/application_runner_chromium.h"
+#include "mojo/common/data_pipe_utils.h"
#include "mojo/public/c/system/main.h"
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/application_impl.h"
+#include "mojo/public/cpp/bindings/strong_binding.h"
#include "mojo/services/window_manager/public/interfaces/window_manager.mojom.h"
#include "net/base/net_errors.h"
-#include "net/server/http_server.h"
-#include "net/server/http_server_request_info.h"
-#include "net/socket/tcp_server_socket.h"
+#include "services/http_server/public/http_server.mojom.h"
+#include "services/http_server/public/http_server_factory.mojom.h"
+#include "services/http_server/public/http_server_util.h"
#include "services/tracing/tracing.mojom.h"
#include "sky/tools/debugger/trace_collector.h"
namespace sky {
namespace debugger {
-namespace {
-const size_t kMinSendBufferSize = 1024 * 1024;
-}
-
-class SkyDebugger : public mojo::ApplicationDelegate,
- public net::HttpServer::Delegate {
+class SkyDebugger : public mojo::ApplicationDelegate {
public:
SkyDebugger() : is_tracing_(false) {}
virtual ~SkyDebugger() {}
private:
+ typedef mojo::Callback<void(http_server::HttpResponsePtr)>
+ HttpResponseCallback;
qsr 2015/02/17 10:33:18 This typedef is already defined for you in the gen
ppi 2015/02/17 12:05:24 Done.
+ class Handler : public http_server::HttpHandler {
qsr 2015/02/17 10:33:18 You can probably get rid of this class by having S
ppi 2015/02/17 12:05:24 Done.
+ public:
+ Handler(mojo::InterfaceRequest<HttpHandler> request, SkyDebugger* debugger)
+ : binding_(this, request.Pass()), debugger_(debugger) {}
+
+ private:
+ // http_server::HttpHandler:
+ void HandleRequest(http_server::HttpRequestPtr request,
+ const HttpResponseCallback& callback) override {
+ debugger_->OnHttpRequest(request.Pass(), callback);
+ }
+
+ mojo::StrongBinding<http_server::HttpHandler> binding_;
+ SkyDebugger* debugger_;
+ };
+
// Overridden from mojo::ApplicationDelegate:
virtual void Initialize(mojo::ApplicationImpl* app) override {
app->ConnectToService("mojo:tracing", &tracing_);
+ app->ConnectToService("mojo:window_manager", &window_manager_);
+
// Format: --args-for="app_url command_port"
if (app->args().size() < 2) {
LOG(ERROR) << "--args-for required to specify command_port";
mojo::ApplicationImpl::Terminate();
return;
}
-
base::StringToUint(app->args()[1], &command_port_);
+ http_server::HttpServerFactoryPtr http_server_factory;
+ app->ConnectToService("mojo:http_server", &http_server_factory);
+ http_server_factory->CreateHttpServer(GetProxy(&http_server_).Pass(),
+ command_port_);
- scoped_ptr<net::ServerSocket> server_socket(
- new net::TCPServerSocket(NULL, net::NetLog::Source()));
- int result =
- server_socket->ListenWithAddressAndPort("0.0.0.0", command_port_, 1);
- if (result != net::OK) {
- LOG(ERROR) << "Failed to bind to port " << command_port_
- << " skydb commands will not work.";
- mojo::ApplicationImpl::Terminate();
- return;
- }
- web_server_.reset(new net::HttpServer(server_socket.Pass(), this));
-
- app->ConnectToService("mojo:window_manager", &window_manager_);
+ http_server::HttpHandlerPtr handler_ptr;
+ new Handler(GetProxy(&handler_ptr).Pass(), this);
+ http_server_->SetHandler(".*", handler_ptr.Pass(),
+ [](bool result) { DCHECK(result); });
}
virtual bool ConfigureIncomingConnection(
@@ -66,55 +77,40 @@ class SkyDebugger : public mojo::ApplicationDelegate,
return true;
}
- // net::HttpServer::Delegate
- void OnConnect(int connection_id) override {}
-
- void OnClose(int connection_id) override {}
-
- void OnHttpRequest(int connection_id,
- const net::HttpServerRequestInfo& info) override {
+ void OnHttpRequest(http_server::HttpRequestPtr request,
+ const HttpResponseCallback& callback) {
// FIXME: We should use use a fancier lookup system more like what
// services/http_server/http_server.cc does with AddHandler.
- if (info.path == "/reload")
- Load(connection_id, url_);
- else if (info.path == "/quit")
- Quit(connection_id);
- else if (info.path == "/load")
- Load(connection_id, info.data);
- else if (info.path == "/start_profiling")
- StartProfiling(connection_id);
- else if (info.path == "/stop_profiling")
- StopProfiling(connection_id);
- else if (info.path == "/start_tracing")
- StartTracing(connection_id);
- else if (info.path == "/stop_tracing")
- StopTracing(connection_id);
- else
- Help(info.path, connection_id);
- }
-
- void OnWebSocketRequest(int connection_id,
- const net::HttpServerRequestInfo& info) override {
- Error(connection_id, "OnWebSocketRequest not implemented");
- }
-
- void OnWebSocketMessage(int connection_id, const std::string& data) override {
- Error(connection_id, "OnWebSocketMessage not implemented");
+ if (request->relative_url == "/reload") {
+ Load(callback, url_);
+ } else if (request->relative_url == "/quit") {
+ Quit();
+ } else if (request->relative_url == "/load") {
+ std::string url;
+ mojo::common::BlockingCopyToString(request->body.Pass(), &url);
+ Load(callback, url);
+ } else if (request->relative_url == "/start_profiling") {
+ StartProfiling(callback);
+ } else if (request->relative_url == "/stop_profiling") {
+ StopProfiling(callback);
+ } else if (request->relative_url == "/start_tracing") {
+ StartTracing(callback);
+ } else if (request->relative_url == "/stop_tracing") {
+ StopTracing(callback);
+ } else {
+ Help(callback, request->relative_url);
+ }
}
- void Error(int connection_id, std::string message) {
- web_server_->Send500(connection_id, message);
+ void Error(const HttpResponseCallback& callback, std::string message) {
+ callback.Run(http_server::CreateHttpResponse(500, message));
}
- void Respond(int connection_id, std::string response) {
- // When sending tracing data back over the wire to the client, we can blow
- // through the default send buffer size.
- web_server_->SetSendBufferSize(
- connection_id, std::max(kMinSendBufferSize, response.length()));
- web_server_->Send200(connection_id, response, "text/plain");
+ void Respond(const HttpResponseCallback& callback, std::string response) {
+ callback.Run(http_server::CreateHttpResponse(200, response));
}
- void Help(std::string path, int connection_id) {
+ void Help(const HttpResponseCallback& callback, std::string path) {
std::string help = base::StringPrintf(
"Sky Debugger running on port %d\n"
"Supported URLs:\n"
@@ -124,14 +120,14 @@ class SkyDebugger : public mojo::ApplicationDelegate,
command_port_);
if (path != "/")
help = "Unknown path: " + path + "\n\n" + help;
- Respond(connection_id, help);
+ Respond(callback, help);
}
- void Load(int connection_id, std::string url) {
+ void Load(const HttpResponseCallback& callback, std::string url) {
url_ = url;
Reload();
std::string response = std::string("Loaded ") + url + "\n";
- Respond(connection_id, response);
+ Respond(callback, response);
}
void Reload() {
@@ -139,14 +135,14 @@ class SkyDebugger : public mojo::ApplicationDelegate,
window_manager_->Embed(url_, nullptr, nullptr);
}
- void Quit(int connection_id) {
+ void Quit() {
// TODO(eseidel): We should orderly shutdown once mojo can.
exit(0);
}
- void StartTracing(int connection_id) {
+ void StartTracing(const HttpResponseCallback& callback) {
if (is_tracing_) {
- Error(connection_id, "Already tracing. Use stop_tracing to stop.\n");
+ Error(callback, "Already tracing. Use stop_tracing to stop.\n");
return;
}
@@ -154,53 +150,54 @@ class SkyDebugger : public mojo::ApplicationDelegate,
mojo::DataPipe pipe;
tracing_->Start(pipe.producer_handle.Pass(), mojo::String("*"));
trace_collector_.reset(new TraceCollector(pipe.consumer_handle.Pass()));
- Respond(connection_id, "Starting trace (type 'stop_tracing' to stop)\n");
+ Respond(callback, "Starting trace (type 'stop_tracing' to stop)\n");
}
- void StopTracing(int connection_id) {
+ void StopTracing(const HttpResponseCallback& callback) {
if (!is_tracing_) {
- Error(connection_id, "Not tracing yet. Use start_tracing to start.\n");
+ Error(callback, "Not tracing yet. Use start_tracing to start.\n");
return;
}
is_tracing_ = false;
tracing_->StopAndFlush();
- trace_collector_->GetTrace(base::Bind(
- &SkyDebugger::OnTraceAvailable, base::Unretained(this), connection_id));
+ trace_collector_->GetTrace(base::Bind(&SkyDebugger::OnTraceAvailable,
+ base::Unretained(this), callback));
}
- void OnTraceAvailable(int connection_id, std::string trace) {
+ void OnTraceAvailable(HttpResponseCallback callback, std::string trace) {
trace_collector_.reset();
- Respond(connection_id, trace);
+ Respond(callback, trace);
}
- void StartProfiling(int connection_id) {
+ void StartProfiling(const HttpResponseCallback& callback) {
#if !defined(NDEBUG) || !defined(ENABLE_PROFILING)
- Error(connection_id,
+ Error(callback,
"Profiling requires is_debug=false and enable_profiling=true");
return;
#else
base::debug::StartProfiling("sky_viewer.pprof");
- Respond(connection_id, "Starting profiling (stop with 'stop_profiling')");
+ Respond(callback, "Starting profiling (stop with 'stop_profiling')");
#endif
}
- void StopProfiling(int connection_id) {
+ void StopProfiling(const HttpResponseCallback& callback) {
if (!base::debug::BeingProfiled()) {
- Error(connection_id, "Profiling not started");
+ Error(callback, "Profiling not started");
return;
}
base::debug::StopProfiling();
- Respond(connection_id, "Stopped profiling");
+ Respond(callback, "Stopped profiling");
}
bool is_tracing_;
mojo::WindowManagerPtr window_manager_;
tracing::TraceCoordinatorPtr tracing_;
std::string url_;
- scoped_ptr<net::HttpServer> web_server_;
uint32_t command_port_;
+ http_server::HttpServerPtr http_server_;
+
scoped_ptr<TraceCollector> trace_collector_;
DISALLOW_COPY_AND_ASSIGN(SkyDebugger);
« no previous file with comments | « sky/tools/debugger/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698