1 // Copyright 2013 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 GCP20_PROTOTYPE_PRINTER_H_
6 #define GCP20_PROTOTYPE_PRINTER_H_
11 #include "base/files/file_path.h"
12 #include "base/memory/weak_ptr.h"
13 #include "cloud_print/gcp20/prototype/cloud_print_requester.h"
14 #include "cloud_print/gcp20/prototype/cloud_print_xmpp_listener.h"
15 #include "cloud_print/gcp20/prototype/dns_sd_server.h"
16 #include "cloud_print/gcp20/prototype/print_job_handler.h"
17 #include "cloud_print/gcp20/prototype/printer_state.h"
18 #include "cloud_print/gcp20/prototype/privet_http_server.h"
19 #include "cloud_print/gcp20/prototype/x_privet_token.h"
21 extern const base::FilePath::CharType kPrinterStatePath
[];
23 // This class maintains work of DNS-SD server, HTTP server and others.
24 class Printer
: public base::SupportsWeakPtr
<Printer
>,
25 public PrivetHttpServer::Delegate
,
26 public CloudPrintRequester::Delegate
,
27 public CloudPrintXmppListener::Delegate
{
29 // Constructs uninitialized object.
32 // Destroys the object.
35 // Starts all servers.
38 // Returns true if printer was started.
39 bool IsRunning() const;
45 FRIEND_TEST_ALL_PREFIXES(Printer
, ValidateCapabilities
);
47 enum ConnectionState
{
54 std::string
GetRawCdd();
56 // PrivetHttpServer::Delegate methods:
57 virtual PrivetHttpServer::RegistrationErrorStatus
RegistrationStart(
58 const std::string
& user
) override
;
59 virtual PrivetHttpServer::RegistrationErrorStatus
RegistrationGetClaimToken(
60 const std::string
& user
,
62 std::string
* claim_url
) override
;
63 virtual PrivetHttpServer::RegistrationErrorStatus
RegistrationComplete(
64 const std::string
& user
,
65 std::string
* device_id
) override
;
66 virtual PrivetHttpServer::RegistrationErrorStatus
RegistrationCancel(
67 const std::string
& user
) override
;
68 virtual void GetRegistrationServerError(std::string
* description
) override
;
69 virtual void CreateInfo(PrivetHttpServer::DeviceInfo
* info
) override
;
70 virtual bool IsRegistered() const override
;
71 virtual bool IsLocalPrintingAllowed() const override
;
72 virtual bool CheckXPrivetTokenHeader(const std::string
& token
) const override
;
73 virtual const base::DictionaryValue
& GetCapabilities() override
;
74 virtual LocalPrintJob::CreateResult
CreateJob(
75 const std::string
& ticket
,
79 std::string
* error_description
) override
;
80 virtual LocalPrintJob::SaveResult
SubmitDoc(
81 const LocalPrintJob
& job
,
84 std::string
* error_description
,
85 int* timeout
) override
;
86 virtual LocalPrintJob::SaveResult
SubmitDocWithId(
87 const LocalPrintJob
& job
,
88 const std::string
& job_id
,
90 std::string
* error_description
,
91 int* timeout
) override
;
92 virtual bool GetJobState(const std::string
& id
,
93 LocalPrintJob::Info
* info
) override
;
95 // CloudRequester::Delegate methods:
96 virtual void OnRegistrationStartResponseParsed(
97 const std::string
& registration_token
,
98 const std::string
& complete_invite_url
,
99 const std::string
& device_id
) override
;
100 virtual void OnRegistrationFinished(
101 const std::string
& refresh_token
,
102 const std::string
& access_token
,
103 int access_token_expires_in_seconds
) override
;
104 virtual void OnXmppJidReceived(const std::string
& xmpp_jid
) override
;
105 virtual void OnAccesstokenReceviced(const std::string
& access_token
,
106 int expires_in_seconds
) override
;
107 virtual void OnRegistrationError(const std::string
& description
) override
;
108 virtual void OnNetworkError() override
;
109 virtual void OnServerError(const std::string
& description
) override
;
110 virtual void OnAuthError() override
;
111 virtual std::string
GetAccessToken() override
;
112 virtual void OnPrintJobsAvailable(
113 const std::vector
<cloud_print_response_parser::Job
>& jobs
) override
;
114 virtual void OnPrintJobDownloaded(
115 const cloud_print_response_parser::Job
& job
) override
;
116 virtual void OnPrintJobDone() override
;
117 virtual void OnLocalSettingsReceived(
118 LocalSettings::State state
,
119 const LocalSettings
& settings
) override
;
120 virtual void OnLocalSettingsUpdated() override
;
122 // CloudPrintXmppListener::Delegate methods:
123 virtual void OnXmppConnected() override
;
124 virtual void OnXmppAuthError() override
;
125 virtual void OnXmppNetworkError() override
;
126 virtual void OnXmppNewPrintJob(const std::string
& device_id
) override
;
127 virtual void OnXmppNewLocalSettings(const std::string
& device_id
) override
;
128 virtual void OnXmppDeleteNotification(const std::string
& device_id
) override
;
130 // Method for trying to reconnecting to server on start or after network fail.
136 // Method to handle pending events.
137 // Do *NOT* call this method instantly. Only with |PostOnIdle|.
140 // Ask Cloud Print server for printjobs.
141 void FetchPrintJobs();
143 // Ask Cloud Print server for new local sendings.
144 void GetLocalSettings();
146 // Applies new local settings to printer.
147 void ApplyLocalSettings(const LocalSettings
& settings
);
149 // Used for erasing all printer info.
150 void OnPrinterDeleted();
152 // Saves |access_token| and calculates time for next update.
153 void RememberAccessToken(const std::string
& access_token
,
154 int expires_in_seconds
);
156 // Sets registration state to error and adds description.
157 void SetRegistrationError(const std::string
& description
);
159 // Checks if register call is called correctly (|user| is correct,
160 // error is no set etc). Returns |false| if error status is put into |status|.
161 // Otherwise no error was occurred.
162 PrivetHttpServer::RegistrationErrorStatus
CheckCommonRegErrors(
163 const std::string
& user
);
165 // Checks if confirmation was received.
166 void WaitUserConfirmation(base::Time valid_until
);
168 // Generates ProxyId for this device.
169 std::string
GenerateProxyId() const;
171 // Creates data for DNS TXT respond.
172 std::vector
<std::string
> CreateTxt() const;
174 // Saving and loading registration info from file.
178 // Adds |OnIdle| method to the MessageLoop.
181 // Registration timeout.
182 void CheckRegistrationExpiration();
184 // Delays expiration after user action.
185 void UpdateRegistrationExpiration();
187 // Deletes registration expiration at all.
188 void InvalidateRegistrationExpiration();
190 // Methods to start HTTP and DNS-SD servers. Return |true| if servers
191 // were started. If failed neither HTTP nor DNS-SD server will be running.
192 bool StartLocalDiscoveryServers();
194 // Methods to start HTTP and DNS-SD servers. Return |true| if servers
196 bool StartDnsServer();
197 bool StartHttpServer();
200 PrivetHttpServer::RegistrationErrorStatus
ConfirmationToRegistrationError(
201 PrinterState::ConfirmationState state
);
203 std::string
ConnectionStateToString(ConnectionState state
) const;
205 // Changes state to OFFLINE and posts TryReconnect.
206 // For registration reconnect is instant every time.
207 void FallOffline(bool instant_reconnect
);
209 // Changes state and update info in DNS server. Returns |true| if state
210 // was changed (otherwise state was the same).
211 bool ChangeState(ConnectionState new_state
);
213 // Contains printers workflow info.
216 // Connection state of device.
217 ConnectionState connection_state_
;
219 // Contains DNS-SD server.
220 DnsSdServer dns_server_
;
222 // Contains Privet HTTP server.
223 PrivetHttpServer http_server_
;
225 // Contains CloudPrint client.
226 scoped_ptr
<CloudPrintRequester
> requester_
;
229 scoped_ptr
<CloudPrintXmppListener
> xmpp_listener_
;
231 XPrivetToken xtoken_
;
233 scoped_ptr
<PrintJobHandler
> print_job_handler_
;
235 // Uses for calculating uptime.
236 base::Time starttime_
;
238 // Uses to validate registration timeout.
239 base::Time registration_expiration_
;
241 // Used for preventing two and more OnIdle posted in message loop.
242 bool on_idle_posted_
;
244 // Contains |true| if Printer has to check pending local settings.
245 bool pending_local_settings_check_
;
247 // Contains |true| if Printer has to check available printjobs.
248 bool pending_print_jobs_check_
;
250 // Contains |true| if Printer has to be deleted.
251 bool pending_deletion_
;
253 DISALLOW_COPY_AND_ASSIGN(Printer
);
256 #endif // GCP20_PROTOTYPE_PRINTER_H_