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
5#ifndef REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_
6#define REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_
7
8#include <string>
9
10#include "base/callback_forward.h"
11#include "base/memory/scoped_ptr.h"
12
13namespace base {
14class DictionaryValue;
15}  // namespace base
16
17namespace remoting {
18
19class DaemonController {
20 public:
21  // Note that these enumeration values are duplicated in host_controller.js and
22  // must be kept in sync.
23  enum State {
24    // Placeholder state for platforms on which the daemon process is not
25    // implemented. The web-app will not show the corresponding UI. This value
26    // will eventually be deprecated or removed.
27    STATE_NOT_IMPLEMENTED = -1,
28    // The daemon is not installed. This is functionally equivalent to
29    // STATE_STOPPED, but the start method is expected to be significantly
30    // slower, and might involve user interaction. It might be appropriate to
31    // indicate this in the UI.
32    STATE_NOT_INSTALLED = 0,
33    // The daemon is being installed.
34    STATE_INSTALLING = 1,
35    // The daemon is installed but not running. Call Start to start it.
36    STATE_STOPPED = 2,
37    // The daemon process is starting.
38    STATE_STARTING = 3,
39    // The daemon process is running. Call Start again to change the PIN or
40    // Stop to stop it.
41    STATE_STARTED = 4,
42    // The daemon process is stopping.
43    STATE_STOPPING = 5,
44    // The state cannot be determined. This could indicate that the plugin
45    // has not been provided with sufficient information, for example, the
46    // user for which to query state on a multi-user system.
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    // Failed to access host directory.
62    RESULT_FAILED_DIRECTORY = 3
63
64    // TODO(sergeyu): Add more error codes when we know how to handle
65    // them in the webapp.
66  };
67
68  // Callback type for GetConfig(). If the host is configured then a dictionary
69  // is returned containing host_id and xmpp_login, with security-sensitive
70  // fields filtered out. An empty dictionary is returned if the host is not
71  // configured, and NULL if the configuration is corrupt or cannot be read.
72  typedef base::Callback<void (scoped_ptr<base::DictionaryValue> config)>
73      GetConfigCallback;
74
75  // Callback used for asynchronous operations, e.g. when
76  // starting/stopping the service.
77  typedef base::Callback<void (AsyncResult result)> CompletionCallback;
78
79  // Callback type for GetVersion().
80  typedef base::Callback<void (const std::string&)> GetVersionCallback;
81
82  // Callback type for GetUsageStatsConsent(). |supported| indicates whether
83  // crash dump reporting is supported by the host. |allowed| indicates if
84  // crash dump reporting is allowed by the user. |set_by_policy| carries
85  // information whether the crash dump reporting is controlled by policy.
86  typedef base::Callback<void (
87      bool supported,
88      bool allowed,
89      bool set_by_policy)> GetUsageStatsConsentCallback;
90
91  virtual ~DaemonController() {}
92
93  // Return the "installed/running" state of the daemon process.
94  //
95  // TODO(sergeyu): This method is called synchronously from the
96  // webapp. In most cases it requires IO operations, so it may block
97  // the user interface. Replace it with asynchronous notifications,
98  // e.g. with StartStateNotifications()/StopStateNotifications() methods.
99  virtual State GetState() = 0;
100
101  // Queries current host configuration. The |callback| is called
102  // after the configuration is read, and any values that might be security
103  // sensitive have been filtered out.
104  virtual void GetConfig(const GetConfigCallback& callback) = 0;
105
106  // Start the daemon process. This may require that the daemon be
107  // downloaded and installed. |done_callback| is called when the
108  // operation is finished or fails.
109  //
110  // TODO(sergeyu): This method writes config and starts the host -
111  // these two steps are merged for simplicity. Consider splitting it
112  // into SetConfig() and Start() once we have basic host setup flow
113  // working.
114  virtual void SetConfigAndStart(scoped_ptr<base::DictionaryValue> config,
115                                 bool consent,
116                                 const CompletionCallback& done) = 0;
117
118  // Updates current host configuration with the values specified in
119  // |config|. Changes must take effect before the call completes.
120  // Any value in the existing configuration that isn't specified in |config|
121  // is preserved. |config| must not contain host_id or xmpp_login values,
122  // because implementations of this method cannot change them.
123  virtual void UpdateConfig(scoped_ptr<base::DictionaryValue> config,
124                            const CompletionCallback& done_callback) = 0;
125
126  // Stop the daemon process. It is permitted to call Stop while the daemon
127  // process is being installed, in which case the installation should be
128  // aborted if possible; if not then it is sufficient to ensure that the
129  // daemon process is not started automatically upon successful installation.
130  // As with Start, Stop may return before the operation is complete--poll
131  // GetState until the state is STATE_STOPPED.
132  virtual void Stop(const CompletionCallback& done_callback) = 0;
133
134  // Caches the native handle of the plugin window so it can be used to focus
135  // elevation prompts properly.
136  virtual void SetWindow(void* window_handle) = 0;
137
138  // Get the version of the daemon as a dotted decimal string of the form
139  // major.minor.build.patch, if it is installed, or "" otherwise.
140  virtual void GetVersion(const GetVersionCallback& done_callback) = 0;
141
142  // Get the user's consent to crash reporting.
143  virtual void GetUsageStatsConsent(
144      const GetUsageStatsConsentCallback& done) = 0;
145
146  static scoped_ptr<DaemonController> Create();
147};
148
149}  // namespace remoting
150
151#endif  // REMOTING_HOST_SETUP_DAEMON_CONTROLLER_H_
152