| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include <algorithm> | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/debug/profiler.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/strings/stringprintf.h" | |
| 11 #include "mojo/application/application_runner_chromium.h" | |
| 12 #include "mojo/common/data_pipe_utils.h" | |
| 13 #include "mojo/public/c/system/main.h" | |
| 14 #include "mojo/public/cpp/application/application_delegate.h" | |
| 15 #include "mojo/public/cpp/application/application_impl.h" | |
| 16 #include "mojo/public/cpp/bindings/binding.h" | |
| 17 #include "mojo/services/http_server/public/cpp/http_server_util.h" | |
| 18 #include "mojo/services/http_server/public/interfaces/http_server.mojom.h" | |
| 19 #include "mojo/services/http_server/public/interfaces/http_server_factory.mojom.
h" | |
| 20 #include "mojo/services/network/public/interfaces/net_address.mojom.h" | |
| 21 #include "mojo/services/window_manager/public/interfaces/window_manager.mojom.h" | |
| 22 #include "services/tracing/tracing.mojom.h" | |
| 23 #include "sky/tools/debugger/trace_collector.h" | |
| 24 | |
| 25 namespace sky { | |
| 26 namespace debugger { | |
| 27 | |
| 28 class SkyDebugger : public mojo::ApplicationDelegate, | |
| 29 public http_server::HttpHandler { | |
| 30 public: | |
| 31 SkyDebugger() : is_tracing_(false), app_(nullptr), handler_binding_(this) {} | |
| 32 ~SkyDebugger() override {} | |
| 33 | |
| 34 private: | |
| 35 // mojo::ApplicationDelegate: | |
| 36 void Initialize(mojo::ApplicationImpl* app) override { | |
| 37 app_ = app; | |
| 38 app->ConnectToService("mojo:window_manager", &window_manager_); | |
| 39 | |
| 40 // Format: --args-for="app_url command_port" | |
| 41 if (app->args().size() < 2) { | |
| 42 LOG(ERROR) << "--args-for required to specify command_port"; | |
| 43 mojo::ApplicationImpl::Terminate(); | |
| 44 return; | |
| 45 } | |
| 46 base::StringToUint(app->args()[1], &command_port_); | |
| 47 http_server::HttpServerFactoryPtr http_server_factory; | |
| 48 app->ConnectToService("mojo:http_server", &http_server_factory); | |
| 49 | |
| 50 mojo::NetAddressPtr local_address(mojo::NetAddress::New()); | |
| 51 local_address->family = mojo::NET_ADDRESS_FAMILY_IPV4; | |
| 52 local_address->ipv4 = mojo::NetAddressIPv4::New(); | |
| 53 local_address->ipv4->addr.resize(4); | |
| 54 local_address->ipv4->addr[0] = 127; | |
| 55 local_address->ipv4->addr[1] = 0; | |
| 56 local_address->ipv4->addr[2] = 0; | |
| 57 local_address->ipv4->addr[3] = 1; | |
| 58 local_address->ipv4->port = command_port_; | |
| 59 http_server_factory->CreateHttpServer(GetProxy(&http_server_).Pass(), | |
| 60 local_address.Pass()); | |
| 61 | |
| 62 http_server::HttpHandlerPtr handler_ptr; | |
| 63 handler_binding_.Bind(GetProxy(&handler_ptr).Pass()); | |
| 64 http_server_->SetHandler(".*", handler_ptr.Pass(), | |
| 65 [](bool result) { DCHECK(result); }); | |
| 66 } | |
| 67 | |
| 68 bool ConfigureIncomingConnection( | |
| 69 mojo::ApplicationConnection* connection) override { | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 73 // http_server::HttpHandler: | |
| 74 void HandleRequest(http_server::HttpRequestPtr request, | |
| 75 const HandleRequestCallback& callback) override { | |
| 76 // FIXME: We should use use a fancier lookup system more like what | |
| 77 // services/http_server/http_server.cc does with AddHandler. | |
| 78 if (request->relative_url == "/reload") { | |
| 79 Load(callback, url_); | |
| 80 } else if (request->relative_url == "/quit") { | |
| 81 Exit(); | |
| 82 } else if (request->relative_url == "/load") { | |
| 83 std::string url; | |
| 84 mojo::common::BlockingCopyToString(request->body.Pass(), &url); | |
| 85 Load(callback, url); | |
| 86 } else if (request->relative_url == "/start_profiling") { | |
| 87 StartProfiling(callback); | |
| 88 } else if (request->relative_url == "/stop_profiling") { | |
| 89 StopProfiling(callback); | |
| 90 } else if (request->relative_url == "/start_tracing") { | |
| 91 StartTracing(callback); | |
| 92 } else if (request->relative_url == "/stop_tracing") { | |
| 93 StopTracing(callback); | |
| 94 } else { | |
| 95 Help(callback, request->relative_url); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void Error(const HandleRequestCallback& callback, std::string message) { | |
| 100 callback.Run(http_server::CreateHttpResponse(500, message)); | |
| 101 } | |
| 102 | |
| 103 void Respond(const HandleRequestCallback& callback, std::string response) { | |
| 104 callback.Run(http_server::CreateHttpResponse(200, response)); | |
| 105 } | |
| 106 | |
| 107 void Help(const HandleRequestCallback& callback, std::string path) { | |
| 108 std::string help = base::StringPrintf( | |
| 109 "Sky Debugger running on port %d\n" | |
| 110 "Supported URLs:\n" | |
| 111 "/reload -- Reload the current page\n" | |
| 112 "/quit -- Quit\n" | |
| 113 "/load -- Load a new URL, url in POST body.\n", | |
| 114 command_port_); | |
| 115 if (path != "/") | |
| 116 help = "Unknown path: " + path + "\n\n" + help; | |
| 117 Respond(callback, help); | |
| 118 } | |
| 119 | |
| 120 void Load(const HandleRequestCallback& callback, std::string url) { | |
| 121 url_ = url; | |
| 122 Reload(); | |
| 123 std::string response = std::string("Loaded ") + url + "\n"; | |
| 124 Respond(callback, response); | |
| 125 } | |
| 126 | |
| 127 void Reload() { | |
| 128 // SimpleWindowManager will wire up necessary services on our behalf. | |
| 129 window_manager_->Embed(url_, nullptr, nullptr); | |
| 130 } | |
| 131 | |
| 132 void Exit() { | |
| 133 // TODO(eseidel): We should orderly shutdown once mojo can. | |
| 134 exit(0); | |
| 135 } | |
| 136 | |
| 137 void StartTracing(const HandleRequestCallback& callback) { | |
| 138 if (is_tracing_) { | |
| 139 Error(callback, "Already tracing. Use stop_tracing to stop.\n"); | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 if (!tracing_) | |
| 144 app_->ConnectToService("mojo:tracing", &tracing_); | |
| 145 is_tracing_ = true; | |
| 146 mojo::DataPipe pipe; | |
| 147 tracing_->Start(pipe.producer_handle.Pass(), mojo::String("*")); | |
| 148 trace_collector_.reset(new TraceCollector(pipe.consumer_handle.Pass())); | |
| 149 Respond(callback, "Starting trace (type 'stop_tracing' to stop)\n"); | |
| 150 } | |
| 151 | |
| 152 void StopTracing(const HandleRequestCallback& callback) { | |
| 153 if (!is_tracing_) { | |
| 154 Error(callback, "Not tracing yet. Use start_tracing to start.\n"); | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 is_tracing_ = false; | |
| 159 tracing_->StopAndFlush(); | |
| 160 trace_collector_->GetTrace(base::Bind(&SkyDebugger::OnTraceAvailable, | |
| 161 base::Unretained(this), callback)); | |
| 162 } | |
| 163 | |
| 164 void OnTraceAvailable(HandleRequestCallback callback, std::string trace) { | |
| 165 trace_collector_.reset(); | |
| 166 Respond(callback, trace); | |
| 167 } | |
| 168 | |
| 169 void StartProfiling(const HandleRequestCallback& callback) { | |
| 170 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING) | |
| 171 Error(callback, | |
| 172 "Profiling requires is_debug=false and enable_profiling=true"); | |
| 173 return; | |
| 174 #else | |
| 175 base::debug::StartProfiling("sky_viewer.pprof"); | |
| 176 Respond(callback, "Starting profiling (stop with 'stop_profiling')"); | |
| 177 #endif | |
| 178 } | |
| 179 | |
| 180 void StopProfiling(const HandleRequestCallback& callback) { | |
| 181 if (!base::debug::BeingProfiled()) { | |
| 182 Error(callback, "Profiling not started"); | |
| 183 return; | |
| 184 } | |
| 185 base::debug::StopProfiling(); | |
| 186 Respond(callback, "Stopped profiling"); | |
| 187 } | |
| 188 | |
| 189 bool is_tracing_; | |
| 190 mojo::ApplicationImpl* app_; | |
| 191 mojo::WindowManagerPtr window_manager_; | |
| 192 tracing::TraceCoordinatorPtr tracing_; | |
| 193 std::string url_; | |
| 194 uint32_t command_port_; | |
| 195 | |
| 196 http_server::HttpServerPtr http_server_; | |
| 197 mojo::Binding<http_server::HttpHandler> handler_binding_; | |
| 198 | |
| 199 scoped_ptr<TraceCollector> trace_collector_; | |
| 200 | |
| 201 DISALLOW_COPY_AND_ASSIGN(SkyDebugger); | |
| 202 }; | |
| 203 | |
| 204 } // namespace debugger | |
| 205 } // namespace sky | |
| 206 | |
| 207 MojoResult MojoMain(MojoHandle application_request) { | |
| 208 mojo::ApplicationRunnerChromium runner(new sky::debugger::SkyDebugger); | |
| 209 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); | |
| 210 return runner.Run(application_request); | |
| 211 } | |
| OLD | NEW |