OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/host/worker_process_launcher.h" | |
6 | |
7 #include <limits> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/process_util.h" | |
11 #include "base/rand_util.h" | |
12 #include "base/stringprintf.h" | |
13 #include "base/threading/thread.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "ipc/ipc_channel_proxy.h" | |
16 #include "ipc/ipc_message.h" | |
17 #include "ipc/ipc_message_macros.h" | |
18 | |
19 namespace remoting { | |
20 | |
21 WorkerProcessLauncherWin::WorkerProcessLauncherWin( | |
22 scoped_refptr<base::MessageLoopProxy> ipc_message_loop) | |
23 : ipc_message_loop_(ipc_message_loop), | |
24 listener_(NULL) { | |
25 } | |
26 | |
27 WorkerProcessLauncherWin::~WorkerProcessLauncherWin() { | |
28 Stop(); | |
29 } | |
30 | |
31 bool WorkerProcessLauncherWin::Start( | |
32 IPC::Channel::Listener* listener, | |
33 const CreateChannelHandleCallback& create_channel_handle, | |
34 const LaunchWorkerCallback& launch_worker) { | |
35 DCHECK(ipc_channel_.get() == NULL); | |
36 DCHECK(listener_ == NULL); | |
37 DCHECK(process_.handle() == NULL); | |
38 DCHECK(process_watcher_.GetWatchedObject() == NULL); | |
39 | |
40 std::string channel_name = GenerateRandomChannelId(); | |
41 | |
42 // Create the IPC channel. Details of channel handle creation are offloaded to | |
43 // the callback. | |
44 IPC::ChannelHandle channel_handle; | |
45 if (create_channel_handle.Run(channel_name, &channel_handle)) { | |
46 ipc_channel_.reset(new IPC::ChannelProxy(channel_handle, | |
47 IPC::Channel::MODE_SERVER, | |
48 this, | |
49 ipc_message_loop_)); | |
50 | |
51 // Launch the process and attach an object watcher to the returned process | |
52 // handle so that we get notified if the process terminates. | |
53 if (launch_worker.Run(channel_name, &process_)) { | |
54 if (process_watcher_.StartWatching(get_process().handle(), this)) { | |
55 listener_ = listener; | |
56 return true; | |
57 } | |
58 | |
59 process_.Terminate(0); | |
60 process_.Close(); | |
61 } | |
62 | |
63 ipc_channel_.reset(); | |
64 } | |
65 | |
66 return false; | |
67 } | |
68 | |
69 void WorkerProcessLauncherWin::Stop() { | |
70 ipc_channel_.reset(); | |
71 listener_ = NULL; | |
72 process_watcher_.StopWatching(); | |
73 process_.Terminate(0); | |
74 process_.Close(); | |
75 } | |
76 | |
77 void WorkerProcessLauncherWin::Send(IPC::Message* message) { | |
78 ipc_channel_->Send(message); | |
79 } | |
80 | |
81 void WorkerProcessLauncherWin::OnObjectSignaled(HANDLE object) { | |
82 DCHECK(process_watcher_.GetWatchedObject() == NULL); | |
83 | |
84 if (listener_) { | |
85 listener_->OnChannelError(); | |
86 Stop(); | |
87 } | |
88 } | |
89 | |
90 bool WorkerProcessLauncherWin::OnMessageReceived(const IPC::Message& message) { | |
91 DCHECK(ipc_channel_.get() != NULL); | |
92 DCHECK(process_.handle() != NULL); | |
93 | |
94 return listener_->OnMessageReceived(message); | |
95 } | |
96 | |
97 void WorkerProcessLauncherWin::OnChannelConnected(int32 peer_pid) { | |
98 DCHECK(ipc_channel_.get() != NULL); | |
99 DCHECK(process_.handle() != NULL); | |
100 | |
101 listener_->OnChannelConnected(peer_pid); | |
102 } | |
103 | |
104 void WorkerProcessLauncherWin::OnChannelError() { | |
105 DCHECK(ipc_channel_.get() != NULL); | |
106 | |
107 listener_->OnChannelError(); | |
108 Stop(); | |
109 } | |
110 | |
111 // N.B. Stolen from src/content/common/child_process_host_impl.cc | |
112 std::string WorkerProcessLauncherWin::GenerateRandomChannelId() { | |
113 return base::StringPrintf("%d.%p.%d", | |
114 base::GetCurrentProcId(), this, | |
Wez
2012/03/27 16:45:45
Do we really want to include the in-memory address
| |
115 base::RandInt(0, std::numeric_limits<int>::max())); | |
116 } | |
117 | |
118 base::Process& WorkerProcessLauncherWin::get_process() { | |
Wez
2012/03/27 16:45:45
nit: Prefer to keep methods defined in the order t
| |
119 return process_; | |
120 } | |
121 | |
122 } // namespace remoting | |
OLD | NEW |