daemon_controller.h 10.0 KB

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