mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 1 | // Copyright 2016 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 COMPONENTS_UI_DEVTOOLS_DEVTOOLS_SERVER_H_ |
| 6 | #define COMPONENTS_UI_DEVTOOLS_DEVTOOLS_SERVER_H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
mhashmi | 6d1b1cb2 | 2016-11-22 16:04:24 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
Lily Houghton | 7f7b2fb | 2018-05-01 21:40:46 | [diff] [blame] | 11 | #include "base/memory/weak_ptr.h" |
Gabriel Charette | 5ff87ce | 2017-05-16 18:03:45 | [diff] [blame] | 12 | #include "base/single_thread_task_runner.h" |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 13 | #include "base/threading/thread.h" |
| 14 | #include "components/ui_devtools/DOM.h" |
| 15 | #include "components/ui_devtools/Forward.h" |
| 16 | #include "components/ui_devtools/Protocol.h" |
| 17 | #include "components/ui_devtools/devtools_client.h" |
thanhph | 3f396851 | 2017-06-21 00:37:23 | [diff] [blame] | 18 | #include "components/ui_devtools/devtools_export.h" |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 19 | #include "components/ui_devtools/string_util.h" |
Lily Houghton | 7f7b2fb | 2018-05-01 21:40:46 | [diff] [blame] | 20 | #include "services/network/public/cpp/server/http_server.h" |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 21 | |
thanhph | 3f396851 | 2017-06-21 00:37:23 | [diff] [blame] | 22 | namespace ui_devtools { |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 23 | |
Lily Houghton | 7f7b2fb | 2018-05-01 21:40:46 | [diff] [blame] | 24 | class UI_DEVTOOLS_EXPORT UiDevToolsServer |
| 25 | : public network::server::HttpServer::Delegate { |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 26 | public: |
Sean Gilhuly | 13fc02ff3 | 2018-11-01 14:05:30 | [diff] [blame] | 27 | // Network tags to be used for the UI and the Viz devtools servers. |
| 28 | static const net::NetworkTrafficAnnotationTag kUIDevtoolsServerTag; |
| 29 | static const net::NetworkTrafficAnnotationTag kVizDevtoolsServerTag; |
| 30 | |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 31 | ~UiDevToolsServer() override; |
| 32 | |
mhashmi | 6d1b1cb2 | 2016-11-22 16:04:24 | [diff] [blame] | 33 | // Returns an empty unique_ptr if ui devtools flag isn't enabled or if a |
Sean Gilhuly | 13fc02ff3 | 2018-11-01 14:05:30 | [diff] [blame] | 34 | // server instance has already been created. |
| 35 | static std::unique_ptr<UiDevToolsServer> CreateForViews( |
Lily Houghton | 7f7b2fb | 2018-05-01 21:40:46 | [diff] [blame] | 36 | network::mojom::NetworkContext* network_context, |
Ahmed Fakhry | f6c1bcd | 2018-11-13 19:08:25 | [diff] [blame^] | 37 | const char* enable_devtools_flag, |
| 38 | int default_port); |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 39 | |
Sean Gilhuly | 13fc02ff3 | 2018-11-01 14:05:30 | [diff] [blame] | 40 | // Assumes that the devtools flag is enabled, and was checked when the socket |
| 41 | // was created. |
| 42 | static std::unique_ptr<UiDevToolsServer> CreateForViz( |
| 43 | network::mojom::TCPServerSocketPtr server_socket, |
| 44 | int port); |
| 45 | |
| 46 | // Creates a TCPServerSocket to be used by a UiDevToolsServer. |
| 47 | static void CreateTCPServerSocket( |
| 48 | network::mojom::TCPServerSocketRequest server_socket_request, |
| 49 | network::mojom::NetworkContext* network_context, |
| 50 | int port, |
| 51 | net::NetworkTrafficAnnotationTag tag, |
| 52 | network::mojom::NetworkContext::CreateTCPServerSocketCallback callback); |
| 53 | |
mhashmi | 6d1b1cb2 | 2016-11-22 16:04:24 | [diff] [blame] | 54 | // Returns a list of attached UiDevToolsClient name + URL |
| 55 | using NameUrlPair = std::pair<std::string, std::string>; |
leon.han | 030d74f | 2016-12-13 02:52:16 | [diff] [blame] | 56 | static std::vector<NameUrlPair> GetClientNamesAndUrls(); |
mhashmi | 6d1b1cb2 | 2016-11-22 16:04:24 | [diff] [blame] | 57 | |
Ahmed Fakhry | f6c1bcd | 2018-11-13 19:08:25 | [diff] [blame^] | 58 | // Returns the port number specified by a command line flag. If a number is |
| 59 | // not specified as a command line argument, returns the |default_port|. |
| 60 | static int GetUiDevToolsPort(const char* enable_devtools_flag, |
| 61 | int default_port); |
Sean Gilhuly | 13fc02ff3 | 2018-11-01 14:05:30 | [diff] [blame] | 62 | |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 63 | void AttachClient(std::unique_ptr<UiDevToolsClient> client); |
| 64 | void SendOverWebSocket(int connection_id, const String& message); |
| 65 | |
Illia Martyniuk | bd1e4485 | 2018-03-13 22:02:59 | [diff] [blame] | 66 | int port() const { return port_; } |
| 67 | |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 68 | private: |
Sean Gilhuly | 13fc02ff3 | 2018-11-01 14:05:30 | [diff] [blame] | 69 | UiDevToolsServer(int port, const net::NetworkTrafficAnnotationTag tag); |
mhashmi | 6011c1bd | 2016-10-31 15:24:29 | [diff] [blame] | 70 | |
Lily Houghton | 7f7b2fb | 2018-05-01 21:40:46 | [diff] [blame] | 71 | void MakeServer(network::mojom::TCPServerSocketPtr server_socket, |
| 72 | int result, |
| 73 | const base::Optional<net::IPEndPoint>& local_addr); |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 74 | |
| 75 | // HttpServer::Delegate |
| 76 | void OnConnect(int connection_id) override; |
Lily Houghton | 7f7b2fb | 2018-05-01 21:40:46 | [diff] [blame] | 77 | void OnHttpRequest( |
| 78 | int connection_id, |
| 79 | const network::server::HttpServerRequestInfo& info) override; |
| 80 | void OnWebSocketRequest( |
| 81 | int connection_id, |
| 82 | const network::server::HttpServerRequestInfo& info) override; |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 83 | void OnWebSocketMessage(int connection_id, const std::string& data) override; |
| 84 | void OnClose(int connection_id) override; |
| 85 | |
| 86 | using ClientsList = std::vector<std::unique_ptr<UiDevToolsClient>>; |
| 87 | using ConnectionsMap = std::map<uint32_t, UiDevToolsClient*>; |
| 88 | ClientsList clients_; |
| 89 | ConnectionsMap connections_; |
| 90 | |
Lily Houghton | 7f7b2fb | 2018-05-01 21:40:46 | [diff] [blame] | 91 | std::unique_ptr<network::server::HttpServer> server_; |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 92 | |
Illia Martyniuk | bd1e4485 | 2018-03-13 22:02:59 | [diff] [blame] | 93 | // The port the devtools server listens on |
| 94 | const int port_; |
| 95 | |
Sean Gilhuly | 13fc02ff3 | 2018-11-01 14:05:30 | [diff] [blame] | 96 | const net::NetworkTrafficAnnotationTag tag_; |
| 97 | |
mhashmi | 6d1b1cb2 | 2016-11-22 16:04:24 | [diff] [blame] | 98 | // The server (owned by ash for now) |
| 99 | static UiDevToolsServer* devtools_server_; |
| 100 | |
Lily Houghton | 7f7b2fb | 2018-05-01 21:40:46 | [diff] [blame] | 101 | SEQUENCE_CHECKER(devtools_server_sequence_); |
| 102 | base::WeakPtrFactory<UiDevToolsServer> weak_ptr_factory_; |
| 103 | |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 104 | DISALLOW_COPY_AND_ASSIGN(UiDevToolsServer); |
| 105 | }; |
| 106 | |
thanhph | 3f396851 | 2017-06-21 00:37:23 | [diff] [blame] | 107 | } // namespace ui_devtools |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 108 | |
| 109 | #endif // COMPONENTS_UI_DEVTOOLS_DEVTOOLS_SERVER_H_ |