summaryrefslogtreecommitdiffstats
path: root/cloud_print/gcp20/prototype/printer.h
blob: b4ee2607764144bea556d2a4f31e1c3e9abc1f26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GCP20_PROTOTYPE_PRINTER_H_
#define GCP20_PROTOTYPE_PRINTER_H_

#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "cloud_print/gcp20/prototype/cloud_print_requester.h"
#include "cloud_print/gcp20/prototype/cloud_print_xmpp_listener.h"
#include "cloud_print/gcp20/prototype/dns_sd_server.h"
#include "cloud_print/gcp20/prototype/print_job_handler.h"
#include "cloud_print/gcp20/prototype/privet_http_server.h"
#include "cloud_print/gcp20/prototype/x_privet_token.h"

extern const base::FilePath::CharType kPrinterStatePath[];

// This class maintains work of DNS-SD server, HTTP server and others.
class Printer : public base::SupportsWeakPtr<Printer>,
                public PrivetHttpServer::Delegate,
                public CloudPrintRequester::Delegate,
                public CloudPrintXmppListener::Delegate {
 public:
  // Constructs uninitialized object.
  Printer();

  // Destroys the object.
  virtual ~Printer();

  // Starts all servers.
  bool Start();

  // Returns true if printer was started.
  bool IsRunning() const;

  // Stops all servers.
  void Stop();

 private:
  struct RegistrationInfo {
    enum RegistrationState {
      DEV_REG_UNREGISTERED,
      DEV_REG_REGISTRATION_STARTED,  // |action=start| was called,
                                     // request to CloudPrint was sent.
      DEV_REG_REGISTRATION_CLAIM_TOKEN_READY,  // The same as previous,
                                               // but request reply is already
                                               // received.
      DEV_REG_REGISTRATION_COMPLETING,  // |action=complete| was called,
                                        // |complete| request was sent.
      DEV_REG_REGISTRATION_ERROR,  // Is set when server error was occurred.
      DEV_REG_REGISTERED,
    };

    enum ConfirmationState {
      CONFIRMATION_PENDING,
      CONFIRMATION_CONFIRMED,
      CONFIRMATION_DISCARDED,
      CONFIRMATION_TIMEOUT,
    };

    RegistrationInfo();
    ~RegistrationInfo();

    std::string user;
    std::string refresh_token;
    std::string device_id;
    std::string xmpp_jid;
    RegistrationState state;
    ConfirmationState confirmation_state;

    std::string registration_token;
    std::string complete_invite_url;

    // Contains error response if |DEV_REG_REGISTRATION_ERROR| is set.
    std::string error_description;
  };

  enum RegistrationAction {
    REG_ACTION_START,
    REG_ACTION_GET_CLAIM_TOKEN,
    REG_ACTION_COMPLETE,
    REG_ACTION_CANCEL
  };

  enum ConnectionState {
    NOT_CONFIGURED,
    OFFLINE,
    ONLINE,
    CONNECTING
  };

  // PrivetHttpServer::Delegate methods:
  virtual PrivetHttpServer::RegistrationErrorStatus RegistrationStart(
      const std::string& user) OVERRIDE;
  virtual PrivetHttpServer::RegistrationErrorStatus RegistrationGetClaimToken(
      const std::string& user,
      std::string* token,
      std::string* claim_url) OVERRIDE;
  virtual PrivetHttpServer::RegistrationErrorStatus RegistrationComplete(
      const std::string& user,
      std::string* device_id) OVERRIDE;
  virtual PrivetHttpServer::RegistrationErrorStatus RegistrationCancel(
      const std::string& user) OVERRIDE;
  virtual void GetRegistrationServerError(std::string* description) OVERRIDE;
  virtual void CreateInfo(PrivetHttpServer::DeviceInfo* info) OVERRIDE;
  virtual bool IsRegistered() const OVERRIDE;
  virtual bool CheckXPrivetTokenHeader(const std::string& token) const OVERRIDE;

  // CloudRequester::Delegate methods:
  virtual void OnRegistrationStartResponseParsed(
      const std::string& registration_token,
      const std::string& complete_invite_url,
      const std::string& device_id) OVERRIDE;
  virtual void OnRegistrationFinished(
      const std::string& refresh_token,
      const std::string& access_token,
      int access_token_expires_in_seconds) OVERRIDE;
  virtual void OnXmppJidReceived(const std::string& xmpp_jid) OVERRIDE;
  virtual void OnAccesstokenReceviced(const std::string& access_token,
                                      int expires_in_seconds) OVERRIDE;
  virtual void OnRegistrationError(const std::string& description) OVERRIDE;
  virtual void OnNetworkError() OVERRIDE;
  virtual void OnServerError(const std::string& description) OVERRIDE;
  virtual void OnAuthError() OVERRIDE;
  virtual std::string GetAccessToken() OVERRIDE;
  virtual void OnPrintJobsAvailable(
      const std::vector<cloud_print_response_parser::Job>& jobs) OVERRIDE;
  virtual void OnPrintJobDownloaded(
      const cloud_print_response_parser::Job& job) OVERRIDE;
  virtual void OnPrintJobDone() OVERRIDE;

  // CloudPrintXmppListener::Delegate methods:
  virtual void OnXmppConnected() OVERRIDE;
  virtual void OnXmppAuthError() OVERRIDE;
  virtual void OnXmppNetworkError() OVERRIDE;
  virtual void OnXmppNewPrintJob(const std::string& device_id) OVERRIDE;
  virtual void OnXmppNewLocalSettings(const std::string& device_id) OVERRIDE;
  virtual void OnXmppDeleteNotification(const std::string& device_id) OVERRIDE;

  // Method for trying to reconnecting to server on start or after network fail.
  void TryConnect();

  // Connects XMPP.
  void ConnectXmpp();

  // Method to handle pending events.
  // Do *NOT* call this method instantly. Only with |PostOnIdle|.
  void OnIdle();

  // Method for checking printer status.
  // (e.g. printjobs, local settings, deleted status).
  void CheckPendingUpdates();

  // Ask CloudPrint server for new local sendings.
  void GetLocalSettings();

  // Ask CloudPrint server for printjobs.
  void FetchPrintJobs();

  // Saves |access_token| and calculates time for next update.
  void RememberAccessToken(const std::string& access_token,
                           int expires_in_seconds);

  // Sets registration state to error and adds description.
  void SetRegistrationError(const std::string& description);

  // Checks if register call is called correctly (|user| is correct,
  // error is no set etc). Returns |false| if error status is put into |status|.
  // Otherwise no error was occurred.
  PrivetHttpServer::RegistrationErrorStatus CheckCommonRegErrors(
      const std::string& user);

  // Checks if confirmation was received.
  void WaitUserConfirmation(base::Time valid_until);

  // Generates ProxyId for this device.
  std::string GenerateProxyId() const;

  // Creates data for DNS TXT respond.
  std::vector<std::string> CreateTxt() const;

  // Saving and loading registration info from file.
  void SaveToFile() const;
  bool LoadFromFile();

  // Adds |OnIdle| method to the MessageLoop.
  void PostOnIdle();

  // Registration timeout.
  void CheckRegistrationExpiration();

  // Delays expiration after user action.
  void UpdateRegistrationExpiration();

  // Deletes registration expiration at all.
  void InvalidateRegistrationExpiration();

  // Converts errors.
  PrivetHttpServer::RegistrationErrorStatus ConfirmationToRegistrationError(
      RegistrationInfo::ConfirmationState state);

  std::string ConnectionStateToString(ConnectionState state) const;

  // Changes state to OFFLINE and posts TryReconnect.
  // For registration reconnect is instant every time.
  void FallOffline(bool instant_reconnect);

  // Changes state and update info in DNS server. Returns |true| if state
  // was changed (otherwise state was the same).
  bool ChangeState(ConnectionState new_state);

  RegistrationInfo reg_info_;

  // Contains DNS-SD server.
  DnsSdServer dns_server_;

  // Contains Privet HTTP server.
  PrivetHttpServer http_server_;

  // Connection state of device.
  ConnectionState connection_state_;

  // Contains CloudPrint client.
  scoped_ptr<CloudPrintRequester> requester_;

  // XMPP Listener.
  scoped_ptr<CloudPrintXmppListener> xmpp_listener_;

  XPrivetToken xtoken_;

  scoped_ptr<PrintJobHandler> print_job_handler_;

  // Last valid |access_token|.
  std::string access_token_;
  base::Time access_token_update_;

  // Uses for calculating uptime.
  base::Time starttime_;

  // Uses to validate registration timeout.
  base::Time registration_expiration_;

  // Used for preventing two and more OnIdle posted in message loop.
  bool on_idle_posted_;

  // Contains |true| if Printer has to check pending local settings.
  bool pending_local_settings_check_;

  // Contains |true| if Printer has to check available printjobs.
  bool pending_print_jobs_check_;

  DISALLOW_COPY_AND_ASSIGN(Printer);
};

#endif  // GCP20_PROTOTYPE_PRINTER_H_