blob: 438a39988a908416fb660ed080eead10e46bf548 [file] [log] [blame]
mhashmi1ec338a2016-10-25 22:13:341// 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
mhashmi6d1b1cb22016-11-22 16:04:2410#include "base/compiler_specific.h"
Lily Houghton7f7b2fb2018-05-01 21:40:4611#include "base/memory/weak_ptr.h"
Gabriel Charette5ff87ce2017-05-16 18:03:4512#include "base/single_thread_task_runner.h"
mhashmi1ec338a2016-10-25 22:13:3413#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"
thanhph3f3968512017-06-21 00:37:2318#include "components/ui_devtools/devtools_export.h"
mhashmi1ec338a2016-10-25 22:13:3419#include "components/ui_devtools/string_util.h"
Lily Houghton7f7b2fb2018-05-01 21:40:4620#include "services/network/public/cpp/server/http_server.h"
mhashmi1ec338a2016-10-25 22:13:3421
thanhph3f3968512017-06-21 00:37:2322namespace ui_devtools {
mhashmi1ec338a2016-10-25 22:13:3423
Lily Houghton7f7b2fb2018-05-01 21:40:4624class UI_DEVTOOLS_EXPORT UiDevToolsServer
25 : public network::server::HttpServer::Delegate {
mhashmi1ec338a2016-10-25 22:13:3426 public:
Sean Gilhuly13fc02ff32018-11-01 14:05:3027 // 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
mhashmi1ec338a2016-10-25 22:13:3431 ~UiDevToolsServer() override;
32
mhashmi6d1b1cb22016-11-22 16:04:2433 // Returns an empty unique_ptr if ui devtools flag isn't enabled or if a
Sean Gilhuly13fc02ff32018-11-01 14:05:3034 // server instance has already been created.
35 static std::unique_ptr<UiDevToolsServer> CreateForViews(
Lily Houghton7f7b2fb2018-05-01 21:40:4636 network::mojom::NetworkContext* network_context,
Ahmed Fakhryf6c1bcd2018-11-13 19:08:2537 const char* enable_devtools_flag,
38 int default_port);
mhashmi1ec338a2016-10-25 22:13:3439
Sean Gilhuly13fc02ff32018-11-01 14:05:3040 // 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
mhashmi6d1b1cb22016-11-22 16:04:2454 // Returns a list of attached UiDevToolsClient name + URL
55 using NameUrlPair = std::pair<std::string, std::string>;
leon.han030d74f2016-12-13 02:52:1656 static std::vector<NameUrlPair> GetClientNamesAndUrls();
mhashmi6d1b1cb22016-11-22 16:04:2457
Ahmed Fakhryf6c1bcd2018-11-13 19:08:2558 // 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 Gilhuly13fc02ff32018-11-01 14:05:3062
mhashmi1ec338a2016-10-25 22:13:3463 void AttachClient(std::unique_ptr<UiDevToolsClient> client);
64 void SendOverWebSocket(int connection_id, const String& message);
65
Illia Martyniukbd1e44852018-03-13 22:02:5966 int port() const { return port_; }
67
mhashmi1ec338a2016-10-25 22:13:3468 private:
Sean Gilhuly13fc02ff32018-11-01 14:05:3069 UiDevToolsServer(int port, const net::NetworkTrafficAnnotationTag tag);
mhashmi6011c1bd2016-10-31 15:24:2970
Lily Houghton7f7b2fb2018-05-01 21:40:4671 void MakeServer(network::mojom::TCPServerSocketPtr server_socket,
72 int result,
73 const base::Optional<net::IPEndPoint>& local_addr);
mhashmi1ec338a2016-10-25 22:13:3474
75 // HttpServer::Delegate
76 void OnConnect(int connection_id) override;
Lily Houghton7f7b2fb2018-05-01 21:40:4677 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;
mhashmi1ec338a2016-10-25 22:13:3483 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 Houghton7f7b2fb2018-05-01 21:40:4691 std::unique_ptr<network::server::HttpServer> server_;
mhashmi1ec338a2016-10-25 22:13:3492
Illia Martyniukbd1e44852018-03-13 22:02:5993 // The port the devtools server listens on
94 const int port_;
95
Sean Gilhuly13fc02ff32018-11-01 14:05:3096 const net::NetworkTrafficAnnotationTag tag_;
97
mhashmi6d1b1cb22016-11-22 16:04:2498 // The server (owned by ash for now)
99 static UiDevToolsServer* devtools_server_;
100
Lily Houghton7f7b2fb2018-05-01 21:40:46101 SEQUENCE_CHECKER(devtools_server_sequence_);
102 base::WeakPtrFactory<UiDevToolsServer> weak_ptr_factory_;
103
mhashmi1ec338a2016-10-25 22:13:34104 DISALLOW_COPY_AND_ASSIGN(UiDevToolsServer);
105};
106
thanhph3f3968512017-06-21 00:37:23107} // namespace ui_devtools
mhashmi1ec338a2016-10-25 22:13:34108
109#endif // COMPONENTS_UI_DEVTOOLS_DEVTOOLS_SERVER_H_