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.
5 #ifndef REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_
6 #define REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
16 class DictionaryValue
;
17 class SingleThreadTaskRunner
;
23 class AutoThreadTaskRunner
;
25 class DaemonController
: public base::RefCountedThreadSafe
<DaemonController
> {
27 // These enumeration values are duplicated in host_controller.js except that
28 // NOT_INSTALLED is missing here. DaemonController runs in either the remoting
29 // host or the native messaging host which are only installed as part of the
30 // host package so the host must have already been installed.
32 // Placeholder state for platforms on which the daemon process is not
33 // implemented. The web-app will not show the corresponding UI. This value
34 // will eventually be deprecated or removed.
35 STATE_NOT_IMPLEMENTED
= 0,
36 // The daemon is installed but not running. Call Start to start it.
38 // The daemon process is starting.
40 // The daemon process is running. Call Start again to change the PIN or
43 // The daemon process is stopping.
45 // The state cannot be determined.
49 // Enum used for completion callback.
53 // The operation has FAILED.
56 // User has cancelled the action (e.g. rejected UAC prompt).
57 // TODO(sergeyu): Current implementations don't return this value.
60 // Failed to access host directory.
61 RESULT_FAILED_DIRECTORY
= 3
63 // TODO(sergeyu): Add more error codes when we know how to handle
64 // them in the webapp.
67 // Callback type for GetConfig(). If the host is configured then a dictionary
68 // is returned containing host_id and xmpp_login, with security-sensitive
69 // fields filtered out. An empty dictionary is returned if the host is not
70 // configured, and nullptr if the configuration is corrupt or cannot be read.
71 typedef base::Callback
<void (scoped_ptr
<base::DictionaryValue
> config
)>
74 // Callback used for asynchronous operations, e.g. when
75 // starting/stopping the service.
76 typedef base::Callback
<void (AsyncResult result
)> CompletionCallback
;
78 struct UsageStatsConsent
{
79 // Indicates whether crash dump reporting is supported by the host.
82 // Indicates if crash dump reporting is allowed by the user.
85 // Carries information whether the crash dump reporting is controlled by
90 // Callback type for GetUsageStatsConsent().
91 typedef base::Callback
<void (const UsageStatsConsent
&)>
92 GetUsageStatsConsentCallback
;
94 // Interface representing the platform-spacific back-end. Most of its methods
95 // are blocking and should be called on a background thread. There are two
97 // - GetState() is synchronous and called on the UI thread. It should avoid
98 // accessing any data members of the implementation.
99 // - SetConfigAndStart(), UpdateConfig() and Stop() indicate completion via
100 // a callback. There methods can be long running and should be caled
101 // on a background thread.
104 virtual ~Delegate() {}
106 // Return the "installed/running" state of the daemon process. This method
107 // should avoid accessing any data members of the implementation.
108 virtual State
GetState() = 0;
110 // Queries current host configuration. Any values that might be security
111 // sensitive have been filtered out.
112 virtual scoped_ptr
<base::DictionaryValue
> GetConfig() = 0;
114 // Starts the daemon process. This may require that the daemon be
115 // downloaded and installed. |done| is invoked on the calling thread when
116 // the operation is completed.
117 virtual void SetConfigAndStart(
118 scoped_ptr
<base::DictionaryValue
> config
,
120 const CompletionCallback
& done
) = 0;
122 // Updates current host configuration with the values specified in
123 // |config|. Any value in the existing configuration that isn't specified in
124 // |config| is preserved. |config| must not contain host_id or xmpp_login
125 // values, because implementations of this method cannot change them. |done|
126 // is invoked on the calling thread when the operation is completed.
127 virtual void UpdateConfig(
128 scoped_ptr
<base::DictionaryValue
> config
,
129 const CompletionCallback
& done
) = 0;
131 // Stops the daemon process. |done| is invoked on the calling thread when
132 // the operation is completed.
133 virtual void Stop(const CompletionCallback
& done
) = 0;
135 // Get the user's consent to crash reporting.
136 virtual UsageStatsConsent
GetUsageStatsConsent() = 0;
139 static scoped_refptr
<DaemonController
> Create();
141 explicit DaemonController(scoped_ptr
<Delegate
> delegate
);
143 // Return the "installed/running" state of the daemon process.
145 // TODO(sergeyu): This method is called synchronously from the
146 // webapp. In most cases it requires IO operations, so it may block
147 // the user interface. Replace it with asynchronous notifications,
148 // e.g. with StartStateNotifications()/StopStateNotifications() methods.
151 // Queries current host configuration. The |done| is called
152 // after the configuration is read, and any values that might be security
153 // sensitive have been filtered out.
154 void GetConfig(const GetConfigCallback
& done
);
156 // Start the daemon process. This may require that the daemon be
157 // downloaded and installed. |done| is called when the
158 // operation is finished or fails.
160 // TODO(sergeyu): This method writes config and starts the host -
161 // these two steps are merged for simplicity. Consider splitting it
162 // into SetConfig() and Start() once we have basic host setup flow
164 void SetConfigAndStart(scoped_ptr
<base::DictionaryValue
> config
,
166 const CompletionCallback
& done
);
168 // Updates current host configuration with the values specified in
169 // |config|. Changes must take effect before the call completes.
170 // Any value in the existing configuration that isn't specified in |config|
171 // is preserved. |config| must not contain host_id or xmpp_login values,
172 // because implementations of this method cannot change them.
173 void UpdateConfig(scoped_ptr
<base::DictionaryValue
> config
,
174 const CompletionCallback
& done
);
176 // Stop the daemon process. It is permitted to call Stop while the daemon
177 // process is being installed, in which case the installation should be
178 // aborted if possible; if not then it is sufficient to ensure that the
179 // daemon process is not started automatically upon successful installation.
180 // As with Start, Stop may return before the operation is complete--poll
181 // GetState until the state is STATE_STOPPED.
182 void Stop(const CompletionCallback
& done
);
184 // Get the user's consent to crash reporting.
185 void GetUsageStatsConsent(const GetUsageStatsConsentCallback
& done
);
188 friend class base::RefCountedThreadSafe
<DaemonController
>;
189 virtual ~DaemonController();
191 // Blocking helper methods used to call the delegate.
192 void DoGetConfig(const GetConfigCallback
& done
);
193 void DoSetConfigAndStart(scoped_ptr
<base::DictionaryValue
> config
,
195 const CompletionCallback
& done
);
196 void DoUpdateConfig(scoped_ptr
<base::DictionaryValue
> config
,
197 const CompletionCallback
& done
);
198 void DoStop(const CompletionCallback
& done
);
199 void DoGetUsageStatsConsent(const GetUsageStatsConsentCallback
& done
);
201 // "Trampoline" callbacks that schedule the next pending request and then
202 // invoke the original caller-supplied callback.
203 void InvokeCompletionCallbackAndScheduleNext(
204 const CompletionCallback
& done
,
206 void InvokeConfigCallbackAndScheduleNext(
207 const GetConfigCallback
& done
,
208 scoped_ptr
<base::DictionaryValue
> config
);
209 void InvokeConsentCallbackAndScheduleNext(
210 const GetUsageStatsConsentCallback
& done
,
211 const UsageStatsConsent
& consent
);
213 // Queue management methods.
215 void ServiceOrQueueRequest(const base::Closure
& request
);
216 void ServiceNextRequest();
218 // Task runner on which all public methods of this class should be called.
219 scoped_refptr
<base::SingleThreadTaskRunner
> caller_task_runner_
;
221 // Task runner used to run blocking calls to the delegate. A single thread
222 // task runner is used to guarantee that one method of the delegate is
224 scoped_refptr
<AutoThreadTaskRunner
> delegate_task_runner_
;
226 scoped_ptr
<AutoThread
> delegate_thread_
;
228 scoped_ptr
<Delegate
> delegate_
;
230 std::queue
<base::Closure
> pending_requests_
;
232 DISALLOW_COPY_AND_ASSIGN(DaemonController
);
235 } // namespace remoting
237 #endif // REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_