Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Unified Diff: cloud_print/gcp20/prototype/printer.cc

Issue 22184007: GCP2.0 Device: Instant AccessToken update on AuthFailed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: cloud_print/gcp20/prototype/printer.cc
diff --git a/cloud_print/gcp20/prototype/printer.cc b/cloud_print/gcp20/prototype/printer.cc
index 5a252c05645529dedf7f3aada60c5dd42b9fbeac..05e4b3d97f94c2399d76e703870420a4970ab259 100644
--- a/cloud_print/gcp20/prototype/printer.cc
+++ b/cloud_print/gcp20/prototype/printer.cc
@@ -38,9 +38,9 @@ const char kPrinterDescription[] = "Printer emulator";
const char kUserConfirmationTitle[] = "Confirm registration: type 'y' if you "
"agree and any other to discard\n";
-const int64 kUserConfirmationTimeout = 30; // in seconds
-
-const uint32 kReconnectTimeout = 5; // in seconds
+const int kUserConfirmationTimeout = 30; // in seconds
+const int kRegistrationTimeout = 60; // in seconds
+const int kReconnectTimeout = 5; // in seconds
const double kTimeToNextAccessTokenUpdate = 0.8; // relatively to living time.
@@ -182,9 +182,8 @@ void Printer::Stop() {
}
void Printer::OnAuthError() {
- access_token_update_ = base::Time::Now();
- ChangeState(OFFLINE);
- // TODO(maksymb): Implement *instant* updating of access_token.
+ access_token_update_ = base::Time::UnixEpoch();
+ ReconnectIn(base::TimeDelta::FromSeconds(0));
}
std::string Printer::GetAccessToken() {
@@ -193,6 +192,14 @@ std::string Printer::GetAccessToken() {
PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationStart(
const std::string& user) {
+ CheckRegistrationExpiration();
+ RegistrationInfo::ConfirmationState conf_state = reg_info_.confirmation_state;
+ if (user != reg_info_.user)
+ if (conf_state == RegistrationInfo::CONFIRMATION_TIMEOUT ||
+ conf_state == RegistrationInfo::CONFIRMATION_DISCARDED) {
+ reg_info_ = RegistrationInfo();
+ }
+
PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user);
if (status != PrivetHttpServer::REG_ERROR_OK)
return status;
@@ -200,6 +207,8 @@ PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationStart(
if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED)
return PrivetHttpServer::REG_ERROR_INVALID_ACTION;
+ UpdateRegistrationExpiration();
+
reg_info_ = RegistrationInfo();
reg_info_.user = user;
reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_STARTED;
@@ -240,6 +249,8 @@ PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationGetClaimToken(
if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED)
return ConfirmationToRegistrationError(reg_info_.confirmation_state);
+ UpdateRegistrationExpiration();
+
// If reply wasn't received yet, reply with |device_busy| error.
if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_STARTED)
return PrivetHttpServer::REG_ERROR_DEVICE_BUSY;
@@ -266,6 +277,8 @@ PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationComplete(
return PrivetHttpServer::REG_ERROR_INVALID_ACTION;
}
+ UpdateRegistrationExpiration();
+
if (reg_info_.confirmation_state != RegistrationInfo::CONFIRMATION_CONFIRMED)
return ConfirmationToRegistrationError(reg_info_.confirmation_state);
@@ -287,6 +300,8 @@ PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationCancel(
if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED)
return PrivetHttpServer::REG_ERROR_INVALID_ACTION;
+ InvalidateRegistrationExpiration();
+
reg_info_ = RegistrationInfo();
requester_.reset(new CloudPrintRequester(GetTaskRunner(), this));
@@ -294,13 +309,15 @@ PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationCancel(
}
void Printer::GetRegistrationServerError(std::string* description) {
- DCHECK_EQ(reg_info_.state, RegistrationInfo::DEV_REG_REGISTRATION_ERROR) <<
- "Method shouldn't be called when not needed.";
+ DCHECK_EQ(reg_info_.state, RegistrationInfo::DEV_REG_REGISTRATION_ERROR)
+ << "Method shouldn't be called when not needed.";
*description = reg_info_.error_description;
}
void Printer::CreateInfo(PrivetHttpServer::DeviceInfo* info) {
+ CheckRegistrationExpiration();
+
// TODO(maksymb): Replace "text" with constants.
*info = PrivetHttpServer::DeviceInfo();
@@ -325,7 +342,8 @@ void Printer::CreateInfo(PrivetHttpServer::DeviceInfo* info) {
info->type.push_back("printer");
}
-bool Printer::IsRegistered() const {
+bool Printer::IsRegistered() {
+ CheckRegistrationExpiration();
return reg_info_.state == RegistrationInfo::DEV_REG_REGISTERED;
}
@@ -343,9 +361,11 @@ void Printer::OnRegistrationStartResponseParsed(
reg_info_.complete_invite_url = complete_invite_url;
}
-void Printer::OnGetAuthCodeResponseParsed(const std::string& refresh_token,
- const std::string& access_token,
- int access_token_expires_in_seconds) {
+void Printer::OnRegistrationFinished(const std::string& refresh_token,
+ const std::string& access_token,
+ int access_token_expires_in_seconds) {
+ InvalidateRegistrationExpiration();
+
reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED;
reg_info_.refresh_token = refresh_token;
RememberAccessToken(access_token, access_token_expires_in_seconds);
@@ -378,8 +398,7 @@ void Printer::OnRegistrationError(const std::string& description) {
LOG(ERROR) << "server_error: " << description;
// TODO(maksymb): Implement waiting after error and timeout of registration.
- reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_ERROR;
- reg_info_.error_description = description;
+ SetRegistrationError(description);
}
void Printer::OnNetworkError() {
@@ -539,8 +558,15 @@ void Printer::RememberAccessToken(const std::string& access_token,
SaveToFile();
}
+void Printer::SetRegistrationError(const std::string& description) {
+ DCHECK(!IsRegistered());
+ reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_ERROR;
+ reg_info_.error_description = description;
+}
+
PrivetHttpServer::RegistrationErrorStatus Printer::CheckCommonRegErrors(
- const std::string& user) const {
+ const std::string& user) {
+ CheckRegistrationExpiration();
DCHECK(!IsRegistered());
if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED &&
@@ -551,10 +577,14 @@ PrivetHttpServer::RegistrationErrorStatus Printer::CheckCommonRegErrors(
if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR)
return PrivetHttpServer::REG_ERROR_SERVER_ERROR;
+ DCHECK(connection_state_ == ONLINE);
+
return PrivetHttpServer::REG_ERROR_OK;
}
void Printer::WaitUserConfirmation(base::Time valid_until) {
+ // TODO(maksymb): Move to separate class.
+
if (base::Time::Now() > valid_until) {
reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_TIMEOUT;
LOG(INFO) << "Confirmation timeout reached.";
@@ -596,7 +626,7 @@ std::vector<std::string> Printer::CreateTxt() const {
return txt;
}
-void Printer::SaveToFile() const {
+void Printer::SaveToFile() {
base::FilePath file_path;
file_path = file_path.AppendASCII(
command_line_reader::ReadStatePath(kPrinterStatePathDefault));
@@ -720,9 +750,28 @@ void Printer::PostOnIdle() {
base::Bind(&Printer::OnIdle, AsWeakPtr()));
}
+void Printer::CheckRegistrationExpiration() {
+ if (!registration_expiration_.is_null() &&
+ registration_expiration_ < base::Time::Now()) {
+ reg_info_ = RegistrationInfo();
+ InvalidateRegistrationExpiration();
+ if (connection_state_ != ONLINE)
+ TryConnect();
+ }
+}
+
+void Printer::UpdateRegistrationExpiration() {
+ registration_expiration_ =
+ base::Time::Now() + base::TimeDelta::FromSeconds(kRegistrationTimeout);
+}
+
+void Printer::InvalidateRegistrationExpiration() {
+ registration_expiration_ = base::Time();
+}
+
PrivetHttpServer::RegistrationErrorStatus
- Printer::ConfirmationToRegistrationError(
- RegistrationInfo::ConfirmationState state) {
+Printer::ConfirmationToRegistrationError(
+ RegistrationInfo::ConfirmationState state) {
switch (state) {
case RegistrationInfo::CONFIRMATION_PENDING:
return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION;
@@ -756,6 +805,20 @@ std::string Printer::ConnectionStateToString(ConnectionState state) const {
}
}
+void Printer::ReconnectIn(const base::TimeDelta& delay) {
+ if (!IsRegistered()) {
+ 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
+ return;
+ }
+
+ requester_.reset();
+ xmpp_listener_.reset();
+ base::MessageLoop::current()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&Printer::TryConnect, AsWeakPtr()),
+ delay);
+}
+
bool Printer::ChangeState(ConnectionState new_state) {
if (connection_state_ == new_state)
return false;
@@ -768,27 +831,8 @@ bool Printer::ChangeState(ConnectionState new_state) {
dns_server_.UpdateMetadata(CreateTxt());
- switch (connection_state_) {
- case CONNECTING:
- break;
-
- case ONLINE:
- break;
-
- case OFFLINE:
- requester_.reset();
- xmpp_listener_.reset();
- base::MessageLoop::current()->PostDelayedTask(
- FROM_HERE,
- base::Bind(&Printer::TryConnect, AsWeakPtr()),
- base::TimeDelta::FromSeconds(kReconnectTimeout));
-
- case NOT_CONFIGURED:
- break;
-
- default:
- NOTREACHED();
- }
+ if (connection_state_ == OFFLINE)
+ ReconnectIn(base::TimeDelta::FromSeconds(kReconnectTimeout));
return true;
}

Powered by Google App Engine
This is Rietveld 408576698