OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file implements the Windows service controlling Me2Me host processes | 5 // This file implements the Windows service controlling Me2Me host processes |
6 // running within user sessions. | 6 // running within user sessions. |
7 | 7 |
8 #include "remoting/host/desktop_process.h" | 8 #include "remoting/host/desktop_process.h" |
9 | 9 |
10 #include "base/at_exit.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/file_path.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/scoped_native_library.h" | |
17 #include "base/stringprintf.h" | |
18 #include "base/utf_string_conversions.h" | |
19 #include "base/win/windows_version.h" | |
20 #include "ipc/ipc_channel_proxy.h" | 16 #include "ipc/ipc_channel_proxy.h" |
21 #include "remoting/base/auto_thread.h" | 17 #include "remoting/base/auto_thread.h" |
22 #include "remoting/base/auto_thread_task_runner.h" | 18 #include "remoting/base/auto_thread_task_runner.h" |
| 19 #include "remoting/host/chromoting_messages.h" |
| 20 #include "remoting/host/desktop_session_agent.h" |
23 #include "remoting/host/host_exit_codes.h" | 21 #include "remoting/host/host_exit_codes.h" |
24 #include "remoting/host/logging.h" | |
25 #include "remoting/host/usage_stats_consent.h" | |
26 | |
27 #if defined(OS_MACOSX) | |
28 #include "base/mac/scoped_nsautorelease_pool.h" | |
29 #endif // defined(OS_MACOSX) | |
30 | |
31 #if defined(OS_WIN) | |
32 #include <commctrl.h> | |
33 #endif // defined(OS_WIN) | |
34 | |
35 namespace { | |
36 | |
37 // The command line switch specifying the name of the daemon IPC endpoint. | |
38 const char kDaemonIpcSwitchName[] = "daemon-pipe"; | |
39 | 22 |
40 const char kIoThreadName[] = "I/O thread"; | 23 const char kIoThreadName[] = "I/O thread"; |
41 | 24 |
42 // "--help" or "--?" prints the usage message. | |
43 const char kHelpSwitchName[] = "help"; | |
44 const char kQuestionSwitchName[] = "?"; | |
45 | |
46 const wchar_t kUsageMessage[] = | |
47 L"\n" | |
48 L"Usage: %ls [options]\n" | |
49 L"\n" | |
50 L"Options:\n" | |
51 L" --help, --? - Print this message.\n"; | |
52 | |
53 void usage(const FilePath& program_name) { | |
54 LOG(INFO) << StringPrintf(kUsageMessage, | |
55 UTF16ToWide(program_name.value()).c_str()); | |
56 } | |
57 | |
58 } // namespace | |
59 | |
60 namespace remoting { | 25 namespace remoting { |
61 | 26 |
62 DesktopProcess::DesktopProcess(const std::string& daemon_channel_name) | 27 DesktopProcess::DesktopProcess(const std::string& daemon_channel_name) |
63 : daemon_channel_name_(daemon_channel_name) { | 28 : daemon_channel_name_(daemon_channel_name) { |
64 } | 29 } |
65 | 30 |
66 DesktopProcess::~DesktopProcess() { | 31 DesktopProcess::~DesktopProcess() { |
67 } | 32 } |
68 | 33 |
69 bool DesktopProcess::OnMessageReceived(const IPC::Message& message) { | 34 bool DesktopProcess::OnMessageReceived(const IPC::Message& message) { |
70 return false; | 35 bool handled = true; |
| 36 IPC_BEGIN_MESSAGE_MAP(DesktopProcess, message) |
| 37 IPC_MESSAGE_HANDLER(ChromotingDaemonDesktopMsg_Crash, OnCrash) |
| 38 IPC_MESSAGE_UNHANDLED(handled = false) |
| 39 IPC_END_MESSAGE_MAP() |
| 40 return handled; |
71 } | 41 } |
72 | 42 |
73 void DesktopProcess::OnChannelConnected(int32 peer_pid) { | 43 void DesktopProcess::OnChannelConnected(int32 peer_pid) { |
| 44 VLOG(1) << "IPC: desktop <- daemon (" << peer_pid << ")"; |
| 45 |
74 NOTIMPLEMENTED(); | 46 NOTIMPLEMENTED(); |
75 } | 47 } |
76 | 48 |
77 void DesktopProcess::OnChannelError() { | 49 void DesktopProcess::OnChannelError() { |
78 LOG(ERROR) << "Failed to connect to '" << daemon_channel_name_ << "'"; | 50 // Shutdown the desktop process. |
79 daemon_channel_.reset(); | 51 daemon_channel_.reset(); |
| 52 desktop_agent_.reset(); |
80 } | 53 } |
81 | 54 |
82 int DesktopProcess::Run() { | 55 int DesktopProcess::Run() { |
83 // Create the UI message loop. | 56 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); |
84 MessageLoop message_loop(MessageLoop::TYPE_UI); | |
85 | 57 |
86 { | 58 { |
87 scoped_refptr<AutoThreadTaskRunner> ui_task_runner = | 59 scoped_refptr<AutoThreadTaskRunner> ui_task_runner = |
88 new remoting::AutoThreadTaskRunner(message_loop.message_loop_proxy(), | 60 new remoting::AutoThreadTaskRunner( |
89 MessageLoop::QuitClosure()); | 61 MessageLoop::current()->message_loop_proxy(), |
| 62 MessageLoop::QuitClosure()); |
90 | 63 |
91 // Launch the I/O thread. | 64 // Launch the I/O thread. |
92 scoped_refptr<AutoThreadTaskRunner> io_task_runner = | 65 scoped_refptr<AutoThreadTaskRunner> io_task_runner = |
93 AutoThread::CreateWithType(kIoThreadName, ui_task_runner, | 66 AutoThread::CreateWithType(kIoThreadName, ui_task_runner, |
94 MessageLoop::TYPE_IO); | 67 MessageLoop::TYPE_IO); |
95 | 68 |
| 69 // Create a desktop agent. |
| 70 desktop_agent_ = DesktopSessionAgent::Create(ui_task_runner, |
| 71 io_task_runner); |
| 72 |
| 73 // Start the agent and create an IPC channel to talk to it. It is safe to |
| 74 // use base::Unretained(this) here because the message loop below will run |
| 75 // until |desktop_agent_| is completely destroyed. |
| 76 IPC::PlatformFileForTransit desktop_pipe; |
| 77 if (!desktop_agent_->Start(base::Bind(&DesktopProcess::OnChannelError, |
| 78 base::Unretained(this)), |
| 79 &desktop_pipe)) { |
| 80 desktop_agent_.reset(); |
| 81 return kInitializationFailed; |
| 82 } |
| 83 |
96 // Connect to the daemon. | 84 // Connect to the daemon. |
97 daemon_channel_.reset(new IPC::ChannelProxy(daemon_channel_name_, | 85 daemon_channel_.reset(new IPC::ChannelProxy(daemon_channel_name_, |
98 IPC::Channel::MODE_CLIENT, | 86 IPC::Channel::MODE_CLIENT, |
99 this, | 87 this, |
100 io_task_runner)); | 88 io_task_runner)); |
| 89 |
| 90 // Pass |desktop_pipe| to the daemon. |
| 91 daemon_channel_->Send( |
| 92 new ChromotingDesktopDaemonMsg_DesktopAttached(desktop_pipe)); |
101 } | 93 } |
102 | 94 |
103 // Run the UI message loop. | 95 // Run the UI message loop. |
104 base::RunLoop run_loop; | 96 base::RunLoop run_loop; |
105 run_loop.Run(); | 97 run_loop.Run(); |
106 return 0; | 98 |
| 99 DCHECK(!daemon_channel_); |
| 100 DCHECK(!desktop_agent_); |
| 101 return kSuccessExitCode; |
| 102 } |
| 103 |
| 104 void DesktopProcess::OnCrash(const std::string& function_name, |
| 105 const std::string& file_name, |
| 106 const int& line_number) { |
| 107 // The daemon requested us to crash the process. |
| 108 CHECK(false); |
107 } | 109 } |
108 | 110 |
109 } // namespace remoting | 111 } // namespace remoting |
110 | |
111 int main(int argc, char** argv) { | |
112 #if defined(OS_MACOSX) | |
113 // Needed so we don't leak objects when threads are created. | |
114 base::mac::ScopedNSAutoreleasePool pool; | |
115 #endif | |
116 | |
117 CommandLine::Init(argc, argv); | |
118 | |
119 // This object instance is required by Chrome code (for example, | |
120 // LazyInstance, MessageLoop). | |
121 base::AtExitManager exit_manager; | |
122 | |
123 remoting::InitHostLogging(); | |
124 | |
125 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
126 if (command_line->HasSwitch(kHelpSwitchName) || | |
127 command_line->HasSwitch(kQuestionSwitchName)) { | |
128 usage(command_line->GetProgram()); | |
129 return remoting::kSuccessExitCode; | |
130 } | |
131 | |
132 std::string channel_name = | |
133 command_line->GetSwitchValueASCII(kDaemonIpcSwitchName); | |
134 | |
135 if (channel_name.empty()) { | |
136 usage(command_line->GetProgram()); | |
137 return remoting::kUsageExitCode; | |
138 } | |
139 | |
140 remoting::DesktopProcess desktop_process(channel_name); | |
141 return desktop_process.Run(); | |
142 } | |
143 | |
144 #if defined(OS_WIN) | |
145 | |
146 int CALLBACK WinMain(HINSTANCE instance, | |
147 HINSTANCE previous_instance, | |
148 LPSTR raw_command_line, | |
149 int show_command) { | |
150 #ifdef OFFICIAL_BUILD | |
151 if (remoting::IsUsageStatsAllowed()) { | |
152 remoting::InitializeCrashReporting(); | |
153 } | |
154 #endif // OFFICIAL_BUILD | |
155 | |
156 // Register and initialize common controls. | |
157 INITCOMMONCONTROLSEX info; | |
158 info.dwSize = sizeof(info); | |
159 info.dwICC = ICC_STANDARD_CLASSES; | |
160 InitCommonControlsEx(&info); | |
161 | |
162 // Mark the process as DPI-aware, so Windows won't scale coordinates in APIs. | |
163 // N.B. This API exists on Vista and above. | |
164 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { | |
165 FilePath path(base::GetNativeLibraryName(UTF8ToUTF16("user32"))); | |
166 base::ScopedNativeLibrary user32(path); | |
167 CHECK(user32.is_valid()); | |
168 | |
169 typedef BOOL (WINAPI * SetProcessDPIAwareFn)(); | |
170 SetProcessDPIAwareFn set_process_dpi_aware = | |
171 static_cast<SetProcessDPIAwareFn>( | |
172 user32.GetFunctionPointer("SetProcessDPIAware")); | |
173 set_process_dpi_aware(); | |
174 } | |
175 | |
176 // CommandLine::Init() ignores the passed |argc| and |argv| on Windows getting | |
177 // the command line from GetCommandLineW(), so we can safely pass NULL here. | |
178 return main(0, NULL); | |
179 } | |
180 | |
181 #endif // defined(OS_WIN) | |
OLD | NEW |