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

Side by Side Diff: cloud_print/gcp20/prototype/printer.cc

Issue 22555003: GCP2.0 Device: Local settings workflow. (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 unified diff | Download patch
« no previous file with comments | « cloud_print/gcp20/prototype/printer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/file_util.h" 13 #include "base/file_util.h"
14 #include "base/guid.h" 14 #include "base/guid.h"
15 #include "base/json/json_reader.h" 15 #include "base/json/json_reader.h"
16 #include "base/json/json_writer.h" 16 #include "base/json/json_writer.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
19 #include "cloud_print/gcp20/prototype/command_line_reader.h" 19 #include "cloud_print/gcp20/prototype/command_line_reader.h"
20 #include "cloud_print/gcp20/prototype/local_settings.h"
20 #include "cloud_print/gcp20/prototype/service_parameters.h" 21 #include "cloud_print/gcp20/prototype/service_parameters.h"
21 #include "cloud_print/gcp20/prototype/special_io.h" 22 #include "cloud_print/gcp20/prototype/special_io.h"
22 #include "net/base/net_util.h" 23 #include "net/base/net_util.h"
23 #include "net/base/url_util.h" 24 #include "net/base/url_util.h"
24 25
25 const char kPrinterStatePathDefault[] = "printer_state.json"; 26 const char kPrinterStatePathDefault[] = "printer_state.json";
26 27
27 namespace { 28 namespace {
28 29
29 const uint16 kHttpPortDefault = 10101; 30 const uint16 kHttpPortDefault = 10101;
30 const uint32 kTtlDefault = 60*60; // in seconds 31 const uint32 kTtlDefault = 60*60; // in seconds
31 const int kXmppPingIntervalDefault = 5*60; // in seconds
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";
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 bool success = net::GetNetworkList(&interfaces); 84 bool success = net::GetNetworkList(&interfaces);
85 DCHECK(success); 85 DCHECK(success);
86 86
87 size_t expected_address_size = return_ipv6_number ? net::kIPv6AddressSize 87 size_t expected_address_size = return_ipv6_number ? net::kIPv6AddressSize
88 : net::kIPv4AddressSize; 88 : net::kIPv4AddressSize;
89 89
90 for (net::NetworkInterfaceList::iterator iter = interfaces.begin(); 90 for (net::NetworkInterfaceList::iterator iter = interfaces.begin();
91 iter != interfaces.end(); ++iter) { 91 iter != interfaces.end(); ++iter) {
92 if (iter->address.size() == expected_address_size && 92 if (iter->address.size() == expected_address_size &&
93 (interface_name.empty() || interface_name == iter->name)) { 93 (interface_name.empty() || interface_name == iter->name)) {
94 LOG(INFO) << net::IPAddressToString(iter->address);
95 return iter->address; 94 return iter->address;
96 } 95 }
97 } 96 }
98 97
99 return net::IPAddressNumber(); 98 return net::IPAddressNumber();
100 } 99 }
101 100
102 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() { 101 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() {
103 return base::MessageLoop::current()->message_loop_proxy(); 102 return base::MessageLoop::current()->message_loop_proxy();
104 } 103 }
105 104
106 } // namespace 105 } // namespace
107 106
108 using cloud_print_response_parser::Job; 107 using cloud_print_response_parser::Job;
109 108
110 Printer::RegistrationInfo::RegistrationInfo() 109 Printer::RegistrationInfo::RegistrationInfo()
111 : state(DEV_REG_UNREGISTERED), 110 : state(DEV_REG_UNREGISTERED),
112 confirmation_state(CONFIRMATION_PENDING) { 111 confirmation_state(CONFIRMATION_PENDING) {
113 } 112 }
114 113
115 Printer::RegistrationInfo::~RegistrationInfo() { 114 Printer::RegistrationInfo::~RegistrationInfo() {
116 } 115 }
117 116
118 Printer::Printer() 117 Printer::Printer()
119 : http_server_(this), 118 : http_server_(this),
120 connection_state_(OFFLINE), 119 connection_state_(OFFLINE),
121 on_idle_posted_(false), 120 on_idle_posted_(false),
122 pending_local_settings_check_(false), 121 pending_local_settings_check_(false),
123 pending_print_jobs_check_(false) { 122 pending_print_jobs_check_(false),
123 pending_deletion_(false) {
124 } 124 }
125 125
126 Printer::~Printer() { 126 Printer::~Printer() {
127 Stop(); 127 Stop();
128 } 128 }
129 129
130 bool Printer::Start() { 130 bool Printer::Start() {
131 if (IsRunning()) 131 if (IsRunning())
132 return true; 132 return true;
133 133
134 // TODO(maksymb): Add switch for command line to control interface name.
135 net::IPAddressNumber ip = GetLocalIp("", false);
136 if (ip.empty()) {
137 LOG(ERROR) << "No local IP found. Cannot start printer.";
138 return false;
139 }
140 VLOG(1) << "Local address: " << net::IPAddressToString(ip);
141
142 uint16 port = command_line_reader::ReadHttpPort(kHttpPortDefault);
143
144 // Starting HTTP server.
145 if (!http_server_.Start(port))
146 return false;
147
148 if (!LoadFromFile()) 134 if (!LoadFromFile())
149 reg_info_ = RegistrationInfo(); 135 reg_info_ = RegistrationInfo();
150 136
151 // Starting DNS-SD server. 137 if (local_settings_.local_discovery) {
152 std::string service_name_prefix = 138 if (!StartHttpServer())
153 command_line_reader::ReadServiceNamePrefix(kServiceNamePrefixDefault); 139 return false;
154 std::string service_domain_name = 140
155 command_line_reader::ReadDomainName(kServiceDomainNameDefault); 141 if (!StartDnsServer()) {
156 if (!dns_server_.Start( 142 http_server_.Shutdown();
157 ServiceParameters(kServiceType, service_name_prefix, service_domain_name, 143 return false;
158 ip, port), 144 }
159 command_line_reader::ReadTtl(kTtlDefault),
160 CreateTxt())) {
161 http_server_.Shutdown();
162 return false;
163 } 145 }
164 146
165 print_job_handler_.reset(new PrintJobHandler); 147 print_job_handler_.reset(new PrintJobHandler);
166 xtoken_ = XPrivetToken(); 148 xtoken_ = XPrivetToken();
167 starttime_ = base::Time::Now(); 149 starttime_ = base::Time::Now();
168 150
169 TryConnect(); 151 TryConnect();
170 return true; 152 return true;
171 } 153 }
172 154
173 bool Printer::IsRunning() const { 155 bool Printer::IsRunning() const {
174 return print_job_handler_; 156 return print_job_handler_;
175 } 157 }
176 158
177 void Printer::Stop() { 159 void Printer::Stop() {
160 if (!IsRunning())
161 return;
178 dns_server_.Shutdown(); 162 dns_server_.Shutdown();
179 http_server_.Shutdown(); 163 http_server_.Shutdown();
180 requester_.reset(); 164 requester_.reset();
181 print_job_handler_.reset(); 165 print_job_handler_.reset();
182 xmpp_listener_.reset(); 166 xmpp_listener_.reset();
183 } 167 }
184 168
185 void Printer::OnAuthError() { 169 void Printer::OnAuthError() {
186 LOG(ERROR) << "Auth error occurred"; 170 LOG(ERROR) << "Auth error occurred";
187 access_token_update_ = base::Time(); 171 access_token_update_ = base::Time();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 LOG(INFO) << "Registration confirmed by default."; 205 LOG(INFO) << "Registration confirmed by default.";
222 } else { 206 } else {
223 printf("%s", kUserConfirmationTitle); 207 printf("%s", kUserConfirmationTitle);
224 base::Time valid_until = base::Time::Now() + 208 base::Time valid_until = base::Time::Now() +
225 base::TimeDelta::FromSeconds(kUserConfirmationTimeout); 209 base::TimeDelta::FromSeconds(kUserConfirmationTimeout);
226 base::MessageLoop::current()->PostTask( 210 base::MessageLoop::current()->PostTask(
227 FROM_HERE, 211 FROM_HERE,
228 base::Bind(&Printer::WaitUserConfirmation, AsWeakPtr(), valid_until)); 212 base::Bind(&Printer::WaitUserConfirmation, AsWeakPtr(), valid_until));
229 } 213 }
230 214
231 requester_->StartRegistration(GenerateProxyId(), kPrinterName, user, kCdd); 215 requester_->StartRegistration(GenerateProxyId(), kPrinterName, user,
216 local_settings_, kCdd);
232 217
233 return PrivetHttpServer::REG_ERROR_OK; 218 return PrivetHttpServer::REG_ERROR_OK;
234 } 219 }
235 220
236 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationGetClaimToken( 221 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationGetClaimToken(
237 const std::string& user, 222 const std::string& user,
238 std::string* token, 223 std::string* token,
239 std::string* claim_url) { 224 std::string* claim_url) {
240 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); 225 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user);
241 if (status != PrivetHttpServer::REG_ERROR_OK) 226 if (status != PrivetHttpServer::REG_ERROR_OK)
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 base::StringPrintf("%s.%s", job.create_time.c_str(), job.job_id.c_str()), 424 base::StringPrintf("%s.%s", job.create_time.c_str(), job.job_id.c_str()),
440 job.title); 425 job.title);
441 requester_->SendPrintJobDone(job.job_id); 426 requester_->SendPrintJobDone(job.job_id);
442 } 427 }
443 428
444 void Printer::OnPrintJobDone() { 429 void Printer::OnPrintJobDone() {
445 VLOG(3) << "Function: " << __FUNCTION__; 430 VLOG(3) << "Function: " << __FUNCTION__;
446 PostOnIdle(); 431 PostOnIdle();
447 } 432 }
448 433
434 void Printer::OnLocalSettingsReceived(LocalSettings::State state,
435 const LocalSettings& settings) {
436 pending_local_settings_check_ = false;
437 switch (state) {
438 case LocalSettings::CURRENT:
439 LOG(INFO) << "No new local settings";
440 PostOnIdle();
441 break;
442 case LocalSettings::PENDING:
443 LOG(INFO) << "New local settings were received";
444 ApplyLocalSettings(settings);
445 break;
446 case LocalSettings::PRINTER_DELETED:
447 LOG(WARNING) << "Printer was deleted on server";
448 pending_deletion_ = true;
449 PostOnIdle();
450 break;
451
452 default:
453 NOTREACHED();
454 }
455 }
456
457 void Printer::OnLocalSettingsUpdated() {
458 PostOnIdle();
459 }
460
449 void Printer::OnXmppConnected() { 461 void Printer::OnXmppConnected() {
450 pending_local_settings_check_ = true; 462 pending_local_settings_check_ = true;
451 pending_print_jobs_check_ = true; 463 pending_print_jobs_check_ = true;
452 ChangeState(ONLINE); 464 ChangeState(ONLINE);
453 PostOnIdle(); 465 PostOnIdle();
454 } 466 }
455 467
456 void Printer::OnXmppAuthError() { 468 void Printer::OnXmppAuthError() {
457 OnAuthError(); 469 OnAuthError();
458 } 470 }
459 471
460 void Printer::OnXmppNetworkError() { 472 void Printer::OnXmppNetworkError() {
461 FallOffline(false); 473 FallOffline(false);
462 } 474 }
463 475
464 void Printer::OnXmppNewPrintJob(const std::string& device_id) { 476 void Printer::OnXmppNewPrintJob(const std::string& device_id) {
465 DCHECK_EQ(reg_info_.device_id, device_id) << "Data should contain printer_id"; 477 DCHECK_EQ(reg_info_.device_id, device_id) << "Data should contain printer_id";
466 pending_print_jobs_check_ = true; 478 pending_print_jobs_check_ = true;
467 } 479 }
468 480
469 void Printer::OnXmppNewLocalSettings(const std::string& device_id) { 481 void Printer::OnXmppNewLocalSettings(const std::string& device_id) {
470 DCHECK_EQ(reg_info_.device_id, device_id) << "Data should contain printer_id"; 482 DCHECK_EQ(reg_info_.device_id, device_id) << "Data should contain printer_id";
471 NOTIMPLEMENTED(); 483 pending_local_settings_check_ = true;
472 } 484 }
473 485
474 void Printer::OnXmppDeleteNotification(const std::string& device_id) { 486 void Printer::OnXmppDeleteNotification(const std::string& device_id) {
475 DCHECK_EQ(reg_info_.device_id, device_id) << "Data should contain printer_id"; 487 DCHECK_EQ(reg_info_.device_id, device_id) << "Data should contain printer_id";
476 NOTIMPLEMENTED(); 488 pending_deletion_ = true;
477 } 489 }
478 490
479 void Printer::TryConnect() { 491 void Printer::TryConnect() {
480 VLOG(3) << "Function: " << __FUNCTION__; 492 VLOG(3) << "Function: " << __FUNCTION__;
481 493
482 ChangeState(CONNECTING); 494 ChangeState(CONNECTING);
483 if (!requester_) 495 if (!requester_)
484 requester_.reset(new CloudPrintRequester(GetTaskRunner(), this)); 496 requester_.reset(new CloudPrintRequester(GetTaskRunner(), this));
485 497
486 if (IsRegistered()) { 498 if (IsRegistered()) {
487 if (access_token_update_ < base::Time::Now()) { 499 if (access_token_update_ < base::Time::Now()) {
488 requester_->UpdateAccesstoken(reg_info_.refresh_token); 500 requester_->UpdateAccesstoken(reg_info_.refresh_token);
489 } else { 501 } else {
490 ConnectXmpp(); 502 ConnectXmpp();
491 } 503 }
492 } else { 504 } else {
493 // TODO(maksymb): Ping google.com to check connection state. 505 // TODO(maksymb): Ping google.com to check connection state.
494 ChangeState(ONLINE); 506 ChangeState(ONLINE);
495 } 507 }
496 } 508 }
497 509
498 void Printer::ConnectXmpp() { 510 void Printer::ConnectXmpp() {
499 xmpp_listener_.reset( 511 xmpp_listener_.reset(
500 new CloudPrintXmppListener(reg_info_.xmpp_jid, kXmppPingIntervalDefault, 512 new CloudPrintXmppListener(reg_info_.xmpp_jid,
513 local_settings_.xmpp_timeout_value,
501 GetTaskRunner(), this)); 514 GetTaskRunner(), this));
502 xmpp_listener_->Connect(access_token_); 515 xmpp_listener_->Connect(access_token_);
503 } 516 }
504 517
505 void Printer::OnIdle() { 518 void Printer::OnIdle() {
506 DCHECK(IsRegistered()); 519 DCHECK(IsRegistered());
507 DCHECK(on_idle_posted_) << "Instant call is not allowed"; 520 DCHECK(on_idle_posted_) << "Instant call is not allowed";
508 on_idle_posted_ = false; 521 on_idle_posted_ = false;
509 522
510 if (connection_state_ != ONLINE) 523 if (connection_state_ != ONLINE)
511 return; 524 return;
512 525
526 if (pending_deletion_) {
527 OnPrinterDeleted();
528 return;
529 }
530
513 if (access_token_update_ < base::Time::Now()) { 531 if (access_token_update_ < base::Time::Now()) {
514 requester_->UpdateAccesstoken(reg_info_.refresh_token); 532 requester_->UpdateAccesstoken(reg_info_.refresh_token);
515 return; 533 return;
516 } 534 }
517 535
518 // TODO(maksymb): Check if privet-accesstoken was requested. 536 // TODO(maksymb): Check if privet-accesstoken was requested.
519 537
520 // TODO(maksymb): Check if local-printing was requested.
521
522 if (pending_local_settings_check_) { 538 if (pending_local_settings_check_) {
523 GetLocalSettings(); 539 GetLocalSettings();
524 return; 540 return;
525 } 541 }
526 542
527 if (pending_print_jobs_check_) { 543 if (pending_print_jobs_check_) {
528 FetchPrintJobs(); 544 FetchPrintJobs();
529 return; 545 return;
530 } 546 }
531 547
532 base::MessageLoop::current()->PostDelayedTask( 548 base::MessageLoop::current()->PostDelayedTask(
533 FROM_HERE, 549 FROM_HERE,
534 base::Bind(&Printer::PostOnIdle, AsWeakPtr()), 550 base::Bind(&Printer::PostOnIdle, AsWeakPtr()),
535 base::TimeDelta::FromMilliseconds(1000)); 551 base::TimeDelta::FromMilliseconds(1000));
536 } 552 }
537 553
538 void Printer::GetLocalSettings() {
539 DCHECK(IsRegistered());
540
541 pending_local_settings_check_ = false;
542 PostOnIdle();
543 }
544
545 void Printer::FetchPrintJobs() { 554 void Printer::FetchPrintJobs() {
546 VLOG(3) << "Function: " << __FUNCTION__; 555 VLOG(3) << "Function: " << __FUNCTION__;
547
548 DCHECK(IsRegistered()); 556 DCHECK(IsRegistered());
549 requester_->FetchPrintJobs(reg_info_.device_id); 557 requester_->FetchPrintJobs(reg_info_.device_id);
550 } 558 }
551 559
560 void Printer::GetLocalSettings() {
561 VLOG(3) << "Function: " << __FUNCTION__;
562 DCHECK(IsRegistered());
563 requester_->RequestLocalSettings(reg_info_.device_id);
564 }
565
566 void Printer::ApplyLocalSettings(const LocalSettings& settings) {
567 local_settings_ = settings;
568 SaveToFile();
569
570 if (local_settings_.local_discovery) {
571 StartDnsServer();
572 StartHttpServer();
573 } else {
574 dns_server_.Shutdown();
575 http_server_.Shutdown();
576 }
577
578 xmpp_listener_->set_ping_interval(local_settings_.xmpp_timeout_value);
579
580 requester_->SendLocalSettings(reg_info_.device_id, local_settings_);
581 }
582
583 void Printer::OnPrinterDeleted() {
584 pending_deletion_ = false;
585
586 reg_info_ = RegistrationInfo();
587 access_token_.clear();
588 access_token_update_ = base::Time();
589 local_settings_ = LocalSettings();
590
591 SaveToFile();
592 Stop();
593 Start();
594 }
595
552 void Printer::RememberAccessToken(const std::string& access_token, 596 void Printer::RememberAccessToken(const std::string& access_token,
553 int expires_in_seconds) { 597 int expires_in_seconds) {
554 using base::Time; 598 using base::Time;
555 using base::TimeDelta; 599 using base::TimeDelta;
556 access_token_ = access_token; 600 access_token_ = access_token;
557 int64 time_to_update = static_cast<int64>(expires_in_seconds * 601 int64 time_to_update = static_cast<int64>(expires_in_seconds *
558 kTimeToNextAccessTokenUpdate); 602 kTimeToNextAccessTokenUpdate);
559 access_token_update_ = Time::Now() + TimeDelta::FromSeconds(time_to_update); 603 access_token_update_ = Time::Now() + TimeDelta::FromSeconds(time_to_update);
560 VLOG(1) << "Current access_token: " << access_token; 604 VLOG(1) << "Current access_token: " << access_token;
561 SaveToFile(); 605 SaveToFile();
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 // TODO(maksymb): Get rid of in-place constants. 684 // TODO(maksymb): Get rid of in-place constants.
641 if (IsRegistered()) { 685 if (IsRegistered()) {
642 json.SetBoolean("registered", true); 686 json.SetBoolean("registered", true);
643 json.SetString("user", reg_info_.user); 687 json.SetString("user", reg_info_.user);
644 json.SetString("device_id", reg_info_.device_id); 688 json.SetString("device_id", reg_info_.device_id);
645 json.SetString("refresh_token", reg_info_.refresh_token); 689 json.SetString("refresh_token", reg_info_.refresh_token);
646 json.SetString("xmpp_jid", reg_info_.xmpp_jid); 690 json.SetString("xmpp_jid", reg_info_.xmpp_jid);
647 json.SetString("access_token", access_token_); 691 json.SetString("access_token", access_token_);
648 json.SetInteger("access_token_update", 692 json.SetInteger("access_token_update",
649 static_cast<int>(access_token_update_.ToTimeT())); 693 static_cast<int>(access_token_update_.ToTimeT()));
694
695 scoped_ptr<base::DictionaryValue> local_settings(new DictionaryValue);
696 local_settings->SetBoolean("local_discovery",
697 local_settings_.local_discovery);
698 local_settings->SetBoolean("access_token_enabled",
699 local_settings_.access_token_enabled);
700 local_settings->SetBoolean("printer/local_printing_enabled",
701 local_settings_.local_printing_enabled);
702 local_settings->SetInteger("xmpp_timeout_value",
703 local_settings_.xmpp_timeout_value);
704 json.Set("local_settings", local_settings.release());
650 } else { 705 } else {
651 json.SetBoolean("registered", false); 706 json.SetBoolean("registered", false);
652 } 707 }
653 708
654 std::string json_str; 709 std::string json_str;
655 base::JSONWriter::WriteWithOptions(&json, 710 base::JSONWriter::WriteWithOptions(&json,
656 base::JSONWriter::OPTIONS_PRETTY_PRINT, 711 base::JSONWriter::OPTIONS_PRETTY_PRINT,
657 &json_str); 712 &json_str);
658 if (!file_util::WriteFile(file_path, json_str.data(), 713 if (!file_util::WriteFile(file_path, json_str.data(),
659 static_cast<int>(json_str.size()))) { 714 static_cast<int>(json_str.size()))) {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 LOG(ERROR) << "Cannot parse |access_token|."; 780 LOG(ERROR) << "Cannot parse |access_token|.";
726 return false; 781 return false;
727 } 782 }
728 783
729 int access_token_update; 784 int access_token_update;
730 if (!json->GetInteger("access_token_update", &access_token_update)) { 785 if (!json->GetInteger("access_token_update", &access_token_update)) {
731 LOG(ERROR) << "Cannot parse |access_token_update|."; 786 LOG(ERROR) << "Cannot parse |access_token_update|.";
732 return false; 787 return false;
733 } 788 }
734 789
790 LocalSettings local_settings;
791 base::DictionaryValue* settings_dict;
792 if (!json->GetDictionary("local_settings", &settings_dict)) {
793 LOG(ERROR) << "Cannot read |local_settings|. Reset to default.";
794 } else {
795 if (!settings_dict->GetBoolean("local_discovery",
796 &local_settings.local_discovery) ||
797 !settings_dict->GetBoolean("access_token_enabled",
798 &local_settings.access_token_enabled) ||
799 !settings_dict->GetBoolean("printer/local_printing_enabled",
800 &local_settings.local_printing_enabled) ||
801 !settings_dict->GetInteger("xmpp_timeout_value",
802 &local_settings.xmpp_timeout_value)) {
803 LOG(ERROR) << "Cannot parse |local_settings|. Reset to default.";
804 local_settings = LocalSettings();
805 }
806 }
807
735 reg_info_ = RegistrationInfo(); 808 reg_info_ = RegistrationInfo();
736 reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED; 809 reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED;
737 reg_info_.user = user; 810 reg_info_.user = user;
738 reg_info_.device_id = device_id; 811 reg_info_.device_id = device_id;
739 reg_info_.refresh_token = refresh_token; 812 reg_info_.refresh_token = refresh_token;
740 reg_info_.xmpp_jid = xmpp_jid; 813 reg_info_.xmpp_jid = xmpp_jid;
741 using base::Time;
742 access_token_ = access_token; 814 access_token_ = access_token;
743 access_token_update_ = Time::FromTimeT(access_token_update); 815 access_token_update_ = base::Time::FromTimeT(access_token_update);
816 local_settings_ = local_settings;
744 817
745 return true; 818 return true;
746 } 819 }
747 820
748 void Printer::PostOnIdle() { 821 void Printer::PostOnIdle() {
749 VLOG(3) << "Function: " << __FUNCTION__; 822 VLOG(3) << "Function: " << __FUNCTION__;
750 DCHECK(!on_idle_posted_) << "Only one instance can be posted."; 823 DCHECK(!on_idle_posted_) << "Only one instance can be posted.";
751 on_idle_posted_ = true; 824 on_idle_posted_ = true;
752 825
753 base::MessageLoop::current()->PostTask( 826 base::MessageLoop::current()->PostTask(
(...skipping 13 matching lines...) Expand all
767 840
768 void Printer::UpdateRegistrationExpiration() { 841 void Printer::UpdateRegistrationExpiration() {
769 registration_expiration_ = 842 registration_expiration_ =
770 base::Time::Now() + base::TimeDelta::FromSeconds(kRegistrationTimeout); 843 base::Time::Now() + base::TimeDelta::FromSeconds(kRegistrationTimeout);
771 } 844 }
772 845
773 void Printer::InvalidateRegistrationExpiration() { 846 void Printer::InvalidateRegistrationExpiration() {
774 registration_expiration_ = base::Time(); 847 registration_expiration_ = base::Time();
775 } 848 }
776 849
850 bool Printer::StartHttpServer() {
851 DCHECK(local_settings_.local_discovery);
852 using command_line_reader::ReadHttpPort;
853 return http_server_.Start(ReadHttpPort(kHttpPortDefault));
854 }
855
856 bool Printer::StartDnsServer() {
857 DCHECK(local_settings_.local_discovery);
858
859 // TODO(maksymb): Add switch for command line to control interface name.
860 net::IPAddressNumber ip = GetLocalIp("", false);
861 if (ip.empty()) {
862 LOG(ERROR) << "No local IP found. Cannot start printer.";
863 return false;
864 }
865 VLOG(1) << "Local address: " << net::IPAddressToString(ip);
866
867 uint16 port = command_line_reader::ReadHttpPort(kHttpPortDefault);
868
869 std::string service_name_prefix =
870 command_line_reader::ReadServiceNamePrefix(kServiceNamePrefixDefault);
871 std::string service_domain_name =
872 command_line_reader::ReadDomainName(kServiceDomainNameDefault);
873
874 ServiceParameters params(kServiceType, service_name_prefix,
875 service_domain_name, ip, port);
876
877 return dns_server_.Start(params,
878 command_line_reader::ReadTtl(kTtlDefault),
879 CreateTxt());
880 }
881
777 PrivetHttpServer::RegistrationErrorStatus 882 PrivetHttpServer::RegistrationErrorStatus
778 Printer::ConfirmationToRegistrationError( 883 Printer::ConfirmationToRegistrationError(
779 RegistrationInfo::ConfirmationState state) { 884 RegistrationInfo::ConfirmationState state) {
780 switch (state) { 885 switch (state) {
781 case RegistrationInfo::CONFIRMATION_PENDING: 886 case RegistrationInfo::CONFIRMATION_PENDING:
782 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; 887 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION;
783 case RegistrationInfo::CONFIRMATION_DISCARDED: 888 case RegistrationInfo::CONFIRMATION_DISCARDED:
784 return PrivetHttpServer::REG_ERROR_USER_CANCEL; 889 return PrivetHttpServer::REG_ERROR_USER_CANCEL;
785 case RegistrationInfo::CONFIRMATION_CONFIRMED: 890 case RegistrationInfo::CONFIRMATION_CONFIRMED:
786 NOTREACHED(); 891 NOTREACHED();
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
840 dns_server_.UpdateMetadata(CreateTxt()); 945 dns_server_.UpdateMetadata(CreateTxt());
841 946
842 if (connection_state_ == OFFLINE) { 947 if (connection_state_ == OFFLINE) {
843 requester_.reset(); 948 requester_.reset();
844 xmpp_listener_.reset(); 949 xmpp_listener_.reset();
845 } 950 }
846 951
847 return true; 952 return true;
848 } 953 }
849 954
OLDNEW
« no previous file with comments | « cloud_print/gcp20/prototype/printer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698