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 | #include "remoting/host/chromoting_host_services_client.h" |
| 6 | |
| 7 | #include "base/bind.h" |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 8 | #include "base/environment.h" |
| 9 | #include "base/notreached.h" |
Yuwei Huang | 18b38bc | 2021-11-02 23:31:04 | [diff] [blame] | 10 | #include "base/sequence_checker.h" |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 11 | #include "build/build_config.h" |
Yuwei Huang | 18b38bc | 2021-11-02 23:31:04 | [diff] [blame] | 12 | #include "mojo/public/cpp/bindings/pending_remote.h" |
| 13 | #include "mojo/public/cpp/system/isolated_connection.h" |
| 14 | #include "remoting/host/ipc_constants.h" |
| 15 | #include "remoting/host/mojom/chromoting_host_services.mojom.h" |
| 16 | |
Xiaohan Wang | 453b3867 | 2022-01-13 20:02:44 | [diff] [blame] | 17 | #if BUILDFLAG(IS_WIN) |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 18 | #include <windows.h> |
| 19 | |
| 20 | #include "remoting/host/win/acl_util.h" |
| 21 | #endif |
| 22 | |
Yuwei Huang | 18b38bc | 2021-11-02 23:31:04 | [diff] [blame] | 23 | namespace remoting { |
| 24 | |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 25 | namespace { |
| 26 | |
Xiaohan Wang | 453b3867 | 2022-01-13 20:02:44 | [diff] [blame] | 27 | #if BUILDFLAG(IS_LINUX) |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 28 | constexpr char kChromeRemoteDesktopSessionEnvVar[] = |
| 29 | "CHROME_REMOTE_DESKTOP_SESSION"; |
| 30 | #endif |
| 31 | |
| 32 | bool g_initialized = false; |
| 33 | |
| 34 | } // namespace |
| 35 | |
Yuwei Huang | 18b38bc | 2021-11-02 23:31:04 | [diff] [blame] | 36 | ChromotingHostServicesClient::ChromotingHostServicesClient() |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 37 | : environment_(base::Environment::Create()), |
| 38 | server_name_(GetChromotingHostServicesServerName()) { |
| 39 | DCHECK(g_initialized) |
| 40 | << "ChromotingHostServicesClient::Initialize() has not been called."; |
| 41 | } |
Yuwei Huang | 18b38bc | 2021-11-02 23:31:04 | [diff] [blame] | 42 | |
| 43 | ChromotingHostServicesClient::~ChromotingHostServicesClient() { |
| 44 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 45 | } |
| 46 | |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 47 | // static |
| 48 | bool ChromotingHostServicesClient::Initialize() { |
| 49 | DCHECK(!g_initialized); |
Xiaohan Wang | 453b3867 | 2022-01-13 20:02:44 | [diff] [blame] | 50 | #if BUILDFLAG(IS_WIN) |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 51 | // The ChromotingHostServices server runs under the LocalService account, |
| 52 | // which normally isn't allowed to query process info like session ID of a |
| 53 | // process running under a different account, so we add an ACL to allow it. |
| 54 | g_initialized = AddProcessAccessRightForWellKnownSid( |
| 55 | WinLocalServiceSid, PROCESS_QUERY_LIMITED_INFORMATION); |
| 56 | #else |
| 57 | // Other platforms don't need initialization. |
| 58 | g_initialized = true; |
| 59 | #endif |
| 60 | return g_initialized; |
| 61 | } |
| 62 | |
| 63 | mojom::ChromotingSessionServices* |
| 64 | ChromotingHostServicesClient::GetSessionServices() const { |
Yuwei Huang | 18b38bc | 2021-11-02 23:31:04 | [diff] [blame] | 65 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 66 | |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 67 | if (!const_cast<ChromotingHostServicesClient*>(this) |
| 68 | ->EnsureSessionServicesBinding()) { |
Yuwei Huang | 18b38bc | 2021-11-02 23:31:04 | [diff] [blame] | 69 | return nullptr; |
| 70 | } |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 71 | return session_services_remote_.get(); |
Yuwei Huang | 18b38bc | 2021-11-02 23:31:04 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | bool ChromotingHostServicesClient::EnsureConnection() { |
| 75 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 76 | |
| 77 | if (remote_.is_bound()) { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | auto endpoint = mojo::NamedPlatformChannel::ConnectToServer(server_name_); |
| 82 | if (!endpoint.is_valid()) { |
| 83 | LOG(WARNING) << "Cannot connect to IPC through server name " << server_name_ |
| 84 | << ". Endpoint is invalid."; |
| 85 | return false; |
| 86 | } |
| 87 | connection_ = std::make_unique<mojo::IsolatedConnection>(); |
| 88 | mojo::PendingRemote<mojom::ChromotingHostServices> pending_remote( |
| 89 | connection_->Connect(std::move(endpoint)), /* version= */ 0); |
| 90 | if (!pending_remote.is_valid()) { |
| 91 | LOG(WARNING) << "Invalid message pipe."; |
| 92 | connection_.reset(); |
| 93 | return false; |
| 94 | } |
| 95 | remote_.Bind(std::move(pending_remote)); |
| 96 | remote_.set_disconnect_handler(base::BindOnce( |
| 97 | &ChromotingHostServicesClient::OnDisconnected, base::Unretained(this))); |
| 98 | return true; |
| 99 | } |
| 100 | |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 101 | bool ChromotingHostServicesClient::EnsureSessionServicesBinding() { |
| 102 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 103 | |
| 104 | if (session_services_remote_.is_bound()) { |
| 105 | return true; |
| 106 | } |
Xiaohan Wang | 453b3867 | 2022-01-13 20:02:44 | [diff] [blame] | 107 | #if BUILDFLAG(IS_LINUX) |
Yuwei Huang | 2796bb6 | 2021-12-01 19:45:39 | [diff] [blame] | 108 | if (!environment_->HasVar(kChromeRemoteDesktopSessionEnvVar)) { |
| 109 | LOG(WARNING) << "Current desktop environment is not remotable."; |
| 110 | return false; |
| 111 | } |
| 112 | #endif |
| 113 | if (!EnsureConnection()) { |
| 114 | return false; |
| 115 | } |
| 116 | remote_->BindSessionServices( |
| 117 | session_services_remote_.BindNewPipeAndPassReceiver()); |
| 118 | session_services_remote_.reset_on_disconnect(); |
| 119 | return true; |
| 120 | } |
| 121 | |
Yuwei Huang | 18b38bc | 2021-11-02 23:31:04 | [diff] [blame] | 122 | void ChromotingHostServicesClient::OnDisconnected() { |
| 123 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 124 | |
| 125 | remote_.reset(); |
| 126 | connection_.reset(); |
| 127 | } |
| 128 | |
| 129 | } // namespace remoting |