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.
5 #ifndef COMPONENTS_DEVTOOLS_HTTP_HANDLER_DEVTOOLS_HTTP_HANDLER_H_
6 #define COMPONENTS_DEVTOOLS_HTTP_HANDLER_DEVTOOLS_HTTP_HANDLER_H_
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/devtools_discovery/devtools_target_descriptor.h"
15 #include "net/http/http_status_code.h"
20 class DictionaryValue
;
27 class HttpServerRequestInfo
;
31 namespace devtools_http_handler
{
33 class DevToolsAgentHostClientImpl
;
34 class DevToolsHttpHandlerDelegate
;
37 // This class is used for managing DevTools remote debugging server.
38 // Clients can connect to the specified ip:port and start debugging
40 class DevToolsHttpHandler
{
43 // Factory of net::ServerSocket. This is to separate instantiating dev tools
44 // and instantiating server sockets.
45 // All methods including destructor are called on a separate thread
46 // different from any BrowserThread instance.
47 class ServerSocketFactory
{
49 virtual ~ServerSocketFactory() {}
51 // Returns a new instance of ServerSocket or nullptr if an error occurred.
52 virtual scoped_ptr
<net::ServerSocket
> CreateForHttpServer();
54 // Creates a named socket for reversed tethering implementation (used with
55 // remote debugging, primarily for mobile).
56 virtual scoped_ptr
<net::ServerSocket
> CreateForTethering(
57 std::string
* out_name
);
60 // Takes ownership over |socket_factory| and |delegate|.
61 // If |frontend_url| is empty, assumes it's bundled, and uses
62 // |delegate->GetFrontendResource()|.
63 // |delegate| is only accessed on UI thread.
64 // If |active_port_output_directory| is non-empty, it is assumed the
65 // socket_factory was initialized with an ephemeral port (0). The
66 // port selected by the OS will be written to a well-known file in
67 // the output directory.
69 scoped_ptr
<ServerSocketFactory
> server_socket_factory
,
70 const std::string
& frontend_url
,
71 DevToolsHttpHandlerDelegate
* delegate
,
72 const base::FilePath
& active_port_output_directory
,
73 const base::FilePath
& debug_frontend_dir
,
74 const std::string
& product_name
,
75 const std::string
& user_agent
);
76 ~DevToolsHttpHandler();
78 // Returns the URL for the file at |path| in frontend.
79 GURL
GetFrontendURL(const std::string
& path
);
82 friend class ServerWrapper
;
83 friend void ServerStartedOnUI(
84 base::WeakPtr
<DevToolsHttpHandler
> handler
,
86 ServerWrapper
* server_wrapper
,
87 DevToolsHttpHandler::ServerSocketFactory
* socket_factory
,
88 scoped_ptr
<net::IPEndPoint
> ip_address
);
90 void OnJsonRequest(int connection_id
,
91 const net::HttpServerRequestInfo
& info
);
92 void OnThumbnailRequest(int connection_id
, const std::string
& target_id
);
93 void OnDiscoveryPageRequest(int connection_id
);
94 void OnFrontendResourceRequest(int connection_id
, const std::string
& path
);
95 void OnWebSocketRequest(int connection_id
,
96 const net::HttpServerRequestInfo
& info
);
97 void OnWebSocketMessage(int connection_id
, const std::string
& data
);
98 void OnClose(int connection_id
);
100 void ServerStarted(base::Thread
* thread
,
101 ServerWrapper
* server_wrapper
,
102 ServerSocketFactory
* socket_factory
,
103 scoped_ptr
<net::IPEndPoint
> ip_address
);
105 devtools_discovery::DevToolsTargetDescriptor
* GetDescriptor(
106 const std::string
& target_id
);
108 void SendJson(int connection_id
,
109 net::HttpStatusCode status_code
,
111 const std::string
& message
);
112 void Send200(int connection_id
,
113 const std::string
& data
,
114 const std::string
& mime_type
);
115 void Send404(int connection_id
);
116 void Send500(int connection_id
,
117 const std::string
& message
);
118 void AcceptWebSocket(int connection_id
,
119 const net::HttpServerRequestInfo
& request
);
121 // Returns the front end url without the host at the beginning.
122 std::string
GetFrontendURLInternal(const std::string target_id
,
123 const std::string
& host
);
125 base::DictionaryValue
* SerializeDescriptor(
126 const devtools_discovery::DevToolsTargetDescriptor
& descriptor
,
127 const std::string
& host
);
129 // The thread used by the devtools handler to run server socket.
130 base::Thread
* thread_
;
131 std::string frontend_url_
;
132 std::string product_name_
;
133 std::string user_agent_
;
134 ServerWrapper
* server_wrapper_
;
135 scoped_ptr
<net::IPEndPoint
> server_ip_address_
;
136 typedef std::map
<int, DevToolsAgentHostClientImpl
*> ConnectionToClientMap
;
137 ConnectionToClientMap connection_to_client_
;
138 const scoped_ptr
<DevToolsHttpHandlerDelegate
> delegate_
;
139 ServerSocketFactory
* socket_factory_
;
140 using DescriptorMap
=
141 std::map
<std::string
, devtools_discovery::DevToolsTargetDescriptor
*>;
142 DescriptorMap descriptor_map_
;
143 base::WeakPtrFactory
<DevToolsHttpHandler
> weak_factory_
;
145 DISALLOW_COPY_AND_ASSIGN(DevToolsHttpHandler
);
148 } // namespace devtools_http_handler
150 #endif // COMPONENTS_DEVTOOLS_HTTP_HANDLER_DEVTOOLS_HTTP_HANDLER_H_