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 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "ipc/ipc_test_base.h" | 7 #include "ipc/ipc_test_base.h" |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/process/kill.h" | 10 #include "base/process/kill.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 scoped_ptr<base::MessageLoop> message_loop) { | 45 scoped_ptr<base::MessageLoop> message_loop) { |
46 DCHECK(!test_client_name.empty()); | 46 DCHECK(!test_client_name.empty()); |
47 DCHECK(test_client_name_.empty()); | 47 DCHECK(test_client_name_.empty()); |
48 DCHECK(!message_loop_); | 48 DCHECK(!message_loop_); |
49 | 49 |
50 test_client_name_ = test_client_name; | 50 test_client_name_ = test_client_name; |
51 message_loop_ = message_loop.Pass(); | 51 message_loop_ = message_loop.Pass(); |
52 } | 52 } |
53 | 53 |
54 void IPCTestBase::CreateChannel(IPC::Listener* listener) { | 54 void IPCTestBase::CreateChannel(IPC::Listener* listener) { |
55 CreateChannelFromChannelHandle( | 55 CreateChannelFromChannelHandle(GetTestChannelHandle(), listener); |
56 GetChannelName(test_client_name_), listener); | |
57 } | 56 } |
58 | 57 |
59 bool IPCTestBase::ConnectChannel() { | 58 bool IPCTestBase::ConnectChannel() { |
60 CHECK(channel_.get()); | 59 CHECK(channel_.get()); |
61 return channel_->Connect(); | 60 return channel_->Connect(); |
62 } | 61 } |
63 | 62 |
64 scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() { | 63 scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() { |
65 return channel_.Pass(); | 64 return channel_.Pass(); |
66 } | 65 } |
(...skipping 16 matching lines...) Expand all Loading... | |
83 channel_ = CreateChannelFactory( | 82 channel_ = CreateChannelFactory( |
84 channel_handle, task_runner().get())->BuildChannel(listener); | 83 channel_handle, task_runner().get())->BuildChannel(listener); |
85 } | 84 } |
86 | 85 |
87 void IPCTestBase::CreateChannelProxy( | 86 void IPCTestBase::CreateChannelProxy( |
88 IPC::Listener* listener, | 87 IPC::Listener* listener, |
89 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { | 88 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
90 CHECK(!channel_.get()); | 89 CHECK(!channel_.get()); |
91 CHECK(!channel_proxy_.get()); | 90 CHECK(!channel_proxy_.get()); |
92 channel_proxy_ = IPC::ChannelProxy::Create( | 91 channel_proxy_ = IPC::ChannelProxy::Create( |
93 CreateChannelFactory(GetChannelName(test_client_name_), | 92 CreateChannelFactory(GetTestChannelHandle(), ipc_task_runner.get()), |
94 ipc_task_runner.get()), | |
95 listener, | 93 listener, |
96 ipc_task_runner); | 94 ipc_task_runner); |
97 } | 95 } |
98 | 96 |
99 void IPCTestBase::DestroyChannelProxy() { | 97 void IPCTestBase::DestroyChannelProxy() { |
100 CHECK(channel_proxy_.get()); | 98 CHECK(channel_proxy_.get()); |
101 channel_proxy_.reset(); | 99 channel_proxy_.reset(); |
102 } | 100 } |
103 | 101 |
102 namespace {} // namespace | |
viettrungluu
2014/09/17 17:33:15
delete
Hajime Morrita
2014/09/17 19:33:48
Oops. done.
| |
103 | |
104 std::string IPCTestBase::GetTestMainName() const { | |
105 return test_client_name_ + "TestClientMain"; | |
106 } | |
107 | |
108 bool IPCTestBase::DidStartClient() { | |
109 DCHECK_NE(base::kNullProcessHandle, client_process_); | |
110 return client_process_ != base::kNullProcessHandle; | |
111 } | |
112 | |
113 #if defined(OS_POSIX) | |
114 | |
104 bool IPCTestBase::StartClient() { | 115 bool IPCTestBase::StartClient() { |
105 DCHECK(client_process_ == base::kNullProcessHandle); | 116 return StartClientWithFD(channel_ |
117 ? channel_->GetClientFileDescriptor() | |
118 : channel_proxy_->GetClientFileDescriptor()); | |
119 } | |
106 | 120 |
107 std::string test_main = test_client_name_ + "TestClientMain"; | 121 bool IPCTestBase::StartClientWithFD(int ipcfd) { |
122 DCHECK_EQ(client_process_, base::kNullProcessHandle); | |
108 | 123 |
109 #if defined(OS_WIN) | |
110 client_process_ = SpawnChild(test_main); | |
111 #elif defined(OS_POSIX) | |
112 base::FileHandleMappingVector fds_to_map; | 124 base::FileHandleMappingVector fds_to_map; |
113 const int ipcfd = channel_.get() | |
114 ? channel_->GetClientFileDescriptor() | |
115 : channel_proxy_->GetClientFileDescriptor(); | |
116 if (ipcfd > -1) | 125 if (ipcfd > -1) |
117 fds_to_map.push_back(std::pair<int, int>(ipcfd, | 126 fds_to_map.push_back(std::pair<int, int>(ipcfd, |
118 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); | 127 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
119 base::LaunchOptions options; | 128 base::LaunchOptions options; |
120 options.fds_to_remap = &fds_to_map; | 129 options.fds_to_remap = &fds_to_map; |
121 client_process_ = SpawnChildWithOptions(test_main, options); | 130 client_process_ = SpawnChildWithOptions(GetTestMainName(), options); |
131 | |
132 return DidStartClient(); | |
133 } | |
134 | |
135 #elif defined(OS_WIN) | |
136 | |
137 bool IPCTestBase::StartClient() { | |
138 DCHECK_EQ(client_process_, base::kNullProcessHandle); | |
139 client_process_ = SpawnChild(GetTestMainName()); | |
140 return DidStartClient(); | |
141 } | |
142 | |
122 #endif | 143 #endif |
123 | 144 |
124 return client_process_ != base::kNullProcessHandle; | |
125 } | |
126 | |
127 bool IPCTestBase::WaitForClientShutdown() { | 145 bool IPCTestBase::WaitForClientShutdown() { |
128 DCHECK(client_process_ != base::kNullProcessHandle); | 146 DCHECK(client_process_ != base::kNullProcessHandle); |
129 | 147 |
130 bool rv = base::WaitForSingleProcess(client_process_, | 148 bool rv = base::WaitForSingleProcess(client_process_, |
131 base::TimeDelta::FromSeconds(5)); | 149 base::TimeDelta::FromSeconds(5)); |
132 base::CloseProcessHandle(client_process_); | 150 base::CloseProcessHandle(client_process_); |
133 client_process_ = base::kNullProcessHandle; | 151 client_process_ = base::kNullProcessHandle; |
134 return rv; | 152 return rv; |
135 } | 153 } |
136 | 154 |
155 IPC::ChannelHandle IPCTestBase::GetTestChannelHandle() { | |
156 return GetChannelName(test_client_name_); | |
157 } | |
158 | |
137 scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() { | 159 scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() { |
138 return message_loop_->message_loop_proxy(); | 160 return message_loop_->message_loop_proxy(); |
139 } | 161 } |
140 | 162 |
141 scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory( | 163 scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory( |
142 const IPC::ChannelHandle& handle, | 164 const IPC::ChannelHandle& handle, |
143 base::TaskRunner* runner) { | 165 base::TaskRunner* runner) { |
144 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER); | 166 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER); |
145 } | 167 } |
OLD | NEW |