- add sources.
[platform/framework/web/crosswalk.git] / src / cloud_print / gcp20 / prototype / gcp20_device.cc
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 <signal.h>
6
7 #include "base/at_exit.h"
8 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/threading/platform_thread.h"
14 #include "base/time/time.h"
15 #include "cloud_print/gcp20/prototype/printer.h"
16
17 namespace {
18
19 const char kHelpMessage[] =
20     "usage: gcp20_device [switches] [options]\n"
21     "\n"
22     "switches:\n"
23     "  --disable-confirmation     disables confirmation of registration\n"
24     "  --disable-method-check     disables HTTP method checking (POST, GET)\n"
25     "  --disable-x-token          disables checking of X-Privet-Token "
26                                  "HTTP header\n"
27     "  -h, --help                 prints this message\n"
28     "  --no-announcement          disables DNS announcements\n"
29     "  --simulate-printing-errors simulates some errors for local printing\n"
30     "  --unicast-respond          DNS responses will be sent in unicast "
31                                  "instead of multicast\n"
32     "\n"
33     "options:\n"
34     "  --domain-name=<name>       sets, should ends with '.local'\n"
35     "  --http-port=<value>        sets port for HTTP server\n"
36     "  --service-name=<name>      sets DNS service name\n"
37     "  --state-path=<path>        sets path to file with registration state\n"
38     "  --ttl=<value>              sets TTL for DNS announcements\n"
39     "\n"
40     "WARNING: mDNS probing is not implemented\n";
41
42 void PrintHelp() {
43   printf("%s", kHelpMessage);
44 }
45
46 void StartPrinter(Printer* printer) {
47   bool success = printer->Start();
48   DCHECK(success);
49 }
50
51 base::RunLoop* g_runner = NULL;
52 Printer* g_printer = NULL;
53 base::MessageLoop* g_message_loop;
54
55 void StopLoop() {
56   // Always do after printer.Stop() to make sure XMPP will
57   // be disabled fully before |Quit| will be called
58   // (XMPP disables itself via MessageLoop call).
59   g_message_loop->PostTask(FROM_HERE, g_runner->QuitClosure());
60   g_message_loop = NULL;
61   g_runner = NULL;
62 }
63
64 void OnAbort(int val) {
65   if (g_printer) {
66     g_message_loop->PostTask(
67         FROM_HERE,
68         base::Bind(&Printer::Stop, base::Unretained(g_printer)));
69     g_message_loop->PostTask(FROM_HERE, base::Bind(&StopLoop));
70     g_printer = NULL;
71   }
72 }
73
74 }  // namespace
75
76 int main(int argc, char* argv[]) {
77   base::AtExitManager at_exit;
78   Printer printer;
79   CommandLine::Init(argc, argv);
80
81   logging::LoggingSettings settings;
82   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
83   logging::InitLogging(settings);
84
85   if (CommandLine::ForCurrentProcess()->HasSwitch("h") ||
86       CommandLine::ForCurrentProcess()->HasSwitch("help")) {
87     PrintHelp();
88     return 0;
89   }
90
91   signal(SIGINT, OnAbort);  // Handle Ctrl+C signal.
92
93   base::MessageLoop loop(base::MessageLoop::TYPE_IO);
94   g_message_loop = &loop;
95   g_message_loop->PostTask(FROM_HERE, base::Bind(&StartPrinter, &printer));
96   base::RunLoop runner;
97   g_printer = &printer;
98   g_runner = &runner;
99   runner.Run();
100
101   return 0;
102 }
103