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::UnixEpoch(); |
Vitaly Buka (NO REVIEWS)
2013/08/15 17:48:06
Why not now?
maksymb
2013/08/15 18:39:13
UnixEpoch() is definitely in the past, Now() is on
Vitaly Buka (NO REVIEWS)
2013/08/15 20:17:06
= base::Time::Time();
On 2013/08/15 18:39:13, mak
maksymb
2013/08/15 20:23:00
Done.
| |
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 DCHECK(connection_state_ == ONLINE); | |
548 | 575 |
549 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED && | 576 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED && |
550 user != reg_info_.user) { | 577 user != reg_info_.user) { |
551 return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; | 578 return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; |
552 } | 579 } |
553 | 580 |
554 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR) | 581 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR) |
555 return PrivetHttpServer::REG_ERROR_SERVER_ERROR; | 582 return PrivetHttpServer::REG_ERROR_SERVER_ERROR; |
556 | 583 |
584 DCHECK(connection_state_ == ONLINE); | |
585 | |
557 return PrivetHttpServer::REG_ERROR_OK; | 586 return PrivetHttpServer::REG_ERROR_OK; |
558 } | 587 } |
559 | 588 |
560 void Printer::WaitUserConfirmation(base::Time valid_until) { | 589 void Printer::WaitUserConfirmation(base::Time valid_until) { |
590 // TODO(maksymb): Move to separate class. | |
591 | |
561 if (base::Time::Now() > valid_until) { | 592 if (base::Time::Now() > valid_until) { |
562 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_TIMEOUT; | 593 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_TIMEOUT; |
563 LOG(INFO) << "Confirmation timeout reached."; | 594 LOG(INFO) << "Confirmation timeout reached."; |
564 return; | 595 return; |
565 } | 596 } |
566 | 597 |
567 if (_kbhit()) { | 598 if (_kbhit()) { |
568 int c = _getche(); | 599 int c = _getche(); |
569 if (c == 'y' || c == 'Y') { | 600 if (c == 'y' || c == 'Y') { |
570 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; | 601 reg_info_.confirmation_state = RegistrationInfo::CONFIRMATION_CONFIRMED; |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
716 void Printer::PostOnIdle() { | 747 void Printer::PostOnIdle() { |
717 VLOG(3) << "Function: " << __FUNCTION__; | 748 VLOG(3) << "Function: " << __FUNCTION__; |
718 DCHECK(!on_idle_posted_) << "Only one instance can be posted."; | 749 DCHECK(!on_idle_posted_) << "Only one instance can be posted."; |
719 on_idle_posted_ = true; | 750 on_idle_posted_ = true; |
720 | 751 |
721 base::MessageLoop::current()->PostTask( | 752 base::MessageLoop::current()->PostTask( |
722 FROM_HERE, | 753 FROM_HERE, |
723 base::Bind(&Printer::OnIdle, AsWeakPtr())); | 754 base::Bind(&Printer::OnIdle, AsWeakPtr())); |
724 } | 755 } |
725 | 756 |
757 void Printer::CheckRegistrationExpiration() { | |
758 if (!registration_expiration_.is_null() && | |
759 registration_expiration_ < base::Time::Now()) { | |
760 reg_info_ = RegistrationInfo(); | |
761 InvalidateRegistrationExpiration(); | |
762 if (connection_state_ != ONLINE) | |
763 TryConnect(); | |
764 } | |
765 } | |
766 | |
767 void Printer::UpdateRegistrationExpiration() { | |
768 registration_expiration_ = | |
769 base::Time::Now() + base::TimeDelta::FromSeconds(kRegistrationTimeout); | |
770 } | |
771 | |
772 void Printer::InvalidateRegistrationExpiration() { | |
773 registration_expiration_ = base::Time(); | |
774 } | |
775 | |
726 PrivetHttpServer::RegistrationErrorStatus | 776 PrivetHttpServer::RegistrationErrorStatus |
727 Printer::ConfirmationToRegistrationError( | 777 Printer::ConfirmationToRegistrationError( |
728 RegistrationInfo::ConfirmationState state) { | 778 RegistrationInfo::ConfirmationState state) { |
729 switch (state) { | 779 switch (state) { |
730 case RegistrationInfo::CONFIRMATION_PENDING: | 780 case RegistrationInfo::CONFIRMATION_PENDING: |
731 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; | 781 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; |
732 case RegistrationInfo::CONFIRMATION_DISCARDED: | 782 case RegistrationInfo::CONFIRMATION_DISCARDED: |
733 return PrivetHttpServer::REG_ERROR_USER_CANCEL; | 783 return PrivetHttpServer::REG_ERROR_USER_CANCEL; |
734 case RegistrationInfo::CONFIRMATION_CONFIRMED: | 784 case RegistrationInfo::CONFIRMATION_CONFIRMED: |
735 NOTREACHED(); | 785 NOTREACHED(); |
736 return PrivetHttpServer::REG_ERROR_OK; | 786 return PrivetHttpServer::REG_ERROR_OK; |
737 case RegistrationInfo::CONFIRMATION_TIMEOUT: | 787 case RegistrationInfo::CONFIRMATION_TIMEOUT: |
738 return PrivetHttpServer::REG_ERROR_CONFIRMATION_TIMEOUT; | 788 return PrivetHttpServer::REG_ERROR_CONFIRMATION_TIMEOUT; |
(...skipping 13 matching lines...) Expand all Loading... | |
752 return "connecting"; | 802 return "connecting"; |
753 case NOT_CONFIGURED: | 803 case NOT_CONFIGURED: |
754 return "not-configured"; | 804 return "not-configured"; |
755 | 805 |
756 default: | 806 default: |
757 NOTREACHED(); | 807 NOTREACHED(); |
758 return ""; | 808 return ""; |
759 } | 809 } |
760 } | 810 } |
761 | 811 |
812 void Printer::FallOffline(bool instant_reconnect) { | |
Vitaly Buka (NO REVIEWS)
2013/08/15 17:48:06
I guess you can remove argument and use TryConnect
maksymb
2013/08/15 18:39:13
1. In this case ChangeState(OFFLINE) won't be call
Vitaly Buka (NO REVIEWS)
2013/08/15 20:17:06
It won't be called anyway
DCHECK is debug only.
O
maksymb
2013/08/15 20:23:00
Moved outside of DCHECK.
| |
813 DCHECK(ChangeState(OFFLINE)); | |
814 | |
815 if (!IsRegistered()) { | |
816 SetRegistrationError("Cannot access server during registration process"); | |
817 TryConnect(); // For registration errors reconnection is instant, because | |
818 // we don't need to do anything when Printer will become | |
819 // connected: we will be waiting for new HTTP requests. | |
820 return; | |
821 } | |
822 | |
823 if (instant_reconnect) { | |
824 TryConnect(); | |
825 } else { | |
826 base::MessageLoop::current()->PostDelayedTask( | |
827 FROM_HERE, | |
828 base::Bind(&Printer::TryConnect, AsWeakPtr()), | |
829 base::TimeDelta::FromSeconds(kReconnectTimeout)); | |
Vitaly Buka (NO REVIEWS)
2013/08/15 17:48:06
Fix alignment
| |
830 } | |
831 } | |
832 | |
762 bool Printer::ChangeState(ConnectionState new_state) { | 833 bool Printer::ChangeState(ConnectionState new_state) { |
763 if (connection_state_ == new_state) | 834 if (connection_state_ == new_state) |
764 return false; | 835 return false; |
765 | 836 |
766 connection_state_ = new_state; | 837 connection_state_ = new_state; |
767 LOG(INFO) << base::StringPrintf( | 838 LOG(INFO) << base::StringPrintf( |
768 "Printer is now %s (%s)", | 839 "Printer is now %s (%s)", |
769 ConnectionStateToString(connection_state_).c_str(), | 840 ConnectionStateToString(connection_state_).c_str(), |
770 IsRegistered() ? "registered" : "unregistered"); | 841 IsRegistered() ? "registered" : "unregistered"); |
771 | 842 |
772 dns_server_.UpdateMetadata(CreateTxt()); | 843 dns_server_.UpdateMetadata(CreateTxt()); |
773 | 844 |
774 switch (connection_state_) { | 845 if (connection_state_ == OFFLINE) { |
775 case CONNECTING: | 846 requester_.reset(); |
776 break; | 847 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 } | 848 } |
795 | 849 |
796 return true; | 850 return true; |
797 } | 851 } |
798 | 852 |
OLD | NEW |