| 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 <limits.h> | 7 #include <limits.h> |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 8 #include <stdio.h> | 10 #include <stdio.h> |
| 9 #include <algorithm> | 11 #include <algorithm> |
| 10 #include <string> | 12 #include <string> |
| 11 #include <vector> | 13 #include <vector> |
| 12 | 14 |
| 13 #include "base/bind.h" | 15 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 15 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
| 16 #include "base/format_macros.h" | 18 #include "base/format_macros.h" |
| 17 #include "base/guid.h" | 19 #include "base/guid.h" |
| 18 #include "base/json/json_reader.h" | 20 #include "base/json/json_reader.h" |
| 19 #include "base/json/json_writer.h" | 21 #include "base/json/json_writer.h" |
| 20 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
| 21 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 23 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 24 #include "cloud_print/gcp20/prototype/command_line_reader.h" | 26 #include "cloud_print/gcp20/prototype/command_line_reader.h" |
| 25 #include "cloud_print/gcp20/prototype/gcp20_switches.h" | 27 #include "cloud_print/gcp20/prototype/gcp20_switches.h" |
| 26 #include "cloud_print/gcp20/prototype/local_settings.h" | 28 #include "cloud_print/gcp20/prototype/local_settings.h" |
| 27 #include "cloud_print/gcp20/prototype/service_parameters.h" | 29 #include "cloud_print/gcp20/prototype/service_parameters.h" |
| 28 #include "cloud_print/gcp20/prototype/special_io.h" | 30 #include "cloud_print/gcp20/prototype/special_io.h" |
| 29 #include "cloud_print/version.h" | 31 #include "cloud_print/version.h" |
| 30 #include "net/base/network_interfaces.h" | 32 #include "net/base/network_interfaces.h" |
| 31 #include "net/base/url_util.h" | 33 #include "net/base/url_util.h" |
| 32 | 34 |
| 33 const char kPrinterStatePathDefault[] = "printer_state.json"; | 35 const char kPrinterStatePathDefault[] = "printer_state.json"; |
| 34 | 36 |
| 35 namespace { | 37 namespace { |
| 36 | 38 |
| 37 const uint16 kHttpPortDefault = 10101; | 39 const uint16_t kHttpPortDefault = 10101; |
| 38 const uint32 kTtlDefault = 60*60; // in seconds | 40 const uint32_t kTtlDefault = 60 * 60; // in seconds |
| 39 | 41 |
| 40 const char kServiceType[] = "_privet._tcp.local"; | 42 const char kServiceType[] = "_privet._tcp.local"; |
| 41 const char kSecondaryServiceType[] = "_printer._sub._privet._tcp.local"; | 43 const char kSecondaryServiceType[] = "_printer._sub._privet._tcp.local"; |
| 42 const char kServiceNamePrefixDefault[] = "gcp20_device_"; | 44 const char kServiceNamePrefixDefault[] = "gcp20_device_"; |
| 43 const char kServiceDomainNameFormatDefault[] = "my-privet-device%d.local"; | 45 const char kServiceDomainNameFormatDefault[] = "my-privet-device%d.local"; |
| 44 | 46 |
| 45 const char kPrinterName[] = "Google GCP2.0 Prototype"; | 47 const char kPrinterName[] = "Google GCP2.0 Prototype"; |
| 46 const char kPrinterDescription[] = "Printer emulator"; | 48 const char kPrinterDescription[] = "Printer emulator"; |
| 47 | 49 |
| 48 const char kUserConfirmationTitle[] = "Confirm registration: type 'y' if you " | 50 const char kUserConfirmationTitle[] = "Confirm registration: type 'y' if you " |
| (...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 SaveToFile(); | 676 SaveToFile(); |
| 675 Stop(); | 677 Stop(); |
| 676 Start(); | 678 Start(); |
| 677 } | 679 } |
| 678 | 680 |
| 679 void Printer::RememberAccessToken(const std::string& access_token, | 681 void Printer::RememberAccessToken(const std::string& access_token, |
| 680 int expires_in_seconds) { | 682 int expires_in_seconds) { |
| 681 using base::Time; | 683 using base::Time; |
| 682 using base::TimeDelta; | 684 using base::TimeDelta; |
| 683 state_.access_token = access_token; | 685 state_.access_token = access_token; |
| 684 int64 time_to_update = static_cast<int64>(expires_in_seconds * | 686 int64_t time_to_update = |
| 685 kTimeToNextAccessTokenUpdate); | 687 static_cast<int64_t>(expires_in_seconds * kTimeToNextAccessTokenUpdate); |
| 686 state_.access_token_update = | 688 state_.access_token_update = |
| 687 Time::Now() + TimeDelta::FromSeconds(time_to_update); | 689 Time::Now() + TimeDelta::FromSeconds(time_to_update); |
| 688 VLOG(0) << "Current access_token: " << access_token; | 690 VLOG(0) << "Current access_token: " << access_token; |
| 689 SaveToFile(); | 691 SaveToFile(); |
| 690 } | 692 } |
| 691 | 693 |
| 692 void Printer::SetRegistrationError(const std::string& description) { | 694 void Printer::SetRegistrationError(const std::string& description) { |
| 693 DCHECK(!IsRegistered()); | 695 DCHECK(!IsRegistered()); |
| 694 state_.registration_state = PrinterState::REGISTRATION_ERROR; | 696 state_.registration_state = PrinterState::REGISTRATION_ERROR; |
| 695 state_.error_description = description; | 697 state_.error_description = description; |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 845 switches::kDisableIpv6)) { | 847 switches::kDisableIpv6)) { |
| 846 ipv6 = GetLocalIp("", true); | 848 ipv6 = GetLocalIp("", true); |
| 847 } | 849 } |
| 848 | 850 |
| 849 // TODO(maksymb): Add switch for command line to control interface name. | 851 // TODO(maksymb): Add switch for command line to control interface name. |
| 850 if (ipv4.empty() && ipv6.empty()) { | 852 if (ipv4.empty() && ipv6.empty()) { |
| 851 LOG(ERROR) << "No local IP found. Cannot start printer."; | 853 LOG(ERROR) << "No local IP found. Cannot start printer."; |
| 852 return false; | 854 return false; |
| 853 } | 855 } |
| 854 | 856 |
| 855 uint16 port = command_line_reader::ReadHttpPort(kHttpPortDefault); | 857 uint16_t port = command_line_reader::ReadHttpPort(kHttpPortDefault); |
| 856 | 858 |
| 857 VLOG_IF(0, !ipv4.empty()) | 859 VLOG_IF(0, !ipv4.empty()) |
| 858 << "Local IPv4 address: " << net::IPAddressToStringWithPort(ipv4, port); | 860 << "Local IPv4 address: " << net::IPAddressToStringWithPort(ipv4, port); |
| 859 VLOG_IF(0, !ipv6.empty()) | 861 VLOG_IF(0, !ipv6.empty()) |
| 860 << "Local IPv6 address: " << net::IPAddressToStringWithPort(ipv6, port); | 862 << "Local IPv6 address: " << net::IPAddressToStringWithPort(ipv6, port); |
| 861 | 863 |
| 862 std::string service_name_prefix = kServiceNamePrefixDefault; | 864 std::string service_name_prefix = kServiceNamePrefixDefault; |
| 863 if (!ipv4.empty()) | 865 if (!ipv4.empty()) |
| 864 service_name_prefix += net::IPAddressToString(ipv4); | 866 service_name_prefix += net::IPAddressToString(ipv4); |
| 865 service_name_prefix = | 867 service_name_prefix = |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 | 958 |
| 957 dns_server_.UpdateMetadata(CreateTxt()); | 959 dns_server_.UpdateMetadata(CreateTxt()); |
| 958 | 960 |
| 959 if (connection_state_ == OFFLINE) { | 961 if (connection_state_ == OFFLINE) { |
| 960 requester_.reset(); | 962 requester_.reset(); |
| 961 xmpp_listener_.reset(); | 963 xmpp_listener_.reset(); |
| 962 } | 964 } |
| 963 | 965 |
| 964 return true; | 966 return true; |
| 965 } | 967 } |
| OLD | NEW |