OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_ | 5 #ifndef REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_ |
6 #define REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_ | 6 #define REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 class DictionaryValue; | 16 class DictionaryValue; |
17 class SingleThreadTaskRunner; | 17 class SingleThreadTaskRunner; |
18 } // namespace base | 18 } // namespace base |
19 | 19 |
| 20 namespace tracked_objects { |
| 21 class Location; |
| 22 } // namespace tracked_objects |
| 23 |
20 namespace remoting { | 24 namespace remoting { |
21 | 25 |
22 class AutoThread; | 26 class AutoThread; |
23 class AutoThreadTaskRunner; | 27 class AutoThreadTaskRunner; |
24 | 28 |
25 class DaemonController : public base::RefCountedThreadSafe<DaemonController> { | 29 class DaemonController : public base::RefCountedThreadSafe<DaemonController> { |
26 public: | 30 public: |
27 // These enumeration values are duplicated in host_controller.js except that | 31 // These enumeration values are duplicated in host_controller.js except that |
28 // NOT_INSTALLED is missing here. DaemonController runs in either the remoting | 32 // 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 | 33 // 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. | 34 // host package so the host must have already been installed. |
31 enum State { | 35 enum State { |
32 // Placeholder state for platforms on which the daemon process is not | 36 // Placeholder state for platforms on which the daemon process is not |
33 // implemented. The web-app will not show the corresponding UI. This value | 37 // implemented. The web-app will not show the corresponding UI. This value |
34 // will eventually be deprecated or removed. | 38 // will eventually be deprecated or removed. |
35 STATE_NOT_IMPLEMENTED = 0, | 39 STATE_NOT_IMPLEMENTED = 0, |
36 // The daemon is installed but not running. Call Start to start it. | 40 // The daemon is installed but not running. Call Start to start it. |
37 STATE_STOPPED = 2, | 41 STATE_STOPPED = 2, |
38 // The daemon process is starting. | 42 // The daemon process is starting. |
39 STATE_STARTING = 3, | 43 STATE_STARTING = 3, |
40 // The daemon process is running. Call Start again to change the PIN or | 44 // The daemon process is running. Call Start again to change the PIN or |
41 // Stop to stop it. | 45 // Stop to stop it. |
42 STATE_STARTED = 4, | 46 STATE_STARTED = 4, |
43 // The daemon process is stopping. | 47 // The daemon process is stopping. |
44 STATE_STOPPING = 5, | 48 STATE_STOPPING = 5, |
45 // The state cannot be determined. | 49 // The state cannot be determined. |
46 STATE_UNKNOWN = 6 | 50 STATE_UNKNOWN = 6 |
47 }; | 51 }; |
48 | 52 |
49 // Enum used for completion callback. | |
50 enum AsyncResult { | |
51 RESULT_OK = 0, | |
52 | |
53 // The operation has FAILED. | |
54 RESULT_FAILED = 1, | |
55 | |
56 // User has cancelled the action (e.g. rejected UAC prompt). | |
57 // TODO(sergeyu): Current implementations don't return this value. | |
58 RESULT_CANCELLED = 2, | |
59 | |
60 // Failed to access host directory. | |
61 RESULT_FAILED_DIRECTORY = 3 | |
62 | |
63 // TODO(sergeyu): Add more error codes when we know how to handle | |
64 // them in the webapp. | |
65 }; | |
66 | |
67 // Callback type for GetConfig(). If the host is configured then a dictionary | 53 // Callback type for GetConfig(). If the host is configured then a dictionary |
68 // is returned containing host_id and xmpp_login, with security-sensitive | 54 // 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 | 55 // 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. | 56 // configured, and nullptr if the configuration is corrupt or cannot be read. |
71 typedef base::Callback<void (scoped_ptr<base::DictionaryValue> config)> | 57 typedef base::Callback<void (scoped_ptr<base::DictionaryValue> config)> |
72 GetConfigCallback; | 58 GetConfigCallback; |
73 | 59 |
74 // Callback used for asynchronous operations, e.g. when | 60 // Callback used to indicate failure of an asynchronous operation. |
75 // starting/stopping the service. | 61 typedef base::Callback<void ( |
76 typedef base::Callback<void (AsyncResult result)> CompletionCallback; | 62 const std::string& error_message, |
| 63 const tracked_objects::Location& location)> ErrorCallback; |
77 | 64 |
78 struct UsageStatsConsent { | 65 struct UsageStatsConsent { |
79 // Indicates whether crash dump reporting is supported by the host. | 66 // Indicates whether crash dump reporting is supported by the host. |
80 bool supported; | 67 bool supported; |
81 | 68 |
82 // Indicates if crash dump reporting is allowed by the user. | 69 // Indicates if crash dump reporting is allowed by the user. |
83 bool allowed; | 70 bool allowed; |
84 | 71 |
85 // Carries information whether the crash dump reporting is controlled by | 72 // Carries information whether the crash dump reporting is controlled by |
86 // policy. | 73 // policy. |
(...skipping 18 matching lines...) Expand all Loading... |
105 | 92 |
106 // Return the "installed/running" state of the daemon process. This method | 93 // Return the "installed/running" state of the daemon process. This method |
107 // should avoid accessing any data members of the implementation. | 94 // should avoid accessing any data members of the implementation. |
108 virtual State GetState() = 0; | 95 virtual State GetState() = 0; |
109 | 96 |
110 // Queries current host configuration. Any values that might be security | 97 // Queries current host configuration. Any values that might be security |
111 // sensitive have been filtered out. | 98 // sensitive have been filtered out. |
112 virtual scoped_ptr<base::DictionaryValue> GetConfig() = 0; | 99 virtual scoped_ptr<base::DictionaryValue> GetConfig() = 0; |
113 | 100 |
114 // Starts the daemon process. This may require that the daemon be | 101 // Starts the daemon process. This may require that the daemon be |
115 // downloaded and installed. |done| is invoked on the calling thread when | 102 // downloaded and installed. |on_done| or |on_error| is invoked on |
116 // the operation is completed. | 103 // the calling thread when the operation is completed. |
117 virtual void SetConfigAndStart( | 104 virtual void SetConfigAndStart( |
118 scoped_ptr<base::DictionaryValue> config, | 105 scoped_ptr<base::DictionaryValue> config, |
119 bool consent, | 106 bool consent, |
120 const CompletionCallback& done) = 0; | 107 const base::Closure& on_done, |
| 108 const ErrorCallback& on_error) = 0; |
121 | 109 |
122 // Updates current host configuration with the values specified in | 110 // Updates current host configuration with the values specified in |
123 // |config|. Any value in the existing configuration that isn't specified in | 111 // |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 | 112 // |config| is preserved. |config| must not contain host_id or xmpp_login |
125 // values, because implementations of this method cannot change them. |done| | 113 // values, because implementations of this method cannot change them. |
126 // is invoked on the calling thread when the operation is completed. | 114 // |on_done| or |on_error| is invoked on the calling thread when the |
| 115 // operation is completed. |
127 virtual void UpdateConfig( | 116 virtual void UpdateConfig( |
128 scoped_ptr<base::DictionaryValue> config, | 117 scoped_ptr<base::DictionaryValue> config, |
129 const CompletionCallback& done) = 0; | 118 const base::Closure& on_done, |
| 119 const ErrorCallback& on_error) = 0; |
130 | 120 |
131 // Stops the daemon process. |done| is invoked on the calling thread when | 121 // Stops the daemon process. |on_done| or |on_error| is invoked on the |
132 // the operation is completed. | 122 // calling thread when the operation is completed. |
133 virtual void Stop(const CompletionCallback& done) = 0; | 123 virtual void Stop( |
| 124 const base::Closure& on_done, |
| 125 const ErrorCallback& on_error) = 0; |
134 | 126 |
135 // Get the user's consent to crash reporting. | 127 // Get the user's consent to crash reporting. |
136 virtual UsageStatsConsent GetUsageStatsConsent() = 0; | 128 virtual UsageStatsConsent GetUsageStatsConsent() = 0; |
137 }; | 129 }; |
138 | 130 |
139 static scoped_refptr<DaemonController> Create(); | 131 static scoped_refptr<DaemonController> Create(); |
140 | 132 |
141 explicit DaemonController(scoped_ptr<Delegate> delegate); | 133 explicit DaemonController(scoped_ptr<Delegate> delegate); |
142 | 134 |
143 // Return the "installed/running" state of the daemon process. | 135 // Return the "installed/running" state of the daemon process. |
(...skipping 12 matching lines...) Expand all Loading... |
156 // Start the daemon process. This may require that the daemon be | 148 // Start the daemon process. This may require that the daemon be |
157 // downloaded and installed. |done| is called when the | 149 // downloaded and installed. |done| is called when the |
158 // operation is finished or fails. | 150 // operation is finished or fails. |
159 // | 151 // |
160 // TODO(sergeyu): This method writes config and starts the host - | 152 // TODO(sergeyu): This method writes config and starts the host - |
161 // these two steps are merged for simplicity. Consider splitting it | 153 // these two steps are merged for simplicity. Consider splitting it |
162 // into SetConfig() and Start() once we have basic host setup flow | 154 // into SetConfig() and Start() once we have basic host setup flow |
163 // working. | 155 // working. |
164 void SetConfigAndStart(scoped_ptr<base::DictionaryValue> config, | 156 void SetConfigAndStart(scoped_ptr<base::DictionaryValue> config, |
165 bool consent, | 157 bool consent, |
166 const CompletionCallback& done); | 158 const base::Closure& on_done, |
| 159 const ErrorCallback& on_error); |
167 | 160 |
168 // Updates current host configuration with the values specified in | 161 // Updates current host configuration with the values specified in |
169 // |config|. Changes must take effect before the call completes. | 162 // |config|. Changes must take effect before the call completes. |
170 // Any value in the existing configuration that isn't specified in |config| | 163 // 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, | 164 // is preserved. |config| must not contain host_id or xmpp_login values, |
172 // because implementations of this method cannot change them. | 165 // because implementations of this method cannot change them. |
173 void UpdateConfig(scoped_ptr<base::DictionaryValue> config, | 166 void UpdateConfig(scoped_ptr<base::DictionaryValue> config, |
174 const CompletionCallback& done); | 167 const base::Closure& on_done, |
| 168 const ErrorCallback& on_error); |
175 | 169 |
176 // Stop the daemon process. It is permitted to call Stop while the daemon | 170 // 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 | 171 // 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 | 172 // aborted if possible; if not then it is sufficient to ensure that the |
179 // daemon process is not started automatically upon successful installation. | 173 // daemon process is not started automatically upon successful installation. |
180 // As with Start, Stop may return before the operation is complete--poll | 174 // As with Start, Stop may return before the operation is complete--poll |
181 // GetState until the state is STATE_STOPPED. | 175 // GetState until the state is STATE_STOPPED. |
182 void Stop(const CompletionCallback& done); | 176 void Stop(const base::Closure& on_done, |
| 177 const ErrorCallback& on_error); |
183 | 178 |
184 // Get the user's consent to crash reporting. | 179 // Get the user's consent to crash reporting. |
185 void GetUsageStatsConsent(const GetUsageStatsConsentCallback& done); | 180 void GetUsageStatsConsent(const GetUsageStatsConsentCallback& done); |
186 | 181 |
187 private: | 182 private: |
188 friend class base::RefCountedThreadSafe<DaemonController>; | 183 friend class base::RefCountedThreadSafe<DaemonController>; |
189 virtual ~DaemonController(); | 184 virtual ~DaemonController(); |
190 | 185 |
191 // Blocking helper methods used to call the delegate. | 186 // Blocking helper methods used to call the delegate. |
192 void DoGetConfig(const GetConfigCallback& done); | 187 void DoGetConfig(const GetConfigCallback& done); |
193 void DoSetConfigAndStart(scoped_ptr<base::DictionaryValue> config, | 188 void DoSetConfigAndStart(scoped_ptr<base::DictionaryValue> config, |
194 bool consent, | 189 bool consent, |
195 const CompletionCallback& done); | 190 const base::Closure& on_done, |
| 191 const ErrorCallback& on_error); |
196 void DoUpdateConfig(scoped_ptr<base::DictionaryValue> config, | 192 void DoUpdateConfig(scoped_ptr<base::DictionaryValue> config, |
197 const CompletionCallback& done); | 193 const base::Closure& on_done, |
198 void DoStop(const CompletionCallback& done); | 194 const ErrorCallback& on_error); |
| 195 void DoStop(const base::Closure& on_done, |
| 196 const ErrorCallback& on_error); |
199 void DoGetUsageStatsConsent(const GetUsageStatsConsentCallback& done); | 197 void DoGetUsageStatsConsent(const GetUsageStatsConsentCallback& done); |
200 | 198 |
201 // "Trampoline" callbacks that schedule the next pending request and then | 199 // "Trampoline" callbacks that schedule the next pending request and then |
202 // invoke the original caller-supplied callback. | 200 // invoke the original caller-supplied callback. |
203 void InvokeCompletionCallbackAndScheduleNext( | 201 void InvokeSuccessCallbackAndScheduleNext( |
204 const CompletionCallback& done, | 202 const base::Closure& callback); |
205 AsyncResult result); | 203 void InvokeErrorCallbackAndScheduleNext( |
| 204 const ErrorCallback& callback, |
| 205 const std::string& error_message, |
| 206 const tracked_objects::Location& location); |
206 void InvokeConfigCallbackAndScheduleNext( | 207 void InvokeConfigCallbackAndScheduleNext( |
207 const GetConfigCallback& done, | 208 const GetConfigCallback& callback, |
208 scoped_ptr<base::DictionaryValue> config); | 209 scoped_ptr<base::DictionaryValue> config); |
209 void InvokeConsentCallbackAndScheduleNext( | 210 void InvokeConsentCallbackAndScheduleNext( |
210 const GetUsageStatsConsentCallback& done, | 211 const GetUsageStatsConsentCallback& callback, |
211 const UsageStatsConsent& consent); | 212 const UsageStatsConsent& consent); |
212 | 213 |
213 // Queue management methods. | 214 // Queue management methods. |
214 void ScheduleNext(); | 215 void ScheduleNext(); |
215 void ServiceOrQueueRequest(const base::Closure& request); | 216 void ServiceOrQueueRequest(const base::Closure& request); |
216 void ServiceNextRequest(); | 217 void ServiceNextRequest(); |
217 | 218 |
218 // Task runner on which all public methods of this class should be called. | 219 // Task runner on which all public methods of this class should be called. |
219 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | 220 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; |
220 | 221 |
221 // Task runner used to run blocking calls to the delegate. A single thread | 222 // 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 | 223 // task runner is used to guarantee that one method of the delegate is |
223 // called at a time. | 224 // called at a time. |
224 scoped_refptr<AutoThreadTaskRunner> delegate_task_runner_; | 225 scoped_refptr<AutoThreadTaskRunner> delegate_task_runner_; |
225 | 226 |
226 scoped_ptr<AutoThread> delegate_thread_; | 227 scoped_ptr<AutoThread> delegate_thread_; |
227 | 228 |
228 scoped_ptr<Delegate> delegate_; | 229 scoped_ptr<Delegate> delegate_; |
229 | 230 |
230 std::queue<base::Closure> pending_requests_; | 231 std::queue<base::Closure> pending_requests_; |
231 | 232 |
232 DISALLOW_COPY_AND_ASSIGN(DaemonController); | 233 DISALLOW_COPY_AND_ASSIGN(DaemonController); |
233 }; | 234 }; |
234 | 235 |
235 } // namespace remoting | 236 } // namespace remoting |
236 | 237 |
237 #endif // REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_ | 238 #endif // REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_ |
OLD | NEW |