| 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 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 const char kServiceType[] = "_privet._tcp.local"; | 33 const char kServiceType[] = "_privet._tcp.local"; |
| 34 const char kServiceNamePrefixDefault[] = "first_gcp20_device"; | 34 const char kServiceNamePrefixDefault[] = "first_gcp20_device"; |
| 35 const char kServiceDomainNameDefault[] = "my-privet-device.local"; | 35 const char kServiceDomainNameDefault[] = "my-privet-device.local"; |
| 36 | 36 |
| 37 const char kPrinterName[] = "Google GCP2.0 Prototype"; | 37 const char kPrinterName[] = "Google GCP2.0 Prototype"; |
| 38 const char kPrinterDescription[] = "Printer emulator"; | 38 const char kPrinterDescription[] = "Printer emulator"; |
| 39 | 39 |
| 40 const char kUserConfirmationTitle[] = "Confirm registration: type 'y' if you " | 40 const char kUserConfirmationTitle[] = "Confirm registration: type 'y' if you " |
| 41 "agree and any other to discard\n"; | 41 "agree and any other to discard\n"; |
| 42 const int64 kUserConfirmationTimeout = 30; // in seconds | 42 const int kUserConfirmationTimeout = 30; // in seconds |
| 43 | 43 const int kRegistrationTimeout = 60; // in seconds |
| 44 const uint32 kReconnectTimeout = 5; // in seconds | 44 const int kReconnectTimeout = 5; // in seconds |
| 45 | 45 |
| 46 const double kTimeToNextAccessTokenUpdate = 0.8; // relatively to living time. | 46 const double kTimeToNextAccessTokenUpdate = 0.8; // relatively to living time. |
| 47 | 47 |
| 48 const char kCdd[] = | 48 const char kCdd[] = |
| 49 "{\n" | 49 "{\n" |
| 50 " 'version': '1.0',\n" | 50 " 'version': '1.0',\n" |
| 51 " 'printer': {\n" | 51 " 'printer': {\n" |
| 52 " 'vendor_capability': [\n" | 52 " 'vendor_capability': [\n" |
| 53 " {\n" | 53 " {\n" |
| 54 " 'id': 'psk:MediaType',\n" | 54 " 'id': 'psk:MediaType',\n" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 void Printer::Stop() { | 177 void Printer::Stop() { |
| 178 dns_server_.Shutdown(); | 178 dns_server_.Shutdown(); |
| 179 http_server_.Shutdown(); | 179 http_server_.Shutdown(); |
| 180 requester_.reset(); | 180 requester_.reset(); |
| 181 print_job_handler_.reset(); | 181 print_job_handler_.reset(); |
| 182 xmpp_listener_.reset(); | 182 xmpp_listener_.reset(); |
| 183 } | 183 } |
| 184 | 184 |
| 185 void Printer::OnAuthError() { | 185 void Printer::OnAuthError() { |
| 186 LOG(ERROR) << "Auth error occurred"; | 186 LOG(ERROR) << "Auth error occurred"; |
| 187 access_token_update_ = base::Time::Now(); | 187 access_token_update_ = base::Time(); |
| 188 ChangeState(OFFLINE); | 188 FallOffline(true); |
| 189 // TODO(maksymb): Implement *instant* updating of access_token. | |
| 190 } | 189 } |
| 191 | 190 |
| 192 std::string Printer::GetAccessToken() { | 191 std::string Printer::GetAccessToken() { |
| 193 return access_token_; | 192 return access_token_; |
| 194 } | 193 } |
| 195 | 194 |
| 196 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationStart( | 195 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationStart( |
| 197 const std::string& user) { | 196 const std::string& user) { |
| 197 CheckRegistrationExpiration(); |
| 198 |
| 199 RegistrationInfo::ConfirmationState conf_state = reg_info_.confirmation_state; |
| 200 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR || |
| 201 conf_state == RegistrationInfo::CONFIRMATION_TIMEOUT || |
| 202 conf_state == RegistrationInfo::CONFIRMATION_DISCARDED) { |
| 203 reg_info_ = RegistrationInfo(); |
| 204 } |
| 205 |
| 198 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); | 206 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); |
| 199 if (status != PrivetHttpServer::REG_ERROR_OK) | 207 if (status != PrivetHttpServer::REG_ERROR_OK) |
| 200 return status; | 208 return status; |
| 201 | 209 |
| 202 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED) | 210 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED) |
| 203 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; | 211 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
| 204 | 212 |
| 213 UpdateRegistrationExpiration(); |
| 214 |
| 205 reg_info_ = RegistrationInfo(); | 215 reg_info_ = RegistrationInfo(); |
| 206 reg_info_.user = user; | 216 reg_info_.user = user; |
| 207 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_STARTED; | 217 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_STARTED; |
| 208 | 218 |
| 209 if (CommandLine::ForCurrentProcess()->HasSwitch("disable-confirmation")) { | 219 if (CommandLine::ForCurrentProcess()->HasSwitch("disable-confirmation")) { |
| 210 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; | 220 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; |
| 211 LOG(INFO) << "Registration confirmed by default."; | 221 LOG(INFO) << "Registration confirmed by default."; |
| 212 } else { | 222 } else { |
| 213 printf("%s", kUserConfirmationTitle); | 223 printf("%s", kUserConfirmationTitle); |
| 214 base::Time valid_until = base::Time::Now() + | 224 base::Time valid_until = base::Time::Now() + |
| (...skipping 20 matching lines...) Expand all Loading... |
| 235 if (reg_info_.state != RegistrationInfo::DEV_REG_REGISTRATION_STARTED && | 245 if (reg_info_.state != RegistrationInfo::DEV_REG_REGISTRATION_STARTED && |
| 236 reg_info_.state != | 246 reg_info_.state != |
| 237 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) | 247 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) |
| 238 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; | 248 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
| 239 | 249 |
| 240 // If |action=getClaimToken| is valid in this state (was checked above) then | 250 // If |action=getClaimToken| is valid in this state (was checked above) then |
| 241 // check confirmation status. | 251 // check confirmation status. |
| 242 if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED) | 252 if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED) |
| 243 return ConfirmationToRegistrationError(reg_info_.confirmation_state); | 253 return ConfirmationToRegistrationError(reg_info_.confirmation_state); |
| 244 | 254 |
| 255 UpdateRegistrationExpiration(); |
| 256 |
| 245 // If reply wasn't received yet, reply with |pending_user_action| error. | 257 // If reply wasn't received yet, reply with |pending_user_action| error. |
| 246 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_STARTED) | 258 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_STARTED) |
| 247 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; | 259 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; |
| 248 | 260 |
| 249 DCHECK_EQ(reg_info_.state, | 261 DCHECK_EQ(reg_info_.state, |
| 250 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY); | 262 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY); |
| 251 DCHECK_EQ(reg_info_.confirmation_state, | 263 DCHECK_EQ(reg_info_.confirmation_state, |
| 252 RegistrationInfo::CONFIRMATION_CONFIRMED); | 264 RegistrationInfo::CONFIRMATION_CONFIRMED); |
| 253 | 265 |
| 254 *token = reg_info_.registration_token; | 266 *token = reg_info_.registration_token; |
| 255 *claim_url = reg_info_.complete_invite_url; | 267 *claim_url = reg_info_.complete_invite_url; |
| 256 return PrivetHttpServer::REG_ERROR_OK; | 268 return PrivetHttpServer::REG_ERROR_OK; |
| 257 } | 269 } |
| 258 | 270 |
| 259 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationComplete( | 271 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationComplete( |
| 260 const std::string& user, | 272 const std::string& user, |
| 261 std::string* device_id) { | 273 std::string* device_id) { |
| 262 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); | 274 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); |
| 263 if (status != PrivetHttpServer::REG_ERROR_OK) | 275 if (status != PrivetHttpServer::REG_ERROR_OK) |
| 264 return status; | 276 return status; |
| 265 | 277 |
| 266 if (reg_info_.state != | 278 if (reg_info_.state != |
| 267 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) { | 279 RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) { |
| 268 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; | 280 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
| 269 } | 281 } |
| 270 | 282 |
| 283 UpdateRegistrationExpiration(); |
| 284 |
| 271 if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED) | 285 if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED) |
| 272 return ConfirmationToRegistrationError(reg_info_.confirmation_state); | 286 return ConfirmationToRegistrationError(reg_info_.confirmation_state); |
| 273 | 287 |
| 274 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_COMPLETING; | 288 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_COMPLETING; |
| 275 requester_->CompleteRegistration(); | 289 requester_->CompleteRegistration(); |
| 276 *device_id = reg_info_.device_id; | 290 *device_id = reg_info_.device_id; |
| 277 | 291 |
| 278 return PrivetHttpServer::REG_ERROR_OK; | 292 return PrivetHttpServer::REG_ERROR_OK; |
| 279 } | 293 } |
| 280 | 294 |
| 281 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationCancel( | 295 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationCancel( |
| 282 const std::string& user) { | 296 const std::string& user) { |
| 283 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); | 297 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); |
| 284 if (status != PrivetHttpServer::REG_ERROR_OK && | 298 if (status != PrivetHttpServer::REG_ERROR_OK && |
| 285 status != PrivetHttpServer::REG_ERROR_SERVER_ERROR) { | 299 status != PrivetHttpServer::REG_ERROR_SERVER_ERROR) { |
| 286 return status; | 300 return status; |
| 287 } | 301 } |
| 288 | 302 |
| 289 if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED) | 303 if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED) |
| 290 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; | 304 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
| 291 | 305 |
| 306 InvalidateRegistrationExpiration(); |
| 307 |
| 292 reg_info_ = RegistrationInfo(); | 308 reg_info_ = RegistrationInfo(); |
| 293 requester_.reset(new CloudPrintRequester(GetTaskRunner(), this)); | 309 requester_.reset(new CloudPrintRequester(GetTaskRunner(), this)); |
| 294 | 310 |
| 295 return PrivetHttpServer::REG_ERROR_OK; | 311 return PrivetHttpServer::REG_ERROR_OK; |
| 296 } | 312 } |
| 297 | 313 |
| 298 void Printer::GetRegistrationServerError(std::string* description) { | 314 void Printer::GetRegistrationServerError(std::string* description) { |
| 299 DCHECK_EQ(reg_info_.state, RegistrationInfo::DEV_REG_REGISTRATION_ERROR) << | 315 DCHECK_EQ(reg_info_.state, RegistrationInfo::DEV_REG_REGISTRATION_ERROR) |
| 300 "Method shouldn't be called when not needed."; | 316 << "Method shouldn't be called when not needed."; |
| 301 | 317 |
| 302 *description = reg_info_.error_description; | 318 *description = reg_info_.error_description; |
| 303 } | 319 } |
| 304 | 320 |
| 305 void Printer::CreateInfo(PrivetHttpServer::DeviceInfo* info) { | 321 void Printer::CreateInfo(PrivetHttpServer::DeviceInfo* info) { |
| 322 CheckRegistrationExpiration(); |
| 323 |
| 306 // TODO(maksymb): Replace "text" with constants. | 324 // TODO(maksymb): Replace "text" with constants. |
| 307 | 325 |
| 308 *info = PrivetHttpServer::DeviceInfo(); | 326 *info = PrivetHttpServer::DeviceInfo(); |
| 309 info->version = "1.0"; | 327 info->version = "1.0"; |
| 310 info->name = kPrinterName; | 328 info->name = kPrinterName; |
| 311 info->description = kPrinterDescription; | 329 info->description = kPrinterDescription; |
| 312 info->url = kCloudPrintUrl; | 330 info->url = kCloudPrintUrl; |
| 313 info->id = reg_info_.device_id; | 331 info->id = reg_info_.device_id; |
| 314 info->device_state = "idle"; | 332 info->device_state = "idle"; |
| 315 info->connection_state = ConnectionStateToString(connection_state_); | 333 info->connection_state = ConnectionStateToString(connection_state_); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 338 void Printer::OnRegistrationStartResponseParsed( | 356 void Printer::OnRegistrationStartResponseParsed( |
| 339 const std::string& registration_token, | 357 const std::string& registration_token, |
| 340 const std::string& complete_invite_url, | 358 const std::string& complete_invite_url, |
| 341 const std::string& device_id) { | 359 const std::string& device_id) { |
| 342 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY; | 360 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY; |
| 343 reg_info_.device_id = device_id; | 361 reg_info_.device_id = device_id; |
| 344 reg_info_.registration_token = registration_token; | 362 reg_info_.registration_token = registration_token; |
| 345 reg_info_.complete_invite_url = complete_invite_url; | 363 reg_info_.complete_invite_url = complete_invite_url; |
| 346 } | 364 } |
| 347 | 365 |
| 348 void Printer::OnGetAuthCodeResponseParsed(const std::string& refresh_token, | 366 void Printer::OnRegistrationFinished(const std::string& refresh_token, |
| 349 const std::string& access_token, | 367 const std::string& access_token, |
| 350 int access_token_expires_in_seconds) { | 368 int access_token_expires_in_seconds) { |
| 369 InvalidateRegistrationExpiration(); |
| 370 |
| 351 reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED; | 371 reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED; |
| 352 reg_info_.refresh_token = refresh_token; | 372 reg_info_.refresh_token = refresh_token; |
| 353 RememberAccessToken(access_token, access_token_expires_in_seconds); | 373 RememberAccessToken(access_token, access_token_expires_in_seconds); |
| 354 TryConnect(); | 374 TryConnect(); |
| 355 } | 375 } |
| 356 | 376 |
| 357 void Printer::OnAccesstokenReceviced(const std::string& access_token, | 377 void Printer::OnAccesstokenReceviced(const std::string& access_token, |
| 358 int expires_in_seconds) { | 378 int expires_in_seconds) { |
| 359 VLOG(3) << "Function: " << __FUNCTION__; | 379 VLOG(3) << "Function: " << __FUNCTION__; |
| 360 RememberAccessToken(access_token, expires_in_seconds); | 380 RememberAccessToken(access_token, expires_in_seconds); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 373 } | 393 } |
| 374 | 394 |
| 375 void Printer::OnXmppJidReceived(const std::string& xmpp_jid) { | 395 void Printer::OnXmppJidReceived(const std::string& xmpp_jid) { |
| 376 reg_info_.xmpp_jid = xmpp_jid; | 396 reg_info_.xmpp_jid = xmpp_jid; |
| 377 } | 397 } |
| 378 | 398 |
| 379 void Printer::OnRegistrationError(const std::string& description) { | 399 void Printer::OnRegistrationError(const std::string& description) { |
| 380 LOG(ERROR) << "server_error: " << description; | 400 LOG(ERROR) << "server_error: " << description; |
| 381 | 401 |
| 382 // TODO(maksymb): Implement waiting after error and timeout of registration. | 402 // TODO(maksymb): Implement waiting after error and timeout of registration. |
| 383 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_ERROR; | 403 SetRegistrationError(description); |
| 384 reg_info_.error_description = description; | |
| 385 } | 404 } |
| 386 | 405 |
| 387 void Printer::OnNetworkError() { | 406 void Printer::OnNetworkError() { |
| 388 VLOG(3) << "Function: " << __FUNCTION__; | 407 VLOG(3) << "Function: " << __FUNCTION__; |
| 389 ChangeState(OFFLINE); | 408 FallOffline(false); |
| 390 } | 409 } |
| 391 | 410 |
| 392 void Printer::OnServerError(const std::string& description) { | 411 void Printer::OnServerError(const std::string& description) { |
| 393 VLOG(3) << "Function: " << __FUNCTION__; | 412 VLOG(3) << "Function: " << __FUNCTION__; |
| 394 LOG(ERROR) << "Server error: " << description; | 413 LOG(ERROR) << "Server error: " << description; |
| 395 | 414 |
| 396 ChangeState(OFFLINE); | 415 FallOffline(false); |
| 397 } | 416 } |
| 398 | 417 |
| 399 void Printer::OnPrintJobsAvailable(const std::vector<Job>& jobs) { | 418 void Printer::OnPrintJobsAvailable(const std::vector<Job>& jobs) { |
| 400 VLOG(3) << "Function: " << __FUNCTION__; | 419 VLOG(3) << "Function: " << __FUNCTION__; |
| 401 | 420 |
| 402 LOG(INFO) << "Available printjobs: " << jobs.size(); | 421 LOG(INFO) << "Available printjobs: " << jobs.size(); |
| 403 | 422 |
| 404 if (jobs.empty()) { | 423 if (jobs.empty()) { |
| 405 pending_print_jobs_check_ = false; | 424 pending_print_jobs_check_ = false; |
| 406 PostOnIdle(); | 425 PostOnIdle(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 432 pending_print_jobs_check_ = true; | 451 pending_print_jobs_check_ = true; |
| 433 ChangeState(ONLINE); | 452 ChangeState(ONLINE); |
| 434 PostOnIdle(); | 453 PostOnIdle(); |
| 435 } | 454 } |
| 436 | 455 |
| 437 void Printer::OnXmppAuthError() { | 456 void Printer::OnXmppAuthError() { |
| 438 OnAuthError(); | 457 OnAuthError(); |
| 439 } | 458 } |
| 440 | 459 |
| 441 void Printer::OnXmppNetworkError() { | 460 void Printer::OnXmppNetworkError() { |
| 442 ChangeState(OFFLINE); | 461 FallOffline(false); |
| 443 } | 462 } |
| 444 | 463 |
| 445 void Printer::OnXmppNewPrintJob(const std::string& device_id) { | 464 void Printer::OnXmppNewPrintJob(const std::string& device_id) { |
| 446 DCHECK_EQ(reg_info_.device_id, device_id) << "Data should contain printer_id"; | 465 DCHECK_EQ(reg_info_.device_id, device_id) << "Data should contain printer_id"; |
| 447 pending_print_jobs_check_ = true; | 466 pending_print_jobs_check_ = true; |
| 448 } | 467 } |
| 449 | 468 |
| 450 void Printer::OnXmppNewLocalSettings(const std::string& device_id) { | 469 void Printer::OnXmppNewLocalSettings(const std::string& device_id) { |
| 451 DCHECK_EQ(reg_info_.device_id, device_id) << "Data should contain printer_id"; | 470 DCHECK_EQ(reg_info_.device_id, device_id) << "Data should contain printer_id"; |
| 452 NOTIMPLEMENTED(); | 471 NOTIMPLEMENTED(); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 using base::Time; | 554 using base::Time; |
| 536 using base::TimeDelta; | 555 using base::TimeDelta; |
| 537 access_token_ = access_token; | 556 access_token_ = access_token; |
| 538 int64 time_to_update = static_cast<int64>(expires_in_seconds * | 557 int64 time_to_update = static_cast<int64>(expires_in_seconds * |
| 539 kTimeToNextAccessTokenUpdate); | 558 kTimeToNextAccessTokenUpdate); |
| 540 access_token_update_ = Time::Now() + TimeDelta::FromSeconds(time_to_update); | 559 access_token_update_ = Time::Now() + TimeDelta::FromSeconds(time_to_update); |
| 541 VLOG(1) << "Current access_token: " << access_token; | 560 VLOG(1) << "Current access_token: " << access_token; |
| 542 SaveToFile(); | 561 SaveToFile(); |
| 543 } | 562 } |
| 544 | 563 |
| 564 void Printer::SetRegistrationError(const std::string& description) { |
| 565 DCHECK(!IsRegistered()); |
| 566 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_ERROR; |
| 567 reg_info_.error_description = description; |
| 568 } |
| 569 |
| 545 PrivetHttpServer::RegistrationErrorStatus Printer::CheckCommonRegErrors( | 570 PrivetHttpServer::RegistrationErrorStatus Printer::CheckCommonRegErrors( |
| 546 const std::string& user) const { | 571 const std::string& user) { |
| 572 CheckRegistrationExpiration(); |
| 547 DCHECK(!IsRegistered()); | 573 DCHECK(!IsRegistered()); |
| 574 if (connection_state_ != ONLINE) |
| 575 return PrivetHttpServer::REG_ERROR_OFFLINE; |
| 548 | 576 |
| 549 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED && | 577 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED && |
| 550 user != reg_info_.user) { | 578 user != reg_info_.user) { |
| 551 return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; | 579 return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; |
| 552 } | 580 } |
| 553 | 581 |
| 554 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR) | 582 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR) |
| 555 return PrivetHttpServer::REG_ERROR_SERVER_ERROR; | 583 return PrivetHttpServer::REG_ERROR_SERVER_ERROR; |
| 556 | 584 |
| 585 DCHECK(connection_state_ == ONLINE); |
| 586 |
| 557 return PrivetHttpServer::REG_ERROR_OK; | 587 return PrivetHttpServer::REG_ERROR_OK; |
| 558 } | 588 } |
| 559 | 589 |
| 560 void Printer::WaitUserConfirmation(base::Time valid_until) { | 590 void Printer::WaitUserConfirmation(base::Time valid_until) { |
| 591 // TODO(maksymb): Move to separate class. |
| 592 |
| 561 if (base::Time::Now() > valid_until) { | 593 if (base::Time::Now() > valid_until) { |
| 562 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_TIMEOUT; | 594 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_TIMEOUT; |
| 563 LOG(INFO) << "Confirmation timeout reached."; | 595 LOG(INFO) << "Confirmation timeout reached."; |
| 564 return; | 596 return; |
| 565 } | 597 } |
| 566 | 598 |
| 567 if (_kbhit()) { | 599 if (_kbhit()) { |
| 568 int c = _getche(); | 600 int c = _getche(); |
| 569 if (c == 'y' || c == 'Y') { | 601 if (c == 'y' || c == 'Y') { |
| 570 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; | 602 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 void Printer::PostOnIdle() { | 748 void Printer::PostOnIdle() { |
| 717 VLOG(3) << "Function: " << __FUNCTION__; | 749 VLOG(3) << "Function: " << __FUNCTION__; |
| 718 DCHECK(!on_idle_posted_) << "Only one instance can be posted."; | 750 DCHECK(!on_idle_posted_) << "Only one instance can be posted."; |
| 719 on_idle_posted_ = true; | 751 on_idle_posted_ = true; |
| 720 | 752 |
| 721 base::MessageLoop::current()->PostTask( | 753 base::MessageLoop::current()->PostTask( |
| 722 FROM_HERE, | 754 FROM_HERE, |
| 723 base::Bind(&Printer::OnIdle, AsWeakPtr())); | 755 base::Bind(&Printer::OnIdle, AsWeakPtr())); |
| 724 } | 756 } |
| 725 | 757 |
| 758 void Printer::CheckRegistrationExpiration() { |
| 759 if (!registration_expiration_.is_null() && |
| 760 registration_expiration_ < base::Time::Now()) { |
| 761 reg_info_ = RegistrationInfo(); |
| 762 InvalidateRegistrationExpiration(); |
| 763 if (connection_state_ != ONLINE) |
| 764 TryConnect(); |
| 765 } |
| 766 } |
| 767 |
| 768 void Printer::UpdateRegistrationExpiration() { |
| 769 registration_expiration_ = |
| 770 base::Time::Now() + base::TimeDelta::FromSeconds(kRegistrationTimeout); |
| 771 } |
| 772 |
| 773 void Printer::InvalidateRegistrationExpiration() { |
| 774 registration_expiration_ = base::Time(); |
| 775 } |
| 776 |
| 726 PrivetHttpServer::RegistrationErrorStatus | 777 PrivetHttpServer::RegistrationErrorStatus |
| 727 Printer::ConfirmationToRegistrationError( | 778 Printer::ConfirmationToRegistrationError( |
| 728 RegistrationInfo::ConfirmationState state) { | 779 RegistrationInfo::ConfirmationState state) { |
| 729 switch (state) { | 780 switch (state) { |
| 730 case RegistrationInfo::CONFIRMATION_PENDING: | 781 case RegistrationInfo::CONFIRMATION_PENDING: |
| 731 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; | 782 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; |
| 732 case RegistrationInfo::CONFIRMATION_DISCARDED: | 783 case RegistrationInfo::CONFIRMATION_DISCARDED: |
| 733 return PrivetHttpServer::REG_ERROR_USER_CANCEL; | 784 return PrivetHttpServer::REG_ERROR_USER_CANCEL; |
| 734 case RegistrationInfo::CONFIRMATION_CONFIRMED: | 785 case RegistrationInfo::CONFIRMATION_CONFIRMED: |
| 735 NOTREACHED(); | 786 NOTREACHED(); |
| 736 return PrivetHttpServer::REG_ERROR_OK; | 787 return PrivetHttpServer::REG_ERROR_OK; |
| 737 case RegistrationInfo::CONFIRMATION_TIMEOUT: | 788 case RegistrationInfo::CONFIRMATION_TIMEOUT: |
| 738 return PrivetHttpServer::REG_ERROR_CONFIRMATION_TIMEOUT; | 789 return PrivetHttpServer::REG_ERROR_CONFIRMATION_TIMEOUT; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 752 return "connecting"; | 803 return "connecting"; |
| 753 case NOT_CONFIGURED: | 804 case NOT_CONFIGURED: |
| 754 return "not-configured"; | 805 return "not-configured"; |
| 755 | 806 |
| 756 default: | 807 default: |
| 757 NOTREACHED(); | 808 NOTREACHED(); |
| 758 return ""; | 809 return ""; |
| 759 } | 810 } |
| 760 } | 811 } |
| 761 | 812 |
| 813 void Printer::FallOffline(bool instant_reconnect) { |
| 814 bool changed = ChangeState(OFFLINE); |
| 815 DCHECK(changed) << "Falling offline from offline is now allowed"; |
| 816 |
| 817 if (!IsRegistered()) |
| 818 SetRegistrationError("Cannot access server during registration process"); |
| 819 |
| 820 if (instant_reconnect) { |
| 821 TryConnect(); |
| 822 } else { |
| 823 base::MessageLoop::current()->PostDelayedTask( |
| 824 FROM_HERE, |
| 825 base::Bind(&Printer::TryConnect, AsWeakPtr()), |
| 826 base::TimeDelta::FromSeconds(kReconnectTimeout)); |
| 827 } |
| 828 } |
| 829 |
| 762 bool Printer::ChangeState(ConnectionState new_state) { | 830 bool Printer::ChangeState(ConnectionState new_state) { |
| 763 if (connection_state_ == new_state) | 831 if (connection_state_ == new_state) |
| 764 return false; | 832 return false; |
| 765 | 833 |
| 766 connection_state_ = new_state; | 834 connection_state_ = new_state; |
| 767 LOG(INFO) << base::StringPrintf( | 835 LOG(INFO) << base::StringPrintf( |
| 768 "Printer is now %s (%s)", | 836 "Printer is now %s (%s)", |
| 769 ConnectionStateToString(connection_state_).c_str(), | 837 ConnectionStateToString(connection_state_).c_str(), |
| 770 IsRegistered() ? "registered" : "unregistered"); | 838 IsRegistered() ? "registered" : "unregistered"); |
| 771 | 839 |
| 772 dns_server_.UpdateMetadata(CreateTxt()); | 840 dns_server_.UpdateMetadata(CreateTxt()); |
| 773 | 841 |
| 774 switch (connection_state_) { | 842 if (connection_state_ == OFFLINE) { |
| 775 case CONNECTING: | 843 requester_.reset(); |
| 776 break; | 844 xmpp_listener_.reset(); |
| 777 | |
| 778 case ONLINE: | |
| 779 break; | |
| 780 | |
| 781 case OFFLINE: | |
| 782 requester_.reset(); | |
| 783 xmpp_listener_.reset(); | |
| 784 base::MessageLoop::current()->PostDelayedTask( | |
| 785 FROM_HERE, | |
| 786 base::Bind(&Printer::TryConnect, AsWeakPtr()), | |
| 787 base::TimeDelta::FromSeconds(kReconnectTimeout)); | |
| 788 | |
| 789 case NOT_CONFIGURED: | |
| 790 break; | |
| 791 | |
| 792 default: | |
| 793 NOTREACHED(); | |
| 794 } | 845 } |
| 795 | 846 |
| 796 return true; | 847 return true; |
| 797 } | 848 } |
| 798 | 849 |
| OLD | NEW |