Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / remoting / host / it2me / it2me_native_messaging_host_main.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 "remoting/host/it2me/it2me_native_messaging_host_main.h"
6
7 #include "base/at_exit.h"
8 #include "base/command_line.h"
9 #include "base/i18n/icu_util.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "media/base/media.h"
13 #include "net/socket/ssl_server_socket.h"
14 #include "remoting/base/breakpad.h"
15 #include "remoting/base/resources.h"
16 #include "remoting/host/it2me/it2me_native_messaging_host.h"
17 #include "remoting/host/logging.h"
18 #include "remoting/host/usage_stats_consent.h"
19
20 #if defined(OS_LINUX)
21 #include <gtk/gtk.h>
22 #endif  // defined(OS_LINUX)
23
24 #if defined(OS_MACOSX)
25 #include "base/mac/scoped_nsautorelease_pool.h"
26 #endif  // defined(OS_MACOSX)
27
28 #if defined(OS_WIN)
29 #include <commctrl.h>
30 #endif  // defined(OS_WIN)
31
32 namespace remoting {
33
34 // Creates a It2MeNativeMessagingHost instance, attaches it to stdin/stdout and
35 // runs the message loop until It2MeNativeMessagingHost signals shutdown.
36 int StartIt2MeNativeMessagingHost() {
37 #if defined(OS_MACOSX)
38   // Needed so we don't leak objects when threads are created.
39   base::mac::ScopedNSAutoreleasePool pool;
40 #endif  // defined(OS_MACOSX)
41
42 #if defined(REMOTING_ENABLE_BREAKPAD)
43   // Initialize Breakpad as early as possible. On Mac the command-line needs to
44   // be initialized first, so that the preference for crash-reporting can be
45   // looked up in the config file.
46   if (IsUsageStatsAllowed()) {
47     InitializeCrashReporting();
48   }
49 #endif  // defined(REMOTING_ENABLE_BREAKPAD)
50
51 #if defined(OS_WIN)
52   // Register and initialize common controls.
53   INITCOMMONCONTROLSEX info;
54   info.dwSize = sizeof(info);
55   info.dwICC = ICC_STANDARD_CLASSES;
56   InitCommonControlsEx(&info);
57 #endif  // defined(OS_WIN)
58
59   // Required to find the ICU data file, used by some file_util routines.
60   base::i18n::InitializeICU();
61
62   remoting::LoadResources("");
63
64   // Cannot use TOOLKIT_GTK because it is not defined when aura is enabled.
65 #if defined(OS_LINUX)
66   // Required for any calls into GTK functions, such as the Disconnect and
67   // Continue windows. Calling with NULL arguments because we don't have
68   // any command line arguments for gtk to consume.
69   gtk_init(NULL, NULL);
70 #endif  // OS_LINUX
71
72   // Enable support for SSL server sockets, which must be done while still
73   // single-threaded.
74   net::EnableSSLServerSockets();
75
76   // Ensures runtime specific CPU features are initialized.
77   media::InitializeCPUSpecificMediaFeatures();
78
79 #if defined(OS_WIN)
80   // GetStdHandle() returns pseudo-handles for stdin and stdout even if
81   // the hosting executable specifies "Windows" subsystem. However the returned
82   // handles are invalid in that case unless standard input and output are
83   // redirected to a pipe or file.
84   base::File read_file(GetStdHandle(STD_INPUT_HANDLE));
85   base::File write_file(GetStdHandle(STD_OUTPUT_HANDLE));
86
87   // After the native messaging channel starts the native messaging reader
88   // will keep doing blocking read operations on the input named pipe.
89   // If any other thread tries to perform any operation on STDIN, it will also
90   // block because the input named pipe is synchronous (non-overlapped).
91   // It is pretty common for a DLL to query the device info (GetFileType) of
92   // the STD* handles at startup. So any LoadLibrary request can potentially
93   // be blocked. To prevent that from happening we close STDIN and STDOUT
94   // handles as soon as we retrieve the corresponding file handles.
95   SetStdHandle(STD_INPUT_HANDLE, NULL);
96   SetStdHandle(STD_OUTPUT_HANDLE, NULL);
97 #elif defined(OS_POSIX)
98   // The files are automatically closed.
99   base::File read_file(STDIN_FILENO);
100   base::File write_file(STDOUT_FILENO);
101 #else
102 #error Not implemented.
103 #endif
104
105   base::MessageLoopForUI message_loop;
106   base::RunLoop run_loop;
107
108   scoped_refptr<AutoThreadTaskRunner> task_runner =
109       new remoting::AutoThreadTaskRunner(message_loop.message_loop_proxy(),
110                                          run_loop.QuitClosure());
111
112   scoped_ptr<It2MeHostFactory> factory(new It2MeHostFactory());
113
114   // Set up the native messaging channel.
115   scoped_ptr<NativeMessagingChannel> channel(
116       new NativeMessagingChannel(read_file.Pass(), write_file.Pass()));
117
118   scoped_ptr<It2MeNativeMessagingHost> host(
119       new It2MeNativeMessagingHost(
120           task_runner, channel.Pass(), factory.Pass()));
121   host->Start(run_loop.QuitClosure());
122
123   // Run the loop until channel is alive.
124   run_loop.Run();
125
126   return kSuccessExitCode;
127 }
128
129 int It2MeNativeMessagingHostMain(int argc, char** argv) {
130   // This object instance is required by Chrome code (such as MessageLoop).
131   base::AtExitManager exit_manager;
132
133   CommandLine::Init(argc, argv);
134   remoting::InitHostLogging();
135
136   return StartIt2MeNativeMessagingHost();
137 }
138
139 }  // namespace remoting