| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> |
| 6 |
| 5 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" |
| 6 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 7 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 9 #include "mojo/application/application_runner_chromium.h" | 12 #include "mojo/application/application_runner_chromium.h" |
| 10 #include "mojo/public/c/system/main.h" | 13 #include "mojo/public/c/system/main.h" |
| 11 #include "mojo/public/cpp/application/application_delegate.h" | 14 #include "mojo/public/cpp/application/application_delegate.h" |
| 12 #include "mojo/public/cpp/application/application_impl.h" | 15 #include "mojo/public/cpp/application/application_impl.h" |
| 13 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 14 #include "net/server/http_server.h" | 17 #include "net/server/http_server.h" |
| 15 #include "net/server/http_server_request_info.h" | 18 #include "net/server/http_server_request_info.h" |
| 16 #include "net/socket/tcp_server_socket.h" | 19 #include "net/socket/tcp_server_socket.h" |
| 17 #include "services/tracing/tracing.mojom.h" | 20 #include "services/tracing/tracing.mojom.h" |
| 18 #include "sky/tools/debugger/debugger.mojom.h" | 21 #include "sky/tools/debugger/debugger.mojom.h" |
| 22 #include "sky/tools/debugger/prompt/trace_collector.h" |
| 19 | 23 |
| 20 namespace sky { | 24 namespace sky { |
| 21 namespace debugger { | 25 namespace debugger { |
| 26 namespace { |
| 22 | 27 |
| 23 class Prompt : public mojo::ApplicationDelegate, public net::HttpServer::Delegat
e { | 28 const size_t kMinSendBufferSize = 1024 * 1024; |
| 29 } |
| 30 |
| 31 class Prompt : public mojo::ApplicationDelegate, |
| 32 public net::HttpServer::Delegate { |
| 24 public: | 33 public: |
| 25 Prompt() | 34 Prompt() |
| 26 : is_tracing_(false), | 35 : is_tracing_(false), |
| 27 weak_ptr_factory_(this) { | 36 weak_ptr_factory_(this) { |
| 28 } | 37 } |
| 29 virtual ~Prompt() { | 38 virtual ~Prompt() { |
| 30 } | 39 } |
| 31 | 40 |
| 32 private: | 41 private: |
| 33 // Overridden from mojo::ApplicationDelegate: | 42 // Overridden from mojo::ApplicationDelegate: |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 int connection_id, const net::HttpServerRequestInfo& info) override { | 100 int connection_id, const net::HttpServerRequestInfo& info) override { |
| 92 web_server_->Send500(connection_id, "http only"); | 101 web_server_->Send500(connection_id, "http only"); |
| 93 } | 102 } |
| 94 | 103 |
| 95 void OnWebSocketMessage( | 104 void OnWebSocketMessage( |
| 96 int connection_id, const std::string& data) override { | 105 int connection_id, const std::string& data) override { |
| 97 web_server_->Send500(connection_id, "http only"); | 106 web_server_->Send500(connection_id, "http only"); |
| 98 } | 107 } |
| 99 | 108 |
| 100 void Respond(int connection_id, std::string response) { | 109 void Respond(int connection_id, std::string response) { |
| 110 // When sending tracing data back over the wire to the client, we can blow |
| 111 // through the default send buffer size. |
| 112 web_server_->SetSendBufferSize( |
| 113 connection_id, std::max(kMinSendBufferSize, response.length())); |
| 101 web_server_->Send200(connection_id, response, "text/plain"); | 114 web_server_->Send200(connection_id, response, "text/plain"); |
| 102 } | 115 } |
| 103 | 116 |
| 104 void Help(std::string path, int connection_id) { | 117 void Help(std::string path, int connection_id) { |
| 105 std::string help = base::StringPrintf("Sky Debugger running on port %d\n" | 118 std::string help = base::StringPrintf("Sky Debugger running on port %d\n" |
| 106 "Supported URLs:\n" | 119 "Supported URLs:\n" |
| 107 "/toggle_tracing -- Start/stop tracing\n" | 120 "/toggle_tracing -- Start/stop tracing\n" |
| 108 "/reload -- Reload the current page\n" | 121 "/reload -- Reload the current page\n" |
| 109 "/inspect -- Start inspector server for current page\n" | 122 "/inspect -- Start inspector server for current page\n" |
| 110 "/quit -- Quit\n" | 123 "/quit -- Quit\n" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 131 Respond(connection_id, | 144 Respond(connection_id, |
| 132 "Open the following URL in Chrome:\n" | 145 "Open the following URL in Chrome:\n" |
| 133 "chrome-devtools://devtools/bundled/devtools.html?ws=localhost:9898\n"); | 146 "chrome-devtools://devtools/bundled/devtools.html?ws=localhost:9898\n"); |
| 134 } | 147 } |
| 135 | 148 |
| 136 void Quit(int connection_id) { | 149 void Quit(int connection_id) { |
| 137 debugger_->Shutdown(); | 150 debugger_->Shutdown(); |
| 138 } | 151 } |
| 139 | 152 |
| 140 void ToggleTracing(int connection_id) { | 153 void ToggleTracing(int connection_id) { |
| 141 std::string response; | 154 bool was_tracing = is_tracing_; |
| 142 if (is_tracing_) { | 155 is_tracing_ = !is_tracing_; |
| 143 response = "Stopping trace (writing to sky_viewer.trace)\n"; | 156 |
| 157 if (was_tracing) { |
| 144 tracing_->StopAndFlush(); | 158 tracing_->StopAndFlush(); |
| 145 } else { | 159 trace_collector_->GetTrace(base::Bind( |
| 146 response = "Starting trace (type 'trace' to stop tracing)\n"; | 160 &Prompt::OnTraceAvailable, base::Unretained(this), connection_id)); |
| 147 tracing_->Start(mojo::String("sky_viewer"), mojo::String("*")); | 161 return; |
| 148 } | 162 } |
| 149 is_tracing_ = !is_tracing_; | 163 |
| 150 Respond(connection_id, response); | 164 mojo::DataPipe pipe; |
| 165 tracing_->Start(pipe.producer_handle.Pass(), mojo::String("*")); |
| 166 trace_collector_.reset(new TraceCollector(pipe.consumer_handle.Pass())); |
| 167 Respond(connection_id, "Starting trace (type 'trace' to stop tracing)\n"); |
| 168 } |
| 169 |
| 170 void OnTraceAvailable(int connection_id, std::string trace) { |
| 171 trace_collector_.reset(); |
| 172 Respond(connection_id, trace); |
| 151 } | 173 } |
| 152 | 174 |
| 153 bool is_tracing_; | 175 bool is_tracing_; |
| 154 DebuggerPtr debugger_; | 176 DebuggerPtr debugger_; |
| 155 tracing::TraceCoordinatorPtr tracing_; | 177 tracing::TraceCoordinatorPtr tracing_; |
| 156 std::string url_; | 178 std::string url_; |
| 157 base::WeakPtrFactory<Prompt> weak_ptr_factory_; | 179 base::WeakPtrFactory<Prompt> weak_ptr_factory_; |
| 158 scoped_ptr<net::HttpServer> web_server_; | 180 scoped_ptr<net::HttpServer> web_server_; |
| 159 uint32_t command_port_; | 181 uint32_t command_port_; |
| 160 | 182 |
| 183 scoped_ptr<TraceCollector> trace_collector_; |
| 184 |
| 161 DISALLOW_COPY_AND_ASSIGN(Prompt); | 185 DISALLOW_COPY_AND_ASSIGN(Prompt); |
| 162 }; | 186 }; |
| 163 | 187 |
| 164 } // namespace debugger | 188 } // namespace debugger |
| 165 } // namespace sky | 189 } // namespace sky |
| 166 | 190 |
| 167 MojoResult MojoMain(MojoHandle shell_handle) { | 191 MojoResult MojoMain(MojoHandle shell_handle) { |
| 168 mojo::ApplicationRunnerChromium runner(new sky::debugger::Prompt); | 192 mojo::ApplicationRunnerChromium runner(new sky::debugger::Prompt); |
| 169 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); | 193 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); |
| 170 return runner.Run(shell_handle); | 194 return runner.Run(shell_handle); |
| 171 } | 195 } |
| OLD | NEW |