From 8e5841144fb76fa5f3d55f2a6f1e37f013fd661a Mon Sep 17 00:00:00 2001 From: vitalybuka Date: Sun, 21 Sep 2014 21:53:24 -0700 Subject: [PATCH] GCP 2.0 prototype switches code cleanup. TBR=gene@chromium.org Review URL: https://codereview.chromium.org/587103002 Cr-Commit-Position: refs/heads/master@{#295925} --- .../gcp20/prototype/command_line_reader.cc | 24 ++++--- cloud_print/gcp20/prototype/dns_sd_server.cc | 10 +-- cloud_print/gcp20/prototype/gcp20_device.cc | 35 ++--------- cloud_print/gcp20/prototype/gcp20_device.gyp | 2 + cloud_print/gcp20/prototype/gcp20_switches.cc | 63 +++++++++++++++++++ cloud_print/gcp20/prototype/gcp20_switches.h | 29 +++++++++ .../gcp20/prototype/print_job_handler.cc | 4 +- .../gcp20/prototype/privet_http_server.cc | 3 +- 8 files changed, 120 insertions(+), 50 deletions(-) create mode 100644 cloud_print/gcp20/prototype/gcp20_switches.cc create mode 100644 cloud_print/gcp20/prototype/gcp20_switches.h diff --git a/cloud_print/gcp20/prototype/command_line_reader.cc b/cloud_print/gcp20/prototype/command_line_reader.cc index 2d9b51ec1b6d..ecab5797d405 100644 --- a/cloud_print/gcp20/prototype/command_line_reader.cc +++ b/cloud_print/gcp20/prototype/command_line_reader.cc @@ -7,20 +7,16 @@ #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 { -const char kHttpPortSwitch[] = "http-port"; -const char kTtlSwitch[] = "ttl"; -const char kServiceNameSwitch[] = "service-name"; -const char kDomainNameSwitch[] = "domain-name"; -const char kStatePathSwitch[] = "state-path"; - uint16 ReadHttpPort(uint16 default_value) { uint32 http_port = 0; std::string http_port_string = - CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kHttpPortSwitch); + CommandLine::ForCurrentProcess()->GetSwitchValueASCII( + switches::kHttpPort); if (!base::StringToUint(http_port_string, &http_port)) http_port = default_value; @@ -38,8 +34,8 @@ uint32 ReadTtl(uint32 default_value) { uint32 ttl = 0; if (!base::StringToUint( - CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kTtlSwitch), - &ttl)) { + CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switches::kTtl), + &ttl)) { ttl = default_value; } @@ -49,14 +45,16 @@ uint32 ReadTtl(uint32 default_value) { std::string ReadServiceNamePrefix(const std::string& default_value) { std::string service_name = - CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kServiceNameSwitch); + 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 = - CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kDomainNameSwitch); + CommandLine::ForCurrentProcess()->GetSwitchValueASCII( + switches::kDomainName); if (domain_name.empty()) return default_value; @@ -77,8 +75,8 @@ std::string ReadDomainName(const std::string& default_value) { } std::string ReadStatePath(const std::string& default_value) { - std::string filename = - CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kStatePathSwitch); + std::string filename = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( + switches::kStatePath); if (filename.empty()) return default_value; diff --git a/cloud_print/gcp20/prototype/dns_sd_server.cc b/cloud_print/gcp20/prototype/dns_sd_server.cc index 1419cc7b4190..23280cb127cf 100644 --- a/cloud_print/gcp20/prototype/dns_sd_server.cc +++ b/cloud_print/gcp20/prototype/dns_sd_server.cc @@ -13,6 +13,7 @@ #include "base/strings/stringprintf.h" #include "cloud_print/gcp20/prototype/dns_packet_parser.h" #include "cloud_print/gcp20/prototype/dns_response_builder.h" +#include "cloud_print/gcp20/prototype/gcp20_switches.h" #include "net/base/dns_util.h" #include "net/base/net_errors.h" #include "net/base/net_util.h" @@ -97,7 +98,7 @@ void DnsSdServer::UpdateMetadata(const std::vector& metadata) { // then send it now. uint32 current_ttl = GetCurrentTLL(); - if (!CommandLine::ForCurrentProcess()->HasSwitch("no-announcement")) { + if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoAnnouncement)) { DnsResponseBuilder builder(current_ttl); builder.AppendTxt(serv_params_.service_name_, current_ttl, metadata_, true); @@ -183,7 +184,7 @@ void DnsSdServer::ProcessMessage(int len, net::IOBufferWithSize* buf) { VLOG(1) << "Current TTL for respond: " << current_ttl; bool unicast_respond = - CommandLine::ForCurrentProcess()->HasSwitch("unicast-respond"); + CommandLine::ForCurrentProcess()->HasSwitch(switches::kUnicastRespond); socket_->SendTo(buffer.get(), buffer.get()->size(), unicast_respond ? recv_address_ : multicast_address_, base::Bind(&DoNothingAfterSendToSocket)); @@ -204,7 +205,8 @@ void DnsSdServer::ProccessQuery(uint32 current_ttl, const DnsQueryRecord& query, builder->AppendPtr(query.qname, current_ttl, serv_params_.service_name_, true); - if (CommandLine::ForCurrentProcess()->HasSwitch("extended-response")) { + if (CommandLine::ForCurrentProcess()->HasSwitch( + switches::kExtendedResponce)) { builder->AppendSrv(serv_params_.service_name_, current_ttl, kSrvPriority, kSrvWeight, serv_params_.http_port_, serv_params_.service_domain_name_, false); @@ -278,7 +280,7 @@ void DnsSdServer::OnDatagramReceived() { } void DnsSdServer::SendAnnouncement(uint32 ttl) { - if (!CommandLine::ForCurrentProcess()->HasSwitch("no-announcement")) { + if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoAnnouncement)) { DnsResponseBuilder builder(ttl); builder.AppendPtr(serv_params_.service_type_, ttl, diff --git a/cloud_print/gcp20/prototype/gcp20_device.cc b/cloud_print/gcp20/prototype/gcp20_device.cc index 2b38a9af4eb3..9bb71b10d20f 100644 --- a/cloud_print/gcp20/prototype/gcp20_device.cc +++ b/cloud_print/gcp20/prototype/gcp20_device.cc @@ -12,38 +12,11 @@ #include "base/run_loop.h" #include "base/threading/platform_thread.h" #include "base/time/time.h" +#include "cloud_print/gcp20/prototype/gcp20_switches.h" #include "cloud_print/gcp20/prototype/printer.h" namespace { -const char kHelpMessage[] = - "usage: gcp20_device [switches] [options]\n" - "\n" - "switches:\n" - " --disable-confirmation disables confirmation of registration\n" - " --disable-method-check disables HTTP method checking (POST, GET)\n" - " --disable-x-token disables checking of X-Privet-Token " - "HTTP header\n" - " -h, --help prints this message\n" - " --no-announcement disables DNS announcements\n" - " --extended-response responds to PTR with additional records\n" - " --simulate-printing-errors simulates some errors for local printing\n" - " --unicast-respond DNS responses will be sent in unicast " - "instead of multicast\n" - "\n" - "options:\n" - " --domain-name= sets, should ends with '.local'\n" - " --http-port= sets port for HTTP server\n" - " --service-name= sets DNS service name\n" - " --state-path= sets path to file with registration state\n" - " --ttl= sets TTL for DNS announcements\n" - "\n" - "WARNING: mDNS probing is not implemented\n"; - -void PrintHelp() { - printf("%s", kHelpMessage); -} - void StartPrinter(Printer* printer) { bool success = printer->Start(); DCHECK(success); @@ -83,9 +56,9 @@ int main(int argc, char* argv[]) { settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; logging::InitLogging(settings); - if (CommandLine::ForCurrentProcess()->HasSwitch("h") || - CommandLine::ForCurrentProcess()->HasSwitch("help")) { - PrintHelp(); + if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kHelp) || + CommandLine::ForCurrentProcess()->HasSwitch(switches::kHelpShort)) { + switches::PrintUsage(); return 0; } diff --git a/cloud_print/gcp20/prototype/gcp20_device.gyp b/cloud_print/gcp20/prototype/gcp20_device.gyp index c47012ae5674..4c32b6e0004f 100644 --- a/cloud_print/gcp20/prototype/gcp20_device.gyp +++ b/cloud_print/gcp20/prototype/gcp20_device.gyp @@ -49,6 +49,8 @@ 'dns_response_builder.h', 'dns_sd_server.cc', 'dns_sd_server.h', + 'gcp20_switches.cc', + 'gcp20_switches.h', 'local_settings.h', 'local_print_job.cc', 'local_print_job.h', diff --git a/cloud_print/gcp20/prototype/gcp20_switches.cc b/cloud_print/gcp20/prototype/gcp20_switches.cc new file mode 100644 index 000000000000..cce7eaf0d842 --- /dev/null +++ b/cloud_print/gcp20/prototype/gcp20_switches.cc @@ -0,0 +1,63 @@ +// Copyright 2014 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/gcp20_switches.h" + +#include "base/files/file_path.h" +#include "base/path_service.h" + +namespace switches { + +const char kDisableConfirmation[] = "disable-confirmation"; +const char kDisableMethodCheck[] = "disable-method-check"; +const char kDisableXTocken[] = "disable-x-token"; +const char kDomainName[] = "domain-name"; +const char kExtendedResponce[] = "extended-response"; +const char kHelpShort[] = "h"; +const char kHelp[] = "help"; +const char kHttpPort[] = "http-port"; +const char kNoAnnouncement[] = "no-announcement"; +const char kServiceName[] = "service-name"; +const char kSimulatePrintingErrors[] = "simulate-printing-errors"; +const char kStatePath[] = "state-path"; +const char kTtl[] = "ttl"; +const char kUnicastRespond[] = "unicast-respond"; + +const struct { + const char* const name; + const char* const description; + const char* const arg; +} kHelpStrings[] = { + {kDisableConfirmation, "disables confirmation of registration", NULL}, + {kDisableMethodCheck, "disables HTTP method checking (POST, GET)", NULL}, + {kDisableXTocken, "disables checking of X-Privet-Token HTTP header", NULL}, + {kNoAnnouncement, "disables DNS announcements", NULL}, + {kExtendedResponce, "responds to PTR with additional records", NULL}, + {kSimulatePrintingErrors, "simulates some errors for local printing", NULL}, + {kUnicastRespond, + "DNS responses will be sent in unicast instead of multicast", NULL}, + {kDomainName, "sets, should ends with '.local'", "DOMAIN"}, + {kHttpPort, "sets port for HTTP server", "PORT"}, + {kServiceName, "sets DNS service name", "SERVICE"}, + {kStatePath, "sets path to file with registration state", "PATH"}, + {kTtl, "sets TTL for DNS announcements", "TTL"}, +}; + +void PrintUsage() { + base::FilePath exe; + PathService::Get(base::FILE_EXE, &exe); + printf("usage: %s [OPTION]...\n\n", exe.BaseName().MaybeAsASCII().c_str()); + for (size_t i = 0; i < arraysize(kHelpStrings); ++i) { + std::string name = kHelpStrings[i].name; + if (kHelpStrings[i].arg) { + name += '='; + name += kHelpStrings[i].arg; + } + name.resize(27, ' '); + printf(" --%s%s\n", name.c_str(), kHelpStrings[i].description); + } + printf("\n WARNING: mDNS probing is not implemented\n"); +} + +} // namespace switches diff --git a/cloud_print/gcp20/prototype/gcp20_switches.h b/cloud_print/gcp20/prototype/gcp20_switches.h new file mode 100644 index 000000000000..f2a6d2296e42 --- /dev/null +++ b/cloud_print/gcp20/prototype/gcp20_switches.h @@ -0,0 +1,29 @@ +// Copyright 2014 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. + +#ifndef CLOUD_PRINT_GCP20_PROTOTYPE_GCP20_SWITCHES_H_ +#define CLOUD_PRINT_GCP20_PROTOTYPE_GCP20_SWITCHES_H_ + +namespace switches { + +extern const char kDisableConfirmation[]; +extern const char kDisableMethodCheck[]; +extern const char kDisableXTocken[]; +extern const char kDomainName[]; +extern const char kExtendedResponce[]; +extern const char kHelpShort[]; +extern const char kHelp[]; +extern const char kHttpPort[]; +extern const char kNoAnnouncement[]; +extern const char kServiceName[]; +extern const char kSimulatePrintingErrors[]; +extern const char kStatePath[]; +extern const char kTtl[]; +extern const char kUnicastRespond[]; + +void PrintUsage(); + +} // namespace switches + +#endif // CLOUD_PRINT_GCP20_PROTOTYPE_GCP20_SWITCHES_H_ diff --git a/cloud_print/gcp20/prototype/print_job_handler.cc b/cloud_print/gcp20/prototype/print_job_handler.cc index 946d5d573844..28bf21c504a8 100644 --- a/cloud_print/gcp20/prototype/print_job_handler.cc +++ b/cloud_print/gcp20/prototype/print_job_handler.cc @@ -15,6 +15,7 @@ #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" #include "base/time/time.h" +#include "cloud_print/gcp20/prototype/gcp20_switches.h" namespace { @@ -84,7 +85,8 @@ LocalPrintJob::CreateResult PrintJobHandler::CreatePrintJob( return LocalPrintJob::CREATE_INVALID_TICKET; // Let's simulate at least some errors just for testing. - if (CommandLine::ForCurrentProcess()->HasSwitch("simulate-printing-errors")) { + if (CommandLine::ForCurrentProcess()->HasSwitch( + switches::kSimulatePrintingErrors)) { if (base::RandDouble() <= kPaperJamProbability) { *error_description = "Paper jam, try again"; return LocalPrintJob::CREATE_PRINTER_ERROR; diff --git a/cloud_print/gcp20/prototype/privet_http_server.cc b/cloud_print/gcp20/prototype/privet_http_server.cc index 41daa8126877..8dfa0cc3a7cc 100644 --- a/cloud_print/gcp20/prototype/privet_http_server.cc +++ b/cloud_print/gcp20/prototype/privet_http_server.cc @@ -7,6 +7,7 @@ #include "base/command_line.h" #include "base/json/json_writer.h" #include "base/strings/stringprintf.h" +#include "cloud_print/gcp20/prototype/gcp20_switches.h" #include "net/base/ip_endpoint.h" #include "net/base/net_errors.h" #include "net/base/url_util.h" @@ -135,7 +136,7 @@ void PrivetHttpServer::OnHttpRequest(int connection_id, if (!ValidateRequestMethod(connection_id, url.path(), info.method)) return; - if (!CommandLine::ForCurrentProcess()->HasSwitch("disable-x-token")) { + if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableXTocken)) { net::HttpServerRequestInfo::HeadersMap::const_iterator iter = info.headers.find("x-privet-token"); if (iter == info.headers.end()) {