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/gcp20_switches.h" 16 #include "cloud_print/gcp20/prototype/printer.h" 17 18 namespace { 19 20 void StartPrinter(Printer* printer) { 21 bool success = printer->Start(); 22 DCHECK(success); 23 } 24 25 base::RunLoop* g_runner = NULL; 26 Printer* g_printer = NULL; 27 base::MessageLoop* g_message_loop; 28 29 void StopLoop() { 30 // Always do after printer.Stop() to make sure XMPP will 31 // be disabled fully before |Quit| will be called 32 // (XMPP disables itself via MessageLoop call). 33 g_message_loop->PostTask(FROM_HERE, g_runner->QuitClosure()); 34 g_message_loop = NULL; 35 g_runner = NULL; 36 } 37 38 void OnAbort(int val) { 39 if (g_printer) { 40 g_message_loop->PostTask( 41 FROM_HERE, 42 base::Bind(&Printer::Stop, base::Unretained(g_printer))); 43 g_message_loop->PostTask(FROM_HERE, base::Bind(&StopLoop)); 44 g_printer = NULL; 45 } 46 } 47 48 } // namespace 49 50 int main(int argc, char* argv[]) { 51 base::AtExitManager at_exit; 52 Printer printer; 53 CommandLine::Init(argc, argv); 54 55 logging::LoggingSettings settings; 56 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; 57 logging::InitLogging(settings); 58 59 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kHelp) || 60 CommandLine::ForCurrentProcess()->HasSwitch(switches::kHelpShort)) { 61 switches::PrintUsage(); 62 return 0; 63 } 64 65 signal(SIGINT, OnAbort); // Handle Ctrl+C signal. 66 67 base::MessageLoopForIO loop; 68 g_message_loop = &loop; 69 g_message_loop->PostTask(FROM_HERE, base::Bind(&StartPrinter, &printer)); 70 base::RunLoop runner; 71 g_printer = &printer; 72 g_runner = &runner; 73 runner.Run(); 74 75 return 0; 76 } 77 78