OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GCP20_PROTOTYPE_PRINTER_H_ | 5 #ifndef GCP20_PROTOTYPE_PRINTER_H_ |
6 #define GCP20_PROTOTYPE_PRINTER_H_ | 6 #define GCP20_PROTOTYPE_PRINTER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "cloud_print/gcp20/prototype/cloud_print_requester.h" | 12 #include "cloud_print/gcp20/prototype/cloud_print_requester.h" |
13 #include "cloud_print/gcp20/prototype/cloud_print_xmpp_listener.h" | |
13 #include "cloud_print/gcp20/prototype/dns_sd_server.h" | 14 #include "cloud_print/gcp20/prototype/dns_sd_server.h" |
14 #include "cloud_print/gcp20/prototype/print_job_handler.h" | 15 #include "cloud_print/gcp20/prototype/print_job_handler.h" |
15 #include "cloud_print/gcp20/prototype/privet_http_server.h" | 16 #include "cloud_print/gcp20/prototype/privet_http_server.h" |
16 #include "cloud_print/gcp20/prototype/x_privet_token.h" | 17 #include "cloud_print/gcp20/prototype/x_privet_token.h" |
17 | 18 |
18 extern const char kPrinterStatePath[]; | 19 extern const char kPrinterStatePath[]; |
19 | 20 |
20 // This class maintains work of DNS-SD server, HTTP server and others. | 21 // This class maintains work of DNS-SD server, HTTP server and others. |
21 class Printer : public base::SupportsWeakPtr<Printer>, | 22 class Printer : public base::SupportsWeakPtr<Printer>, |
22 public PrivetHttpServer::Delegate, | 23 public PrivetHttpServer::Delegate, |
23 public CloudPrintRequester::Delegate { | 24 public CloudPrintRequester::Delegate, |
25 public CloudPrintXmppListener::Delegate { | |
24 public: | 26 public: |
25 // Constructs uninitialized object. | 27 // Constructs uninitialized object. |
26 Printer(); | 28 Printer(); |
27 | 29 |
28 // Destroys the object. | 30 // Destroys the object. |
29 virtual ~Printer(); | 31 virtual ~Printer(); |
30 | 32 |
31 // Starts all servers. | 33 // Starts all servers. |
32 bool Start(); | 34 bool Start(); |
33 | 35 |
34 // Returns true if printer was started. | 36 // Returns true if printer was started. |
35 bool IsOnline() const; | 37 bool IsRunning() const; |
36 | |
37 // Method for trying to reconnecting to server. | |
38 void WakeUp(); | |
39 | 38 |
40 // Stops all servers. | 39 // Stops all servers. |
41 void Stop(); | 40 void Stop(); |
42 | 41 |
43 private: | 42 private: |
44 struct RegistrationInfo { | 43 struct RegistrationInfo { |
45 enum RegistrationState { | 44 enum RegistrationState { |
46 DEV_REG_UNREGISTERED, | 45 DEV_REG_UNREGISTERED, |
47 DEV_REG_REGISTRATION_STARTED, // |action=start| was called, | 46 DEV_REG_REGISTRATION_STARTED, // |action=start| was called, |
48 // request to CloudPrint was sent. | 47 // request to CloudPrint was sent. |
(...skipping 12 matching lines...) Expand all Loading... | |
61 CONFIRMATION_DISCARDED, | 60 CONFIRMATION_DISCARDED, |
62 CONFIRMATION_TIMEOUT, | 61 CONFIRMATION_TIMEOUT, |
63 }; | 62 }; |
64 | 63 |
65 RegistrationInfo(); | 64 RegistrationInfo(); |
66 ~RegistrationInfo(); | 65 ~RegistrationInfo(); |
67 | 66 |
68 std::string user; | 67 std::string user; |
69 std::string refresh_token; | 68 std::string refresh_token; |
70 std::string device_id; | 69 std::string device_id; |
70 std::string xmpp_jid; | |
71 RegistrationState state; | 71 RegistrationState state; |
72 ConfirmationState confirmation_state; | 72 ConfirmationState confirmation_state; |
73 | 73 |
74 std::string registration_token; | 74 std::string registration_token; |
75 std::string complete_invite_url; | 75 std::string complete_invite_url; |
76 | 76 |
77 // Contains error response if |DEV_REG_REGISTRATION_ERROR| is set. | 77 // Contains error response if |DEV_REG_REGISTRATION_ERROR| is set. |
78 std::string error_description; | 78 std::string error_description; |
79 }; | 79 }; |
80 | 80 |
(...skipping 27 matching lines...) Expand all Loading... | |
108 virtual void CreateInfo(PrivetHttpServer::DeviceInfo* info) OVERRIDE; | 108 virtual void CreateInfo(PrivetHttpServer::DeviceInfo* info) OVERRIDE; |
109 virtual bool IsRegistered() const OVERRIDE; | 109 virtual bool IsRegistered() const OVERRIDE; |
110 virtual bool CheckXPrivetTokenHeader(const std::string& token) const OVERRIDE; | 110 virtual bool CheckXPrivetTokenHeader(const std::string& token) const OVERRIDE; |
111 | 111 |
112 // CloudRequester::Delegate methods: | 112 // CloudRequester::Delegate methods: |
113 virtual void OnRegistrationStartResponseParsed( | 113 virtual void OnRegistrationStartResponseParsed( |
114 const std::string& registration_token, | 114 const std::string& registration_token, |
115 const std::string& complete_invite_url, | 115 const std::string& complete_invite_url, |
116 const std::string& device_id) OVERRIDE; | 116 const std::string& device_id) OVERRIDE; |
117 virtual void OnGetAuthCodeResponseParsed( | 117 virtual void OnGetAuthCodeResponseParsed( |
118 const std::string& refresh_token) OVERRIDE; | 118 const std::string& refresh_token, |
gene
2013/07/25 20:36:54
nit: wrong indentation
maksymb
2013/07/25 20:54:46
Done.
| |
119 const std::string& access_token, | |
120 int access_token_expires_in_seconds) OVERRIDE; | |
121 virtual void OnXmppJidReceived(const std::string& xmpp_jid) OVERRIDE; | |
122 virtual void OnAccesstokenReceviced(const std::string& access_token, | |
123 int expires_in_seconds) OVERRIDE; | |
119 virtual void OnRegistrationError(const std::string& description) OVERRIDE; | 124 virtual void OnRegistrationError(const std::string& description) OVERRIDE; |
125 virtual void OnNetworkError() OVERRIDE; | |
120 virtual void OnServerError(const std::string& description) OVERRIDE; | 126 virtual void OnServerError(const std::string& description) OVERRIDE; |
121 virtual void OnNetworkError() OVERRIDE; | 127 virtual void OnAuthError() OVERRIDE; |
128 virtual std::string GetAccessToken() OVERRIDE; | |
122 virtual void OnPrintJobsAvailable( | 129 virtual void OnPrintJobsAvailable( |
123 const std::vector<cloud_print_response_parser::Job>& jobs) OVERRIDE; | 130 const std::vector<cloud_print_response_parser::Job>& jobs) OVERRIDE; |
124 virtual void OnPrintJobDownloaded( | 131 virtual void OnPrintJobDownloaded( |
125 const cloud_print_response_parser::Job& job) OVERRIDE; | 132 const cloud_print_response_parser::Job& job) OVERRIDE; |
126 virtual void OnPrintJobDone() OVERRIDE; | 133 virtual void OnPrintJobDone() OVERRIDE; |
127 | 134 |
135 // CloudPrintXmppListener::Delegate methods: | |
136 virtual void OnXmppConnected() OVERRIDE; | |
137 virtual void OnXmppAuthError() OVERRIDE; | |
138 virtual void OnXmppNetworkError() OVERRIDE; | |
139 virtual void OnXmppNewPrintJob(const std::string& device_id) OVERRIDE; | |
140 virtual void OnXmppNewLocalSettings(const std::string& device_id) OVERRIDE; | |
141 virtual void OnXmppDeleteNotification(const std::string& device_id) OVERRIDE; | |
142 | |
143 // Method for trying to reconnecting to server on start or after network fail. | |
144 void TryConnect(); | |
145 | |
146 // Connects XMPP. | |
147 void ConnectXmpp(); | |
148 | |
149 // Method to handle pending events. | |
150 // Do *NOT* call this method instantly. Only with |PostOnIdle|. | |
151 void OnIdle(); | |
152 | |
153 // Method for checking printer status. | |
154 // (e.g. printjobs, local settings, deleted status). | |
155 void CheckPendingUpdates(); | |
156 | |
157 // Ask CloudPrint server for new local sendings. | |
158 void GetLocalSettings(); | |
159 | |
160 // Ask CloudPrint server for printjobs. | |
161 void FetchPrintJobs(); | |
162 | |
163 // Saves |access_token| and calculates time for next update. | |
164 void RememberAccessToken(const std::string& access_token, | |
165 int expires_in_seconds); | |
166 | |
128 // Checks if register call is called correctly (|user| is correct, | 167 // Checks if register call is called correctly (|user| is correct, |
129 // error is no set etc). Returns |false| if error status is put into |status|. | 168 // error is no set etc). Returns |false| if error status is put into |status|. |
130 // Otherwise no error was occurred. | 169 // Otherwise no error was occurred. |
131 PrivetHttpServer::RegistrationErrorStatus CheckCommonRegErrors( | 170 PrivetHttpServer::RegistrationErrorStatus CheckCommonRegErrors( |
132 const std::string& user) const; | 171 const std::string& user) const; |
133 | 172 |
134 // Checks if confirmation was received. | 173 // Checks if confirmation was received. |
135 void WaitUserConfirmation(base::Time valid_until); | 174 void WaitUserConfirmation(base::Time valid_until); |
136 | 175 |
137 // Generates ProxyId for this device. | 176 // Generates ProxyId for this device. |
138 std::string GenerateProxyId() const; | 177 std::string GenerateProxyId() const; |
139 | 178 |
140 // Creates data for DNS TXT respond. | 179 // Creates data for DNS TXT respond. |
141 std::vector<std::string> CreateTxt() const; | 180 std::vector<std::string> CreateTxt() const; |
142 | 181 |
143 // Ask CloudPrint server for printjobs. | |
144 void FetchPrintJobs(); | |
145 | |
146 // Saving and loading registration info from file. | 182 // Saving and loading registration info from file. |
147 void SaveToFile(const base::FilePath& file_path) const; | 183 void SaveToFile(const base::FilePath& file_path) const; |
148 bool LoadFromFile(const base::FilePath& file_path); | 184 bool LoadFromFile(const base::FilePath& file_path); |
149 | 185 |
150 // Adds |WakeUp| method to the MessageLoop. | 186 // Adds |OnIdle| method to the MessageLoop. |
151 void PostWakeUp(); | 187 void PostOnIdle(); |
152 | |
153 // Adds |WakeUp| method to the MessageLoop with certain |delay|. | |
154 void PostDelayedWakeUp(const base::TimeDelta& delay); | |
155 | 188 |
156 // Converts errors. | 189 // Converts errors. |
157 PrivetHttpServer::RegistrationErrorStatus ConfirmationToRegistrationError( | 190 PrivetHttpServer::RegistrationErrorStatus ConfirmationToRegistrationError( |
158 RegistrationInfo::ConfirmationState state); | 191 RegistrationInfo::ConfirmationState state); |
159 | 192 |
160 std::string ConnectionStateToString(ConnectionState state) const; | 193 std::string ConnectionStateToString(ConnectionState state) const; |
161 | 194 |
162 // Changes state and update info in DNS server. | 195 // Changes state and update info in DNS server. |
163 bool ChangeState(ConnectionState new_state); | 196 bool ChangeState(ConnectionState new_state); |
164 | 197 |
165 RegistrationInfo reg_info_; | 198 RegistrationInfo reg_info_; |
166 | 199 |
167 // Contains DNS-SD server. | 200 // Contains DNS-SD server. |
168 DnsSdServer dns_server_; | 201 DnsSdServer dns_server_; |
169 | 202 |
170 // Contains Privet HTTP server. | 203 // Contains Privet HTTP server. |
171 PrivetHttpServer http_server_; | 204 PrivetHttpServer http_server_; |
172 | 205 |
173 // Connection state of device. | 206 // Connection state of device. |
174 ConnectionState connection_state_; | 207 ConnectionState connection_state_; |
175 | 208 |
176 // Contains CloudPrint client. | 209 // Contains CloudPrint client. |
177 scoped_ptr<CloudPrintRequester> requester_; | 210 scoped_ptr<CloudPrintRequester> requester_; |
178 | 211 |
212 // XMPP Listener. | |
213 scoped_ptr<CloudPrintXmppListener> xmpp_listener_; | |
214 | |
179 XPrivetToken xtoken_; | 215 XPrivetToken xtoken_; |
180 | 216 |
181 scoped_ptr<PrintJobHandler> print_job_handler_; | 217 scoped_ptr<PrintJobHandler> print_job_handler_; |
182 | 218 |
219 // Last valid |access_token|. | |
220 std::string access_token_; | |
221 base::Time access_token_update_; | |
222 | |
183 // Uses for calculating uptime. | 223 // Uses for calculating uptime. |
184 base::Time starttime_; | 224 base::Time starttime_; |
185 | 225 |
226 // Used for preventing two and more OnIdle posted in message loop. | |
227 bool on_idle_posted_; | |
228 | |
229 // Contains |true| if Printer has to check pending local settings. | |
230 bool pending_local_settings_check_; | |
231 | |
232 // Contains |true| if Printer has to check available printjobs. | |
233 bool pending_print_jobs_check_; | |
234 | |
186 DISALLOW_COPY_AND_ASSIGN(Printer); | 235 DISALLOW_COPY_AND_ASSIGN(Printer); |
187 }; | 236 }; |
188 | 237 |
189 #endif // GCP20_PROTOTYPE_PRINTER_H_ | 238 #endif // GCP20_PROTOTYPE_PRINTER_H_ |
190 | 239 |
OLD | NEW |