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 "base/strings/string_number_conversions.h" | |
8 #include "base/strings/stringprintf.h" | |
7 #include "mojo/application/application_runner_chromium.h" | 9 #include "mojo/application/application_runner_chromium.h" |
8 #include "mojo/public/c/system/main.h" | 10 #include "mojo/public/c/system/main.h" |
9 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
10 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
11 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
12 #include "net/server/http_server.h" | 14 #include "net/server/http_server.h" |
13 #include "net/server/http_server_request_info.h" | 15 #include "net/server/http_server_request_info.h" |
14 #include "net/socket/tcp_server_socket.h" | 16 #include "net/socket/tcp_server_socket.h" |
15 #include "services/tracing/tracing.mojom.h" | 17 #include "services/tracing/tracing.mojom.h" |
16 #include "sky/tools/debugger/debugger.mojom.h" | 18 #include "sky/tools/debugger/debugger.mojom.h" |
17 | 19 |
18 namespace sky { | 20 namespace sky { |
19 namespace debugger { | 21 namespace debugger { |
20 | 22 |
21 class Prompt : public mojo::ApplicationDelegate, public net::HttpServer::Delegat e { | 23 class Prompt : public mojo::ApplicationDelegate, public net::HttpServer::Delegat e { |
22 public: | 24 public: |
23 Prompt() | 25 Prompt() |
24 : is_tracing_(false), | 26 : is_tracing_(false), |
25 weak_ptr_factory_(this) { | 27 weak_ptr_factory_(this) { |
26 } | 28 } |
27 virtual ~Prompt() { | 29 virtual ~Prompt() { |
28 } | 30 } |
29 | 31 |
30 private: | 32 private: |
31 // Overridden from mojo::ApplicationDelegate: | 33 // Overridden from mojo::ApplicationDelegate: |
32 virtual void Initialize(mojo::ApplicationImpl* app) override { | 34 virtual void Initialize(mojo::ApplicationImpl* app) override { |
33 app->ConnectToService("mojo:tracing", &tracing_); | 35 app->ConnectToService("mojo:tracing", &tracing_); |
34 if (app->args().size() > 1) | 36 // app_url, command_port, url_to_load |
35 url_ = app->args()[1]; | 37 if (app->args().size() < 2) { |
36 else { | 38 LOG(ERROR) << "--args-for required to specify command_port"; |
37 url_ = "https://raw.githubusercontent.com/domokit/mojo/master/sky/" | 39 exit(2); |
38 "examples/home.sky"; | |
39 } | 40 } |
41 | |
42 base::StringToUint(app->args()[1], &command_port_); | |
43 | |
40 scoped_ptr<net::ServerSocket> server_socket( | 44 scoped_ptr<net::ServerSocket> server_socket( |
41 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 45 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
42 // FIXME: This port needs to be configurable, as-is we can only run | 46 int result = server_socket->ListenWithAddressAndPort("0.0.0.0", command_port _, 1); |
43 // one copy of mojo_shell with sky at a time! | |
44 uint16 port = 7777; | |
45 int result = server_socket->ListenWithAddressAndPort("0.0.0.0", port, 1); | |
46 if (result != net::OK) { | 47 if (result != net::OK) { |
47 // FIXME: Should we quit here? | 48 // FIXME: Should we quit here? |
48 LOG(ERROR) << "Failed to bind to port " << port | 49 LOG(ERROR) << "Failed to bind to port " << command_port_ |
49 << " skydb commands will not work, exiting."; | 50 << " skydb commands will not work, exiting."; |
50 exit(2); | 51 exit(2); |
51 return; | 52 return; |
52 } | 53 } |
53 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); | 54 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
54 } | 55 } |
55 | 56 |
56 virtual bool ConfigureIncomingConnection( | 57 virtual bool ConfigureIncomingConnection( |
57 mojo::ApplicationConnection* connection) override { | 58 mojo::ApplicationConnection* connection) override { |
58 connection->ConnectToService(&debugger_); | 59 connection->ConnectToService(&debugger_); |
59 Reload(); | |
60 return true; | 60 return true; |
61 } | 61 } |
62 | 62 |
63 // net::HttpServer::Delegate | 63 // net::HttpServer::Delegate |
64 void OnConnect(int connection_id) override { | 64 void OnConnect(int connection_id) override { |
65 } | 65 } |
66 | 66 |
67 void OnClose(int connection_id) override { | 67 void OnClose(int connection_id) override { |
68 } | 68 } |
69 | 69 |
(...skipping 25 matching lines...) Expand all Loading... | |
95 void OnWebSocketMessage( | 95 void OnWebSocketMessage( |
96 int connection_id, const std::string& data) override { | 96 int connection_id, const std::string& data) override { |
97 web_server_->Send500(connection_id, "http only"); | 97 web_server_->Send500(connection_id, "http only"); |
98 } | 98 } |
99 | 99 |
100 void Respond(int connection_id, std::string response) { | 100 void Respond(int connection_id, std::string response) { |
101 web_server_->Send200(connection_id, response, "text/plain"); | 101 web_server_->Send200(connection_id, response, "text/plain"); |
102 } | 102 } |
103 | 103 |
104 void Help(std::string path, int connection_id) { | 104 void Help(std::string path, int connection_id) { |
105 std::string help = "Sky Debugger\n" | 105 std::string help = base::StringPrintf("Sky Debugger running on port %d\n" |
106 "Supported URLs:\n" | 106 "Supported URLs:\n" |
107 "/toggle_tracing -- Start/stop tracing\n" | 107 "/toggle_tracing -- Start/stop tracing\n" |
108 "/reload -- Reload the current page\n" | 108 "/reload -- Reload the current page\n" |
109 "/inspect -- Start inspector server for current page\n" | 109 "/inspect -- Start inspector server for current page\n" |
110 "/quit -- Quit\n" | 110 "/quit -- Quit\n" |
111 "/load -- Load a new URL, url in POST body.\n"; | 111 "/load -- Load a new URL, url in POST body.\n", |
112 command_port_); | |
112 if (path != "/") | 113 if (path != "/") |
113 help = "Unknown path: " + path + "\n\n" + help; | 114 help = "Unknown path: " + path + "\n\n" + help; |
114 Respond(connection_id, help); | 115 Respond(connection_id, help); |
115 } | 116 } |
116 | 117 |
117 void Load(int connection_id, std::string url) { | 118 void Load(int connection_id, std::string url) { |
118 url_ = url; | 119 url_ = url; |
119 Reload(); | 120 Reload(); |
120 Respond(connection_id, "OK\n"); | 121 std::string response = std::string("Loaded ") + url + "\n"; |
122 Respond(connection_id, response); | |
121 } | 123 } |
122 | 124 |
123 void Reload() { | 125 void Reload() { |
124 debugger_->NavigateToURL(url_); | 126 debugger_->NavigateToURL(url_); |
125 } | 127 } |
126 | 128 |
127 void Inspect(int connection_id) { | 129 void Inspect(int connection_id) { |
128 debugger_->InjectInspector(); | 130 debugger_->InjectInspector(); |
129 Respond(connection_id, | 131 Respond(connection_id, |
130 "Open the following URL in Chrome:\n" | 132 "Open the following URL in Chrome:\n" |
(...skipping 16 matching lines...) Expand all Loading... | |
147 is_tracing_ = !is_tracing_; | 149 is_tracing_ = !is_tracing_; |
148 Respond(connection_id, response); | 150 Respond(connection_id, response); |
149 } | 151 } |
150 | 152 |
151 bool is_tracing_; | 153 bool is_tracing_; |
152 DebuggerPtr debugger_; | 154 DebuggerPtr debugger_; |
153 tracing::TraceCoordinatorPtr tracing_; | 155 tracing::TraceCoordinatorPtr tracing_; |
154 std::string url_; | 156 std::string url_; |
155 base::WeakPtrFactory<Prompt> weak_ptr_factory_; | 157 base::WeakPtrFactory<Prompt> weak_ptr_factory_; |
156 scoped_ptr<net::HttpServer> web_server_; | 158 scoped_ptr<net::HttpServer> web_server_; |
159 unsigned command_port_; | |
jamesr
2015/01/08 23:50:38
please use uint32_t. unsigned is longer to type a
| |
157 | 160 |
158 DISALLOW_COPY_AND_ASSIGN(Prompt); | 161 DISALLOW_COPY_AND_ASSIGN(Prompt); |
159 }; | 162 }; |
160 | 163 |
161 } // namespace debugger | 164 } // namespace debugger |
162 } // namespace sky | 165 } // namespace sky |
163 | 166 |
164 MojoResult MojoMain(MojoHandle shell_handle) { | 167 MojoResult MojoMain(MojoHandle shell_handle) { |
165 mojo::ApplicationRunnerChromium runner(new sky::debugger::Prompt); | 168 mojo::ApplicationRunnerChromium runner(new sky::debugger::Prompt); |
166 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); | 169 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); |
167 return runner.Run(shell_handle); | 170 return runner.Run(shell_handle); |
168 } | 171 } |
OLD | NEW |