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 PrivetHttpServer::RegistrationErrorStatus
RegistrationStart(
58 const std::string
& user
) override
;
59 PrivetHttpServer::RegistrationErrorStatus
RegistrationGetClaimToken(
60 const std::string
& user
,
62 std::string
* claim_url
) override
;
63 PrivetHttpServer::RegistrationErrorStatus
RegistrationComplete(
64 const std::string
& user
,
65 std::string
* device_id
) override
;
66 PrivetHttpServer::RegistrationErrorStatus
RegistrationCancel(
67 const std::string
& user
) override
;
68 void GetRegistrationServerError(std::string
* description
) override
;
69 void CreateInfo(PrivetHttpServer::DeviceInfo
* info
) override
;
70 bool IsRegistered() const override
;
71 bool IsLocalPrintingAllowed() const override
;
72 bool CheckXPrivetTokenHeader(const std::string
& token
) const override
;
73 const base::DictionaryValue
& GetCapabilities() override
;
74 LocalPrintJob::CreateResult
CreateJob(
75 const std::string
& ticket
,
79 std::string
* error_description
) override
;
80 LocalPrintJob::SaveResult
SubmitDoc(
81 const LocalPrintJob
& job
,
84 std::string
* error_description
,
85 int* timeout
) override
;
86 LocalPrintJob::SaveResult
SubmitDocWithId(
87 const LocalPrintJob
& job
,
88 const std::string
& job_id
,
90 std::string
* error_description
,
91 int* timeout
) override
;
92 bool GetJobState(const std::string
& id
, LocalPrintJob::Info
* info
) override
;
94 // CloudRequester::Delegate methods:
95 void OnRegistrationStartResponseParsed(
96 const std::string
& registration_token
,
97 const std::string
& complete_invite_url
,
98 const std::string
& device_id
) override
;
99 void OnRegistrationFinished(
100 const std::string
& refresh_token
,
101 const std::string
& access_token
,
102 int access_token_expires_in_seconds
) override
;
103 void OnXmppJidReceived(const std::string
& xmpp_jid
) override
;
104 void OnAccesstokenReceviced(const std::string
& access_token
,
105 int expires_in_seconds
) override
;
106 void OnRegistrationError(const std::string
& description
) override
;
107 void OnNetworkError() override
;
108 void OnServerError(const std::string
& description
) override
;
109 void OnAuthError() override
;
110 std::string
GetAccessToken() override
;
111 void OnPrintJobsAvailable(
112 const std::vector
<cloud_print_response_parser::Job
>& jobs
) override
;
113 void OnPrintJobDownloaded(
114 const cloud_print_response_parser::Job
& job
) override
;
115 void OnPrintJobDone() override
;
116 void OnLocalSettingsReceived(
117 LocalSettings::State state
,
118 const LocalSettings
& settings
) override
;
119 void OnLocalSettingsUpdated() override
;
121 // CloudPrintXmppListener::Delegate methods:
122 void OnXmppConnected() override
;
123 void OnXmppAuthError() override
;
124 void OnXmppNetworkError() override
;
125 void OnXmppNewPrintJob(const std::string
& device_id
) override
;
126 void OnXmppNewLocalSettings(const std::string
& device_id
) override
;
127 void OnXmppDeleteNotification(const std::string
& device_id
) override
;
129 // Method for trying to reconnecting to server on start or after network fail.
135 // Method to handle pending events.
136 // Do *NOT* call this method instantly. Only with |PostOnIdle|.
139 // Ask Cloud Print server for printjobs.
140 void FetchPrintJobs();
142 // Ask Cloud Print server for new local sendings.
143 void GetLocalSettings();
145 // Applies new local settings to printer.
146 void ApplyLocalSettings(const LocalSettings
& settings
);
148 // Used for erasing all printer info.
149 void OnPrinterDeleted();
151 // Saves |access_token| and calculates time for next update.
152 void RememberAccessToken(const std::string
& access_token
,
153 int expires_in_seconds
);
155 // Sets registration state to error and adds description.
156 void SetRegistrationError(const std::string
& description
);
158 // Checks if register call is called correctly (|user| is correct,
159 // error is no set etc). Returns |false| if error status is put into |status|.
160 // Otherwise no error was occurred.
161 PrivetHttpServer::RegistrationErrorStatus
CheckCommonRegErrors(
162 const std::string
& user
);
164 // Checks if confirmation was received.
165 void WaitUserConfirmation(base::Time valid_until
);
167 // Generates ProxyId for this device.
168 std::string
GenerateProxyId() const;
170 // Creates data for DNS TXT respond.
171 std::vector
<std::string
> CreateTxt() const;
173 // Saving and loading registration info from file.
177 // Adds |OnIdle| method to the MessageLoop.
180 // Registration timeout.
181 void CheckRegistrationExpiration();
183 // Delays expiration after user action.
184 void UpdateRegistrationExpiration();
186 // Deletes registration expiration at all.
187 void InvalidateRegistrationExpiration();
189 // Methods to start HTTP and DNS-SD servers. Return |true| if servers
190 // were started. If failed neither HTTP nor DNS-SD server will be running.
191 bool StartLocalDiscoveryServers();
193 // Methods to start HTTP and DNS-SD servers. Return |true| if servers
195 bool StartDnsServer();
196 bool StartHttpServer();
199 PrivetHttpServer::RegistrationErrorStatus
ConfirmationToRegistrationError(
200 PrinterState::ConfirmationState state
);
202 std::string
ConnectionStateToString(ConnectionState state
) const;
204 // Changes state to OFFLINE and posts TryReconnect.
205 // For registration reconnect is instant every time.
206 void FallOffline(bool instant_reconnect
);
208 // Changes state and update info in DNS server. Returns |true| if state
209 // was changed (otherwise state was the same).
210 bool ChangeState(ConnectionState new_state
);
212 // Contains printers workflow info.
215 // Connection state of device.
216 ConnectionState connection_state_
;
218 // Contains DNS-SD server.
219 DnsSdServer dns_server_
;
221 // Contains Privet HTTP server.
222 PrivetHttpServer http_server_
;
224 // Contains CloudPrint client.
225 scoped_ptr
<CloudPrintRequester
> requester_
;
228 scoped_ptr
<CloudPrintXmppListener
> xmpp_listener_
;
230 XPrivetToken xtoken_
;
232 scoped_ptr
<PrintJobHandler
> print_job_handler_
;
234 // Uses for calculating uptime.
235 base::Time starttime_
;
237 // Uses to validate registration timeout.
238 base::Time registration_expiration_
;
240 // Used for preventing two and more OnIdle posted in message loop.
241 bool on_idle_posted_
;
243 // Contains |true| if Printer has to check pending local settings.
244 bool pending_local_settings_check_
;
246 // Contains |true| if Printer has to check available printjobs.
247 bool pending_print_jobs_check_
;
249 // Contains |true| if Printer has to be deleted.
250 bool pending_deletion_
;
252 DISALLOW_COPY_AND_ASSIGN(Printer
);
255 #endif // GCP20_PROTOTYPE_PRINTER_H_