blob: 727d6d7b2d9d737eeded0ab372a5e969075eed07 [file] [log] [blame]
Avi Drissmand6cdf9b2022-09-15 19:52:531// Copyright 2012 The Chromium Authors
[email protected]f4f1be5d2012-02-07 05:35:192// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]39e537e2012-10-01 18:11:345#ifndef REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_
6#define REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_
[email protected]f4f1be5d2012-02-07 05:35:197
dcheng0765c492016-04-06 22:41:538#include <memory>
[email protected]f4f1be5d2012-02-07 05:35:199#include <string>
10
Brett Wilsonb02c0a22017-09-25 22:34:4211#include "base/containers/queue.h"
Avi Drissman135261e2023-01-11 22:43:1512#include "base/functional/callback.h"
[email protected]8c5d6c02013-09-10 00:54:0513#include "base/memory/ref_counted.h"
Nan Lin4fcf95d52022-07-01 21:05:4614#include "base/values.h"
15#include "third_party/abseil-cpp/absl/types/optional.h"
[email protected]0c5d1162012-03-28 01:07:3016
17namespace base {
[email protected]8c5d6c02013-09-10 00:54:0518class SingleThreadTaskRunner;
[email protected]0c5d1162012-03-28 01:07:3019} // namespace base
20
[email protected]f4f1be5d2012-02-07 05:35:1921namespace remoting {
22
[email protected]8c5d6c02013-09-10 00:54:0523class AutoThread;
24class AutoThreadTaskRunner;
25
26class DaemonController : public base::RefCountedThreadSafe<DaemonController> {
[email protected]f4f1be5d2012-02-07 05:35:1927 public:
weitaosud0adb362015-02-19 20:23:4328 // 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]f4f1be5d2012-02-07 05:35:1932 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.
weitaosud0adb362015-02-19 20:23:4336 STATE_NOT_IMPLEMENTED = 0,
[email protected]cfb7e0612012-03-30 23:58:5637 // 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]f4f1be5d2012-02-07 05:35:1941 // The daemon process is running. Call Start again to change the PIN or
42 // Stop to stop it.
[email protected]cfb7e0612012-03-30 23:58:5643 STATE_STARTED = 4,
44 // The daemon process is stopping.
45 STATE_STOPPING = 5,
weitaosud0adb362015-02-19 20:23:4346 // The state cannot be determined.
[email protected]f600c5782012-04-03 08:26:4747 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]f4f1be5d2012-02-07 05:35:1963 };
64
[email protected]719b5842012-04-28 07:06:3165 // 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
sergeyuc5f104b2015-01-09 19:33:2468 // configured, and nullptr if the configuration is corrupt or cannot be read.
Nan Lin4fcf95d52022-07-01 21:05:4669 typedef base::OnceCallback<void(absl::optional<base::Value::Dict> config)>
[email protected]0c5d1162012-03-28 01:07:3070 GetConfigCallback;
71
[email protected]f600c5782012-04-03 08:26:4772 // Callback used for asynchronous operations, e.g. when
73 // starting/stopping the service.
Evan Stade92d31402020-06-26 19:29:2074 typedef base::OnceCallback<void(AsyncResult result)> CompletionCallback;
[email protected]f600c5782012-04-03 08:26:4775
Lambros Lambrou04f5f76a2019-11-13 01:59:4676 // Callback used to notify a Boolean result.
77 typedef base::OnceCallback<void(bool)> BoolCallback;
78
[email protected]8c5d6c02013-09-10 00:54:0579 struct UsageStatsConsent {
[email protected]9fe0bfec2013-09-10 23:36:3280 // Indicates whether crash dump reporting is supported by the host.
[email protected]8c5d6c02013-09-10 00:54:0581 bool supported;
[email protected]9fe0bfec2013-09-10 23:36:3282
83 // Indicates if crash dump reporting is allowed by the user.
[email protected]8c5d6c02013-09-10 00:54:0584 bool allowed;
[email protected]9fe0bfec2013-09-10 23:36:3285
86 // Carries information whether the crash dump reporting is controlled by
87 // policy.
[email protected]8c5d6c02013-09-10 00:54:0588 bool set_by_policy;
89 };
90
[email protected]9fe0bfec2013-09-10 23:36:3291 // Callback type for GetUsageStatsConsent().
Evan Stade92d31402020-06-26 19:29:2092 typedef base::OnceCallback<void(const UsageStatsConsent&)>
[email protected]8c5d6c02013-09-10 00:54:0593 GetUsageStatsConsentCallback;
[email protected]c481bb62012-06-22 01:04:3694
[email protected]8c5d6c02013-09-10 00:54:0595 // Interface representing the platform-spacific back-end. Most of its methods
[email protected]ba23e5d32013-12-04 20:49:1096 // are blocking and should be called on a background thread. There are two
[email protected]8c5d6c02013-09-10 00:54:0597 // exceptions:
98 // - GetState() is synchronous and called on the UI thread. It should avoid
99 // accessing any data members of the implementation.
[email protected]9fe0bfec2013-09-10 23:36:32100 // - SetConfigAndStart(), UpdateConfig() and Stop() indicate completion via
[email protected]ba23e5d32013-12-04 20:49:10101 // a callback. There methods can be long running and should be caled
102 // on a background thread.
[email protected]8c5d6c02013-09-10 00:54:05103 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 Lin4fcf95d52022-07-01 21:05:46113 virtual absl::optional<base::Value::Dict> GetConfig() = 0;
[email protected]8c5d6c02013-09-10 00:54:05114
Gary Kacmarcik0964ed52019-11-07 00:57:27115 // Checks to verify that the required OS permissions have been granted to
Lambros Lambrou04f5f76a2019-11-13 01:59:46116 // 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 Kacmarcikdf3c1432019-11-15 01:23:05119 virtual void CheckPermission(bool it2me, BoolCallback callback) = 0;
Gary Kacmarcik0964ed52019-11-07 00:57:27120
[email protected]8c5d6c02013-09-10 00:54:05121 // Starts the daemon process. This may require that the daemon be
[email protected]9fe0bfec2013-09-10 23:36:32122 // downloaded and installed. |done| is invoked on the calling thread when
123 // the operation is completed.
Nan Lin4fcf95d52022-07-01 21:05:46124 virtual void SetConfigAndStart(base::Value::Dict config,
125 bool consent,
126 CompletionCallback done) = 0;
[email protected]8c5d6c02013-09-10 00:54:05127
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]9fe0bfec2013-09-10 23:36:32131 // values, because implementations of this method cannot change them. |done|
132 // is invoked on the calling thread when the operation is completed.
Nan Lin4fcf95d52022-07-01 21:05:46133 virtual void UpdateConfig(base::Value::Dict config,
Evan Stade92d31402020-06-26 19:29:20134 CompletionCallback done) = 0;
[email protected]8c5d6c02013-09-10 00:54:05135
[email protected]9fe0bfec2013-09-10 23:36:32136 // Stops the daemon process. |done| is invoked on the calling thread when
137 // the operation is completed.
Evan Stade92d31402020-06-26 19:29:20138 virtual void Stop(CompletionCallback done) = 0;
[email protected]8c5d6c02013-09-10 00:54:05139
[email protected]8c5d6c02013-09-10 00:54:05140 // Get the user's consent to crash reporting.
141 virtual UsageStatsConsent GetUsageStatsConsent() = 0;
142 };
143
144 static scoped_refptr<DaemonController> Create();
145
dcheng0765c492016-04-06 22:41:53146 explicit DaemonController(std::unique_ptr<Delegate> delegate);
[email protected]f4f1be5d2012-02-07 05:35:19147
Peter Boström7c0127b2021-10-05 18:39:56148 DaemonController(const DaemonController&) = delete;
149 DaemonController& operator=(const DaemonController&) = delete;
150
[email protected]f4f1be5d2012-02-07 05:35:19151 // Return the "installed/running" state of the daemon process.
[email protected]0c5d1162012-03-28 01:07:30152 //
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]8c5d6c02013-09-10 00:54:05157 State GetState();
[email protected]f4f1be5d2012-02-07 05:35:19158
[email protected]8c5d6c02013-09-10 00:54:05159 // Queries current host configuration. The |done| is called
[email protected]015ce9f2012-04-25 22:57:34160 // after the configuration is read, and any values that might be security
161 // sensitive have been filtered out.
Evan Stade92d31402020-06-26 19:29:20162 void GetConfig(GetConfigCallback done);
[email protected]f4f1be5d2012-02-07 05:35:19163
Gary Kacmarcik0964ed52019-11-07 00:57:27164 // Checks to see if the required OS permissions have been granted. This may
165 // show a dialog to the user requesting the permissions.
Lambros Lambrou04f5f76a2019-11-13 01:59:46166 // Notifies the callback when permission status is established, passing true
167 // iff all required permissions have been granted.
Gary Kacmarcikdf3c1432019-11-15 01:23:05168 void CheckPermission(bool it2me, BoolCallback callback);
Gary Kacmarcik0964ed52019-11-07 00:57:27169
[email protected]f600c5782012-04-03 08:26:47170 // Start the daemon process. This may require that the daemon be
[email protected]8c5d6c02013-09-10 00:54:05171 // downloaded and installed. |done| is called when the
[email protected]f600c5782012-04-03 08:26:47172 // operation is finished or fails.
[email protected]3f01c462012-03-09 20:50:23173 //
[email protected]0c5d1162012-03-28 01:07:30174 // 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 Lin4fcf95d52022-07-01 21:05:46178 void SetConfigAndStart(base::Value::Dict config,
[email protected]8c5d6c02013-09-10 00:54:05179 bool consent,
Evan Stade92d31402020-06-26 19:29:20180 CompletionCallback done);
[email protected]0c5d1162012-03-28 01:07:30181
[email protected]af10eb42012-04-04 05:08:27182 // Updates current host configuration with the values specified in
[email protected]c90ec6552012-04-09 19:46:41183 // |config|. Changes must take effect before the call completes.
[email protected]015ce9f2012-04-25 22:57:34184 // 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 Lin4fcf95d52022-07-01 21:05:46187 void UpdateConfig(base::Value::Dict config, CompletionCallback done);
[email protected]f4f1be5d2012-02-07 05:35:19188
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]3f01c462012-03-09 20:50:23193 // As with Start, Stop may return before the operation is complete--poll
194 // GetState until the state is STATE_STOPPED.
Evan Stade92d31402020-06-26 19:29:20195 void Stop(CompletionCallback done);
[email protected]f4f1be5d2012-02-07 05:35:19196
[email protected]c481bb62012-06-22 01:04:36197 // Get the user's consent to crash reporting.
Evan Stade92d31402020-06-26 19:29:20198 void GetUsageStatsConsent(GetUsageStatsConsentCallback done);
[email protected]c481bb62012-06-22 01:04:36199
[email protected]8c5d6c02013-09-10 00:54:05200 private:
201 friend class base::RefCountedThreadSafe<DaemonController>;
202 virtual ~DaemonController();
203
204 // Blocking helper methods used to call the delegate.
Evan Stade92d31402020-06-26 19:29:20205 void DoGetConfig(GetConfigCallback done);
Nan Lin4fcf95d52022-07-01 21:05:46206 void DoSetConfigAndStart(base::Value::Dict config,
[email protected]8c5d6c02013-09-10 00:54:05207 bool consent,
Evan Stade92d31402020-06-26 19:29:20208 CompletionCallback done);
Nan Lin4fcf95d52022-07-01 21:05:46209 void DoUpdateConfig(base::Value::Dict config, CompletionCallback done);
Evan Stade92d31402020-06-26 19:29:20210 void DoStop(CompletionCallback done);
211 void DoGetUsageStatsConsent(GetUsageStatsConsentCallback done);
[email protected]8c5d6c02013-09-10 00:54:05212
213 // "Trampoline" callbacks that schedule the next pending request and then
214 // invoke the original caller-supplied callback.
Evan Stade92d31402020-06-26 19:29:20215 void InvokeCompletionCallbackAndScheduleNext(CompletionCallback done,
216 AsyncResult result);
[email protected]8c5d6c02013-09-10 00:54:05217 void InvokeConfigCallbackAndScheduleNext(
Evan Stade92d31402020-06-26 19:29:20218 GetConfigCallback done,
Nan Lin4fcf95d52022-07-01 21:05:46219 absl::optional<base::Value::Dict> config);
Evan Stade92d31402020-06-26 19:29:20220 void InvokeConsentCallbackAndScheduleNext(GetUsageStatsConsentCallback done,
221 const UsageStatsConsent& consent);
[email protected]8c5d6c02013-09-10 00:54:05222
223 // Queue management methods.
Evan Stade92d31402020-06-26 19:29:20224 void OnServicingDone();
225 void ServiceOrQueueRequest(base::OnceClosure request);
[email protected]8c5d6c02013-09-10 00:54:05226 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]9fe0bfec2013-09-10 23:36:32232 // task runner is used to guarantee that one method of the delegate is
[email protected]8c5d6c02013-09-10 00:54:05233 // called at a time.
234 scoped_refptr<AutoThreadTaskRunner> delegate_task_runner_;
235
dcheng0765c492016-04-06 22:41:53236 std::unique_ptr<AutoThread> delegate_thread_;
[email protected]8c5d6c02013-09-10 00:54:05237
dcheng0765c492016-04-06 22:41:53238 std::unique_ptr<Delegate> delegate_;
[email protected]8c5d6c02013-09-10 00:54:05239
Evan Stade92d31402020-06-26 19:29:20240 bool servicing_request_ = false;
241 base::queue<base::OnceClosure> pending_requests_;
[email protected]f4f1be5d2012-02-07 05:35:19242};
243
244} // namespace remoting
245
[email protected]39e537e2012-10-01 18:11:34246#endif // REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_