summaryrefslogtreecommitdiffstats
path: root/cloud_print/gcp20/prototype/printer.h
blob: 1c839396ded6c378294dcc2b41fdb950720aa564 (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
// 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/printer_state.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:
  FRIEND_TEST_ALL_PREFIXES(Printer, ValidateCapabilities);

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

  std::string GetRawCdd();

  // 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 IsLocalPrintingAllowed() const override;
  virtual bool CheckXPrivetTokenHeader(const std::string& token) const override;
  virtual const base::DictionaryValue& GetCapabilities() override;
  virtual LocalPrintJob::CreateResult CreateJob(
      const std::string& ticket,
      std::string* job_id,
      int* expires_in,
      int* error_timeout,
      std::string* error_description) override;
  virtual LocalPrintJob::SaveResult SubmitDoc(
      const LocalPrintJob& job,
      std::string* job_id,
      int* expires_in,
      std::string* error_description,
      int* timeout) override;
  virtual LocalPrintJob::SaveResult SubmitDocWithId(
      const LocalPrintJob& job,
      const std::string& job_id,
      int* expires_in,
      std::string* error_description,
      int* timeout) override;
  virtual bool GetJobState(const std::string& id,
                           LocalPrintJob::Info* info) 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;
  virtual void OnLocalSettingsReceived(
      LocalSettings::State state,
      const LocalSettings& settings) override;
  virtual void OnLocalSettingsUpdated() 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();

  // Ask Cloud Print server for printjobs.
  void FetchPrintJobs();

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

  // Applies new local settings to printer.
  void ApplyLocalSettings(const LocalSettings& settings);

  // Used for erasing all printer info.
  void OnPrinterDeleted();

  // 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();
  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();

  // Methods to start HTTP and DNS-SD servers. Return |true| if servers
  // were started. If failed neither HTTP nor DNS-SD server will be running.
  bool StartLocalDiscoveryServers();

  // Methods to start HTTP and DNS-SD servers. Return |true| if servers
  // were started.
  bool StartDnsServer();
  bool StartHttpServer();

  // Converts errors.
  PrivetHttpServer::RegistrationErrorStatus ConfirmationToRegistrationError(
      PrinterState::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);

  // Contains printers workflow info.
  PrinterState state_;

  // Connection state of device.
  ConnectionState connection_state_;

  // Contains DNS-SD server.
  DnsSdServer dns_server_;

  // Contains Privet HTTP server.
  PrivetHttpServer http_server_;

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

  // XMPP Listener.
  scoped_ptr<CloudPrintXmppListener> xmpp_listener_;

  XPrivetToken xtoken_;

  scoped_ptr<PrintJobHandler> print_job_handler_;

  // 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_;

  // Contains |true| if Printer has to be deleted.
  bool pending_deletion_;

  DISALLOW_COPY_AND_ASSIGN(Printer);
};

#endif  // GCP20_PROTOTYPE_PRINTER_H_