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 <stdio.h> | 8 #include <stdio.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/format_macros.h" | 16 #include "base/format_macros.h" |
17 #include "base/guid.h" | 17 #include "base/guid.h" |
18 #include "base/json/json_reader.h" | 18 #include "base/json/json_reader.h" |
19 #include "base/json/json_writer.h" | 19 #include "base/json/json_writer.h" |
20 #include "base/rand_util.h" | 20 #include "base/rand_util.h" |
21 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
22 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
23 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
24 #include "cloud_print/gcp20/prototype/command_line_reader.h" | 24 #include "cloud_print/gcp20/prototype/command_line_reader.h" |
| 25 #include "cloud_print/gcp20/prototype/gcp20_switches.h" |
25 #include "cloud_print/gcp20/prototype/local_settings.h" | 26 #include "cloud_print/gcp20/prototype/local_settings.h" |
26 #include "cloud_print/gcp20/prototype/service_parameters.h" | 27 #include "cloud_print/gcp20/prototype/service_parameters.h" |
27 #include "cloud_print/gcp20/prototype/special_io.h" | 28 #include "cloud_print/gcp20/prototype/special_io.h" |
28 #include "cloud_print/version.h" | 29 #include "cloud_print/version.h" |
29 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
30 #include "net/base/url_util.h" | 31 #include "net/base/url_util.h" |
31 | 32 |
32 const char kPrinterStatePathDefault[] = "printer_state.json"; | 33 const char kPrinterStatePathDefault[] = "printer_state.json"; |
33 | 34 |
34 namespace { | 35 namespace { |
(...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 if (!StartDnsServer()) { | 826 if (!StartDnsServer()) { |
826 http_server_.Shutdown(); | 827 http_server_.Shutdown(); |
827 return false; | 828 return false; |
828 } | 829 } |
829 return true; | 830 return true; |
830 } | 831 } |
831 | 832 |
832 bool Printer::StartDnsServer() { | 833 bool Printer::StartDnsServer() { |
833 DCHECK(state_.local_settings.local_discovery); | 834 DCHECK(state_.local_settings.local_discovery); |
834 | 835 |
| 836 net::IPAddressNumber ipv4; |
| 837 net::IPAddressNumber ipv6; |
| 838 |
| 839 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 840 switches::kDisableIpv4)) { |
| 841 ipv4 = GetLocalIp("", false); |
| 842 } |
| 843 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 844 switches::kDisableIpv6)) { |
| 845 ipv6 = GetLocalIp("", true); |
| 846 } |
| 847 |
835 // TODO(maksymb): Add switch for command line to control interface name. | 848 // TODO(maksymb): Add switch for command line to control interface name. |
836 net::IPAddressNumber ip = GetLocalIp("", false); | 849 if (ipv4.empty() && ipv6.empty()) { |
837 if (ip.empty()) { | |
838 LOG(ERROR) << "No local IP found. Cannot start printer."; | 850 LOG(ERROR) << "No local IP found. Cannot start printer."; |
839 return false; | 851 return false; |
840 } | 852 } |
841 VLOG(0) << "Local address: " << net::IPAddressToString(ip); | |
842 | 853 |
843 uint16 port = command_line_reader::ReadHttpPort(kHttpPortDefault); | 854 uint16 port = command_line_reader::ReadHttpPort(kHttpPortDefault); |
844 | 855 |
845 std::string service_name_prefix = | 856 VLOG_IF(0, !ipv4.empty()) |
846 command_line_reader::ReadServiceNamePrefix(kServiceNamePrefixDefault + | 857 << "Local IPv4 address: " << net::IPAddressToStringWithPort(ipv4, port); |
847 net::IPAddressToString(ip)); | 858 VLOG_IF(0, !ipv6.empty()) |
848 std::replace(service_name_prefix .begin(), service_name_prefix .end(), | 859 << "Local IPv6 address: " << net::IPAddressToStringWithPort(ipv6, port); |
849 '.', '_'); | 860 |
| 861 std::string service_name_prefix = kServiceNamePrefixDefault; |
| 862 if (!ipv4.empty()) |
| 863 service_name_prefix += net::IPAddressToString(ipv4); |
| 864 service_name_prefix = |
| 865 command_line_reader::ReadServiceNamePrefix(service_name_prefix); |
| 866 std::replace( |
| 867 service_name_prefix.begin(), service_name_prefix.end(), '.', '_'); |
850 | 868 |
851 std::string service_domain_name = | 869 std::string service_domain_name = |
852 command_line_reader::ReadDomainName( | 870 command_line_reader::ReadDomainName( |
853 base::StringPrintf(kServiceDomainNameFormatDefault, | 871 base::StringPrintf(kServiceDomainNameFormatDefault, |
854 base::RandInt(0, INT_MAX))); | 872 base::RandInt(0, INT_MAX))); |
855 | 873 |
856 ServiceParameters params(kServiceType, kSecondaryServiceType, | 874 ServiceParameters params(kServiceType, |
857 service_name_prefix, service_domain_name, | 875 kSecondaryServiceType, |
858 ip, GetLocalIp("", true), port); | 876 service_name_prefix, |
| 877 service_domain_name, |
| 878 ipv4, |
| 879 ipv6, |
| 880 port); |
859 | 881 |
860 return dns_server_.Start(params, | 882 return dns_server_.Start(params, |
861 command_line_reader::ReadTtl(kTtlDefault), | 883 command_line_reader::ReadTtl(kTtlDefault), |
862 CreateTxt()); | 884 CreateTxt()); |
863 } | 885 } |
864 | 886 |
865 bool Printer::StartHttpServer() { | 887 bool Printer::StartHttpServer() { |
866 DCHECK(state_.local_settings.local_discovery); | 888 DCHECK(state_.local_settings.local_discovery); |
867 using command_line_reader::ReadHttpPort; | 889 using command_line_reader::ReadHttpPort; |
868 return http_server_.Start(ReadHttpPort(kHttpPortDefault)); | 890 return http_server_.Start(ReadHttpPort(kHttpPortDefault)); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
933 | 955 |
934 dns_server_.UpdateMetadata(CreateTxt()); | 956 dns_server_.UpdateMetadata(CreateTxt()); |
935 | 957 |
936 if (connection_state_ == OFFLINE) { | 958 if (connection_state_ == OFFLINE) { |
937 requester_.reset(); | 959 requester_.reset(); |
938 xmpp_listener_.reset(); | 960 xmpp_listener_.reset(); |
939 } | 961 } |
940 | 962 |
941 return true; | 963 return true; |
942 } | 964 } |
OLD | NEW |