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 |
| 11 #include "base/bind.h" |
11 #include "base/command_line.h" | 12 #include "base/command_line.h" |
12 #include "base/file_util.h" | 13 #include "base/file_util.h" |
13 #include "base/guid.h" | 14 #include "base/guid.h" |
14 #include "base/json/json_reader.h" | 15 #include "base/json/json_reader.h" |
15 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/stringprintf.h" |
17 #include "cloud_print/gcp20/prototype/command_line_reader.h" | 19 #include "cloud_print/gcp20/prototype/command_line_reader.h" |
18 #include "cloud_print/gcp20/prototype/service_parameters.h" | 20 #include "cloud_print/gcp20/prototype/service_parameters.h" |
19 #include "cloud_print/gcp20/prototype/special_io.h" | 21 #include "cloud_print/gcp20/prototype/special_io.h" |
20 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
21 #include "net/base/url_util.h" | 23 #include "net/base/url_util.h" |
22 | 24 |
23 const base::FilePath::CharType kPrinterStatePath[] = | 25 const base::FilePath::CharType kPrinterStatePath[] = |
24 FILE_PATH_LITERAL("printer_state.json"); | 26 FILE_PATH_LITERAL("printer_state.json"); |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 const char kServiceType[] = "_privet._tcp.local"; | 30 const char kServiceType[] = "_privet._tcp.local"; |
29 const char kServiceNamePrefix[] = "first_gcp20_device"; | 31 const char kServiceNamePrefix[] = "first_gcp20_device"; |
30 const char kServiceDomainName[] = "my-privet-device.local"; | 32 const char kServiceDomainName[] = "my-privet-device.local"; |
31 | 33 |
32 const char kPrinterName[] = "Google GCP2.0 Prototype"; | 34 const char kPrinterName[] = "Google GCP2.0 Prototype"; |
33 const char kPrinterDescription[] = "Printer emulator"; | 35 const char kPrinterDescription[] = "Printer emulator"; |
34 | 36 |
35 const char kUserConfirmationTitle[] = "Confirm registration: type 'y' if you " | 37 const char kUserConfirmationTitle[] = "Confirm registration: type 'y' if you " |
36 "agree and any other to discard\n"; | 38 "agree and any other to discard\n"; |
37 const int64 kUserConfirmationTimeout = 30; // in seconds | 39 const int64 kUserConfirmationTimeout = 30; // in seconds |
38 | 40 |
| 41 const uint32 kReconnectTimeout = 5; // in seconds |
| 42 const uint32 kPrintJobsTimeout = 10; // in seconds |
| 43 |
39 const char kCdd[] = | 44 const char kCdd[] = |
40 "{\n" | 45 "{\n" |
41 " 'version': '1.0',\n" | 46 " 'version': '1.0',\n" |
42 " 'printer': {\n" | 47 " 'printer': {\n" |
43 " 'vendor_capability': [\n" | 48 " 'vendor_capability': [\n" |
44 " {\n" | 49 " {\n" |
45 " 'id': 'psk:MediaType',\n" | 50 " 'id': 'psk:MediaType',\n" |
46 " 'display_name': 'Media Type',\n" | 51 " 'display_name': 'Media Type',\n" |
47 " 'type': 'SELECT',\n" | 52 " 'type': 'SELECT',\n" |
48 " 'select_cap': {\n" | 53 " 'select_cap': {\n" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 LOG(INFO) << net::IPAddressToString(iter->address); | 90 LOG(INFO) << net::IPAddressToString(iter->address); |
86 return iter->address; | 91 return iter->address; |
87 } | 92 } |
88 } | 93 } |
89 | 94 |
90 return net::IPAddressNumber(); | 95 return net::IPAddressNumber(); |
91 } | 96 } |
92 | 97 |
93 } // namespace | 98 } // namespace |
94 | 99 |
| 100 using cloud_print_response_parser::Job; |
| 101 |
95 Printer::RegistrationInfo::RegistrationInfo() | 102 Printer::RegistrationInfo::RegistrationInfo() |
96 : state(DEV_REG_UNREGISTERED), | 103 : state(DEV_REG_UNREGISTERED), |
97 confirmation_state(CONFIRMATION_PENDING) { | 104 confirmation_state(CONFIRMATION_PENDING) { |
98 } | 105 } |
99 | 106 |
100 Printer::RegistrationInfo::~RegistrationInfo() { | 107 Printer::RegistrationInfo::~RegistrationInfo() { |
101 } | 108 } |
102 | 109 |
103 Printer::Printer() : http_server_(this) { | 110 Printer::Printer() : http_server_(this), connection_state_(OFFLINE) { |
104 } | 111 } |
105 | 112 |
106 Printer::~Printer() { | 113 Printer::~Printer() { |
107 Stop(); | 114 Stop(); |
108 } | 115 } |
109 | 116 |
110 bool Printer::Start() { | 117 bool Printer::Start() { |
111 if (IsOnline()) | 118 if (IsOnline()) |
112 return true; | 119 return true; |
113 | 120 |
(...skipping 26 matching lines...) Expand all Loading... |
140 | 147 |
141 // Creating Cloud Requester. | 148 // Creating Cloud Requester. |
142 requester_.reset( | 149 requester_.reset( |
143 new CloudPrintRequester( | 150 new CloudPrintRequester( |
144 base::MessageLoop::current()->message_loop_proxy(), | 151 base::MessageLoop::current()->message_loop_proxy(), |
145 this)); | 152 this)); |
146 | 153 |
147 xtoken_ = XPrivetToken(); | 154 xtoken_ = XPrivetToken(); |
148 starttime_ = base::Time::Now(); | 155 starttime_ = base::Time::Now(); |
149 | 156 |
| 157 print_job_handler_.reset(new PrintJobHandler); |
| 158 connection_state_ = CONNECTING; |
| 159 WakeUp(); |
| 160 |
150 return true; | 161 return true; |
151 } | 162 } |
152 | 163 |
153 bool Printer::IsOnline() const { | 164 bool Printer::IsOnline() const { |
154 return requester_; | 165 return requester_; |
155 } | 166 } |
156 | 167 |
| 168 void Printer::WakeUp() { |
| 169 VLOG(3) << "Function: " << __FUNCTION__; |
| 170 |
| 171 if (!IsRegistered()) |
| 172 return; |
| 173 |
| 174 FetchPrintJobs(); |
| 175 } |
| 176 |
157 void Printer::Stop() { | 177 void Printer::Stop() { |
158 dns_server_.Shutdown(); | 178 dns_server_.Shutdown(); |
159 http_server_.Shutdown(); | 179 http_server_.Shutdown(); |
160 requester_.reset(); | 180 requester_.reset(); |
| 181 print_job_handler_.reset(); |
161 } | 182 } |
162 | 183 |
163 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationStart( | 184 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationStart( |
164 const std::string& user) { | 185 const std::string& user) { |
165 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); | 186 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); |
166 if (status != PrivetHttpServer::REG_ERROR_OK) | 187 if (status != PrivetHttpServer::REG_ERROR_OK) |
167 return status; | 188 return status; |
168 | 189 |
169 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED) | 190 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED) |
170 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; | 191 return PrivetHttpServer::REG_ERROR_INVALID_ACTION; |
171 | 192 |
172 reg_info_ = RegistrationInfo(); | 193 reg_info_ = RegistrationInfo(); |
173 reg_info_.user = user; | 194 reg_info_.user = user; |
174 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_STARTED; | 195 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_STARTED; |
175 | 196 |
176 printf(kUserConfirmationTitle); | 197 printf(kUserConfirmationTitle); |
177 base::Time valid_until = base::Time::Now() + | 198 base::Time valid_until = base::Time::Now() + |
178 base::TimeDelta::FromSeconds(kUserConfirmationTimeout); | 199 base::TimeDelta::FromSeconds(kUserConfirmationTimeout); |
179 base::MessageLoop::current()->PostTask( | 200 base::MessageLoop::current()->PostTask( |
180 FROM_HERE, | 201 FROM_HERE, |
181 base::Bind(&Printer::WaitUserConfirmation, AsWeakPtr(), valid_until)); | 202 base::Bind(&Printer::WaitUserConfirmation, AsWeakPtr(), valid_until)); |
182 | 203 |
183 requester_->StartRegistration(GenerateProxyId(), kPrinterName, user, kCdd); | 204 requester_->StartRegistration(GenerateProxyId(), kPrinterName, user, kCdd); |
184 | 205 |
185 return PrivetHttpServer::REG_ERROR_OK; | 206 return PrivetHttpServer::REG_ERROR_OK; |
186 } | 207 } |
187 | 208 |
188 bool Printer::CheckXPrivetTokenHeader(const std::string& token) const { | |
189 return xtoken_.CheckValidXToken(token); | |
190 } | |
191 | |
192 bool Printer::IsRegistered() const { | |
193 return reg_info_.state == RegistrationInfo::DEV_REG_REGISTERED; | |
194 } | |
195 | |
196 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationGetClaimToken( | 209 PrivetHttpServer::RegistrationErrorStatus Printer::RegistrationGetClaimToken( |
197 const std::string& user, | 210 const std::string& user, |
198 std::string* token, | 211 std::string* token, |
199 std::string* claim_url) { | 212 std::string* claim_url) { |
200 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); | 213 PrivetHttpServer::RegistrationErrorStatus status = CheckCommonRegErrors(user); |
201 if (status != PrivetHttpServer::REG_ERROR_OK) | 214 if (status != PrivetHttpServer::REG_ERROR_OK) |
202 return status; | 215 return status; |
203 | 216 |
204 // Check if |action=start| was called, but |action=complete| wasn't. | 217 // Check if |action=start| was called, but |action=complete| wasn't. |
205 if (reg_info_.state != RegistrationInfo::DEV_REG_REGISTRATION_STARTED && | 218 if (reg_info_.state != RegistrationInfo::DEV_REG_REGISTRATION_STARTED && |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 void Printer::CreateInfo(PrivetHttpServer::DeviceInfo* info) { | 290 void Printer::CreateInfo(PrivetHttpServer::DeviceInfo* info) { |
278 // TODO(maksymb): Replace "text" with constants. | 291 // TODO(maksymb): Replace "text" with constants. |
279 | 292 |
280 *info = PrivetHttpServer::DeviceInfo(); | 293 *info = PrivetHttpServer::DeviceInfo(); |
281 info->version = "1.0"; | 294 info->version = "1.0"; |
282 info->name = kPrinterName; | 295 info->name = kPrinterName; |
283 info->description = kPrinterDescription; | 296 info->description = kPrinterDescription; |
284 info->url = kCloudPrintUrl; | 297 info->url = kCloudPrintUrl; |
285 info->id = reg_info_.device_id; | 298 info->id = reg_info_.device_id; |
286 info->device_state = "idle"; | 299 info->device_state = "idle"; |
287 info->connection_state = "offline"; | 300 info->connection_state = ConnectionStateToString(connection_state_); |
288 info->manufacturer = "Google"; | 301 info->manufacturer = "Google"; |
289 info->model = "Prototype"; | 302 info->model = "Prototype"; |
290 info->serial_number = "2.3.5.7.13.17.19.31.61.89.107.127.521.607.1279.2203"; | 303 info->serial_number = "2.3.5.7.13.17.19.31.61.89.107.127.521.607.1279.2203"; |
291 info->firmware = "3.7.31.127.8191.131071.524287.2147483647"; | 304 info->firmware = "3.7.31.127.8191.131071.524287.2147483647"; |
292 info->uptime = static_cast<int>((base::Time::Now() - starttime_).InSeconds()); | 305 info->uptime = static_cast<int>((base::Time::Now() - starttime_).InSeconds()); |
293 | 306 |
294 info->x_privet_token = xtoken_.GenerateXToken(); | 307 info->x_privet_token = xtoken_.GenerateXToken(); |
295 | 308 |
296 if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED) | 309 if (reg_info_.state == RegistrationInfo::DEV_REG_UNREGISTERED) |
297 info->api.push_back("/privet/register"); | 310 info->api.push_back("/privet/register"); |
298 | 311 |
299 info->type.push_back("printer"); | 312 info->type.push_back("printer"); |
300 } | 313 } |
301 | 314 |
| 315 bool Printer::IsRegistered() const { |
| 316 return reg_info_.state == RegistrationInfo::DEV_REG_REGISTERED; |
| 317 } |
| 318 |
| 319 bool Printer::CheckXPrivetTokenHeader(const std::string& token) const { |
| 320 return xtoken_.CheckValidXToken(token); |
| 321 } |
| 322 |
302 void Printer::OnRegistrationStartResponseParsed( | 323 void Printer::OnRegistrationStartResponseParsed( |
303 const std::string& registration_token, | 324 const std::string& registration_token, |
304 const std::string& complete_invite_url, | 325 const std::string& complete_invite_url, |
305 const std::string& device_id) { | 326 const std::string& device_id) { |
306 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY; | 327 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_CLAIM_TOKEN_READY; |
307 reg_info_.device_id = device_id; | 328 reg_info_.device_id = device_id; |
308 reg_info_.registration_token = registration_token; | 329 reg_info_.registration_token = registration_token; |
309 reg_info_.complete_invite_url = complete_invite_url; | 330 reg_info_.complete_invite_url = complete_invite_url; |
310 } | 331 } |
311 | 332 |
312 void Printer::OnGetAuthCodeResponseParsed(const std::string& refresh_token) { | 333 void Printer::OnGetAuthCodeResponseParsed(const std::string& refresh_token) { |
313 reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED; | 334 reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED; |
314 reg_info_.refresh_token = refresh_token; | 335 reg_info_.refresh_token = refresh_token; |
315 SaveToFile(base::FilePath(kPrinterStatePath)); | 336 SaveToFile(base::FilePath(kPrinterStatePath)); |
| 337 FetchPrintJobs(); |
316 } | 338 } |
317 | 339 |
318 void Printer::OnRegistrationError(const std::string& description) { | 340 void Printer::OnRegistrationError(const std::string& description) { |
319 LOG(ERROR) << "server_error: " << description; | 341 LOG(ERROR) << "server_error: " << description; |
320 | 342 |
321 // TODO(maksymb): Implement waiting after error and timeout of registration. | 343 // TODO(maksymb): Implement waiting after error and timeout of registration. |
322 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_ERROR; | 344 reg_info_.state = RegistrationInfo::DEV_REG_REGISTRATION_ERROR; |
323 reg_info_.error_description = description; | 345 reg_info_.error_description = description; |
324 } | 346 } |
325 | 347 |
| 348 void Printer::OnServerError(const std::string& description) { |
| 349 VLOG(3) << "Function: " << __FUNCTION__; |
| 350 LOG(ERROR) << "Server error: " << description; |
| 351 |
| 352 PostDelayedWakeUp(base::TimeDelta::FromSeconds(kReconnectTimeout)); |
| 353 } |
| 354 |
| 355 void Printer::OnNetworkError() { |
| 356 VLOG(3) << "Function: " << __FUNCTION__; |
| 357 ChangeState(OFFLINE); |
| 358 PostDelayedWakeUp(base::TimeDelta::FromSeconds(kReconnectTimeout)); |
| 359 } |
| 360 |
| 361 void Printer::OnPrintJobsAvailable(const std::vector<Job>& jobs) { |
| 362 VLOG(3) << "Function: " << __FUNCTION__; |
| 363 ChangeState(ONLINE); |
| 364 |
| 365 LOG(INFO) << "Available printjobs: " << jobs.size(); |
| 366 |
| 367 if (jobs.empty()) { |
| 368 PostDelayedWakeUp(base::TimeDelta::FromSeconds(kPrintJobsTimeout)); |
| 369 return; |
| 370 } |
| 371 |
| 372 // TODO(maksymb): After finishing XMPP add 'Printjobs available' flag. |
| 373 LOG(INFO) << "Downloading first printjob."; |
| 374 requester_->RequestPrintJob(jobs[0]); |
| 375 return; |
| 376 } |
| 377 |
| 378 void Printer::OnPrintJobDownloaded(const Job& job) { |
| 379 VLOG(3) << "Function: " << __FUNCTION__; |
| 380 print_job_handler_->SavePrintJob( |
| 381 job.file, |
| 382 job.ticket, |
| 383 base::StringPrintf("%s.%s", job.create_time.c_str(), job.job_id.c_str()), |
| 384 job.title); |
| 385 requester_->SendPrintJobDone(job.job_id); |
| 386 } |
| 387 |
| 388 void Printer::OnPrintJobDone() { |
| 389 VLOG(3) << "Function: " << __FUNCTION__; |
| 390 // TODO(maksymb): Replace PostTask with with XMPP notifications. |
| 391 PostWakeUp(); |
| 392 } |
| 393 |
326 PrivetHttpServer::RegistrationErrorStatus Printer::CheckCommonRegErrors( | 394 PrivetHttpServer::RegistrationErrorStatus Printer::CheckCommonRegErrors( |
327 const std::string& user) const { | 395 const std::string& user) const { |
328 DCHECK(!IsRegistered()); | 396 DCHECK(!IsRegistered()); |
329 | 397 |
330 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED && | 398 if (reg_info_.state != RegistrationInfo::DEV_REG_UNREGISTERED && |
331 user != reg_info_.user) { | 399 user != reg_info_.user) { |
332 return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; | 400 return PrivetHttpServer::REG_ERROR_DEVICE_BUSY; |
333 } | 401 } |
334 | 402 |
335 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR) | 403 if (reg_info_.state == RegistrationInfo::DEV_REG_REGISTRATION_ERROR) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 } | 436 } |
369 | 437 |
370 std::vector<std::string> Printer::CreateTxt() const { | 438 std::vector<std::string> Printer::CreateTxt() const { |
371 std::vector<std::string> txt; | 439 std::vector<std::string> txt; |
372 txt.push_back("txtvers=1"); | 440 txt.push_back("txtvers=1"); |
373 txt.push_back("ty=" + std::string(kPrinterName)); | 441 txt.push_back("ty=" + std::string(kPrinterName)); |
374 txt.push_back("note=" + std::string(kPrinterDescription)); | 442 txt.push_back("note=" + std::string(kPrinterDescription)); |
375 txt.push_back("url=" + std::string(kCloudPrintUrl)); | 443 txt.push_back("url=" + std::string(kCloudPrintUrl)); |
376 txt.push_back("type=printer"); | 444 txt.push_back("type=printer"); |
377 txt.push_back("id=" + reg_info_.device_id); | 445 txt.push_back("id=" + reg_info_.device_id); |
378 txt.push_back("cs=offline"); | 446 txt.push_back("cs=" + ConnectionStateToString(connection_state_)); |
379 | 447 |
380 return txt; | 448 return txt; |
381 } | 449 } |
382 | 450 |
| 451 void Printer::FetchPrintJobs() { |
| 452 VLOG(3) << "Function: " << __FUNCTION__; |
| 453 |
| 454 if (!IsRegistered()) |
| 455 return; |
| 456 |
| 457 if (requester_->IsBusy()) { |
| 458 PostDelayedWakeUp(base::TimeDelta::FromSeconds(kReconnectTimeout)); |
| 459 } else { |
| 460 requester_->FetchPrintJobs(reg_info_.refresh_token, reg_info_.device_id); |
| 461 } |
| 462 } |
| 463 |
383 void Printer::SaveToFile(const base::FilePath& file_path) const { | 464 void Printer::SaveToFile(const base::FilePath& file_path) const { |
384 base::DictionaryValue json; | 465 base::DictionaryValue json; |
385 // TODO(maksymb): Get rid of in-place constants. | 466 // TODO(maksymb): Get rid of in-place constants. |
386 if (IsRegistered()) { | 467 if (IsRegistered()) { |
387 json.SetBoolean("registered", true); | 468 json.SetBoolean("registered", true); |
388 json.SetString("user", reg_info_.user); | 469 json.SetString("user", reg_info_.user); |
389 json.SetString("device_id", reg_info_.device_id); | 470 json.SetString("device_id", reg_info_.device_id); |
390 json.SetString("refresh_token", reg_info_.refresh_token); | 471 json.SetString("refresh_token", reg_info_.refresh_token); |
391 } else { | 472 } else { |
392 json.SetBoolean("registered", false); | 473 json.SetBoolean("registered", false); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 | 535 |
455 reg_info_ = RegistrationInfo(); | 536 reg_info_ = RegistrationInfo(); |
456 reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED; | 537 reg_info_.state = RegistrationInfo::DEV_REG_REGISTERED; |
457 reg_info_.user = user; | 538 reg_info_.user = user; |
458 reg_info_.device_id = device_id; | 539 reg_info_.device_id = device_id; |
459 reg_info_.refresh_token = refresh_token; | 540 reg_info_.refresh_token = refresh_token; |
460 | 541 |
461 return true; | 542 return true; |
462 } | 543 } |
463 | 544 |
| 545 void Printer::PostWakeUp() { |
| 546 VLOG(3) << "Function: " << __FUNCTION__; |
| 547 base::MessageLoop::current()->PostTask( |
| 548 FROM_HERE, |
| 549 base::Bind(&Printer::WakeUp, AsWeakPtr())); |
| 550 } |
| 551 |
| 552 void Printer::PostDelayedWakeUp(const base::TimeDelta& delay) { |
| 553 VLOG(3) << "Function: " << __FUNCTION__; |
| 554 base::MessageLoop::current()->PostDelayedTask( |
| 555 FROM_HERE, |
| 556 base::Bind(&Printer::WakeUp, AsWeakPtr()), |
| 557 delay); |
| 558 } |
| 559 |
464 PrivetHttpServer::RegistrationErrorStatus | 560 PrivetHttpServer::RegistrationErrorStatus |
465 Printer::ConfirmationToRegistrationError( | 561 Printer::ConfirmationToRegistrationError( |
466 RegistrationInfo::ConfirmationState state) { | 562 RegistrationInfo::ConfirmationState state) { |
467 switch (state) { | 563 switch (state) { |
468 case RegistrationInfo::CONFIRMATION_PENDING: | 564 case RegistrationInfo::CONFIRMATION_PENDING: |
469 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; | 565 return PrivetHttpServer::REG_ERROR_PENDING_USER_ACTION; |
470 case RegistrationInfo::CONFIRMATION_DISCARDED: | 566 case RegistrationInfo::CONFIRMATION_DISCARDED: |
471 return PrivetHttpServer::REG_ERROR_USER_CANCEL; | 567 return PrivetHttpServer::REG_ERROR_USER_CANCEL; |
472 case RegistrationInfo::CONFIRMATION_CONFIRMED: | 568 case RegistrationInfo::CONFIRMATION_CONFIRMED: |
473 NOTREACHED(); | 569 NOTREACHED(); |
474 return PrivetHttpServer::REG_ERROR_OK; | 570 return PrivetHttpServer::REG_ERROR_OK; |
475 case RegistrationInfo::CONFIRMATION_TIMEOUT: | 571 case RegistrationInfo::CONFIRMATION_TIMEOUT: |
476 return PrivetHttpServer::REG_ERROR_CONFIRMATION_TIMEOUT; | 572 return PrivetHttpServer::REG_ERROR_CONFIRMATION_TIMEOUT; |
477 default: | 573 default: |
478 NOTREACHED(); | 574 NOTREACHED(); |
479 return PrivetHttpServer::REG_ERROR_OK; | 575 return PrivetHttpServer::REG_ERROR_OK; |
480 } | 576 } |
481 } | 577 } |
482 | 578 |
| 579 std::string Printer::ConnectionStateToString(ConnectionState state) const { |
| 580 switch (state) { |
| 581 case OFFLINE: |
| 582 return "offline"; |
| 583 case ONLINE: |
| 584 return "online"; |
| 585 case CONNECTING: |
| 586 return "connecting"; |
| 587 case NOT_CONFIGURED: |
| 588 return "not-configured"; |
| 589 |
| 590 default: |
| 591 NOTREACHED(); |
| 592 return ""; |
| 593 } |
| 594 } |
| 595 |
| 596 bool Printer::ChangeState(ConnectionState new_state) { |
| 597 if (connection_state_ == new_state) |
| 598 return false; |
| 599 |
| 600 VLOG(1) << "Printer is now " << ConnectionStateToString(new_state); |
| 601 connection_state_ = new_state; |
| 602 dns_server_.UpdateMetadata(CreateTxt()); |
| 603 return true; |
| 604 } |
| 605 |
OLD | NEW |