[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 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 "ppapi/proxy/ppapi_proxy_test.h" |
| 6 | |
[email protected] | b75673fe | 2012-10-17 17:59:14 | [diff] [blame] | 7 | #include <sstream> |
| 8 | |
[email protected] | 1f6581c | 2011-10-04 23:10:15 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 10 | #include "base/bind_helpers.h" |
[email protected] | 7ccb707 | 2013-06-10 20:56:28 | [diff] [blame] | 11 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 12 | #include "base/observer_list.h" |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 13 | #include "base/process/process_handle.h" |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 14 | #include "base/run_loop.h" |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 15 | #include "ipc/ipc_sync_channel.h" |
dmichael | 6b328f3d | 2014-09-29 23:49:02 | [diff] [blame] | 16 | #include "ipc/message_filter.h" |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 17 | #include "ppapi/c/pp_errors.h" |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 18 | #include "ppapi/c/private/ppb_proxy_private.h" |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 19 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 20 | #include "ppapi/proxy/ppb_message_loop_proxy.h" |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 21 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 22 | namespace ppapi { |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 23 | namespace proxy { |
| 24 | |
| 25 | namespace { |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 26 | // HostDispatcher requires a PPB_Proxy_Private, so we always provide a fallback |
| 27 | // do-nothing implementation. |
| 28 | void PluginCrashed(PP_Module module) { |
| 29 | NOTREACHED(); |
| 30 | }; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 31 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 32 | PP_Instance GetInstanceForResource(PP_Resource resource) { |
| 33 | // If a test relies on this, we need to implement it. |
| 34 | NOTREACHED(); |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | void SetReserveInstanceIDCallback(PP_Module module, |
| 39 | PP_Bool (*is_seen)(PP_Module, PP_Instance)) { |
| 40 | // This function gets called in HostDispatcher's constructor. We simply don't |
| 41 | // worry about Instance uniqueness in tests, so we can ignore the call. |
| 42 | } |
| 43 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 44 | void AddRefModule(PP_Module module) {} |
| 45 | void ReleaseModule(PP_Module module) {} |
[email protected] | fa4dd291 | 2011-10-17 21:23:29 | [diff] [blame] | 46 | PP_Bool IsInModuleDestructor(PP_Module module) { return PP_FALSE; } |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 47 | |
[email protected] | fa4dd291 | 2011-10-17 21:23:29 | [diff] [blame] | 48 | PPB_Proxy_Private ppb_proxy_private = { |
| 49 | &PluginCrashed, |
| 50 | &GetInstanceForResource, |
| 51 | &SetReserveInstanceIDCallback, |
[email protected] | fa4dd291 | 2011-10-17 21:23:29 | [diff] [blame] | 52 | &AddRefModule, |
| 53 | &ReleaseModule, |
| 54 | &IsInModuleDestructor |
| 55 | }; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 56 | |
| 57 | // We allow multiple harnesses at a time to respond to 'GetInterface' calls. |
| 58 | // We assume that only 1 harness's GetInterface function will ever support a |
| 59 | // given interface name. In practice, there will either be only 1 GetInterface |
| 60 | // handler (for PluginProxyTest or HostProxyTest), or there will be only 2 |
| 61 | // GetInterface handlers (for TwoWayTest). In the latter case, one handler is |
| 62 | // for the PluginProxyTestHarness and should only respond for PPP interfaces, |
| 63 | // and the other handler is for the HostProxyTestHarness which should only |
| 64 | // ever respond for PPB interfaces. |
| 65 | ObserverList<ProxyTestHarnessBase> get_interface_handlers_; |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 66 | |
| 67 | const void* MockGetInterface(const char* name) { |
danakj | 4ef352d | 2015-03-09 17:45:39 | [diff] [blame] | 68 | ObserverList<ProxyTestHarnessBase>::Iterator it(&get_interface_handlers_); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 69 | while (ProxyTestHarnessBase* observer = it.GetNext()) { |
| 70 | const void* interface = observer->GetInterface(name); |
| 71 | if (interface) |
| 72 | return interface; |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 73 | } |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 74 | if (strcmp(name, PPB_PROXY_PRIVATE_INTERFACE) == 0) |
| 75 | return &ppb_proxy_private; |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | void SetUpRemoteHarness(ProxyTestHarnessBase* harness, |
| 80 | const IPC::ChannelHandle& handle, |
| 81 | base::MessageLoopProxy* ipc_message_loop_proxy, |
| 82 | base::WaitableEvent* shutdown_event, |
| 83 | base::WaitableEvent* harness_set_up) { |
| 84 | harness->SetUpHarnessWithChannel(handle, ipc_message_loop_proxy, |
| 85 | shutdown_event, false); |
| 86 | harness_set_up->Signal(); |
| 87 | } |
| 88 | |
| 89 | void TearDownRemoteHarness(ProxyTestHarnessBase* harness, |
| 90 | base::WaitableEvent* harness_torn_down) { |
| 91 | harness->TearDownHarness(); |
| 92 | harness_torn_down->Signal(); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 95 | void RunTaskOnRemoteHarness(const base::Closure& task, |
| 96 | base::WaitableEvent* task_complete) { |
| 97 | task.Run(); |
| 98 | task_complete->Signal(); |
| 99 | } |
| 100 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 101 | } // namespace |
| 102 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 103 | // ProxyTestHarnessBase -------------------------------------------------------- |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 104 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 105 | ProxyTestHarnessBase::ProxyTestHarnessBase() : pp_module_(0x98765), |
| 106 | pp_instance_(0x12345) { |
| 107 | get_interface_handlers_.AddObserver(this); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 110 | ProxyTestHarnessBase::~ProxyTestHarnessBase() { |
| 111 | get_interface_handlers_.RemoveObserver(this); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 114 | const void* ProxyTestHarnessBase::GetInterface(const char* name) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 115 | return registered_interfaces_[name]; |
| 116 | } |
| 117 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 118 | void ProxyTestHarnessBase::RegisterTestInterface(const char* name, |
[email protected] | 99ff993 | 2011-09-07 14:14:54 | [diff] [blame] | 119 | const void* test_interface) { |
| 120 | registered_interfaces_[name] = test_interface; |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 121 | } |
| 122 | |
kareng | 1c62eeb | 2014-11-08 16:35:03 | [diff] [blame] | 123 | bool ProxyTestHarnessBase::SupportsInterface(const char* name) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 124 | sink().ClearMessages(); |
| 125 | |
| 126 | // IPC doesn't actually write to this when we send a message manually |
| 127 | // not actually using IPC. |
| 128 | bool unused_result = false; |
kareng | 1c62eeb | 2014-11-08 16:35:03 | [diff] [blame] | 129 | PpapiMsg_SupportsInterface msg(name, &unused_result); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 130 | GetDispatcher()->OnMessageReceived(msg); |
| 131 | |
| 132 | const IPC::Message* reply_msg = |
| 133 | sink().GetUniqueMessageMatching(IPC_REPLY_ID); |
| 134 | EXPECT_TRUE(reply_msg); |
| 135 | if (!reply_msg) |
| 136 | return false; |
| 137 | |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 138 | base::TupleTypes<PpapiMsg_SupportsInterface::ReplyParam>::ValueTuple |
| 139 | reply_data; |
kareng | 1c62eeb | 2014-11-08 16:35:03 | [diff] [blame] | 140 | EXPECT_TRUE(PpapiMsg_SupportsInterface::ReadReplyParam( |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 141 | reply_msg, &reply_data)); |
| 142 | |
| 143 | sink().ClearMessages(); |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 144 | return base::get<0>(reply_data); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 147 | // PluginProxyTestHarness ------------------------------------------------------ |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 148 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 149 | PluginProxyTestHarness::PluginProxyTestHarness( |
| 150 | GlobalsConfiguration globals_config) |
| 151 | : globals_config_(globals_config) { |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 154 | PluginProxyTestHarness::~PluginProxyTestHarness() { |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 157 | PpapiGlobals* PluginProxyTestHarness::GetGlobals() { |
| 158 | return plugin_globals_.get(); |
| 159 | } |
| 160 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 161 | Dispatcher* PluginProxyTestHarness::GetDispatcher() { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 162 | return plugin_dispatcher_.get(); |
| 163 | } |
| 164 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 165 | void PluginProxyTestHarness::SetUpHarness() { |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 166 | // These must be first since the dispatcher set-up uses them. |
dmichael | b11ca7b | 2015-04-02 16:59:40 | [diff] [blame] | 167 | CreatePluginGlobals(nullptr /* ipc_task_runner */); |
[email protected] | b73c659 | 2013-03-30 17:08:13 | [diff] [blame] | 168 | // Some of the methods called during set-up check that the lock is held. |
| 169 | ProxyAutoLock lock; |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 170 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 171 | resource_tracker().DidCreateInstance(pp_instance()); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 172 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 173 | plugin_dispatcher_.reset(new PluginDispatcher( |
[email protected] | bc2eeb4 | 2012-05-02 22:35:53 | [diff] [blame] | 174 | &MockGetInterface, |
[email protected] | 195d4cde | 2012-10-02 18:12:41 | [diff] [blame] | 175 | PpapiPermissions(), |
[email protected] | bc2eeb4 | 2012-05-02 22:35:53 | [diff] [blame] | 176 | false)); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 177 | plugin_dispatcher_->InitWithTestSink(&sink()); |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 178 | // The plugin proxy delegate is needed for |
| 179 | // |PluginProxyDelegate::GetBrowserSender| which is used |
| 180 | // in |ResourceCreationProxy::GetConnection| to get the channel to the |
| 181 | // browser. In this case we just use the |plugin_dispatcher_| as the channel |
| 182 | // for test purposes. |
| 183 | plugin_delegate_mock_.set_browser_sender(plugin_dispatcher_.get()); |
dmichael | fee3a51 | 2014-09-18 21:32:13 | [diff] [blame] | 184 | PluginGlobals::Get()->SetPluginProxyDelegate(&plugin_delegate_mock_); |
[email protected] | 22fdaa6 | 2012-11-30 01:55:44 | [diff] [blame] | 185 | plugin_dispatcher_->DidCreateInstance(pp_instance()); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 188 | void PluginProxyTestHarness::SetUpHarnessWithChannel( |
| 189 | const IPC::ChannelHandle& channel_handle, |
| 190 | base::MessageLoopProxy* ipc_message_loop, |
| 191 | base::WaitableEvent* shutdown_event, |
| 192 | bool is_client) { |
| 193 | // These must be first since the dispatcher set-up uses them. |
dmichael | b11ca7b | 2015-04-02 16:59:40 | [diff] [blame] | 194 | scoped_refptr<base::TaskRunner> ipc_task_runner(ipc_message_loop); |
| 195 | CreatePluginGlobals(ipc_message_loop); |
[email protected] | b73c659 | 2013-03-30 17:08:13 | [diff] [blame] | 196 | // Some of the methods called during set-up check that the lock is held. |
| 197 | ProxyAutoLock lock; |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 198 | |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 199 | resource_tracker().DidCreateInstance(pp_instance()); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 200 | plugin_delegate_mock_.Init(ipc_message_loop, shutdown_event); |
| 201 | |
| 202 | plugin_dispatcher_.reset(new PluginDispatcher( |
[email protected] | bc2eeb4 | 2012-05-02 22:35:53 | [diff] [blame] | 203 | &MockGetInterface, |
[email protected] | 195d4cde | 2012-10-02 18:12:41 | [diff] [blame] | 204 | PpapiPermissions(), |
[email protected] | bc2eeb4 | 2012-05-02 22:35:53 | [diff] [blame] | 205 | false)); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 206 | plugin_dispatcher_->InitPluginWithChannel(&plugin_delegate_mock_, |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 207 | base::kNullProcessId, |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 208 | channel_handle, |
| 209 | is_client); |
[email protected] | 22fdaa6 | 2012-11-30 01:55:44 | [diff] [blame] | 210 | plugin_delegate_mock_.set_browser_sender(plugin_dispatcher_.get()); |
dmichael | fee3a51 | 2014-09-18 21:32:13 | [diff] [blame] | 211 | PluginGlobals::Get()->SetPluginProxyDelegate(&plugin_delegate_mock_); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 212 | plugin_dispatcher_->DidCreateInstance(pp_instance()); |
| 213 | } |
| 214 | |
| 215 | void PluginProxyTestHarness::TearDownHarness() { |
[email protected] | b73c659 | 2013-03-30 17:08:13 | [diff] [blame] | 216 | { |
| 217 | // Some of the methods called during tear-down check that the lock is held. |
| 218 | ProxyAutoLock lock; |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 219 | |
[email protected] | b73c659 | 2013-03-30 17:08:13 | [diff] [blame] | 220 | plugin_dispatcher_->DidDestroyInstance(pp_instance()); |
| 221 | plugin_dispatcher_.reset(); |
| 222 | |
| 223 | resource_tracker().DidDeleteInstance(pp_instance()); |
| 224 | } |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 225 | plugin_globals_.reset(); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 226 | } |
| 227 | |
dmichael | b11ca7b | 2015-04-02 16:59:40 | [diff] [blame] | 228 | void PluginProxyTestHarness::CreatePluginGlobals( |
| 229 | const scoped_refptr<base::TaskRunner>& ipc_task_runner) { |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 230 | if (globals_config_ == PER_THREAD_GLOBALS) { |
dmichael | b11ca7b | 2015-04-02 16:59:40 | [diff] [blame] | 231 | plugin_globals_.reset(new PluginGlobals(PpapiGlobals::PerThreadForTest(), |
| 232 | ipc_task_runner)); |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 233 | PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals()); |
| 234 | } else { |
dmichael | b11ca7b | 2015-04-02 16:59:40 | [diff] [blame] | 235 | plugin_globals_.reset(new PluginGlobals(ipc_task_runner)); |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
skyostil | 12262cf | 2015-05-21 14:49:31 | [diff] [blame] | 239 | base::SingleThreadTaskRunner* |
| 240 | PluginProxyTestHarness::PluginDelegateMock::GetIPCTaskRunner() { |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 241 | return ipc_message_loop_; |
| 242 | } |
| 243 | |
| 244 | base::WaitableEvent* |
| 245 | PluginProxyTestHarness::PluginDelegateMock::GetShutdownEvent() { |
| 246 | return shutdown_event_; |
| 247 | } |
| 248 | |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 249 | IPC::PlatformFileForTransit |
| 250 | PluginProxyTestHarness::PluginDelegateMock::ShareHandleWithRemote( |
| 251 | base::PlatformFile handle, |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 252 | base::ProcessId /* remote_pid */, |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 253 | bool should_close_source) { |
| 254 | return IPC::GetFileHandleForProcess(handle, |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 255 | base::GetCurrentProcessHandle(), |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 256 | should_close_source); |
| 257 | } |
| 258 | |
erikchen | 4fc32d5 | 2015-06-02 02:16:38 | [diff] [blame^] | 259 | base::SharedMemoryHandle |
| 260 | PluginProxyTestHarness::PluginDelegateMock::ShareSharedMemoryHandleWithRemote( |
| 261 | const base::SharedMemoryHandle& handle, |
| 262 | base::ProcessId remote_pid) { |
| 263 | #if defined(OS_POSIX) |
| 264 | return ShareHandleWithRemote(handle.fd, remote_pid, false); |
| 265 | #elif defined(OS_WIN) |
| 266 | return ShareHandleWithRemote(handle, remote_pid, false); |
| 267 | #else |
| 268 | #error Not implemented. |
| 269 | #endif |
| 270 | } |
| 271 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 272 | std::set<PP_Instance>* |
| 273 | PluginProxyTestHarness::PluginDelegateMock::GetGloballySeenInstanceIDSet() { |
| 274 | return &instance_id_set_; |
| 275 | } |
| 276 | |
[email protected] | 6fc87e0 | 2011-12-20 19:18:45 | [diff] [blame] | 277 | uint32 PluginProxyTestHarness::PluginDelegateMock::Register( |
| 278 | PluginDispatcher* plugin_dispatcher) { |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | void PluginProxyTestHarness::PluginDelegateMock::Unregister( |
| 283 | uint32 plugin_dispatcher_id) { |
| 284 | } |
| 285 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 286 | IPC::Sender* PluginProxyTestHarness::PluginDelegateMock::GetBrowserSender() { |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 287 | return browser_sender_; |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 288 | } |
| 289 | |
[email protected] | 2306303a | 2012-06-11 18:10:37 | [diff] [blame] | 290 | std::string PluginProxyTestHarness::PluginDelegateMock::GetUILanguage() { |
| 291 | return std::string("en-US"); |
| 292 | } |
| 293 | |
[email protected] | 6fc87e0 | 2011-12-20 19:18:45 | [diff] [blame] | 294 | void PluginProxyTestHarness::PluginDelegateMock::PreCacheFont( |
| 295 | const void* logfontw) { |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 296 | } |
[email protected] | 7f801d8 | 2011-07-08 23:30:11 | [diff] [blame] | 297 | |
[email protected] | 72a1072 | 2012-06-27 19:30:58 | [diff] [blame] | 298 | void PluginProxyTestHarness::PluginDelegateMock::SetActiveURL( |
| 299 | const std::string& url) { |
| 300 | } |
| 301 | |
[email protected] | 1d14806 | 2013-07-25 20:25:45 | [diff] [blame] | 302 | PP_Resource PluginProxyTestHarness::PluginDelegateMock::CreateBrowserFont( |
| 303 | Connection connection, |
| 304 | PP_Instance instance, |
| 305 | const PP_BrowserFont_Trusted_Description& desc, |
| 306 | const Preferences& prefs) { |
| 307 | return 0; |
| 308 | } |
| 309 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 310 | // PluginProxyTest ------------------------------------------------------------- |
| 311 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 312 | PluginProxyTest::PluginProxyTest() : PluginProxyTestHarness(SINGLETON_GLOBALS) { |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | PluginProxyTest::~PluginProxyTest() { |
| 316 | } |
| 317 | |
| 318 | void PluginProxyTest::SetUp() { |
| 319 | SetUpHarness(); |
| 320 | } |
| 321 | |
| 322 | void PluginProxyTest::TearDown() { |
| 323 | TearDownHarness(); |
| 324 | } |
| 325 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 326 | // PluginProxyMultiThreadTest -------------------------------------------------- |
| 327 | |
| 328 | PluginProxyMultiThreadTest::PluginProxyMultiThreadTest() { |
| 329 | } |
| 330 | |
| 331 | PluginProxyMultiThreadTest::~PluginProxyMultiThreadTest() { |
| 332 | } |
| 333 | |
| 334 | void PluginProxyMultiThreadTest::RunTest() { |
| 335 | main_thread_message_loop_proxy_ = |
| 336 | PpapiGlobals::Get()->GetMainThreadMessageLoop(); |
| 337 | ASSERT_EQ(main_thread_message_loop_proxy_.get(), |
Daniel Cheng | 6d3ae97 | 2014-08-26 00:27:38 | [diff] [blame] | 338 | base::MessageLoopProxy::current().get()); |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 339 | nested_main_thread_message_loop_.reset(new base::RunLoop()); |
| 340 | |
| 341 | secondary_thread_.reset(new base::DelegateSimpleThread( |
| 342 | this, "PluginProxyMultiThreadTest")); |
| 343 | |
| 344 | { |
| 345 | ProxyAutoLock auto_lock; |
| 346 | |
| 347 | // MessageLoopResource assumes that the proxy lock has been acquired. |
| 348 | secondary_thread_message_loop_ = new MessageLoopResource(pp_instance()); |
| 349 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 350 | ASSERT_EQ(PP_OK, |
| 351 | secondary_thread_message_loop_->PostWork( |
| 352 | PP_MakeCompletionCallback( |
| 353 | &PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread, |
| 354 | this), |
| 355 | 0)); |
| 356 | } |
| 357 | |
| 358 | SetUpTestOnMainThread(); |
| 359 | |
| 360 | secondary_thread_->Start(); |
| 361 | nested_main_thread_message_loop_->Run(); |
| 362 | secondary_thread_->Join(); |
| 363 | |
| 364 | { |
| 365 | ProxyAutoLock auto_lock; |
| 366 | |
| 367 | // The destruction requires a valid PpapiGlobals instance, so we should |
| 368 | // explicitly release it. |
| 369 | secondary_thread_message_loop_ = NULL; |
| 370 | } |
| 371 | |
| 372 | secondary_thread_.reset(NULL); |
| 373 | nested_main_thread_message_loop_.reset(NULL); |
| 374 | main_thread_message_loop_proxy_ = NULL; |
| 375 | } |
| 376 | |
| 377 | void PluginProxyMultiThreadTest::CheckOnThread(ThreadType thread_type) { |
| 378 | ProxyAutoLock auto_lock; |
| 379 | if (thread_type == MAIN_THREAD) { |
| 380 | ASSERT_TRUE(MessageLoopResource::GetCurrent()->is_main_thread_loop()); |
| 381 | } else { |
| 382 | ASSERT_EQ(secondary_thread_message_loop_.get(), |
| 383 | MessageLoopResource::GetCurrent()); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void PluginProxyMultiThreadTest::PostQuitForMainThread() { |
| 388 | main_thread_message_loop_proxy_->PostTask( |
| 389 | FROM_HERE, |
| 390 | base::Bind(&PluginProxyMultiThreadTest::QuitNestedLoop, |
| 391 | base::Unretained(this))); |
| 392 | } |
| 393 | |
| 394 | void PluginProxyMultiThreadTest::PostQuitForSecondaryThread() { |
| 395 | ProxyAutoLock auto_lock; |
| 396 | secondary_thread_message_loop_->PostQuit(PP_TRUE); |
| 397 | } |
| 398 | |
| 399 | void PluginProxyMultiThreadTest::Run() { |
| 400 | ProxyAutoLock auto_lock; |
| 401 | ASSERT_EQ(PP_OK, secondary_thread_message_loop_->AttachToCurrentThread()); |
| 402 | ASSERT_EQ(PP_OK, secondary_thread_message_loop_->Run()); |
[email protected] | 1b77701 | 2013-07-15 20:42:38 | [diff] [blame] | 403 | secondary_thread_message_loop_->DetachFromThread(); |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void PluginProxyMultiThreadTest::QuitNestedLoop() { |
| 407 | nested_main_thread_message_loop_->Quit(); |
| 408 | } |
| 409 | |
| 410 | // static |
| 411 | void PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread( |
| 412 | void* user_data, |
| 413 | int32_t result) { |
| 414 | EXPECT_EQ(PP_OK, result); |
| 415 | PluginProxyMultiThreadTest* thiz = |
| 416 | static_cast<PluginProxyMultiThreadTest*>(user_data); |
| 417 | thiz->CheckOnThread(SECONDARY_THREAD); |
| 418 | thiz->SetUpTestOnSecondaryThread(); |
| 419 | } |
| 420 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 421 | // HostProxyTestHarness -------------------------------------------------------- |
| 422 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 423 | HostProxyTestHarness::HostProxyTestHarness(GlobalsConfiguration globals_config) |
dmichael | 6b328f3d | 2014-09-29 23:49:02 | [diff] [blame] | 424 | : globals_config_(globals_config) { |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | HostProxyTestHarness::~HostProxyTestHarness() { |
| 428 | } |
| 429 | |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 430 | PpapiGlobals* HostProxyTestHarness::GetGlobals() { |
| 431 | return host_globals_.get(); |
| 432 | } |
| 433 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 434 | Dispatcher* HostProxyTestHarness::GetDispatcher() { |
| 435 | return host_dispatcher_.get(); |
| 436 | } |
| 437 | |
| 438 | void HostProxyTestHarness::SetUpHarness() { |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 439 | // These must be first since the dispatcher set-up uses them. |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 440 | CreateHostGlobals(); |
| 441 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 442 | host_dispatcher_.reset(new HostDispatcher( |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 443 | pp_module(), |
[email protected] | 8be4584 | 2012-04-13 19:49:29 | [diff] [blame] | 444 | &MockGetInterface, |
[email protected] | 73e20db | 2012-12-12 22:27:06 | [diff] [blame] | 445 | PpapiPermissions::AllPermissions())); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 446 | host_dispatcher_->InitWithTestSink(&sink()); |
| 447 | HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get()); |
| 448 | } |
| 449 | |
| 450 | void HostProxyTestHarness::SetUpHarnessWithChannel( |
| 451 | const IPC::ChannelHandle& channel_handle, |
| 452 | base::MessageLoopProxy* ipc_message_loop, |
| 453 | base::WaitableEvent* shutdown_event, |
| 454 | bool is_client) { |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 455 | // These must be first since the dispatcher set-up uses them. |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 456 | CreateHostGlobals(); |
| 457 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 458 | delegate_mock_.Init(ipc_message_loop, shutdown_event); |
[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 459 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 460 | host_dispatcher_.reset(new HostDispatcher( |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 461 | pp_module(), |
[email protected] | 8be4584 | 2012-04-13 19:49:29 | [diff] [blame] | 462 | &MockGetInterface, |
[email protected] | 73e20db | 2012-12-12 22:27:06 | [diff] [blame] | 463 | PpapiPermissions::AllPermissions())); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 464 | ppapi::Preferences preferences; |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 465 | host_dispatcher_->InitHostWithChannel(&delegate_mock_, |
| 466 | base::kNullProcessId, channel_handle, |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 467 | is_client, preferences); |
| 468 | HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get()); |
| 469 | } |
| 470 | |
| 471 | void HostProxyTestHarness::TearDownHarness() { |
| 472 | HostDispatcher::RemoveForInstance(pp_instance()); |
| 473 | host_dispatcher_.reset(); |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 474 | host_globals_.reset(); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 475 | } |
| 476 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 477 | void HostProxyTestHarness::CreateHostGlobals() { |
dmichael | bd96c1e | 2015-02-12 00:58:08 | [diff] [blame] | 478 | disable_locking_.reset(new ProxyLock::LockingDisablerForTest); |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 479 | if (globals_config_ == PER_THREAD_GLOBALS) { |
| 480 | host_globals_.reset(new TestGlobals(PpapiGlobals::PerThreadForTest())); |
| 481 | PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals()); |
| 482 | } else { |
| 483 | host_globals_.reset(new TestGlobals()); |
| 484 | } |
| 485 | } |
| 486 | |
skyostil | 12262cf | 2015-05-21 14:49:31 | [diff] [blame] | 487 | base::MessageLoopProxy* HostProxyTestHarness::DelegateMock::GetIPCTaskRunner() { |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 488 | return ipc_message_loop_; |
| 489 | } |
| 490 | |
| 491 | base::WaitableEvent* HostProxyTestHarness::DelegateMock::GetShutdownEvent() { |
| 492 | return shutdown_event_; |
| 493 | } |
| 494 | |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 495 | IPC::PlatformFileForTransit |
| 496 | HostProxyTestHarness::DelegateMock::ShareHandleWithRemote( |
| 497 | base::PlatformFile handle, |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 498 | base::ProcessId /* remote_pid */, |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 499 | bool should_close_source) { |
| 500 | return IPC::GetFileHandleForProcess(handle, |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 501 | base::GetCurrentProcessHandle(), |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 502 | should_close_source); |
| 503 | } |
| 504 | |
erikchen | 4fc32d5 | 2015-06-02 02:16:38 | [diff] [blame^] | 505 | base::SharedMemoryHandle |
| 506 | HostProxyTestHarness::DelegateMock::ShareSharedMemoryHandleWithRemote( |
| 507 | const base::SharedMemoryHandle& handle, |
| 508 | base::ProcessId remote_pid) { |
| 509 | #if defined(OS_POSIX) |
| 510 | return ShareHandleWithRemote(handle.fd, remote_pid, false); |
| 511 | #elif defined(OS_WIN) |
| 512 | return ShareHandleWithRemote(handle, remote_pid, false); |
| 513 | #else |
| 514 | #error Not implemented. |
| 515 | #endif |
| 516 | } |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 517 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 518 | // HostProxyTest --------------------------------------------------------------- |
| 519 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 520 | HostProxyTest::HostProxyTest() : HostProxyTestHarness(SINGLETON_GLOBALS) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | HostProxyTest::~HostProxyTest() { |
| 524 | } |
| 525 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 526 | void HostProxyTest::SetUp() { |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 527 | SetUpHarness(); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | void HostProxyTest::TearDown() { |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 531 | TearDownHarness(); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 532 | } |
| 533 | |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 534 | // TwoWayTest --------------------------------------------------------------- |
| 535 | |
| 536 | TwoWayTest::TwoWayTest(TwoWayTest::TwoWayTestMode test_mode) |
| 537 | : test_mode_(test_mode), |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 538 | host_(ProxyTestHarnessBase::PER_THREAD_GLOBALS), |
| 539 | plugin_(ProxyTestHarnessBase::PER_THREAD_GLOBALS), |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 540 | io_thread_("TwoWayTest_IOThread"), |
| 541 | plugin_thread_("TwoWayTest_PluginThread"), |
| 542 | remote_harness_(NULL), |
| 543 | local_harness_(NULL), |
| 544 | channel_created_(true, false), |
| 545 | shutdown_event_(true, false) { |
| 546 | if (test_mode == TEST_PPP_INTERFACE) { |
| 547 | remote_harness_ = &plugin_; |
| 548 | local_harness_ = &host_; |
| 549 | } else { |
| 550 | remote_harness_ = &host_; |
| 551 | local_harness_ = &plugin_; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | TwoWayTest::~TwoWayTest() { |
| 556 | shutdown_event_.Signal(); |
| 557 | } |
| 558 | |
| 559 | void TwoWayTest::SetUp() { |
| 560 | base::Thread::Options options; |
[email protected] | d2881d8 | 2013-05-06 19:23:08 | [diff] [blame] | 561 | options.message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 562 | io_thread_.StartWithOptions(options); |
| 563 | plugin_thread_.Start(); |
| 564 | |
[email protected] | b75673fe | 2012-10-17 17:59:14 | [diff] [blame] | 565 | // Construct the IPC handle name using the process ID so we can safely run |
| 566 | // multiple |TwoWayTest|s concurrently. |
| 567 | std::ostringstream handle_name; |
| 568 | handle_name << "TwoWayTestChannel" << base::GetCurrentProcId(); |
| 569 | IPC::ChannelHandle handle(handle_name.str()); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 570 | base::WaitableEvent remote_harness_set_up(true, false); |
| 571 | plugin_thread_.message_loop_proxy()->PostTask( |
| 572 | FROM_HERE, |
[email protected] | 1f6581c | 2011-10-04 23:10:15 | [diff] [blame] | 573 | base::Bind(&SetUpRemoteHarness, |
| 574 | remote_harness_, |
| 575 | handle, |
| 576 | io_thread_.message_loop_proxy(), |
| 577 | &shutdown_event_, |
| 578 | &remote_harness_set_up)); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 579 | remote_harness_set_up.Wait(); |
| 580 | local_harness_->SetUpHarnessWithChannel(handle, |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 581 | io_thread_.message_loop_proxy().get(), |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 582 | &shutdown_event_, |
| 583 | true); // is_client |
| 584 | } |
| 585 | |
| 586 | void TwoWayTest::TearDown() { |
| 587 | base::WaitableEvent remote_harness_torn_down(true, false); |
| 588 | plugin_thread_.message_loop_proxy()->PostTask( |
| 589 | FROM_HERE, |
[email protected] | 1f6581c | 2011-10-04 23:10:15 | [diff] [blame] | 590 | base::Bind(&TearDownRemoteHarness, |
| 591 | remote_harness_, |
| 592 | &remote_harness_torn_down)); |
[email protected] | 912f3d6c | 2011-06-29 18:26:36 | [diff] [blame] | 593 | remote_harness_torn_down.Wait(); |
| 594 | |
| 595 | local_harness_->TearDownHarness(); |
| 596 | |
| 597 | io_thread_.Stop(); |
| 598 | } |
| 599 | |
[email protected] | d57fd38 | 2012-09-19 00:55:42 | [diff] [blame] | 600 | void TwoWayTest::PostTaskOnRemoteHarness(const base::Closure& task) { |
| 601 | base::WaitableEvent task_complete(true, false); |
| 602 | plugin_thread_.message_loop_proxy()->PostTask(FROM_HERE, |
| 603 | base::Bind(&RunTaskOnRemoteHarness, |
| 604 | task, |
| 605 | &task_complete)); |
| 606 | task_complete.Wait(); |
| 607 | } |
| 608 | |
| 609 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 610 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 611 | } // namespace ppapi |