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 "sky/tools/debugger/debugger.h" | 5 #include <algorithm> |
6 | 6 |
7 #include "services/window_manager/basic_focus_rules.h" | 7 #include "base/bind.h" |
| 8 #include "base/debug/profiler.h" |
| 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/stringprintf.h" |
| 12 #include "mojo/application/application_runner_chromium.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/services/window_manager/public/interfaces/window_manager.mojom.h" |
| 17 #include "net/base/net_errors.h" |
| 18 #include "net/server/http_server.h" |
| 19 #include "net/server/http_server_request_info.h" |
| 20 #include "net/socket/tcp_server_socket.h" |
| 21 #include "services/tracing/tracing.mojom.h" |
| 22 #include "sky/tools/debugger/trace_collector.h" |
8 | 23 |
9 namespace sky { | 24 namespace sky { |
10 namespace debugger { | 25 namespace debugger { |
11 | 26 namespace { |
12 SkyDebugger::SkyDebugger() | 27 |
13 : window_manager_app_(new window_manager::WindowManagerApp(this, this)), | 28 const size_t kMinSendBufferSize = 1024 * 1024; |
14 root_(nullptr), | |
15 content_(nullptr), | |
16 navigator_host_factory_(this), | |
17 weak_factory_(this) { | |
18 exposed_services_impl_.AddService(&navigator_host_factory_); | |
19 } | 29 } |
20 | 30 |
21 SkyDebugger::~SkyDebugger() { | 31 class SkyDebugger : public mojo::ApplicationDelegate, |
22 } | 32 public net::HttpServer::Delegate { |
23 | 33 public: |
24 base::WeakPtr<SkyDebugger> SkyDebugger::GetWeakPtr() { | 34 SkyDebugger() : is_tracing_(false), weak_ptr_factory_(this) {} |
25 return weak_factory_.GetWeakPtr(); | 35 virtual ~SkyDebugger() {} |
26 } | 36 |
27 | 37 private: |
28 void SkyDebugger::Initialize(mojo::ApplicationImpl* app) { | 38 // Overridden from mojo::ApplicationDelegate: |
29 window_manager_app_->Initialize(app); | 39 virtual void Initialize(mojo::ApplicationImpl* app) override { |
30 app->ConnectToApplication("mojo:sky_debugger_prompt"); | 40 app->ConnectToService("mojo:tracing", &tracing_); |
31 | 41 // Format: --args-for="app_url command_port" |
32 // Format: --args-for="app_url default_url" | 42 if (app->args().size() < 2) { |
33 if (app->args().size() > 1) | 43 LOG(ERROR) << "--args-for required to specify command_port"; |
34 default_url_ = app->args()[1]; | 44 mojo::ApplicationImpl::Terminate(); |
35 } | 45 return; |
36 | 46 } |
37 bool SkyDebugger::ConfigureIncomingConnection( | 47 |
38 mojo::ApplicationConnection* connection) { | 48 base::StringToUint(app->args()[1], &command_port_); |
39 window_manager_app_->ConfigureIncomingConnection(connection); | 49 |
40 connection->AddService(this); | 50 scoped_ptr<net::ServerSocket> server_socket( |
41 | 51 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
42 if (!default_url_.empty()) | 52 int result = |
43 NavigateToURL(default_url_); // Schedule a navigation in the new embedding. | 53 server_socket->ListenWithAddressAndPort("0.0.0.0", command_port_, 1); |
44 return true; | 54 if (result != net::OK) { |
45 } | 55 LOG(ERROR) << "Failed to bind to port " << command_port_ |
46 | 56 << " skydb commands will not work."; |
47 bool SkyDebugger::ConfigureOutgoingConnection( | 57 mojo::ApplicationImpl::Terminate(); |
48 mojo::ApplicationConnection* connection) { | 58 return; |
49 window_manager_app_->ConfigureOutgoingConnection(connection); | 59 } |
50 connection->AddService(this); | 60 web_server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
51 return true; | 61 |
52 } | 62 app->ConnectToService("mojo:window_manager", &window_manager_); |
53 | 63 } |
54 void SkyDebugger::OnEmbed( | 64 |
55 mojo::View* root, | 65 virtual bool ConfigureIncomingConnection( |
56 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 66 mojo::ApplicationConnection* connection) override { |
57 mojo::ServiceProviderPtr exposed_services) { | 67 return true; |
58 root_ = root; | 68 } |
59 root_->AddObserver(this); | 69 |
60 | 70 // net::HttpServer::Delegate |
61 window_manager_app_->SetViewportSize(gfx::Size(320, 640)); | 71 void OnConnect(int connection_id) override {} |
62 | 72 |
63 content_ = root->view_manager()->CreateView(); | 73 void OnClose(int connection_id) override {} |
64 content_->SetBounds(root_->bounds()); | 74 |
65 root_->AddChild(content_); | 75 void OnHttpRequest(int connection_id, |
66 content_->SetVisible(true); | 76 const net::HttpServerRequestInfo& info) override { |
67 | 77 // FIXME: We should use use a fancier lookup system more like what |
68 window_manager_app_->InitFocus( | 78 // services/http_server/http_server.cc does with AddHandler. |
69 make_scoped_ptr(new window_manager::BasicFocusRules(root_))); | 79 if (info.path == "/reload") |
70 | 80 Load(connection_id, url_); |
71 if (!pending_url_.empty()) | 81 else if (info.path == "/quit") |
72 NavigateToURL(pending_url_); | 82 Quit(connection_id); |
73 } | 83 else if (info.path == "/load") |
74 | 84 Load(connection_id, info.data); |
75 void SkyDebugger::Embed(const mojo::String& url, | 85 else if (info.path == "/start_profiling") |
76 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 86 StartProfiling(connection_id); |
77 mojo::ServiceProviderPtr exposed_services) { | 87 else if (info.path == "/stop_profiling") |
78 content_->Embed(url, nullptr, nullptr); | 88 StopProfiling(connection_id); |
79 } | 89 else if (info.path == "/start_tracing") |
80 | 90 StartTracing(connection_id); |
81 void SkyDebugger::OnViewManagerDisconnected(mojo::ViewManager* view_manager) { | 91 else if (info.path == "/stop_tracing") |
82 root_ = nullptr; | 92 StopTracing(connection_id); |
83 } | 93 else |
84 | 94 Help(info.path, connection_id); |
85 void SkyDebugger::OnViewDestroyed(mojo::View* view) { | 95 } |
86 view->RemoveObserver(this); | 96 |
87 } | 97 void OnWebSocketRequest(int connection_id, |
88 | 98 const net::HttpServerRequestInfo& info) override { |
89 void SkyDebugger::OnViewBoundsChanged(mojo::View* view, | 99 Error(connection_id, "OnWebSocketRequest not implemented"); |
90 const mojo::Rect& old_bounds, | 100 } |
91 const mojo::Rect& new_bounds) { | 101 |
92 content_->SetBounds(new_bounds); | 102 void OnWebSocketMessage(int connection_id, const std::string& data) override { |
93 } | 103 Error(connection_id, "OnWebSocketMessage not implemented"); |
94 | 104 } |
95 void SkyDebugger::Create(mojo::ApplicationConnection* connection, | 105 |
96 mojo::InterfaceRequest<Debugger> request) { | 106 void Error(int connection_id, std::string message) { |
97 mojo::WeakBindToRequest(this, &request); | 107 web_server_->Send500(connection_id, message); |
98 } | 108 } |
99 | 109 |
100 void SkyDebugger::NavigateToURL(const mojo::String& url) { | 110 void Respond(int connection_id, std::string response) { |
101 // We can get Navigate commands before we've actually been | 111 // When sending tracing data back over the wire to the client, we can blow |
102 // embedded into the view and content_ created. | 112 // through the default send buffer size. |
103 // Just save the last one. | 113 web_server_->SetSendBufferSize( |
104 if (content_) { | 114 connection_id, std::max(kMinSendBufferSize, response.length())); |
105 mojo::ServiceProviderPtr exposed_services; | 115 web_server_->Send200(connection_id, response, "text/plain"); |
106 exposed_services_impl_.Bind(GetProxy(&exposed_services)); | 116 } |
107 content_->Embed(url, GetProxy(&viewer_services_), exposed_services.Pass()); | 117 |
108 } else { | 118 void Help(std::string path, int connection_id) { |
109 pending_url_ = url; | 119 std::string help = base::StringPrintf( |
110 } | 120 "Sky Debugger running on port %d\n" |
111 } | 121 "Supported URLs:\n" |
112 | 122 "/reload -- Reload the current page\n" |
113 void SkyDebugger::Shutdown() { | 123 "/quit -- Quit\n" |
114 // Make sure we shut down mojo before quitting the message loop or things | 124 "/load -- Load a new URL, url in POST body.\n", |
115 // like blink::shutdown() may try to talk to the message loop and crash. | 125 command_port_); |
116 window_manager_app_.reset(); | 126 if (path != "/") |
117 | 127 help = "Unknown path: " + path + "\n\n" + help; |
118 // TODO(eseidel): This still hits an X11 error which I don't understand | 128 Respond(connection_id, help); |
119 // "X Error of failed request: GLXBadDrawable", crbug.com/430581 | 129 } |
120 mojo::ApplicationImpl::Terminate(); | 130 |
121 // TODO(eseidel): REMOVE THIS, temporarily fast-exit now to stop confusing | 131 void Load(int connection_id, std::string url) { |
122 // folks with exit-time crashes due to GLXBadDrawable above. | 132 url_ = url; |
123 exit(0); | 133 Reload(); |
124 } | 134 std::string response = std::string("Loaded ") + url + "\n"; |
125 | 135 Respond(connection_id, response); |
126 void SkyDebugger::InjectInspector() { | 136 } |
127 InspectorServicePtr inspector_service; | 137 |
128 mojo::ConnectToService(viewer_services_.get(), &inspector_service); | 138 void Reload() { |
129 inspector_service->Inject(); | 139 // SimpleWindowManager will wire up necessary services on our behalf. |
130 } | 140 window_manager_->Embed(url_, nullptr, nullptr); |
| 141 } |
| 142 |
| 143 void Quit(int connection_id) { |
| 144 // TODO(eseidel): We should orderly shutdown once mojo can. |
| 145 exit(0); |
| 146 } |
| 147 |
| 148 void StartTracing(int connection_id) { |
| 149 if (is_tracing_) { |
| 150 Error(connection_id, "Already tracing. Use stop_tracing to stop.\n"); |
| 151 return; |
| 152 } |
| 153 |
| 154 is_tracing_ = true; |
| 155 mojo::DataPipe pipe; |
| 156 tracing_->Start(pipe.producer_handle.Pass(), mojo::String("*")); |
| 157 trace_collector_.reset(new TraceCollector(pipe.consumer_handle.Pass())); |
| 158 Respond(connection_id, "Starting trace (type 'stop_tracing' to stop)\n"); |
| 159 } |
| 160 |
| 161 void StopTracing(int connection_id) { |
| 162 if (!is_tracing_) { |
| 163 Error(connection_id, "Not tracing yet. Use start_tracing to start.\n"); |
| 164 return; |
| 165 } |
| 166 |
| 167 is_tracing_ = false; |
| 168 tracing_->StopAndFlush(); |
| 169 trace_collector_->GetTrace(base::Bind( |
| 170 &SkyDebugger::OnTraceAvailable, base::Unretained(this), connection_id)); |
| 171 } |
| 172 |
| 173 void OnTraceAvailable(int connection_id, std::string trace) { |
| 174 trace_collector_.reset(); |
| 175 Respond(connection_id, trace); |
| 176 } |
| 177 |
| 178 void StartProfiling(int connection_id) { |
| 179 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING) |
| 180 Error(connection_id, |
| 181 "Profiling requires is_debug=false and enable_profiling=true"); |
| 182 return; |
| 183 #else |
| 184 base::debug::StartProfiling("sky_viewer.pprof"); |
| 185 Respond(connection_id, "Starting profiling (stop with 'stop_profiling')"); |
| 186 #endif |
| 187 } |
| 188 |
| 189 void StopProfiling(int connection_id) { |
| 190 if (!base::debug::BeingProfiled()) { |
| 191 Error(connection_id, "Profiling not started"); |
| 192 return; |
| 193 } |
| 194 base::debug::StopProfiling(); |
| 195 Respond(connection_id, "Stopped profiling"); |
| 196 } |
| 197 |
| 198 bool is_tracing_; |
| 199 mojo::WindowManagerPtr window_manager_; |
| 200 tracing::TraceCoordinatorPtr tracing_; |
| 201 std::string url_; |
| 202 base::WeakPtrFactory<SkyDebugger> weak_ptr_factory_; |
| 203 scoped_ptr<net::HttpServer> web_server_; |
| 204 uint32_t command_port_; |
| 205 |
| 206 scoped_ptr<TraceCollector> trace_collector_; |
| 207 |
| 208 DISALLOW_COPY_AND_ASSIGN(SkyDebugger); |
| 209 }; |
131 | 210 |
132 } // namespace debugger | 211 } // namespace debugger |
133 } // namespace sky | 212 } // namespace sky |
| 213 |
| 214 MojoResult MojoMain(MojoHandle shell_handle) { |
| 215 mojo::ApplicationRunnerChromium runner(new sky::debugger::SkyDebugger); |
| 216 runner.set_message_loop_type(base::MessageLoop::TYPE_IO); |
| 217 return runner.Run(shell_handle); |
| 218 } |
OLD | NEW |