OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "content/browser/debugger/devtools_http_protocol_handler.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/compiler_specific.h" | |
11 #include "base/json/json_writer.h" | |
12 #include "base/lazy_instance.h" | |
13 #include "base/logging.h" | |
14 #include "base/message_loop_proxy.h" | |
15 #include "base/string_number_conversions.h" | |
16 #include "base/stringprintf.h" | |
17 #include "base/threading/thread.h" | |
18 #include "base/utf_string_conversions.h" | |
19 #include "base/values.h" | |
20 #include "content/browser/tab_contents/tab_contents.h" | |
21 #include "content/browser/tab_contents/tab_contents_observer.h" | |
22 #include "content/common/devtools_messages.h" | |
23 #include "content/public/browser/browser_thread.h" | |
24 #include "content/public/browser/devtools_agent_host_registry.h" | |
25 #include "content/public/browser/devtools_client_host.h" | |
26 #include "content/public/browser/devtools_manager.h" | |
27 #include "googleurl/src/gurl.h" | |
28 #include "net/base/escape.h" | |
29 #include "net/base/io_buffer.h" | |
30 #include "net/server/http_server_request_info.h" | |
31 #include "net/url_request/url_request_context.h" | |
32 | |
33 using content::BrowserThread; | |
34 using content::DevToolsAgentHost; | |
35 using content::DevToolsAgentHostRegistry; | |
36 using content::DevToolsClientHost; | |
37 using content::DevToolsManager; | |
38 | |
39 const int kBufferSize = 16 * 1024; | |
40 | |
41 namespace { | |
42 | |
43 // An internal implementation of DevToolsClientHost that delegates | |
44 // messages sent for DevToolsClient to a DebuggerShell instance. | |
45 class DevToolsClientHostImpl : public DevToolsClientHost { | |
46 public: | |
47 DevToolsClientHostImpl( | |
48 net::HttpServer* server, | |
49 int connection_id) | |
50 : server_(server), | |
51 connection_id_(connection_id) { | |
52 } | |
53 ~DevToolsClientHostImpl() {} | |
54 | |
55 // DevToolsClientHost interface | |
56 virtual void InspectedTabClosing() { | |
57 BrowserThread::PostTask( | |
58 BrowserThread::IO, | |
59 FROM_HERE, | |
60 base::Bind(&net::HttpServer::Close, server_, connection_id_)); | |
61 } | |
62 | |
63 virtual void DispatchOnInspectorFrontend(const std::string& data) { | |
64 BrowserThread::PostTask( | |
65 BrowserThread::IO, | |
66 FROM_HERE, | |
67 base::Bind(&net::HttpServer::SendOverWebSocket, | |
68 server_, | |
69 connection_id_, | |
70 data)); | |
71 } | |
72 | |
73 virtual void TabReplaced(TabContents* new_tab) { | |
74 } | |
75 | |
76 private: | |
77 virtual void FrameNavigating(const std::string& url) {} | |
78 net::HttpServer* server_; | |
79 int connection_id_; | |
80 }; | |
81 | |
82 static int next_id = 1; | |
83 | |
84 class TabContentsIDHelper : public TabContentsObserver { | |
85 public: | |
86 | |
87 static int GetID(TabContents* tab) { | |
88 TabContentsToIdMap::iterator it = tabcontents_to_id_.Get().find(tab); | |
89 if (it != tabcontents_to_id_.Get().end()) | |
90 return it->second; | |
91 TabContentsIDHelper* wrapper = new TabContentsIDHelper(tab); | |
92 return wrapper->id_; | |
93 } | |
94 | |
95 static TabContents* GetTabContents(int id) { | |
96 IdToTabContentsMap::iterator it = id_to_tabcontents_.Get().find(id); | |
97 if (it != id_to_tabcontents_.Get().end()) | |
98 return it->second; | |
99 return NULL; | |
100 } | |
101 | |
102 private: | |
103 explicit TabContentsIDHelper(TabContents* tab) | |
104 : TabContentsObserver(tab), | |
105 id_(next_id++) { | |
106 id_to_tabcontents_.Get()[id_] = tab; | |
107 tabcontents_to_id_.Get()[tab] = id_; | |
108 } | |
109 | |
110 virtual ~TabContentsIDHelper() {} | |
111 | |
112 virtual void TabContentsDestroyed(TabContents* tab) { | |
113 id_to_tabcontents_.Get().erase(id_); | |
114 tabcontents_to_id_.Get().erase(tab); | |
115 delete this; | |
116 } | |
117 | |
118 int id_; | |
119 typedef std::map<int, TabContents*> IdToTabContentsMap; | |
120 static base::LazyInstance<IdToTabContentsMap, | |
121 base::LeakyLazyInstanceTraits<IdToTabContentsMap> > | |
122 id_to_tabcontents_; | |
123 typedef std::map<TabContents*, int> TabContentsToIdMap; | |
124 static base::LazyInstance<TabContentsToIdMap, | |
125 base::LeakyLazyInstanceTraits<TabContentsToIdMap> > | |
126 tabcontents_to_id_; | |
127 }; | |
128 | |
129 base::LazyInstance< | |
130 TabContentsIDHelper::IdToTabContentsMap, | |
131 base::LeakyLazyInstanceTraits<TabContentsIDHelper::IdToTabContentsMap> > | |
132 TabContentsIDHelper::id_to_tabcontents_ = LAZY_INSTANCE_INITIALIZER; | |
133 base::LazyInstance< | |
134 TabContentsIDHelper::TabContentsToIdMap, | |
135 base::LeakyLazyInstanceTraits<TabContentsIDHelper::TabContentsToIdMap> > | |
136 TabContentsIDHelper::tabcontents_to_id_ = LAZY_INSTANCE_INITIALIZER; | |
137 | |
138 } // namespace | |
139 | |
140 // static | |
141 scoped_refptr<DevToolsHttpProtocolHandler> DevToolsHttpProtocolHandler::Start( | |
142 const std::string& ip, | |
143 int port, | |
144 const std::string& frontend_url, | |
145 Delegate* delegate) { | |
146 scoped_refptr<DevToolsHttpProtocolHandler> http_handler = | |
147 new DevToolsHttpProtocolHandler(ip, port, frontend_url, delegate); | |
148 http_handler->Start(); | |
149 return http_handler; | |
150 } | |
151 | |
152 DevToolsHttpProtocolHandler::~DevToolsHttpProtocolHandler() { | |
153 // Stop() must be called prior to this being called | |
154 DCHECK(server_.get() == NULL); | |
155 } | |
156 | |
157 void DevToolsHttpProtocolHandler::Start() { | |
158 BrowserThread::PostTask( | |
159 BrowserThread::IO, FROM_HERE, | |
160 base::Bind(&DevToolsHttpProtocolHandler::Init, this)); | |
161 } | |
162 | |
163 void DevToolsHttpProtocolHandler::Stop() { | |
164 BrowserThread::PostTask( | |
165 BrowserThread::IO, FROM_HERE, | |
166 base::Bind(&DevToolsHttpProtocolHandler::Teardown, this)); | |
167 } | |
168 | |
169 void DevToolsHttpProtocolHandler::OnHttpRequest( | |
170 int connection_id, | |
171 const net::HttpServerRequestInfo& info) { | |
172 if (info.path == "/json") { | |
173 // Pages discovery json request. | |
174 BrowserThread::PostTask( | |
175 BrowserThread::UI, | |
176 FROM_HERE, | |
177 base::Bind(&DevToolsHttpProtocolHandler::OnJsonRequestUI, | |
178 this, | |
179 connection_id, | |
180 info)); | |
181 return; | |
182 } | |
183 | |
184 // Proxy static files from chrome-devtools://devtools/*. | |
185 net::URLRequestContext* request_context = delegate_->GetURLRequestContext(); | |
186 if (!request_context) { | |
187 server_->Send404(connection_id); | |
188 return; | |
189 } | |
190 | |
191 if (info.path == "" || info.path == "/") { | |
192 std::string response = delegate_->GetDiscoveryPageHTML(); | |
193 server_->Send200(connection_id, response, "text/html; charset=UTF-8"); | |
194 return; | |
195 } | |
196 | |
197 net::URLRequest* request; | |
198 | |
199 if (info.path.find("/devtools/") == 0) { | |
200 request = new net::URLRequest(GURL("chrome-devtools:/" + info.path), this); | |
201 } else if (info.path.find("/thumb/") == 0) { | |
202 request = new net::URLRequest(GURL("chrome:/" + info.path), this); | |
203 } else { | |
204 server_->Send404(connection_id); | |
205 return; | |
206 } | |
207 | |
208 Bind(request, connection_id); | |
209 request->set_context(request_context); | |
210 request->Start(); | |
211 } | |
212 | |
213 void DevToolsHttpProtocolHandler::OnWebSocketRequest( | |
214 int connection_id, | |
215 const net::HttpServerRequestInfo& request) { | |
216 BrowserThread::PostTask( | |
217 BrowserThread::UI, | |
218 FROM_HERE, | |
219 base::Bind( | |
220 &DevToolsHttpProtocolHandler::OnWebSocketRequestUI, | |
221 this, | |
222 connection_id, | |
223 request)); | |
224 } | |
225 | |
226 void DevToolsHttpProtocolHandler::OnWebSocketMessage( | |
227 int connection_id, | |
228 const std::string& data) { | |
229 BrowserThread::PostTask( | |
230 BrowserThread::UI, | |
231 FROM_HERE, | |
232 base::Bind( | |
233 &DevToolsHttpProtocolHandler::OnWebSocketMessageUI, | |
234 this, | |
235 connection_id, | |
236 data)); | |
237 } | |
238 | |
239 void DevToolsHttpProtocolHandler::OnClose(int connection_id) { | |
240 ConnectionToRequestsMap::iterator it = | |
241 connection_to_requests_io_.find(connection_id); | |
242 if (it != connection_to_requests_io_.end()) { | |
243 // Dispose delegating socket. | |
244 for (std::set<net::URLRequest*>::iterator it2 = it->second.begin(); | |
245 it2 != it->second.end(); ++it2) { | |
246 net::URLRequest* request = *it2; | |
247 request->Cancel(); | |
248 request_to_connection_io_.erase(request); | |
249 request_to_buffer_io_.erase(request); | |
250 delete request; | |
251 } | |
252 connection_to_requests_io_.erase(connection_id); | |
253 } | |
254 | |
255 BrowserThread::PostTask( | |
256 BrowserThread::UI, | |
257 FROM_HERE, | |
258 base::Bind( | |
259 &DevToolsHttpProtocolHandler::OnCloseUI, | |
260 this, | |
261 connection_id)); | |
262 } | |
263 | |
264 struct PageInfo | |
265 { | |
266 int id; | |
267 std::string url; | |
268 bool attached; | |
269 std::string title; | |
270 std::string thumbnail_url; | |
271 std::string favicon_url; | |
272 }; | |
273 typedef std::vector<PageInfo> PageList; | |
274 | |
275 static PageList GeneratePageList( | |
276 DevToolsHttpProtocolHandler::Delegate* delegate, | |
277 int connection_id, | |
278 const net::HttpServerRequestInfo& info) { | |
279 typedef DevToolsHttpProtocolHandler::InspectableTabs Tabs; | |
280 Tabs inspectable_tabs = delegate->GetInspectableTabs(); | |
281 | |
282 PageList page_list; | |
283 for (Tabs::iterator it = inspectable_tabs.begin(); | |
284 it != inspectable_tabs.end(); ++it) { | |
285 | |
286 TabContents* tab_contents = *it; | |
287 NavigationController& controller = tab_contents->controller(); | |
288 | |
289 NavigationEntry* entry = controller.GetActiveEntry(); | |
290 if (entry == NULL || !entry->url().is_valid()) | |
291 continue; | |
292 | |
293 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost( | |
294 tab_contents->render_view_host()); | |
295 DevToolsClientHost* client_host = DevToolsManager::GetInstance()-> | |
296 GetDevToolsClientHostFor(agent); | |
297 PageInfo page_info; | |
298 page_info.id = TabContentsIDHelper::GetID(tab_contents); | |
299 page_info.attached = client_host != NULL; | |
300 page_info.url = entry->url().spec(); | |
301 page_info.title = UTF16ToUTF8(net::EscapeForHTML(entry->title())); | |
302 page_info.thumbnail_url = "/thumb/" + entry->url().spec(); | |
303 page_info.favicon_url = entry->favicon().url().spec(); | |
304 page_list.push_back(page_info); | |
305 } | |
306 return page_list; | |
307 } | |
308 | |
309 void DevToolsHttpProtocolHandler::OnJsonRequestUI( | |
310 int connection_id, | |
311 const net::HttpServerRequestInfo& info) { | |
312 PageList page_list = GeneratePageList(delegate_.get(), | |
313 connection_id, info); | |
314 ListValue json_pages_list; | |
315 std::string host = info.headers["Host"]; | |
316 for (PageList::iterator i = page_list.begin(); | |
317 i != page_list.end(); ++i) { | |
318 | |
319 DictionaryValue* page_info = new DictionaryValue; | |
320 json_pages_list.Append(page_info); | |
321 page_info->SetString("title", i->title); | |
322 page_info->SetString("url", i->url); | |
323 page_info->SetString("thumbnailUrl", i->thumbnail_url); | |
324 page_info->SetString("faviconUrl", i->favicon_url); | |
325 if (!i->attached) { | |
326 page_info->SetString("webSocketDebuggerUrl", | |
327 base::StringPrintf("ws://%s/devtools/page/%d", | |
328 host.c_str(), | |
329 i->id)); | |
330 page_info->SetString("devtoolsFrontendUrl", | |
331 base::StringPrintf("%s?host=%s&page=%d", | |
332 overridden_frontend_url_.c_str(), | |
333 host.c_str(), | |
334 i->id)); | |
335 } | |
336 } | |
337 | |
338 std::string response; | |
339 base::JSONWriter::Write(&json_pages_list, true, &response); | |
340 Send200(connection_id, response, "application/json; charset=UTF-8"); | |
341 } | |
342 | |
343 void DevToolsHttpProtocolHandler::OnWebSocketRequestUI( | |
344 int connection_id, | |
345 const net::HttpServerRequestInfo& request) { | |
346 std::string prefix = "/devtools/page/"; | |
347 size_t pos = request.path.find(prefix); | |
348 if (pos != 0) { | |
349 Send404(connection_id); | |
350 return; | |
351 } | |
352 std::string page_id = request.path.substr(prefix.length()); | |
353 int id = 0; | |
354 if (!base::StringToInt(page_id, &id)) { | |
355 Send500(connection_id, "Invalid page id: " + page_id); | |
356 return; | |
357 } | |
358 | |
359 TabContents* tab_contents = TabContentsIDHelper::GetTabContents(id); | |
360 if (tab_contents == NULL) { | |
361 Send500(connection_id, "No such page id: " + page_id); | |
362 return; | |
363 } | |
364 | |
365 DevToolsManager* manager = DevToolsManager::GetInstance(); | |
366 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost( | |
367 tab_contents->render_view_host()); | |
368 if (manager->GetDevToolsClientHostFor(agent)) { | |
369 Send500(connection_id, "Page with given id is being inspected: " + page_id); | |
370 return; | |
371 } | |
372 | |
373 DevToolsClientHostImpl* client_host = | |
374 new DevToolsClientHostImpl(server_, connection_id); | |
375 connection_to_client_host_ui_[connection_id] = client_host; | |
376 | |
377 manager->RegisterDevToolsClientHostFor(agent, client_host); | |
378 | |
379 AcceptWebSocket(connection_id, request); | |
380 } | |
381 | |
382 void DevToolsHttpProtocolHandler::OnWebSocketMessageUI( | |
383 int connection_id, | |
384 const std::string& data) { | |
385 ConnectionToClientHostMap::iterator it = | |
386 connection_to_client_host_ui_.find(connection_id); | |
387 if (it == connection_to_client_host_ui_.end()) | |
388 return; | |
389 | |
390 DevToolsManager* manager = DevToolsManager::GetInstance(); | |
391 manager->DispatchOnInspectorBackend(it->second, data); | |
392 } | |
393 | |
394 void DevToolsHttpProtocolHandler::OnCloseUI(int connection_id) { | |
395 ConnectionToClientHostMap::iterator it = | |
396 connection_to_client_host_ui_.find(connection_id); | |
397 if (it != connection_to_client_host_ui_.end()) { | |
398 DevToolsClientHostImpl* client_host = | |
399 static_cast<DevToolsClientHostImpl*>(it->second); | |
400 DevToolsManager::GetInstance()->ClientHostClosing(client_host); | |
401 delete client_host; | |
402 connection_to_client_host_ui_.erase(connection_id); | |
403 } | |
404 } | |
405 | |
406 void DevToolsHttpProtocolHandler::OnResponseStarted(net::URLRequest* request) { | |
407 RequestToSocketMap::iterator it = request_to_connection_io_.find(request); | |
408 if (it == request_to_connection_io_.end()) | |
409 return; | |
410 | |
411 int connection_id = it->second; | |
412 | |
413 std::string content_type; | |
414 request->GetMimeType(&content_type); | |
415 | |
416 if (request->status().is_success()) { | |
417 server_->Send(connection_id, | |
418 base::StringPrintf("HTTP/1.1 200 OK\r\n" | |
419 "Content-Type:%s\r\n" | |
420 "Transfer-Encoding: chunked\r\n" | |
421 "\r\n", | |
422 content_type.c_str())); | |
423 } else { | |
424 server_->Send404(connection_id); | |
425 } | |
426 | |
427 int bytes_read = 0; | |
428 // Some servers may treat HEAD requests as GET requests. To free up the | |
429 // network connection as soon as possible, signal that the request has | |
430 // completed immediately, without trying to read any data back (all we care | |
431 // about is the response code and headers, which we already have). | |
432 net::IOBuffer* buffer = request_to_buffer_io_[request].get(); | |
433 if (request->status().is_success()) | |
434 request->Read(buffer, kBufferSize, &bytes_read); | |
435 OnReadCompleted(request, bytes_read); | |
436 } | |
437 | |
438 void DevToolsHttpProtocolHandler::OnReadCompleted(net::URLRequest* request, | |
439 int bytes_read) { | |
440 RequestToSocketMap::iterator it = request_to_connection_io_.find(request); | |
441 if (it == request_to_connection_io_.end()) | |
442 return; | |
443 | |
444 int connection_id = it->second; | |
445 | |
446 net::IOBuffer* buffer = request_to_buffer_io_[request].get(); | |
447 do { | |
448 if (!request->status().is_success() || bytes_read <= 0) | |
449 break; | |
450 std::string chunk_size = base::StringPrintf("%X\r\n", bytes_read); | |
451 server_->Send(connection_id, chunk_size); | |
452 server_->Send(connection_id, buffer->data(), bytes_read); | |
453 server_->Send(connection_id, "\r\n"); | |
454 } while (request->Read(buffer, kBufferSize, &bytes_read)); | |
455 | |
456 | |
457 // See comments re: HEAD requests in OnResponseStarted(). | |
458 if (!request->status().is_io_pending()) { | |
459 server_->Send(connection_id, "0\r\n\r\n"); | |
460 RequestCompleted(request); | |
461 } | |
462 } | |
463 | |
464 DevToolsHttpProtocolHandler::DevToolsHttpProtocolHandler( | |
465 const std::string& ip, | |
466 int port, | |
467 const std::string& frontend_host, | |
468 Delegate* delegate) | |
469 : ip_(ip), | |
470 port_(port), | |
471 overridden_frontend_url_(frontend_host), | |
472 delegate_(delegate) { | |
473 if (overridden_frontend_url_.empty()) | |
474 overridden_frontend_url_ = "/devtools/devtools.html"; | |
475 } | |
476 | |
477 void DevToolsHttpProtocolHandler::Init() { | |
478 server_ = new net::HttpServer(ip_, port_, this); | |
479 } | |
480 | |
481 // Run on I/O thread | |
482 void DevToolsHttpProtocolHandler::Teardown() { | |
483 server_ = NULL; | |
484 } | |
485 | |
486 void DevToolsHttpProtocolHandler::Bind(net::URLRequest* request, | |
487 int connection_id) { | |
488 request_to_connection_io_[request] = connection_id; | |
489 ConnectionToRequestsMap::iterator it = | |
490 connection_to_requests_io_.find(connection_id); | |
491 if (it == connection_to_requests_io_.end()) { | |
492 std::pair<int, std::set<net::URLRequest*> > value( | |
493 connection_id, | |
494 std::set<net::URLRequest*>()); | |
495 it = connection_to_requests_io_.insert(value).first; | |
496 } | |
497 it->second.insert(request); | |
498 request_to_buffer_io_[request] = new net::IOBuffer(kBufferSize); | |
499 } | |
500 | |
501 void DevToolsHttpProtocolHandler::RequestCompleted(net::URLRequest* request) { | |
502 RequestToSocketMap::iterator it = request_to_connection_io_.find(request); | |
503 if (it == request_to_connection_io_.end()) | |
504 return; | |
505 | |
506 int connection_id = it->second; | |
507 request_to_connection_io_.erase(request); | |
508 ConnectionToRequestsMap::iterator it2 = | |
509 connection_to_requests_io_.find(connection_id); | |
510 it2->second.erase(request); | |
511 request_to_buffer_io_.erase(request); | |
512 delete request; | |
513 } | |
514 | |
515 void DevToolsHttpProtocolHandler::Send200(int connection_id, | |
516 const std::string& data, | |
517 const std::string& mime_type) { | |
518 BrowserThread::PostTask( | |
519 BrowserThread::IO, FROM_HERE, | |
520 base::Bind(&net::HttpServer::Send200, | |
521 server_.get(), | |
522 connection_id, | |
523 data, | |
524 mime_type)); | |
525 } | |
526 | |
527 void DevToolsHttpProtocolHandler::Send404(int connection_id) { | |
528 BrowserThread::PostTask( | |
529 BrowserThread::IO, FROM_HERE, | |
530 base::Bind(&net::HttpServer::Send404, server_.get(), connection_id)); | |
531 } | |
532 | |
533 void DevToolsHttpProtocolHandler::Send500(int connection_id, | |
534 const std::string& message) { | |
535 BrowserThread::PostTask( | |
536 BrowserThread::IO, FROM_HERE, | |
537 base::Bind(&net::HttpServer::Send500, server_.get(), connection_id, | |
538 message)); | |
539 } | |
540 | |
541 void DevToolsHttpProtocolHandler::AcceptWebSocket( | |
542 int connection_id, | |
543 const net::HttpServerRequestInfo& request) { | |
544 BrowserThread::PostTask( | |
545 BrowserThread::IO, FROM_HERE, | |
546 base::Bind(&net::HttpServer::AcceptWebSocket, server_.get(), | |
547 connection_id, request)); | |
548 } | |
OLD | NEW |