summaryrefslogtreecommitdiffstats
path: root/cloud_print/gcp20/prototype/command_line_reader.cc
blob: 8663efa40de12592eaf762ee9c11a12ce207ab43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cloud_print/gcp20/prototype/command_line_reader.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"

namespace command_line_reader {

uint16 ReadHttpPort(uint16 default_value) {
  uint32 http_port = 0;

  std::string http_port_string =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kHttpPort);

  if (!base::StringToUint(http_port_string, &http_port))
    http_port = default_value;

  if (http_port > kuint16max) {
    LOG(ERROR) << "HTTP Port is too large";
    http_port = default_value;
  }

  VLOG(1) << "HTTP port for responses: " << http_port;
  return static_cast<uint16>(http_port);
}

uint32 ReadTtl(uint32 default_value) {
  uint32 ttl = 0;
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();

  if (!base::StringToUint(
          command_line->GetSwitchValueASCII(switches::kTtl),
          &ttl)) {
    ttl = default_value;
  }

  VLOG(1) << "TTL for announcements: " << ttl;
  return ttl;
}

std::string ReadServiceNamePrefix(const std::string& default_value) {
  std::string service_name =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kServiceName);

  return service_name.empty() ? default_value : service_name;
}

std::string ReadDomainName(const std::string& default_value) {
  std::string domain_name =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kDomainName);

  if (domain_name.empty())
    return default_value;

  std::string suffix = ".local";
  if (domain_name == suffix) {
    LOG(ERROR) << "Domain name cannot be only \"" << suffix << "\"";
    return default_value;
  }

  if (domain_name.size() < suffix.size() ||
      domain_name.substr(domain_name.size() - suffix.size()) != suffix) {
    LOG(ERROR) << "Domain name should end with \"" << suffix << "\"";
    return default_value;
  }

  return domain_name;
}

std::string ReadStatePath(const std::string& default_value) {
  std::string filename =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kStatePath);

  if (filename.empty())
    return default_value;
  return filename;
}

}  // namespace command_line_reader