Chromium Code Reviews| 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 |
| 104 bool IPCTestBase::StartClient() { | 102 bool IPCTestBase::StartClient() { |
| 103 CHECK(channel_ || channel_proxy_); | |
| 104 #if defined(OS_WIN) | |
| 105 return StartClientInternal(0); | |
|
viettrungluu
2014/09/15 22:37:27
I don't know if I'm a huge fan of this.
I'd like
Hajime Morrita
2014/09/15 23:51:32
Tried this and now looks simpler. Thanks for the s
| |
| 106 #elif defined(OS_POSIX) | |
| 107 return StartClientInternal( | |
| 108 channel_ | |
| 109 ? channel_->GetClientFileDescriptor() | |
| 110 : channel_proxy_->GetClientFileDescriptor()); | |
| 111 #else | |
| 112 NOTREACHED(); | |
| 113 return false; | |
| 114 #endif | |
| 115 } | |
| 116 | |
| 117 #if defined(OS_POSIX) | |
| 118 bool IPCTestBase::StartClientWithFD(int ipcfd) { | |
| 119 return StartClientInternal(ipcfd); | |
| 120 } | |
| 121 #endif | |
| 122 | |
| 123 bool IPCTestBase::StartClientInternal(int ipcfd) { | |
| 105 DCHECK(client_process_ == base::kNullProcessHandle); | 124 DCHECK(client_process_ == base::kNullProcessHandle); |
|
viettrungluu
2014/09/15 22:37:27
DCHECK_EQ here too
Hajime Morrita
2014/09/15 23:51:32
Done.
| |
| 106 | 125 |
| 107 std::string test_main = test_client_name_ + "TestClientMain"; | 126 std::string test_main = test_client_name_ + "TestClientMain"; |
| 108 | 127 |
| 109 #if defined(OS_WIN) | 128 #if defined(OS_WIN) |
| 110 client_process_ = SpawnChild(test_main); | 129 client_process_ = SpawnChild(test_main); |
| 111 #elif defined(OS_POSIX) | 130 #elif defined(OS_POSIX) |
| 112 base::FileHandleMappingVector fds_to_map; | 131 base::FileHandleMappingVector fds_to_map; |
| 113 const int ipcfd = channel_.get() | |
| 114 ? channel_->GetClientFileDescriptor() | |
| 115 : channel_proxy_->GetClientFileDescriptor(); | |
| 116 if (ipcfd > -1) | 132 if (ipcfd > -1) |
| 117 fds_to_map.push_back(std::pair<int, int>(ipcfd, | 133 fds_to_map.push_back(std::pair<int, int>(ipcfd, |
| 118 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); | 134 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
| 119 base::LaunchOptions options; | 135 base::LaunchOptions options; |
| 120 options.fds_to_remap = &fds_to_map; | 136 options.fds_to_remap = &fds_to_map; |
| 121 client_process_ = SpawnChildWithOptions(test_main, options); | 137 client_process_ = SpawnChildWithOptions(test_main, options); |
| 122 #endif | 138 #endif |
| 123 | 139 |
| 140 if (channel_) | |
| 141 channel_->OnClientLaunched(client_process_); | |
|
viettrungluu
2014/09/15 22:37:27
It seems dubious to call these even when client_pr
Hajime Morrita
2014/09/15 23:51:32
True. Added DCHECK() as there are no cases where t
| |
| 142 if (channel_proxy_) | |
| 143 channel_proxy_->OnClientLaunched(client_process_); | |
| 144 | |
| 124 return client_process_ != base::kNullProcessHandle; | 145 return client_process_ != base::kNullProcessHandle; |
| 125 } | 146 } |
| 126 | 147 |
| 127 bool IPCTestBase::WaitForClientShutdown() { | 148 bool IPCTestBase::WaitForClientShutdown() { |
| 128 DCHECK(client_process_ != base::kNullProcessHandle); | 149 DCHECK(client_process_ != base::kNullProcessHandle); |
| 129 | 150 |
| 130 bool rv = base::WaitForSingleProcess(client_process_, | 151 bool rv = base::WaitForSingleProcess(client_process_, |
| 131 base::TimeDelta::FromSeconds(5)); | 152 base::TimeDelta::FromSeconds(5)); |
| 132 base::CloseProcessHandle(client_process_); | 153 base::CloseProcessHandle(client_process_); |
| 133 client_process_ = base::kNullProcessHandle; | 154 client_process_ = base::kNullProcessHandle; |
| 134 return rv; | 155 return rv; |
| 135 } | 156 } |
| 136 | 157 |
| 158 IPC::ChannelHandle IPCTestBase::GetTestChannelHandle() { | |
| 159 return GetChannelName(test_client_name_); | |
| 160 } | |
| 161 | |
| 137 scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() { | 162 scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() { |
| 138 return message_loop_->message_loop_proxy(); | 163 return message_loop_->message_loop_proxy(); |
| 139 } | 164 } |
| 140 | 165 |
| 141 scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory( | 166 scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory( |
| 142 const IPC::ChannelHandle& handle, | 167 const IPC::ChannelHandle& handle, |
| 143 base::TaskRunner* runner) { | 168 base::TaskRunner* runner) { |
| 144 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER); | 169 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER); |
| 145 } | 170 } |
| OLD | NEW |