summaryrefslogtreecommitdiffstats
path: root/cloud_print/gcp20/prototype/gcp20_device.cc
blob: 9bb71b10d20f2c00ec5d21f6ec1a5943ce0e41d6 (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
// 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 <signal.h>

#include "base/at_exit.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#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 {

void StartPrinter(Printer* printer) {
  bool success = printer->Start();
  DCHECK(success);
}

base::RunLoop* g_runner = NULL;
Printer* g_printer = NULL;
base::MessageLoop* g_message_loop;

void StopLoop() {
  // Always do after printer.Stop() to make sure XMPP will
  // be disabled fully before |Quit| will be called
  // (XMPP disables itself via MessageLoop call).
  g_message_loop->PostTask(FROM_HERE, g_runner->QuitClosure());
  g_message_loop = NULL;
  g_runner = NULL;
}

void OnAbort(int val) {
  if (g_printer) {
    g_message_loop->PostTask(
        FROM_HERE,
        base::Bind(&Printer::Stop, base::Unretained(g_printer)));
    g_message_loop->PostTask(FROM_HERE, base::Bind(&StopLoop));
    g_printer = NULL;
  }
}

}  // namespace

int main(int argc, char* argv[]) {
  base::AtExitManager at_exit;
  Printer printer;
  CommandLine::Init(argc, argv);

  logging::LoggingSettings settings;
  settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
  logging::InitLogging(settings);

  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kHelp) ||
      CommandLine::ForCurrentProcess()->HasSwitch(switches::kHelpShort)) {
    switches::PrintUsage();
    return 0;
  }

  signal(SIGINT, OnAbort);  // Handle Ctrl+C signal.

  base::MessageLoopForIO loop;
  g_message_loop = &loop;
  g_message_loop->PostTask(FROM_HERE, base::Bind(&StartPrinter, &printer));
  base::RunLoop runner;
  g_printer = &printer;
  g_runner = &runner;
  runner.Run();

  return 0;
}