Chromium Code Reviews| Index: cloud_print/gcp20/prototype/printer.cc |
| diff --git a/cloud_print/gcp20/prototype/printer.cc b/cloud_print/gcp20/prototype/printer.cc |
| index 97565f551ffc7141292553cfbc5484584562fdc5..c31b9970c317072afa0f3406c7e313c922e1f5bd 100644 |
| --- a/cloud_print/gcp20/prototype/printer.cc |
| +++ b/cloud_print/gcp20/prototype/printer.cc |
| @@ -4,6 +4,7 @@ |
| #include "cloud_print/gcp20/prototype/printer.h" |
| +#include <stdio.h> |
| #include <string> |
| #include <vector> |
| @@ -15,21 +16,26 @@ |
| #include "base/strings/string_number_conversions.h" |
| #include "cloud_print/gcp20/prototype/command_line_reader.h" |
| #include "cloud_print/gcp20/prototype/service_parameters.h" |
| +#include "cloud_print/gcp20/prototype/special_io.h" |
| #include "net/base/net_util.h" |
| #include "net/base/url_util.h" |
| -const char* kPrinterStatePath = "printer_state.json"; |
| +const char kPrinterStatePath[] = "printer_state.json"; |
| namespace { |
| -const char* kServiceType = "_privet._tcp.local"; |
| -const char* kServiceNamePrefix = "first_gcp20_device"; |
| -const char* kServiceDomainName = "my-privet-device.local"; |
| +const char kServiceType[] = "_privet._tcp.local"; |
| +const char kServiceNamePrefix[] = "first_gcp20_device"; |
| +const char kServiceDomainName[] = "my-privet-device.local"; |
| -const char* kPrinterName = "Google GCP2.0 Prototype"; |
| -const char* kPrinterDescription = "Printer emulator"; |
| +const char kPrinterName[] = "Google GCP2.0 Prototype"; |
| +const char kPrinterDescription[] = "Printer emulator"; |
| -const char* kCdd = |
| +const char kUserConfirmationTitle[] = "Confirm registration: type 'y' if you " |
| + "agree and any other to discard"; |
| +const uint kUserConfirmationTimeout = 30; // in seconds |
| + |
| +const char kCdd[] = |
| "{\n" |
| " 'version': '1.0',\n" |
| " 'printer': {\n" |
| @@ -85,7 +91,9 @@ net::IPAddressNumber GetLocalIp(const std::string& interface_name, |
| } // namespace |
| -Printer::RegistrationInfo::RegistrationInfo() : state(DEV_REG_UNREGISTERED) { |
| +Printer::RegistrationInfo::RegistrationInfo() |
| + : state(DEV_REG_UNREGISTERED), |
| + confirmation_state(CONFIRMATION_PENDING) { |
| } |
| Printer::RegistrationInfo::~RegistrationInfo() { |
| @@ -164,6 +172,14 @@ PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationStart( |
| reg_info_.user = user; |
| reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_STARTED; |
| + printf("%s\n", kUserConfirmationTitle); |
|
gene
2013/07/18 09:56:46
Why don't you just printf(kUserConfirmationTitle);
maksymb
2013/07/22 19:52:16
Done.
|
| + base::Time valid_until = base::Time::Now() + |
| + base::TimeDelta::FromSeconds(kUserConfirmationTimeout); |
| + base::MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&Printer::WaitUserConfirmation, |
| + base::Unretained(this), valid_until)); |
| + |
| requester_->StartRegistration(GenerateProxyId(), kPrinterName, user, kCdd); |
| return PrivetHttpServer::REG_ERROR_OK; |
| @@ -185,19 +201,20 @@ PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationGetClaimToken( |
| if (status != PrivetHttpServer::REG_ERROR_OK) |
| return status; |
| - // TODO(maksymb): Add user confirmation. |
| + if (reg_info_.state != RegistrationInfo::DEV_REG_REGISTRATION_STARTED && |
| + reg_info_.state != |
| + RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) |
| + return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
|
gene
2013/07/18 09:56:46
I think it should be different errors if registrat
maksymb
2013/07/22 19:52:16
Added comments.
|
| + |
| + if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED) |
| + return ConfirmationToRegistrationError(reg_info_.confirmation_state); |
| if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_STARTED) |
| return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; |
| - if (reg_info_.state == |
| - RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY) { |
| - *token = reg_info_.registration_token; |
| - *claim_url = reg_info_.complete_invite_url; |
| - return PrivetHttpServer::REG_ERROR_OK; |
| - } |
| - |
| - return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
| + *token = reg_info_.registration_token; |
| + *claim_url = reg_info_.complete_invite_url; |
| + return PrivetHttpServer::REG_ERROR_OK; |
| } |
| PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationComplete( |
| @@ -212,9 +229,11 @@ PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationComplete( |
| return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
| } |
| + if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED) |
| + return ConfirmationToRegistrationError(reg_info_.confirmation_state); |
| + |
| reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_COMPLETING; |
| requester_->CompleteRegistration(); |
| - |
| *device_id = reg_info_.device_id; |
| return PrivetHttpServer::REG_ERROR_OK; |
| @@ -232,6 +251,10 @@ PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationCancel( |
| return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
| reg_info_ = RegistrationInfo(); |
| + requester_.reset(new CloudPrintRequester( |
| + base::MessageLoop::current()->message_loop_proxy(), |
| + this)); // Forget all old queries. |
| + |
| return PrivetHttpServer::REG_ERROR_OK; |
| } |
| @@ -306,6 +329,32 @@ PrivetHttpServer::RegistrationErrorStatus Printer::CheckCommonRegErrors( |
| return PrivetHttpServer::REG_ERROR_OK; |
| } |
| +void Printer::WaitUserConfirmation(base::Time valid_until) { |
| + if (base::Time::Now() > valid_until) { |
| + reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_TIMEOUT; |
| + LOG(INFO) << "Confirmation timeout reached."; |
| + return; |
| + } |
| + |
| + if (kbhit()) { |
| + int c = getche(); |
| + if (c == 'y' || c == 'Y') { |
| + reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; |
| + LOG(INFO) << "Registration confirmed by user."; |
| + } else { |
| + reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_DISCARDED; |
| + LOG(INFO) << "Registration discarded by user."; |
| + } |
| + return; |
| + } |
| + |
| + base::MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&Printer::WaitUserConfirmation, base::Unretained(this), |
| + valid_until), |
| + base::TimeDelta::FromMilliseconds(100)); |
| +} |
| + |
| std::string Printer::GenerateProxyId() const { |
| return "{" + base::GenerateGUID() +"}"; |
| } |
| @@ -404,3 +453,22 @@ bool Printer::LoadFromFile(const std::string& filename) { |
| return true; |
| } |
| +PrivetHttpServer::RegistrationErrorStatus |
| + Printer::ConfirmationToRegistrationError( |
| + RegistrationInfo::ConfirmationState state) { |
| + switch (state) { |
| + case RegistrationInfo::CONFIRMATION_PENDING: |
| + return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; |
| + case RegistrationInfo::CONFIRMATION_DISCARDED: |
| + return PrivetHttpServer::REG_ERROR_USER_CANCEL; |
| + case RegistrationInfo::CONFIRMATION_CONFIRMED: |
| + NOTREACHED(); |
| + return PrivetHttpServer::REG_ERROR_OK; |
| + case RegistrationInfo::CONFIRMATION_TIMEOUT: |
| + return PrivetHttpServer::REG_ERROR_CONFIRMATION_TIMEOUT; |
| + default: |
| + NOTREACHED(); |
| + return PrivetHttpServer::REG_ERROR_OK; |
| + } |
| +} |
| + |