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