devtools_server.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef COMPONENTS_UI_DEVTOOLS_DEVTOOLS_SERVER_H_
  5. #define COMPONENTS_UI_DEVTOOLS_DEVTOOLS_SERVER_H_
  6. #include <vector>
  7. #include "base/callback_forward.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/strings/string_piece_forward.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "base/threading/thread.h"
  13. #include "components/ui_devtools/devtools_client.h"
  14. #include "components/ui_devtools/devtools_export.h"
  15. #include "components/ui_devtools/dom.h"
  16. #include "components/ui_devtools/forward.h"
  17. #include "components/ui_devtools/protocol.h"
  18. #include "mojo/public/cpp/bindings/pending_receiver.h"
  19. #include "mojo/public/cpp/bindings/pending_remote.h"
  20. #include "services/network/public/cpp/server/http_server.h"
  21. #include "services/network/public/mojom/network_context.mojom.h"
  22. namespace ui_devtools {
  23. class TracingAgent;
  24. class UI_DEVTOOLS_EXPORT UiDevToolsServer
  25. : public network::server::HttpServer::Delegate {
  26. public:
  27. // Network tags to be used for the UI devtools servers.
  28. static const net::NetworkTrafficAnnotationTag kUIDevtoolsServerTag;
  29. UiDevToolsServer(const UiDevToolsServer&) = delete;
  30. UiDevToolsServer& operator=(const UiDevToolsServer&) = delete;
  31. ~UiDevToolsServer() override;
  32. // Returns an empty unique_ptr if ui devtools flag isn't enabled or if a
  33. // server instance has already been created. If |port| is 0, the server will
  34. // choose an available port. If |port| is 0 and |active_port_output_directory|
  35. // is present, the server will write the chosen port to
  36. // |kUIDevToolsActivePortFileName| on |active_port_output_directory|.
  37. static std::unique_ptr<UiDevToolsServer> CreateForViews(
  38. network::mojom::NetworkContext* network_context,
  39. int port,
  40. const base::FilePath& active_port_output_directory = base::FilePath());
  41. // Creates a TCPServerSocket to be used by a UiDevToolsServer.
  42. static void CreateTCPServerSocket(
  43. mojo::PendingReceiver<network::mojom::TCPServerSocket>
  44. server_socket_receiver,
  45. network::mojom::NetworkContext* network_context,
  46. int port,
  47. net::NetworkTrafficAnnotationTag tag,
  48. network::mojom::NetworkContext::CreateTCPServerSocketCallback callback);
  49. // Returns a list of attached UiDevToolsClient name + URL
  50. using NameUrlPair = std::pair<std::string, std::string>;
  51. static std::vector<NameUrlPair> GetClientNamesAndUrls();
  52. // Returns true if UI Devtools is enabled by the given commandline switch.
  53. static bool IsUiDevToolsEnabled(const char* enable_devtools_flag);
  54. // Returns the port number specified by a command line flag. If a number is
  55. // not specified as a command line argument, returns the |default_port|.
  56. static int GetUiDevToolsPort(const char* enable_devtools_flag,
  57. int default_port);
  58. void AttachClient(std::unique_ptr<UiDevToolsClient> client);
  59. void SendOverWebSocket(int connection_id, base::StringPiece message);
  60. int port() const { return port_; }
  61. TracingAgent* tracing_agent() { return tracing_agent_; }
  62. void set_tracing_agent(TracingAgent* agent) { tracing_agent_ = agent; }
  63. // Sets the callback which will be invoked when the session is closed.
  64. // Marks as a const function so it can be called after the server is set up
  65. // and used as a constant instance.
  66. void SetOnSessionEnded(base::OnceClosure callback) const;
  67. // Sets a callback that tests can use to wait for the server to be ready to
  68. // accept connections.
  69. void SetOnSocketConnectedForTesting(base::OnceClosure on_socket_connected);
  70. private:
  71. UiDevToolsServer(int port,
  72. const net::NetworkTrafficAnnotationTag tag,
  73. const base::FilePath& active_port_output_directory);
  74. void MakeServer(
  75. mojo::PendingRemote<network::mojom::TCPServerSocket> server_socket,
  76. int result,
  77. const absl::optional<net::IPEndPoint>& local_addr);
  78. // HttpServer::Delegate
  79. void OnConnect(int connection_id) override;
  80. void OnHttpRequest(
  81. int connection_id,
  82. const network::server::HttpServerRequestInfo& info) override;
  83. void OnWebSocketRequest(
  84. int connection_id,
  85. const network::server::HttpServerRequestInfo& info) override;
  86. void OnWebSocketMessage(int connection_id, std::string data) override;
  87. void OnClose(int connection_id) override;
  88. using ClientsList = std::vector<std::unique_ptr<UiDevToolsClient>>;
  89. using ConnectionsMap = std::map<uint32_t, UiDevToolsClient*>;
  90. ClientsList clients_;
  91. ConnectionsMap connections_;
  92. std::unique_ptr<network::server::HttpServer> server_;
  93. // The port the devtools server listens on
  94. int port_;
  95. // Output directory for |kUIDevToolsActivePortFileName| when
  96. // --enable-ui-devtools=0.
  97. base::FilePath active_port_output_directory_;
  98. const net::NetworkTrafficAnnotationTag tag_;
  99. TracingAgent* tracing_agent_ = nullptr;
  100. // Invoked when the server doesn't have any live connection.
  101. mutable base::OnceClosure on_session_ended_;
  102. base::OnceClosure on_socket_connected_;
  103. // The server (owned by Chrome for now)
  104. static UiDevToolsServer* devtools_server_;
  105. SEQUENCE_CHECKER(devtools_server_sequence_);
  106. base::WeakPtrFactory<UiDevToolsServer> weak_ptr_factory_{this};
  107. };
  108. } // namespace ui_devtools
  109. #endif // COMPONENTS_UI_DEVTOOLS_DEVTOOLS_SERVER_H_