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 3 // found in the LICENSE file. 4 5 #include "cloud_print/gcp20/prototype/command_line_reader.h" 6 7 #include "base/command_line.h" 8 #include "base/logging.h" 9 #include "base/strings/string_number_conversions.h" 10 11 namespace command_line_reader { 12 13 const char kHttpPortSwitch[] = "http-port"; 14 const char kTtlSwitch[] = "ttl"; 15 const char kServiceNameSwitch[] = "service-name"; 16 const char kDomainNameSwitch[] = "domain-name"; 17 const char kStatePathSwitch[] = "state-path"; 18 19 uint16 ReadHttpPort(uint16 default_value) { 20 uint32 http_port = 0; 21 22 std::string http_port_string = 23 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kHttpPortSwitch); 24 25 if (!base::StringToUint(http_port_string, &http_port)) 26 http_port = default_value; 27 28 if (http_port > kuint16max) { 29 LOG(ERROR) << "HTTP Port is too large"; 30 http_port = default_value; 31 } 32 33 VLOG(1) << "HTTP port for responses: " << http_port; 34 return static_cast<uint16>(http_port); 35 } 36 37 uint32 ReadTtl(uint32 default_value) { 38 uint32 ttl = 0; 39 40 if (!base::StringToUint( 41 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kTtlSwitch), 42 &ttl)) { 43 ttl = default_value; 44 } 45 46 VLOG(1) << "TTL for announcements: " << ttl; 47 return ttl; 48 } 49 50 std::string ReadServiceNamePrefix(const std::string& default_value) { 51 std::string service_name = 52 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kServiceNameSwitch); 53 54 return service_name.empty() ? default_value : service_name; 55 } 56 57 std::string ReadDomainName(const std::string& default_value) { 58 std::string domain_name = 59 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kDomainNameSwitch); 60 61 if (domain_name.empty()) 62 return default_value; 63 64 std::string suffix = ".local"; 65 if (domain_name == suffix) { 66 LOG(ERROR) << "Domain name cannot be only \"" << suffix << "\""; 67 return default_value; 68 } 69 70 if (domain_name.size() < suffix.size() || 71 domain_name.substr(domain_name.size() - suffix.size()) != suffix) { 72 LOG(ERROR) << "Domain name should end with \"" << suffix << "\""; 73 return default_value; 74 } 75 76 return domain_name; 77 } 78 79 std::string ReadStatePath(const std::string& default_value) { 80 std::string filename = 81 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kStatePathSwitch); 82 83 if (filename.empty()) 84 return default_value; 85 return filename; 86 } 87 88 } // namespace command_line_reader 89 90