blob: 379a5b95f28e20173333ec1edc935bde72d567ee [file] [log] [blame]
Yuwei Huang18b38bc2021-11-02 23:31:041// 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 Huang9dd5f8f2021-12-02 22:55:0514#include "remoting/host/chromoting_host_services_provider.h"
Yuwei Huang18b38bc2021-11-02 23:31:0415#include "remoting/host/mojom/chromoting_host_services.mojom.h"
16
Yuwei Huang2796bb62021-12-01 19:45:3917namespace base {
18class Environment;
19} // namespace base
20
Yuwei Huang18b38bc2021-11-02 23:31:0421namespace mojo {
22class IsolatedConnection;
23} // namespace mojo
24
25namespace 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 Huang9dd5f8f2021-12-02 22:55:0531class ChromotingHostServicesClient final
32 : public ChromotingHostServicesProvider {
Yuwei Huang18b38bc2021-11-02 23:31:0433 public:
34 ChromotingHostServicesClient();
35 ChromotingHostServicesClient(const ChromotingHostServicesClient&) = delete;
36 ChromotingHostServicesClient& operator=(const ChromotingHostServicesClient&) =
37 delete;
Yuwei Huang9dd5f8f2021-12-02 22:55:0538 ~ChromotingHostServicesClient() override;
Yuwei Huang18b38bc2021-11-02 23:31:0439
Yuwei Huang2796bb62021-12-01 19:45:3940 // 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 Huang9dd5f8f2021-12-02 22:55:0551 mojom::ChromotingSessionServices* GetSessionServices() const override;
Yuwei Huang18b38bc2021-11-02 23:31:0452
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 Huang2796bb62021-12-01 19:45:3959 bool EnsureSessionServicesBinding();
60
Yuwei Huang18b38bc2021-11-02 23:31:0461 void OnDisconnected();
62
63 SEQUENCE_CHECKER(sequence_checker_);
64
Yuwei Huang2796bb62021-12-01 19:45:3965 std::unique_ptr<base::Environment> environment_;
Yuwei Huang18b38bc2021-11-02 23:31:0466 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 Huang2796bb62021-12-01 19:45:3971 mojo::Remote<mojom::ChromotingSessionServices> session_services_remote_
72 GUARDED_BY_CONTEXT(sequence_checker_);
Yuwei Huang18b38bc2021-11-02 23:31:0473};
74
75} // namespace remoting
76
77#endif // REMOTING_HOST_CHROMOTING_HOST_SERVICES_CLIENT_H_