Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 39e537e | 2012-10-01 18:11:34 | [diff] [blame] | 5 | #ifndef REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_ |
| 6 | #define REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_ |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 7 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 8 | #include <memory> |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 11 | #include "base/containers/queue.h" |
Avi Drissman | 135261e | 2023-01-11 22:43:15 | [diff] [blame^] | 12 | #include "base/functional/callback.h" |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
Nan Lin | 4fcf95d5 | 2022-07-01 21:05:46 | [diff] [blame] | 14 | #include "base/values.h" |
| 15 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | 0c5d116 | 2012-03-28 01:07:30 | [diff] [blame] | 16 | |
| 17 | namespace base { |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 18 | class SingleThreadTaskRunner; |
[email protected] | 0c5d116 | 2012-03-28 01:07:30 | [diff] [blame] | 19 | } // namespace base |
| 20 | |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 21 | namespace remoting { |
| 22 | |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 23 | class AutoThread; |
| 24 | class AutoThreadTaskRunner; |
| 25 | |
| 26 | class DaemonController : public base::RefCountedThreadSafe<DaemonController> { |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 27 | public: |
weitaosu | d0adb36 | 2015-02-19 20:23:43 | [diff] [blame] | 28 | // These enumeration values are duplicated in host_controller.js except that |
| 29 | // NOT_INSTALLED is missing here. DaemonController runs in either the remoting |
| 30 | // host or the native messaging host which are only installed as part of the |
| 31 | // host package so the host must have already been installed. |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 32 | enum State { |
| 33 | // Placeholder state for platforms on which the daemon process is not |
| 34 | // implemented. The web-app will not show the corresponding UI. This value |
| 35 | // will eventually be deprecated or removed. |
weitaosu | d0adb36 | 2015-02-19 20:23:43 | [diff] [blame] | 36 | STATE_NOT_IMPLEMENTED = 0, |
[email protected] | cfb7e061 | 2012-03-30 23:58:56 | [diff] [blame] | 37 | // The daemon is installed but not running. Call Start to start it. |
| 38 | STATE_STOPPED = 2, |
| 39 | // The daemon process is starting. |
| 40 | STATE_STARTING = 3, |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 41 | // The daemon process is running. Call Start again to change the PIN or |
| 42 | // Stop to stop it. |
[email protected] | cfb7e061 | 2012-03-30 23:58:56 | [diff] [blame] | 43 | STATE_STARTED = 4, |
| 44 | // The daemon process is stopping. |
| 45 | STATE_STOPPING = 5, |
weitaosu | d0adb36 | 2015-02-19 20:23:43 | [diff] [blame] | 46 | // The state cannot be determined. |
[email protected] | f600c578 | 2012-04-03 08:26:47 | [diff] [blame] | 47 | STATE_UNKNOWN = 6 |
| 48 | }; |
| 49 | |
| 50 | // Enum used for completion callback. |
| 51 | enum AsyncResult { |
| 52 | RESULT_OK = 0, |
| 53 | |
| 54 | // The operation has FAILED. |
| 55 | RESULT_FAILED = 1, |
| 56 | |
| 57 | // User has cancelled the action (e.g. rejected UAC prompt). |
| 58 | // TODO(sergeyu): Current implementations don't return this value. |
| 59 | RESULT_CANCELLED = 2, |
| 60 | |
| 61 | // TODO(sergeyu): Add more error codes when we know how to handle |
| 62 | // them in the webapp. |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 63 | }; |
| 64 | |
[email protected] | 719b584 | 2012-04-28 07:06:31 | [diff] [blame] | 65 | // Callback type for GetConfig(). If the host is configured then a dictionary |
| 66 | // is returned containing host_id and xmpp_login, with security-sensitive |
| 67 | // fields filtered out. An empty dictionary is returned if the host is not |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 68 | // configured, and nullptr if the configuration is corrupt or cannot be read. |
Nan Lin | 4fcf95d5 | 2022-07-01 21:05:46 | [diff] [blame] | 69 | typedef base::OnceCallback<void(absl::optional<base::Value::Dict> config)> |
[email protected] | 0c5d116 | 2012-03-28 01:07:30 | [diff] [blame] | 70 | GetConfigCallback; |
| 71 | |
[email protected] | f600c578 | 2012-04-03 08:26:47 | [diff] [blame] | 72 | // Callback used for asynchronous operations, e.g. when |
| 73 | // starting/stopping the service. |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 74 | typedef base::OnceCallback<void(AsyncResult result)> CompletionCallback; |
[email protected] | f600c578 | 2012-04-03 08:26:47 | [diff] [blame] | 75 | |
Lambros Lambrou | 04f5f76a | 2019-11-13 01:59:46 | [diff] [blame] | 76 | // Callback used to notify a Boolean result. |
| 77 | typedef base::OnceCallback<void(bool)> BoolCallback; |
| 78 | |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 79 | struct UsageStatsConsent { |
[email protected] | 9fe0bfec | 2013-09-10 23:36:32 | [diff] [blame] | 80 | // Indicates whether crash dump reporting is supported by the host. |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 81 | bool supported; |
[email protected] | 9fe0bfec | 2013-09-10 23:36:32 | [diff] [blame] | 82 | |
| 83 | // Indicates if crash dump reporting is allowed by the user. |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 84 | bool allowed; |
[email protected] | 9fe0bfec | 2013-09-10 23:36:32 | [diff] [blame] | 85 | |
| 86 | // Carries information whether the crash dump reporting is controlled by |
| 87 | // policy. |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 88 | bool set_by_policy; |
| 89 | }; |
| 90 | |
[email protected] | 9fe0bfec | 2013-09-10 23:36:32 | [diff] [blame] | 91 | // Callback type for GetUsageStatsConsent(). |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 92 | typedef base::OnceCallback<void(const UsageStatsConsent&)> |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 93 | GetUsageStatsConsentCallback; |
[email protected] | c481bb6 | 2012-06-22 01:04:36 | [diff] [blame] | 94 | |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 95 | // Interface representing the platform-spacific back-end. Most of its methods |
[email protected] | ba23e5d3 | 2013-12-04 20:49:10 | [diff] [blame] | 96 | // are blocking and should be called on a background thread. There are two |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 97 | // exceptions: |
| 98 | // - GetState() is synchronous and called on the UI thread. It should avoid |
| 99 | // accessing any data members of the implementation. |
[email protected] | 9fe0bfec | 2013-09-10 23:36:32 | [diff] [blame] | 100 | // - SetConfigAndStart(), UpdateConfig() and Stop() indicate completion via |
[email protected] | ba23e5d3 | 2013-12-04 20:49:10 | [diff] [blame] | 101 | // a callback. There methods can be long running and should be caled |
| 102 | // on a background thread. |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 103 | class Delegate { |
| 104 | public: |
| 105 | virtual ~Delegate() {} |
| 106 | |
| 107 | // Return the "installed/running" state of the daemon process. This method |
| 108 | // should avoid accessing any data members of the implementation. |
| 109 | virtual State GetState() = 0; |
| 110 | |
| 111 | // Queries current host configuration. Any values that might be security |
| 112 | // sensitive have been filtered out. |
Nan Lin | 4fcf95d5 | 2022-07-01 21:05:46 | [diff] [blame] | 113 | virtual absl::optional<base::Value::Dict> GetConfig() = 0; |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 114 | |
Gary Kacmarcik | 0964ed5 | 2019-11-07 00:57:27 | [diff] [blame] | 115 | // Checks to verify that the required OS permissions have been granted to |
Lambros Lambrou | 04f5f76a | 2019-11-13 01:59:46 | [diff] [blame] | 116 | // the host process, querying the user if necessary. Notifies the callback |
| 117 | // when permission status is established, passing true iff all required |
| 118 | // permissions have been granted. |
Gary Kacmarcik | df3c143 | 2019-11-15 01:23:05 | [diff] [blame] | 119 | virtual void CheckPermission(bool it2me, BoolCallback callback) = 0; |
Gary Kacmarcik | 0964ed5 | 2019-11-07 00:57:27 | [diff] [blame] | 120 | |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 121 | // Starts the daemon process. This may require that the daemon be |
[email protected] | 9fe0bfec | 2013-09-10 23:36:32 | [diff] [blame] | 122 | // downloaded and installed. |done| is invoked on the calling thread when |
| 123 | // the operation is completed. |
Nan Lin | 4fcf95d5 | 2022-07-01 21:05:46 | [diff] [blame] | 124 | virtual void SetConfigAndStart(base::Value::Dict config, |
| 125 | bool consent, |
| 126 | CompletionCallback done) = 0; |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 127 | |
| 128 | // Updates current host configuration with the values specified in |
| 129 | // |config|. Any value in the existing configuration that isn't specified in |
| 130 | // |config| is preserved. |config| must not contain host_id or xmpp_login |
[email protected] | 9fe0bfec | 2013-09-10 23:36:32 | [diff] [blame] | 131 | // values, because implementations of this method cannot change them. |done| |
| 132 | // is invoked on the calling thread when the operation is completed. |
Nan Lin | 4fcf95d5 | 2022-07-01 21:05:46 | [diff] [blame] | 133 | virtual void UpdateConfig(base::Value::Dict config, |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 134 | CompletionCallback done) = 0; |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 135 | |
[email protected] | 9fe0bfec | 2013-09-10 23:36:32 | [diff] [blame] | 136 | // Stops the daemon process. |done| is invoked on the calling thread when |
| 137 | // the operation is completed. |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 138 | virtual void Stop(CompletionCallback done) = 0; |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 139 | |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 140 | // Get the user's consent to crash reporting. |
| 141 | virtual UsageStatsConsent GetUsageStatsConsent() = 0; |
| 142 | }; |
| 143 | |
| 144 | static scoped_refptr<DaemonController> Create(); |
| 145 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 146 | explicit DaemonController(std::unique_ptr<Delegate> delegate); |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 147 | |
Peter Boström | 7c0127b | 2021-10-05 18:39:56 | [diff] [blame] | 148 | DaemonController(const DaemonController&) = delete; |
| 149 | DaemonController& operator=(const DaemonController&) = delete; |
| 150 | |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 151 | // Return the "installed/running" state of the daemon process. |
[email protected] | 0c5d116 | 2012-03-28 01:07:30 | [diff] [blame] | 152 | // |
| 153 | // TODO(sergeyu): This method is called synchronously from the |
| 154 | // webapp. In most cases it requires IO operations, so it may block |
| 155 | // the user interface. Replace it with asynchronous notifications, |
| 156 | // e.g. with StartStateNotifications()/StopStateNotifications() methods. |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 157 | State GetState(); |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 158 | |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 159 | // Queries current host configuration. The |done| is called |
[email protected] | 015ce9f | 2012-04-25 22:57:34 | [diff] [blame] | 160 | // after the configuration is read, and any values that might be security |
| 161 | // sensitive have been filtered out. |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 162 | void GetConfig(GetConfigCallback done); |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 163 | |
Gary Kacmarcik | 0964ed5 | 2019-11-07 00:57:27 | [diff] [blame] | 164 | // Checks to see if the required OS permissions have been granted. This may |
| 165 | // show a dialog to the user requesting the permissions. |
Lambros Lambrou | 04f5f76a | 2019-11-13 01:59:46 | [diff] [blame] | 166 | // Notifies the callback when permission status is established, passing true |
| 167 | // iff all required permissions have been granted. |
Gary Kacmarcik | df3c143 | 2019-11-15 01:23:05 | [diff] [blame] | 168 | void CheckPermission(bool it2me, BoolCallback callback); |
Gary Kacmarcik | 0964ed5 | 2019-11-07 00:57:27 | [diff] [blame] | 169 | |
[email protected] | f600c578 | 2012-04-03 08:26:47 | [diff] [blame] | 170 | // Start the daemon process. This may require that the daemon be |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 171 | // downloaded and installed. |done| is called when the |
[email protected] | f600c578 | 2012-04-03 08:26:47 | [diff] [blame] | 172 | // operation is finished or fails. |
[email protected] | 3f01c46 | 2012-03-09 20:50:23 | [diff] [blame] | 173 | // |
[email protected] | 0c5d116 | 2012-03-28 01:07:30 | [diff] [blame] | 174 | // TODO(sergeyu): This method writes config and starts the host - |
| 175 | // these two steps are merged for simplicity. Consider splitting it |
| 176 | // into SetConfig() and Start() once we have basic host setup flow |
| 177 | // working. |
Nan Lin | 4fcf95d5 | 2022-07-01 21:05:46 | [diff] [blame] | 178 | void SetConfigAndStart(base::Value::Dict config, |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 179 | bool consent, |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 180 | CompletionCallback done); |
[email protected] | 0c5d116 | 2012-03-28 01:07:30 | [diff] [blame] | 181 | |
[email protected] | af10eb4 | 2012-04-04 05:08:27 | [diff] [blame] | 182 | // Updates current host configuration with the values specified in |
[email protected] | c90ec655 | 2012-04-09 19:46:41 | [diff] [blame] | 183 | // |config|. Changes must take effect before the call completes. |
[email protected] | 015ce9f | 2012-04-25 22:57:34 | [diff] [blame] | 184 | // Any value in the existing configuration that isn't specified in |config| |
| 185 | // is preserved. |config| must not contain host_id or xmpp_login values, |
| 186 | // because implementations of this method cannot change them. |
Nan Lin | 4fcf95d5 | 2022-07-01 21:05:46 | [diff] [blame] | 187 | void UpdateConfig(base::Value::Dict config, CompletionCallback done); |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 188 | |
| 189 | // Stop the daemon process. It is permitted to call Stop while the daemon |
| 190 | // process is being installed, in which case the installation should be |
| 191 | // aborted if possible; if not then it is sufficient to ensure that the |
| 192 | // daemon process is not started automatically upon successful installation. |
[email protected] | 3f01c46 | 2012-03-09 20:50:23 | [diff] [blame] | 193 | // As with Start, Stop may return before the operation is complete--poll |
| 194 | // GetState until the state is STATE_STOPPED. |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 195 | void Stop(CompletionCallback done); |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 196 | |
[email protected] | c481bb6 | 2012-06-22 01:04:36 | [diff] [blame] | 197 | // Get the user's consent to crash reporting. |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 198 | void GetUsageStatsConsent(GetUsageStatsConsentCallback done); |
[email protected] | c481bb6 | 2012-06-22 01:04:36 | [diff] [blame] | 199 | |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 200 | private: |
| 201 | friend class base::RefCountedThreadSafe<DaemonController>; |
| 202 | virtual ~DaemonController(); |
| 203 | |
| 204 | // Blocking helper methods used to call the delegate. |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 205 | void DoGetConfig(GetConfigCallback done); |
Nan Lin | 4fcf95d5 | 2022-07-01 21:05:46 | [diff] [blame] | 206 | void DoSetConfigAndStart(base::Value::Dict config, |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 207 | bool consent, |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 208 | CompletionCallback done); |
Nan Lin | 4fcf95d5 | 2022-07-01 21:05:46 | [diff] [blame] | 209 | void DoUpdateConfig(base::Value::Dict config, CompletionCallback done); |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 210 | void DoStop(CompletionCallback done); |
| 211 | void DoGetUsageStatsConsent(GetUsageStatsConsentCallback done); |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 212 | |
| 213 | // "Trampoline" callbacks that schedule the next pending request and then |
| 214 | // invoke the original caller-supplied callback. |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 215 | void InvokeCompletionCallbackAndScheduleNext(CompletionCallback done, |
| 216 | AsyncResult result); |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 217 | void InvokeConfigCallbackAndScheduleNext( |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 218 | GetConfigCallback done, |
Nan Lin | 4fcf95d5 | 2022-07-01 21:05:46 | [diff] [blame] | 219 | absl::optional<base::Value::Dict> config); |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 220 | void InvokeConsentCallbackAndScheduleNext(GetUsageStatsConsentCallback done, |
| 221 | const UsageStatsConsent& consent); |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 222 | |
| 223 | // Queue management methods. |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 224 | void OnServicingDone(); |
| 225 | void ServiceOrQueueRequest(base::OnceClosure request); |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 226 | void ServiceNextRequest(); |
| 227 | |
| 228 | // Task runner on which all public methods of this class should be called. |
| 229 | scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; |
| 230 | |
| 231 | // Task runner used to run blocking calls to the delegate. A single thread |
[email protected] | 9fe0bfec | 2013-09-10 23:36:32 | [diff] [blame] | 232 | // task runner is used to guarantee that one method of the delegate is |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 233 | // called at a time. |
| 234 | scoped_refptr<AutoThreadTaskRunner> delegate_task_runner_; |
| 235 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 236 | std::unique_ptr<AutoThread> delegate_thread_; |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 237 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 238 | std::unique_ptr<Delegate> delegate_; |
[email protected] | 8c5d6c0 | 2013-09-10 00:54:05 | [diff] [blame] | 239 | |
Evan Stade | 92d3140 | 2020-06-26 19:29:20 | [diff] [blame] | 240 | bool servicing_request_ = false; |
| 241 | base::queue<base::OnceClosure> pending_requests_; |
[email protected] | f4f1be5d | 2012-02-07 05:35:19 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | } // namespace remoting |
| 245 | |
[email protected] | 39e537e | 2012-10-01 18:11:34 | [diff] [blame] | 246 | #endif // REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_ |