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 #include "cloud_print/gcp20/prototype/printer.h" | 5 #include "cloud_print/gcp20/prototype/printer.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... | |
31 | 31 |
32 const char kServiceType[] = "_privet._tcp.local"; | 32 const char kServiceType[] = "_privet._tcp.local"; |
33 const char kServiceNamePrefixDefault[] = "first_gcp20_device"; | 33 const char kServiceNamePrefixDefault[] = "first_gcp20_device"; |
34 const char kServiceDomainNameDefault[] = "my-privet-device.local"; | 34 const char kServiceDomainNameDefault[] = "my-privet-device.local"; |
35 | 35 |
36 const char kPrinterName[] = "Google GCP2.0 Prototype"; | 36 const char kPrinterName[] = "Google GCP2.0 Prototype"; |
37 const char kPrinterDescription[] = "Printer emulator"; | 37 const char kPrinterDescription[] = "Printer emulator"; |
38 | 38 |
39 const char kUserConfirmationTitle[] = "Confirm registration: type 'y' if you " | 39 const char kUserConfirmationTitle[] = "Confirm registration: type 'y' if you " |
40 "agree and any other to discard\n"; | 40 "agree and any other to discard\n"; |
41 const int64 kUserConfirmationTimeout = 30; // in seconds | 41 const int kUserConfirmationTimeout = 30; // in seconds |
42 | 42 const int kRegistrationTimeout = 60; // in seconds |
43 const uint32 kReconnectTimeout = 5; // in seconds | 43 const int kReconnectTimeout = 5; // in seconds |
44 | 44 |
45 const double kTimeToNextAccessTokenUpdate = 0.8; // relatively to living time. | 45 const double kTimeToNextAccessTokenUpdate = 0.8; // relatively to living time. |
46 | 46 |
47 const char kCdd[] = | 47 const char kCdd[] = |
48 "{\n" | 48 "{\n" |
49 " 'version': '1.0',\n" | 49 " 'version': '1.0',\n" |
50 " 'printer': {\n" | 50 " 'printer': {\n" |
51 " 'vendor_capability': [\n" | 51 " 'vendor_capability': [\n" |
52 " {\n" | 52 " {\n" |
53 " 'id': 'psk:MediaType',\n" | 53 " 'id': 'psk:MediaType',\n" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 | 175 |
176 void Printer::Stop() { | 176 void Printer::Stop() { |
177 dns_server_.Shutdown(); | 177 dns_server_.Shutdown(); |
178 http_server_.Shutdown(); | 178 http_server_.Shutdown(); |
179 requester_.reset(); | 179 requester_.reset(); |
180 print_job_handler_.reset(); | 180 print_job_handler_.reset(); |
181 xmpp_listener_.reset(); | 181 xmpp_listener_.reset(); |
182 } | 182 } |
183 | 183 |
184 void Printer::OnAuthError() { | 184 void Printer::OnAuthError() { |
185 access_token_update_ = base::Time::Now(); | 185 access_token_update_ = base::Time::UnixEpoch(); |
186 ChangeState(OFFLINE); | 186 ReconnectIn(base::TimeDelta::FromSeconds(0)); |
187 // TODO(maksymb): Implement *instant* updating of access_token. | |
188 } | 187 } |
189 | 188 |
190 std::string Printer::GetAccessToken() { | 189 std::string Printer::GetAccessToken() { |
191 return access_token_; | 190 return access_token_; |
192 } | 191 } |
193 | 192 |
194 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationStart( | 193 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationStart( |
195 const std::string& user) { | 194 const std::string& user) { |
195 CheckRegistrationExpiration(); | |
196 RegistrationInfo::ConfirmationState conf_state = reg_info_.confirmation_state; | |
197 if (user != reg_info_.user) | |
198 if (conf_state == RegistrationInfo::CONFIRMATION_TIMEOUT || | |
199 conf_state == RegistrationInfo::CONFIRMATION_DISCARDED) { | |
200 reg_info_ = RegistrationInfo(); | |
201 } | |
202 | |
196 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); | 203 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); |
197 if (status != PrivetHttpServer::REG_ERROR_OK) | 204 if (status != PrivetHttpServer::REG_ERROR_OK) |
198 return status; | 205 return status; |
199 | 206 |
200 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED) | 207 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED) |
201 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; | 208 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
202 | 209 |
210 UpdateRegistrationExpiration(); | |
211 | |
203 reg_info_ = RegistrationInfo(); | 212 reg_info_ = RegistrationInfo(); |
204 reg_info_.user = user; | 213 reg_info_.user = user; |
205 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_STARTED; | 214 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_STARTED; |
206 | 215 |
207 if (CommandLine::ForCurrentProcess()->HasSwitch("disable-confirmation")) { | 216 if (CommandLine::ForCurrentProcess()->HasSwitch("disable-confirmation")) { |
208 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; | 217 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; |
209 LOG(INFO) << "Registration confirmed by default."; | 218 LOG(INFO) << "Registration confirmed by default."; |
210 } else { | 219 } else { |
211 printf("%s", kUserConfirmationTitle); | 220 printf("%s", kUserConfirmationTitle); |
212 base::Time valid_until = base::Time::Now() + | 221 base::Time valid_until = base::Time::Now() + |
(...skipping 20 matching lines...) Expand all Loading... | |
233 if (reg_info_.state != RegistrationInfo::DEV_REG_REGISTRATION_STARTED && | 242 if (reg_info_.state != RegistrationInfo::DEV_REG_REGISTRATION_STARTED && |
234 reg_info_.state != | 243 reg_info_.state != |
235 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) | 244 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) |
236 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; | 245 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
237 | 246 |
238 // If |action=getClaimToken| is valid in this state (was checked above) then | 247 // If |action=getClaimToken| is valid in this state (was checked above) then |
239 // check confirmation status. | 248 // check confirmation status. |
240 if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED) | 249 if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED) |
241 return ConfirmationToRegistrationError(reg_info_.confirmation_state); | 250 return ConfirmationToRegistrationError(reg_info_.confirmation_state); |
242 | 251 |
252 UpdateRegistrationExpiration(); | |
253 | |
243 // If reply wasn't received yet, reply with |device_busy| error. | 254 // If reply wasn't received yet, reply with |device_busy| error. |
244 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_STARTED) | 255 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_STARTED) |
245 return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; | 256 return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; |
246 | 257 |
247 DCHECK_EQ(reg_info_.state, | 258 DCHECK_EQ(reg_info_.state, |
248 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY); | 259 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY); |
249 DCHECK_EQ(reg_info_.confirmation_state, | 260 DCHECK_EQ(reg_info_.confirmation_state, |
250 RegistrationInfo::CONFIRMATION_CONFIRMED); | 261 RegistrationInfo::CONFIRMATION_CONFIRMED); |
251 | 262 |
252 *token = reg_info_.registration_token; | 263 *token = reg_info_.registration_token; |
253 *claim_url = reg_info_.complete_invite_url; | 264 *claim_url = reg_info_.complete_invite_url; |
254 return PrivetHttpServer::REG_ERROR_OK; | 265 return PrivetHttpServer::REG_ERROR_OK; |
255 } | 266 } |
256 | 267 |
257 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationComplete( | 268 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationComplete( |
258 const std::string& user, | 269 const std::string& user, |
259 std::string* device_id) { | 270 std::string* device_id) { |
260 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); | 271 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); |
261 if (status != PrivetHttpServer::REG_ERROR_OK) | 272 if (status != PrivetHttpServer::REG_ERROR_OK) |
262 return status; | 273 return status; |
263 | 274 |
264 if (reg_info_.state != | 275 if (reg_info_.state != |
265 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) { | 276 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) { |
266 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; | 277 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
267 } | 278 } |
268 | 279 |
280 UpdateRegistrationExpiration(); | |
281 | |
269 if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED) | 282 if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED) |
270 return ConfirmationToRegistrationError(reg_info_.confirmation_state); | 283 return ConfirmationToRegistrationError(reg_info_.confirmation_state); |
271 | 284 |
272 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_COMPLETING; | 285 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_COMPLETING; |
273 requester_->CompleteRegistration(); | 286 requester_->CompleteRegistration(); |
274 *device_id = reg_info_.device_id; | 287 *device_id = reg_info_.device_id; |
275 | 288 |
276 return PrivetHttpServer::REG_ERROR_OK; | 289 return PrivetHttpServer::REG_ERROR_OK; |
277 } | 290 } |
278 | 291 |
279 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationCancel( | 292 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationCancel( |
280 const std::string& user) { | 293 const std::string& user) { |
281 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); | 294 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); |
282 if (status != PrivetHttpServer::REG_ERROR_OK && | 295 if (status != PrivetHttpServer::REG_ERROR_OK && |
283 status != PrivetHttpServer::REG_ERROR_SERVER_ERROR) { | 296 status != PrivetHttpServer::REG_ERROR_SERVER_ERROR) { |
284 return status; | 297 return status; |
285 } | 298 } |
286 | 299 |
287 if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED) | 300 if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED) |
288 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; | 301 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
289 | 302 |
303 InvalidateRegistrationExpiration(); | |
304 | |
290 reg_info_ = RegistrationInfo(); | 305 reg_info_ = RegistrationInfo(); |
291 requester_.reset(new CloudPrintRequester(GetTaskRunner(), this)); | 306 requester_.reset(new CloudPrintRequester(GetTaskRunner(), this)); |
292 | 307 |
293 return PrivetHttpServer::REG_ERROR_OK; | 308 return PrivetHttpServer::REG_ERROR_OK; |
294 } | 309 } |
295 | 310 |
296 void Printer::GetRegistrationServerError(std::string* description) { | 311 void Printer::GetRegistrationServerError(std::string* description) { |
297 DCHECK_EQ(reg_info_.state, RegistrationInfo::DEV_REG_REGISTRATION_ERROR) << | 312 DCHECK_EQ(reg_info_.state, RegistrationInfo::DEV_REG_REGISTRATION_ERROR) |
298 "Method shouldn't be called when not needed."; | 313 << "Method shouldn't be called when not needed."; |
299 | 314 |
300 *description = reg_info_.error_description; | 315 *description = reg_info_.error_description; |
301 } | 316 } |
302 | 317 |
303 void Printer::CreateInfo(PrivetHttpServer::DeviceInfo* info) { | 318 void Printer::CreateInfo(PrivetHttpServer::DeviceInfo* info) { |
319 CheckRegistrationExpiration(); | |
320 | |
304 // TODO(maksymb): Replace "text" with constants. | 321 // TODO(maksymb): Replace "text" with constants. |
305 | 322 |
306 *info = PrivetHttpServer::DeviceInfo(); | 323 *info = PrivetHttpServer::DeviceInfo(); |
307 info->version = "1.0"; | 324 info->version = "1.0"; |
308 info->name = kPrinterName; | 325 info->name = kPrinterName; |
309 info->description = kPrinterDescription; | 326 info->description = kPrinterDescription; |
310 info->url = kCloudPrintUrl; | 327 info->url = kCloudPrintUrl; |
311 info->id = reg_info_.device_id; | 328 info->id = reg_info_.device_id; |
312 info->device_state = "idle"; | 329 info->device_state = "idle"; |
313 info->connection_state = ConnectionStateToString(connection_state_); | 330 info->connection_state = ConnectionStateToString(connection_state_); |
314 info->manufacturer = "Google"; | 331 info->manufacturer = "Google"; |
315 info->model = "Prototype"; | 332 info->model = "Prototype"; |
316 info->serial_number = "2.3.5.7.13.17.19.31.61.89.107.127.521.607.1279.2203"; | 333 info->serial_number = "2.3.5.7.13.17.19.31.61.89.107.127.521.607.1279.2203"; |
317 info->firmware = "3.7.31.127.8191.131071.524287.2147483647"; | 334 info->firmware = "3.7.31.127.8191.131071.524287.2147483647"; |
318 info->uptime = static_cast<int>((base::Time::Now() - starttime_).InSeconds()); | 335 info->uptime = static_cast<int>((base::Time::Now() - starttime_).InSeconds()); |
319 | 336 |
320 info->x_privet_token = xtoken_.GenerateXToken(); | 337 info->x_privet_token = xtoken_.GenerateXToken(); |
321 | 338 |
322 if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED) | 339 if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED) |
323 info->api.push_back("/privet/register"); | 340 info->api.push_back("/privet/register"); |
324 | 341 |
325 info->type.push_back("printer"); | 342 info->type.push_back("printer"); |
326 } | 343 } |
327 | 344 |
328 bool Printer::IsRegistered() const { | 345 bool Printer::IsRegistered() { |
346 CheckRegistrationExpiration(); | |
329 return reg_info_.state == RegistrationInfo::DEV_REG_REGISTERED; | 347 return reg_info_.state == RegistrationInfo::DEV_REG_REGISTERED; |
330 } | 348 } |
331 | 349 |
332 bool Printer::CheckXPrivetTokenHeader(const std::string& token) const { | 350 bool Printer::CheckXPrivetTokenHeader(const std::string& token) const { |
333 return xtoken_.CheckValidXToken(token); | 351 return xtoken_.CheckValidXToken(token); |
334 } | 352 } |
335 | 353 |
336 void Printer::OnRegistrationStartResponseParsed( | 354 void Printer::OnRegistrationStartResponseParsed( |
337 const std::string& registration_token, | 355 const std::string& registration_token, |
338 const std::string& complete_invite_url, | 356 const std::string& complete_invite_url, |
339 const std::string& device_id) { | 357 const std::string& device_id) { |
340 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY; | 358 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY; |
341 reg_info_.device_id = device_id; | 359 reg_info_.device_id = device_id; |
342 reg_info_.registration_token = registration_token; | 360 reg_info_.registration_token = registration_token; |
343 reg_info_.complete_invite_url = complete_invite_url; | 361 reg_info_.complete_invite_url = complete_invite_url; |
344 } | 362 } |
345 | 363 |
346 void Printer::OnGetAuthCodeResponseParsed(const std::string& refresh_token, | 364 void Printer::OnRegistrationFinished(const std::string& refresh_token, |
347 const std::string& access_token, | 365 const std::string& access_token, |
348 int access_token_expires_in_seconds) { | 366 int access_token_expires_in_seconds) { |
367 InvalidateRegistrationExpiration(); | |
368 | |
349 reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED; | 369 reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED; |
350 reg_info_.refresh_token = refresh_token; | 370 reg_info_.refresh_token = refresh_token; |
351 RememberAccessToken(access_token, access_token_expires_in_seconds); | 371 RememberAccessToken(access_token, access_token_expires_in_seconds); |
352 TryConnect(); | 372 TryConnect(); |
353 } | 373 } |
354 | 374 |
355 void Printer::OnAccesstokenReceviced(const std::string& access_token, | 375 void Printer::OnAccesstokenReceviced(const std::string& access_token, |
356 int expires_in_seconds) { | 376 int expires_in_seconds) { |
357 VLOG(3) << "Function: " << __FUNCTION__; | 377 VLOG(3) << "Function: " << __FUNCTION__; |
358 RememberAccessToken(access_token, expires_in_seconds); | 378 RememberAccessToken(access_token, expires_in_seconds); |
(...skipping 12 matching lines...) Expand all Loading... | |
371 } | 391 } |
372 | 392 |
373 void Printer::OnXmppJidReceived(const std::string& xmpp_jid) { | 393 void Printer::OnXmppJidReceived(const std::string& xmpp_jid) { |
374 reg_info_.xmpp_jid = xmpp_jid; | 394 reg_info_.xmpp_jid = xmpp_jid; |
375 } | 395 } |
376 | 396 |
377 void Printer::OnRegistrationError(const std::string& description) { | 397 void Printer::OnRegistrationError(const std::string& description) { |
378 LOG(ERROR) << "server_error: " << description; | 398 LOG(ERROR) << "server_error: " << description; |
379 | 399 |
380 // TODO(maksymb): Implement waiting after error and timeout of registration. | 400 // TODO(maksymb): Implement waiting after error and timeout of registration. |
381 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_ERROR; | 401 SetRegistrationError(description); |
382 reg_info_.error_description = description; | |
383 } | 402 } |
384 | 403 |
385 void Printer::OnNetworkError() { | 404 void Printer::OnNetworkError() { |
386 VLOG(3) << "Function: " << __FUNCTION__; | 405 VLOG(3) << "Function: " << __FUNCTION__; |
387 ChangeState(OFFLINE); | 406 ChangeState(OFFLINE); |
388 } | 407 } |
389 | 408 |
390 void Printer::OnServerError(const std::string& description) { | 409 void Printer::OnServerError(const std::string& description) { |
391 VLOG(3) << "Function: " << __FUNCTION__; | 410 VLOG(3) << "Function: " << __FUNCTION__; |
392 LOG(ERROR) << "Server error: " << description; | 411 LOG(ERROR) << "Server error: " << description; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
532 using base::Time; | 551 using base::Time; |
533 using base::TimeDelta; | 552 using base::TimeDelta; |
534 access_token_ = access_token; | 553 access_token_ = access_token; |
535 int64 time_to_update = static_cast<int64>(expires_in_seconds * | 554 int64 time_to_update = static_cast<int64>(expires_in_seconds * |
536 kTimeToNextAccessTokenUpdate); | 555 kTimeToNextAccessTokenUpdate); |
537 access_token_update_ = Time::Now() + TimeDelta::FromSeconds(time_to_update); | 556 access_token_update_ = Time::Now() + TimeDelta::FromSeconds(time_to_update); |
538 VLOG(1) << "Current access_token: " << access_token; | 557 VLOG(1) << "Current access_token: " << access_token; |
539 SaveToFile(); | 558 SaveToFile(); |
540 } | 559 } |
541 | 560 |
561 void Printer::SetRegistrationError(const std::string& description) { | |
562 DCHECK(!IsRegistered()); | |
563 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_ERROR; | |
564 reg_info_.error_description = description; | |
565 } | |
566 | |
542 PrivetHttpServer::RegistrationErrorStatus Printer::CheckCommonRegErrors( | 567 PrivetHttpServer::RegistrationErrorStatus Printer::CheckCommonRegErrors( |
543 const std::string& user) const { | 568 const std::string& user) { |
569 CheckRegistrationExpiration(); | |
544 DCHECK(!IsRegistered()); | 570 DCHECK(!IsRegistered()); |
545 | 571 |
546 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED && | 572 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED && |
547 user != reg_info_.user) { | 573 user != reg_info_.user) { |
548 return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; | 574 return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; |
549 } | 575 } |
550 | 576 |
551 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR) | 577 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR) |
552 return PrivetHttpServer::REG_ERROR_SERVER_ERROR; | 578 return PrivetHttpServer::REG_ERROR_SERVER_ERROR; |
553 | 579 |
580 DCHECK(connection_state_ == ONLINE); | |
581 | |
554 return PrivetHttpServer::REG_ERROR_OK; | 582 return PrivetHttpServer::REG_ERROR_OK; |
555 } | 583 } |
556 | 584 |
557 void Printer::WaitUserConfirmation(base::Time valid_until) { | 585 void Printer::WaitUserConfirmation(base::Time valid_until) { |
586 // TODO(maksymb): Move to separate class. | |
587 | |
558 if (base::Time::Now() > valid_until) { | 588 if (base::Time::Now() > valid_until) { |
559 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_TIMEOUT; | 589 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_TIMEOUT; |
560 LOG(INFO) << "Confirmation timeout reached."; | 590 LOG(INFO) << "Confirmation timeout reached."; |
561 return; | 591 return; |
562 } | 592 } |
563 | 593 |
564 if (_kbhit()) { | 594 if (_kbhit()) { |
565 int c = _getche(); | 595 int c = _getche(); |
566 if (c == 'y' || c == 'Y') { | 596 if (c == 'y' || c == 'Y') { |
567 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; | 597 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; |
(...skipping 21 matching lines...) Expand all Loading... | |
589 txt.push_back("ty=" + std::string(kPrinterName)); | 619 txt.push_back("ty=" + std::string(kPrinterName)); |
590 txt.push_back("note=" + std::string(kPrinterDescription)); | 620 txt.push_back("note=" + std::string(kPrinterDescription)); |
591 txt.push_back("url=" + std::string(kCloudPrintUrl)); | 621 txt.push_back("url=" + std::string(kCloudPrintUrl)); |
592 txt.push_back("type=printer"); | 622 txt.push_back("type=printer"); |
593 txt.push_back("id=" + reg_info_.device_id); | 623 txt.push_back("id=" + reg_info_.device_id); |
594 txt.push_back("cs=" + ConnectionStateToString(connection_state_)); | 624 txt.push_back("cs=" + ConnectionStateToString(connection_state_)); |
595 | 625 |
596 return txt; | 626 return txt; |
597 } | 627 } |
598 | 628 |
599 void Printer::SaveToFile() const { | 629 void Printer::SaveToFile() { |
600 base::FilePath file_path; | 630 base::FilePath file_path; |
601 file_path = file_path.AppendASCII( | 631 file_path = file_path.AppendASCII( |
602 command_line_reader::ReadStatePath(kPrinterStatePathDefault)); | 632 command_line_reader::ReadStatePath(kPrinterStatePathDefault)); |
603 | 633 |
604 base::DictionaryValue json; | 634 base::DictionaryValue json; |
605 // TODO(maksymb): Get rid of in-place constants. | 635 // TODO(maksymb): Get rid of in-place constants. |
606 if (IsRegistered()) { | 636 if (IsRegistered()) { |
607 json.SetBoolean("registered", true); | 637 json.SetBoolean("registered", true); |
608 json.SetString("user", reg_info_.user); | 638 json.SetString("user", reg_info_.user); |
609 json.SetString("device_id", reg_info_.device_id); | 639 json.SetString("device_id", reg_info_.device_id); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
713 void Printer::PostOnIdle() { | 743 void Printer::PostOnIdle() { |
714 VLOG(3) << "Function: " << __FUNCTION__; | 744 VLOG(3) << "Function: " << __FUNCTION__; |
715 DCHECK(!on_idle_posted_) << "Only one instance can be posted."; | 745 DCHECK(!on_idle_posted_) << "Only one instance can be posted."; |
716 on_idle_posted_ = true; | 746 on_idle_posted_ = true; |
717 | 747 |
718 base::MessageLoop::current()->PostTask( | 748 base::MessageLoop::current()->PostTask( |
719 FROM_HERE, | 749 FROM_HERE, |
720 base::Bind(&Printer::OnIdle, AsWeakPtr())); | 750 base::Bind(&Printer::OnIdle, AsWeakPtr())); |
721 } | 751 } |
722 | 752 |
753 void Printer::CheckRegistrationExpiration() { | |
754 if (!registration_expiration_.is_null() && | |
755 registration_expiration_ < base::Time::Now()) { | |
756 reg_info_ = RegistrationInfo(); | |
757 InvalidateRegistrationExpiration(); | |
758 if (connection_state_ != ONLINE) | |
759 TryConnect(); | |
760 } | |
761 } | |
762 | |
763 void Printer::UpdateRegistrationExpiration() { | |
764 registration_expiration_ = | |
765 base::Time::Now() + base::TimeDelta::FromSeconds(kRegistrationTimeout); | |
766 } | |
767 | |
768 void Printer::InvalidateRegistrationExpiration() { | |
769 registration_expiration_ = base::Time(); | |
770 } | |
771 | |
723 PrivetHttpServer::RegistrationErrorStatus | 772 PrivetHttpServer::RegistrationErrorStatus |
724 Printer::ConfirmationToRegistrationError( | 773 Printer::ConfirmationToRegistrationError( |
725 RegistrationInfo::ConfirmationState state) { | 774 RegistrationInfo::ConfirmationState state) { |
726 switch (state) { | 775 switch (state) { |
727 case RegistrationInfo::CONFIRMATION_PENDING: | 776 case RegistrationInfo::CONFIRMATION_PENDING: |
728 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; | 777 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; |
729 case RegistrationInfo::CONFIRMATION_DISCARDED: | 778 case RegistrationInfo::CONFIRMATION_DISCARDED: |
730 return PrivetHttpServer::REG_ERROR_USER_CANCEL; | 779 return PrivetHttpServer::REG_ERROR_USER_CANCEL; |
731 case RegistrationInfo::CONFIRMATION_CONFIRMED: | 780 case RegistrationInfo::CONFIRMATION_CONFIRMED: |
732 NOTREACHED(); | 781 NOTREACHED(); |
733 return PrivetHttpServer::REG_ERROR_OK; | 782 return PrivetHttpServer::REG_ERROR_OK; |
734 case RegistrationInfo::CONFIRMATION_TIMEOUT: | 783 case RegistrationInfo::CONFIRMATION_TIMEOUT: |
735 return PrivetHttpServer::REG_ERROR_CONFIRMATION_TIMEOUT; | 784 return PrivetHttpServer::REG_ERROR_CONFIRMATION_TIMEOUT; |
(...skipping 13 matching lines...) Expand all Loading... | |
749 return "connecting"; | 798 return "connecting"; |
750 case NOT_CONFIGURED: | 799 case NOT_CONFIGURED: |
751 return "not-configured"; | 800 return "not-configured"; |
752 | 801 |
753 default: | 802 default: |
754 NOTREACHED(); | 803 NOTREACHED(); |
755 return ""; | 804 return ""; |
756 } | 805 } |
757 } | 806 } |
758 | 807 |
808 void Printer::ReconnectIn(const base::TimeDelta& delay) { | |
809 if (!IsRegistered()) { | |
810 SetRegistrationError("Cannot access server during registration process"); | |
gene
2013/08/08 02:01:55
I am not sure I understand in what cases this erro
maksymb
2013/08/08 18:35:46
Once something bad happened during registration, t
| |
811 return; | |
812 } | |
813 | |
814 requester_.reset(); | |
815 xmpp_listener_.reset(); | |
816 base::MessageLoop::current()->PostDelayedTask( | |
817 FROM_HERE, | |
818 base::Bind(&Printer::TryConnect, AsWeakPtr()), | |
819 delay); | |
820 } | |
821 | |
759 bool Printer::ChangeState(ConnectionState new_state) { | 822 bool Printer::ChangeState(ConnectionState new_state) { |
760 if (connection_state_ == new_state) | 823 if (connection_state_ == new_state) |
761 return false; | 824 return false; |
762 | 825 |
763 connection_state_ = new_state; | 826 connection_state_ = new_state; |
764 LOG(INFO) << base::StringPrintf( | 827 LOG(INFO) << base::StringPrintf( |
765 "Printer is now %s (%s)", | 828 "Printer is now %s (%s)", |
766 ConnectionStateToString(connection_state_).c_str(), | 829 ConnectionStateToString(connection_state_).c_str(), |
767 IsRegistered() ? "registered" : "unregistered"); | 830 IsRegistered() ? "registered" : "unregistered"); |
768 | 831 |
769 dns_server_.UpdateMetadata(CreateTxt()); | 832 dns_server_.UpdateMetadata(CreateTxt()); |
770 | 833 |
771 switch (connection_state_) { | 834 if (connection_state_ == OFFLINE) |
772 case CONNECTING: | 835 ReconnectIn(base::TimeDelta::FromSeconds(kReconnectTimeout)); |
773 break; | |
774 | |
775 case ONLINE: | |
776 break; | |
777 | |
778 case OFFLINE: | |
779 requester_.reset(); | |
780 xmpp_listener_.reset(); | |
781 base::MessageLoop::current()->PostDelayedTask( | |
782 FROM_HERE, | |
783 base::Bind(&Printer::TryConnect, AsWeakPtr()), | |
784 base::TimeDelta::FromSeconds(kReconnectTimeout)); | |
785 | |
786 case NOT_CONFIGURED: | |
787 break; | |
788 | |
789 default: | |
790 NOTREACHED(); | |
791 } | |
792 | 836 |
793 return true; | 837 return true; |
794 } | 838 } |
795 | 839 |
OLD | NEW |