chromoting_host_services_client.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2021 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 REMOTING_HOST_CHROMOTING_HOST_SERVICES_CLIENT_H_
  5. #define REMOTING_HOST_CHROMOTING_HOST_SERVICES_CLIENT_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/sequence_checker.h"
  9. #include "base/thread_annotations.h"
  10. #include "build/build_config.h"
  11. #include "mojo/public/cpp/bindings/remote.h"
  12. #include "mojo/public/cpp/platform/named_platform_channel.h"
  13. #include "remoting/host/chromoting_host_services_provider.h"
  14. #include "remoting/host/mojom/chromoting_host_services.mojom.h"
  15. namespace base {
  16. class Environment;
  17. } // namespace base
  18. namespace mojo {
  19. class IsolatedConnection;
  20. } // namespace mojo
  21. namespace remoting {
  22. // Maintains connection to a ChromotingHostServices server, and provides the
  23. // ChromotingHostServices interface. Note that each process should have only one
  24. // ChromotingHostServicesClient instance. Making multiple connections to the
  25. // ChromotingHostServices server is not supported.
  26. class ChromotingHostServicesClient final
  27. : public ChromotingHostServicesProvider {
  28. public:
  29. ChromotingHostServicesClient();
  30. ChromotingHostServicesClient(const ChromotingHostServicesClient&) = delete;
  31. ChromotingHostServicesClient& operator=(const ChromotingHostServicesClient&) =
  32. delete;
  33. ~ChromotingHostServicesClient() override;
  34. // Configures the current process to allow it to communicate with the
  35. // ChromotingHostServices server. Must be called once before using any
  36. // instance of ChromotingHostServicesClient.
  37. // Returns a boolean that indicates whether the initialization succeeded.
  38. static bool Initialize();
  39. // Gets the ChromotingSessionServices. Always null-check before using it, as
  40. // nullptr will be returned if the connection could not be established.
  41. // Note that when the session is not remoted, you will still get a callable
  42. // interface, but all outgoing IPCs will be silently dropped, and any pending
  43. // receivers/remotes/message pipes sent will be closed.
  44. mojom::ChromotingSessionServices* GetSessionServices() const override;
  45. private:
  46. friend class ChromotingHostServicesClientTest;
  47. #if BUILDFLAG(IS_LINUX)
  48. static constexpr char kChromeRemoteDesktopSessionEnvVar[] =
  49. "CHROME_REMOTE_DESKTOP_SESSION";
  50. #endif
  51. ChromotingHostServicesClient(
  52. std::unique_ptr<base::Environment> environment,
  53. const mojo::NamedPlatformChannel::ServerName& server_name);
  54. // Attempts to connect to the IPC server if the connection has not been
  55. // established. Returns a boolean indicating whether there is a valid IPC
  56. // connection to the chromoting host.
  57. bool EnsureConnection();
  58. bool EnsureSessionServicesBinding();
  59. void OnDisconnected();
  60. void OnSessionDisconnected();
  61. SEQUENCE_CHECKER(sequence_checker_);
  62. std::unique_ptr<base::Environment> environment_;
  63. mojo::NamedPlatformChannel::ServerName server_name_;
  64. std::unique_ptr<mojo::IsolatedConnection> connection_
  65. GUARDED_BY_CONTEXT(sequence_checker_);
  66. mojo::Remote<mojom::ChromotingHostServices> remote_
  67. GUARDED_BY_CONTEXT(sequence_checker_);
  68. mojo::Remote<mojom::ChromotingSessionServices> session_services_remote_
  69. GUARDED_BY_CONTEXT(sequence_checker_);
  70. base::OnceClosure on_session_disconnected_callback_for_testing_;
  71. };
  72. } // namespace remoting
  73. #endif // REMOTING_HOST_CHROMOTING_HOST_SERVICES_CLIENT_H_