1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_HTTP_HANDLER_IMPL_H_ 6 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_HTTP_HANDLER_IMPL_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "content/common/content_export.h" 16 #include "content/public/browser/devtools_http_handler.h" 17 #include "content/public/browser/devtools_http_handler_delegate.h" 18 #include "net/http/http_status_code.h" 19 #include "net/server/http_server.h" 20 21 namespace base { 22 class DictionaryValue; 23 class ListValue; 24 class Thread; 25 class Value; 26 } 27 28 namespace net { 29 class StreamListenSocketFactory; 30 class URLRequestContextGetter; 31 } 32 33 namespace content { 34 35 class DevToolsBrowserTarget; 36 class DevToolsClientHost; 37 38 class DevToolsHttpHandlerImpl 39 : public DevToolsHttpHandler, 40 public base::RefCountedThreadSafe<DevToolsHttpHandlerImpl>, 41 public net::HttpServer::Delegate { 42 private: 43 friend class base::RefCountedThreadSafe<DevToolsHttpHandlerImpl>; 44 friend class DevToolsHttpHandler; 45 46 // Takes ownership over |socket_factory|. 47 DevToolsHttpHandlerImpl(const net::StreamListenSocketFactory* socket_factory, 48 const std::string& frontend_url, 49 DevToolsHttpHandlerDelegate* delegate, 50 const base::FilePath& active_port_output_directory); 51 virtual ~DevToolsHttpHandlerImpl(); 52 void Start(); 53 54 // DevToolsHttpHandler implementation. 55 virtual void Stop() OVERRIDE; 56 virtual GURL GetFrontendURL() OVERRIDE; 57 58 // net::HttpServer::Delegate implementation. 59 virtual void OnHttpRequest(int connection_id, 60 const net::HttpServerRequestInfo& info) OVERRIDE; 61 virtual void OnWebSocketRequest( 62 int connection_id, 63 const net::HttpServerRequestInfo& info) OVERRIDE; 64 virtual void OnWebSocketMessage(int connection_id, 65 const std::string& data) OVERRIDE; 66 virtual void OnClose(int connection_id) OVERRIDE; 67 68 void OnJsonRequestUI(int connection_id, 69 const net::HttpServerRequestInfo& info); 70 void OnThumbnailRequestUI(int connection_id, const GURL& page_url); 71 void OnDiscoveryPageRequestUI(int connection_id); 72 73 void OnWebSocketRequestUI(int connection_id, 74 const net::HttpServerRequestInfo& info); 75 void OnWebSocketMessageUI(int connection_id, const std::string& data); 76 void OnCloseUI(int connection_id); 77 78 void ResetHandlerThread(); 79 void ResetHandlerThreadAndRelease(); 80 81 void OnTargetListReceived( 82 int connection_id, 83 const std::string& host, 84 const DevToolsHttpHandlerDelegate::TargetList& targets); 85 86 DevToolsTarget* GetTarget(const std::string& id); 87 88 void Init(); 89 void Teardown(); 90 91 void StartHandlerThread(); 92 void StopHandlerThread(); 93 94 void WriteActivePortToUserProfile(); 95 96 void SendJson(int connection_id, 97 net::HttpStatusCode status_code, 98 base::Value* value, 99 const std::string& message); 100 void Send200(int connection_id, 101 const std::string& data, 102 const std::string& mime_type); 103 void Send404(int connection_id); 104 void Send500(int connection_id, 105 const std::string& message); 106 void AcceptWebSocket(int connection_id, 107 const net::HttpServerRequestInfo& request); 108 109 // Returns the front end url without the host at the beginning. 110 std::string GetFrontendURLInternal(const std::string target_id, 111 const std::string& host); 112 113 base::DictionaryValue* SerializeTarget(const DevToolsTarget& target, 114 const std::string& host); 115 116 // The thread used by the devtools handler to run server socket. 117 scoped_ptr<base::Thread> thread_; 118 119 std::string frontend_url_; 120 scoped_ptr<const net::StreamListenSocketFactory> socket_factory_; 121 scoped_refptr<net::HttpServer> server_; 122 typedef std::map<int, DevToolsClientHost*> ConnectionToClientHostMap; 123 ConnectionToClientHostMap connection_to_client_host_ui_; 124 scoped_ptr<DevToolsHttpHandlerDelegate> delegate_; 125 base::FilePath active_port_output_directory_; 126 typedef std::map<std::string, DevToolsTarget*> TargetMap; 127 TargetMap target_map_; 128 typedef std::map<int, scoped_refptr<DevToolsBrowserTarget> > BrowserTargets; 129 BrowserTargets browser_targets_; 130 DISALLOW_COPY_AND_ASSIGN(DevToolsHttpHandlerImpl); 131 }; 132 133 } // namespace content 134 135 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_HTTP_HANDLER_IMPL_H_ 136