| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
| 7 #include "mojo/application/application_runner_chromium.h" | 7 #include "mojo/application/application_runner_chromium.h" |
| 8 #include "mojo/public/c/system/main.h" | 8 #include "mojo/public/c/system/main.h" |
| 9 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
| 10 #include "mojo/public/cpp/application/application_impl.h" | 10 #include "mojo/public/cpp/application/application_impl.h" |
| 11 #include "net/base/net_errors.h" |
| 11 #include "net/server/http_server.h" | 12 #include "net/server/http_server.h" |
| 12 #include "net/server/http_server_request_info.h" | 13 #include "net/server/http_server_request_info.h" |
| 13 #include "net/socket/tcp_server_socket.h" | 14 #include "net/socket/tcp_server_socket.h" |
| 14 #include "services/tracing/tracing.mojom.h" | 15 #include "services/tracing/tracing.mojom.h" |
| 15 #include "sky/tools/debugger/debugger.mojom.h" | 16 #include "sky/tools/debugger/debugger.mojom.h" |
| 16 #include <iostream> | |
| 17 | 17 |
| 18 namespace sky { | 18 namespace sky { |
| 19 namespace debugger { | 19 namespace debugger { |
| 20 | 20 |
| 21 class Prompt : public mojo::ApplicationDelegate, public net::HttpServer::Delegat
e { | 21 class Prompt : public mojo::ApplicationDelegate, public net::HttpServer::Delegat
e { |
| 22 public: | 22 public: |
| 23 Prompt() | 23 Prompt() |
| 24 : is_tracing_(false), | 24 : is_tracing_(false), |
| 25 weak_ptr_factory_(this) { | 25 weak_ptr_factory_(this) { |
| 26 } | 26 } |
| 27 virtual ~Prompt() { | 27 virtual ~Prompt() { |
| 28 } | 28 } |
| 29 | 29 |
| 30 private: | 30 private: |
| 31 // Overridden from mojo::ApplicationDelegate: | 31 // Overridden from mojo::ApplicationDelegate: |
| 32 virtual void Initialize(mojo::ApplicationImpl* app) override { | 32 virtual void Initialize(mojo::ApplicationImpl* app) override { |
| 33 app->ConnectToService("mojo:tracing", &tracing_); | 33 app->ConnectToService("mojo:tracing", &tracing_); |
| 34 if (app->args().size() > 1) | 34 if (app->args().size() > 1) |
| 35 url_ = app->args()[1]; | 35 url_ = app->args()[1]; |
| 36 else { | 36 else { |
| 37 url_ = "https://raw.githubusercontent.com/domokit/mojo/master/sky/" | 37 url_ = "https://raw.githubusercontent.com/domokit/mojo/master/sky/" |
| 38 "examples/home.sky"; | 38 "examples/home.sky"; |
| 39 } | 39 } |
| 40 scoped_ptr<net::ServerSocket> server_socket( | 40 scoped_ptr<net::ServerSocket> server_socket( |
| 41 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 41 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
| 42 // FIXME: This port needs to be configurable, as-is we can only run | 42 // FIXME: This port needs to be configurable, as-is we can only run |
| 43 // one copy of mojo_shell with sky at a time! | 43 // one copy of mojo_shell with sky at a time! |
| 44 server_socket->ListenWithAddressAndPort("0.0.0.0", 7777, 1); | 44 uint16 port = 7777; |
| 45 int result = server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); |
| 46 if (result != net::OK) { |
| 47 // FIXME: Should we quit here? |
| 48 LOG(ERROR) << "Failed to bind to port " << port |
| 49 << " skydb commands will not work, exiting."; |
| 50 exit(2); |
| 51 return; |
| 52 } |
| 45 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); | 53 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
| 46 } | 54 } |
| 47 | 55 |
| 48 virtual bool ConfigureIncomingConnection( | 56 virtual bool ConfigureIncomingConnection( |
| 49 mojo::ApplicationConnection* connection) override { | 57 mojo::ApplicationConnection* connection) override { |
| 50 connection->ConnectToService(&debugger_); | 58 connection->ConnectToService(&debugger_); |
| 51 std::cout << "Loading " << url_ << std::endl; | |
| 52 Reload(); | 59 Reload(); |
| 53 return true; | 60 return true; |
| 54 } | 61 } |
| 55 | 62 |
| 56 // net::HttpServer::Delegate | 63 // net::HttpServer::Delegate |
| 57 void OnConnect(int connection_id) override { | 64 void OnConnect(int connection_id) override { |
| 58 } | 65 } |
| 59 | 66 |
| 60 void OnClose(int connection_id) override { | 67 void OnClose(int connection_id) override { |
| 61 } | 68 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 } | 125 } |
| 119 | 126 |
| 120 void Inspect(int connection_id) { | 127 void Inspect(int connection_id) { |
| 121 debugger_->InjectInspector(); | 128 debugger_->InjectInspector(); |
| 122 Respond(connection_id, | 129 Respond(connection_id, |
| 123 "Open the following URL in Chrome:\n" | 130 "Open the following URL in Chrome:\n" |
| 124 "chrome-devtools://devtools/bundled/devtools.html?ws=localhost:9898\n"); | 131 "chrome-devtools://devtools/bundled/devtools.html?ws=localhost:9898\n"); |
| 125 } | 132 } |
| 126 | 133 |
| 127 void Quit(int connection_id) { | 134 void Quit(int connection_id) { |
| 128 std::cout << "quitting" << std::endl; | |
| 129 debugger_->Shutdown(); | 135 debugger_->Shutdown(); |
| 130 } | 136 } |
| 131 | 137 |
| 132 void ToggleTracing(int connection_id) { | 138 void ToggleTracing(int connection_id) { |
| 139 std::string response; |
| 133 if (is_tracing_) { | 140 if (is_tracing_) { |
| 134 std::cout << "Stopping trace (writing to sky_viewer.trace)" << std::endl; | 141 response = "Stopping trace (writing to sky_viewer.trace)\n"; |
| 135 tracing_->StopAndFlush(); | 142 tracing_->StopAndFlush(); |
| 136 } else { | 143 } else { |
| 137 std::cout << "Starting trace (type 'trace' to stop tracing)" << std::endl; | 144 response = "Starting trace (type 'trace' to stop tracing)\n"; |
| 138 tracing_->Start(mojo::String("sky_viewer"), mojo::String("*")); | 145 tracing_->Start(mojo::String("sky_viewer"), mojo::String("*")); |
| 139 } | 146 } |
| 140 is_tracing_ = !is_tracing_; | 147 is_tracing_ = !is_tracing_; |
| 141 Respond(connection_id, "OK\n"); | 148 Respond(connection_id, response); |
| 142 } | 149 } |
| 143 | 150 |
| 144 bool is_tracing_; | 151 bool is_tracing_; |
| 145 DebuggerPtr debugger_; | 152 DebuggerPtr debugger_; |
| 146 tracing::TraceCoordinatorPtr tracing_; | 153 tracing::TraceCoordinatorPtr tracing_; |
| 147 std::string url_; | 154 std::string url_; |
| 148 base::WeakPtrFactory<Prompt> weak_ptr_factory_; | 155 base::WeakPtrFactory<Prompt> weak_ptr_factory_; |
| 149 scoped_ptr<net::HttpServer> web_server_; | 156 scoped_ptr<net::HttpServer> web_server_; |
| 150 | 157 |
| 151 DISALLOW_COPY_AND_ASSIGN(Prompt); | 158 DISALLOW_COPY_AND_ASSIGN(Prompt); |
| 152 }; | 159 }; |
| 153 | 160 |
| 154 } // namespace debugger | 161 } // namespace debugger |
| 155 } // namespace sky | 162 } // namespace sky |
| 156 | 163 |
| 157 MojoResult MojoMain(MojoHandle shell_handle) { | 164 MojoResult MojoMain(MojoHandle shell_handle) { |
| 158 mojo::ApplicationRunnerChromium runner(new sky::debugger::Prompt); | 165 mojo::ApplicationRunnerChromium runner(new sky::debugger::Prompt); |
| 159 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); | 166 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); |
| 160 return runner.Run(shell_handle); | 167 return runner.Run(shell_handle); |
| 161 } | 168 } |
| OLD | NEW |