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