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