blob: 587d0de315dd4ec5c99593c2abb8046ba764ce72 [file] [log] [blame]
[email protected]73097562012-01-12 19:38:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f24448db2011-01-27 20:40:392// 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
tzika0f614d2016-03-08 00:35:157#include <tuple>
[email protected]b75673fe2012-10-17 17:59:148
[email protected]1f6581c2011-10-04 23:10:159#include "base/bind.h"
[email protected]7ef6b79b2013-01-17 02:38:2410#include "base/bind_helpers.h"
skyostil23490d42015-06-12 12:54:2611#include "base/location.h"
[email protected]912f3d6c2011-06-29 18:26:3612#include "base/observer_list.h"
rvargas079d1842014-10-17 22:32:1613#include "base/process/process_handle.h"
[email protected]7ef6b79b2013-01-17 02:38:2414#include "base/run_loop.h"
skyostil23490d42015-06-12 12:54:2615#include "base/single_thread_task_runner.h"
gabb19326e2016-05-11 18:10:3116#include "base/threading/thread_task_runner_handle.h"
[email protected]912f3d6c2011-06-29 18:26:3617#include "ipc/ipc_sync_channel.h"
dmichael6b328f3d2014-09-29 23:49:0218#include "ipc/message_filter.h"
[email protected]f24448db2011-01-27 20:40:3919#include "ppapi/c/pp_errors.h"
[email protected]912f3d6c2011-06-29 18:26:3620#include "ppapi/c/private/ppb_proxy_private.h"
[email protected]465faa22011-02-08 16:31:4621#include "ppapi/proxy/ppapi_messages.h"
[email protected]7ef6b79b2013-01-17 02:38:2422#include "ppapi/proxy/ppb_message_loop_proxy.h"
[email protected]f24448db2011-01-27 20:40:3923
[email protected]4d2efd22011-08-18 21:58:0224namespace ppapi {
[email protected]f24448db2011-01-27 20:40:3925namespace proxy {
26
27namespace {
[email protected]912f3d6c2011-06-29 18:26:3628// HostDispatcher requires a PPB_Proxy_Private, so we always provide a fallback
29// do-nothing implementation.
30void PluginCrashed(PP_Module module) {
31 NOTREACHED();
32};
[email protected]f24448db2011-01-27 20:40:3933
[email protected]912f3d6c2011-06-29 18:26:3634PP_Instance GetInstanceForResource(PP_Resource resource) {
35 // If a test relies on this, we need to implement it.
36 NOTREACHED();
37 return 0;
38}
39
40void SetReserveInstanceIDCallback(PP_Module module,
41 PP_Bool (*is_seen)(PP_Module, PP_Instance)) {
42 // This function gets called in HostDispatcher's constructor. We simply don't
43 // worry about Instance uniqueness in tests, so we can ignore the call.
44}
45
[email protected]912f3d6c2011-06-29 18:26:3646void AddRefModule(PP_Module module) {}
47void ReleaseModule(PP_Module module) {}
[email protected]fa4dd2912011-10-17 21:23:2948PP_Bool IsInModuleDestructor(PP_Module module) { return PP_FALSE; }
[email protected]912f3d6c2011-06-29 18:26:3649
[email protected]fa4dd2912011-10-17 21:23:2950PPB_Proxy_Private ppb_proxy_private = {
51 &PluginCrashed,
52 &GetInstanceForResource,
53 &SetReserveInstanceIDCallback,
[email protected]fa4dd2912011-10-17 21:23:2954 &AddRefModule,
55 &ReleaseModule,
56 &IsInModuleDestructor
57};
[email protected]912f3d6c2011-06-29 18:26:3658
59// We allow multiple harnesses at a time to respond to 'GetInterface' calls.
60// We assume that only 1 harness's GetInterface function will ever support a
61// given interface name. In practice, there will either be only 1 GetInterface
62// handler (for PluginProxyTest or HostProxyTest), or there will be only 2
63// GetInterface handlers (for TwoWayTest). In the latter case, one handler is
64// for the PluginProxyTestHarness and should only respond for PPP interfaces,
65// and the other handler is for the HostProxyTestHarness which should only
66// ever respond for PPB interfaces.
brettw236d3172015-06-03 16:31:4367base::ObserverList<ProxyTestHarnessBase> get_interface_handlers_;
[email protected]465faa22011-02-08 16:31:4668
69const void* MockGetInterface(const char* name) {
dcheng9fae50222016-10-14 03:21:0470 for (auto& observer : get_interface_handlers_) {
71 const void* interface = observer.GetInterface(name);
[email protected]912f3d6c2011-06-29 18:26:3672 if (interface)
73 return interface;
[email protected]465faa22011-02-08 16:31:4674 }
[email protected]912f3d6c2011-06-29 18:26:3675 if (strcmp(name, PPB_PROXY_PRIVATE_INTERFACE) == 0)
76 return &ppb_proxy_private;
77 return NULL;
78}
79
80void SetUpRemoteHarness(ProxyTestHarnessBase* harness,
81 const IPC::ChannelHandle& handle,
skyostil23490d42015-06-12 12:54:2682 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:3683 base::WaitableEvent* shutdown_event,
84 base::WaitableEvent* harness_set_up) {
skyostil23490d42015-06-12 12:54:2685 harness->SetUpHarnessWithChannel(handle, ipc_task_runner, shutdown_event,
86 false);
[email protected]912f3d6c2011-06-29 18:26:3687 harness_set_up->Signal();
88}
89
90void TearDownRemoteHarness(ProxyTestHarnessBase* harness,
91 base::WaitableEvent* harness_torn_down) {
92 harness->TearDownHarness();
93 harness_torn_down->Signal();
[email protected]f24448db2011-01-27 20:40:3994}
95
[email protected]d57fd382012-09-19 00:55:4296void RunTaskOnRemoteHarness(const base::Closure& task,
97 base::WaitableEvent* task_complete) {
98 task.Run();
99 task_complete->Signal();
100}
101
[email protected]f24448db2011-01-27 20:40:39102} // namespace
103
[email protected]912f3d6c2011-06-29 18:26:36104// ProxyTestHarnessBase --------------------------------------------------------
[email protected]465faa22011-02-08 16:31:46105
[email protected]912f3d6c2011-06-29 18:26:36106ProxyTestHarnessBase::ProxyTestHarnessBase() : pp_module_(0x98765),
107 pp_instance_(0x12345) {
108 get_interface_handlers_.AddObserver(this);
[email protected]465faa22011-02-08 16:31:46109}
110
[email protected]912f3d6c2011-06-29 18:26:36111ProxyTestHarnessBase::~ProxyTestHarnessBase() {
112 get_interface_handlers_.RemoveObserver(this);
[email protected]465faa22011-02-08 16:31:46113}
114
[email protected]912f3d6c2011-06-29 18:26:36115const void* ProxyTestHarnessBase::GetInterface(const char* name) {
[email protected]465faa22011-02-08 16:31:46116 return registered_interfaces_[name];
117}
118
[email protected]912f3d6c2011-06-29 18:26:36119void ProxyTestHarnessBase::RegisterTestInterface(const char* name,
[email protected]99ff9932011-09-07 14:14:54120 const void* test_interface) {
121 registered_interfaces_[name] = test_interface;
[email protected]465faa22011-02-08 16:31:46122}
123
kareng1c62eeb2014-11-08 16:35:03124bool ProxyTestHarnessBase::SupportsInterface(const char* name) {
[email protected]465faa22011-02-08 16:31:46125 sink().ClearMessages();
126
127 // IPC doesn't actually write to this when we send a message manually
128 // not actually using IPC.
129 bool unused_result = false;
kareng1c62eeb2014-11-08 16:35:03130 PpapiMsg_SupportsInterface msg(name, &unused_result);
[email protected]465faa22011-02-08 16:31:46131 GetDispatcher()->OnMessageReceived(msg);
132
133 const IPC::Message* reply_msg =
134 sink().GetUniqueMessageMatching(IPC_REPLY_ID);
135 EXPECT_TRUE(reply_msg);
136 if (!reply_msg)
137 return false;
138
mdempsky390ab1e2016-03-07 22:18:35139 PpapiMsg_SupportsInterface::ReplyParam reply_data;
kareng1c62eeb2014-11-08 16:35:03140 EXPECT_TRUE(PpapiMsg_SupportsInterface::ReadReplyParam(
[email protected]465faa22011-02-08 16:31:46141 reply_msg, &reply_data));
142
143 sink().ClearMessages();
tzika0f614d2016-03-08 00:35:15144 return std::get<0>(reply_data);
[email protected]465faa22011-02-08 16:31:46145}
146
[email protected]912f3d6c2011-06-29 18:26:36147// PluginProxyTestHarness ------------------------------------------------------
[email protected]465faa22011-02-08 16:31:46148
[email protected]7ef6b79b2013-01-17 02:38:24149PluginProxyTestHarness::PluginProxyTestHarness(
150 GlobalsConfiguration globals_config)
151 : globals_config_(globals_config) {
[email protected]f24448db2011-01-27 20:40:39152}
153
[email protected]912f3d6c2011-06-29 18:26:36154PluginProxyTestHarness::~PluginProxyTestHarness() {
[email protected]f24448db2011-01-27 20:40:39155}
156
[email protected]d57fd382012-09-19 00:55:42157PpapiGlobals* PluginProxyTestHarness::GetGlobals() {
158 return plugin_globals_.get();
159}
160
[email protected]912f3d6c2011-06-29 18:26:36161Dispatcher* PluginProxyTestHarness::GetDispatcher() {
[email protected]465faa22011-02-08 16:31:46162 return plugin_dispatcher_.get();
163}
164
[email protected]912f3d6c2011-06-29 18:26:36165void PluginProxyTestHarness::SetUpHarness() {
[email protected]f24448db2011-01-27 20:40:39166 // These must be first since the dispatcher set-up uses them.
dmichaelb11ca7b2015-04-02 16:59:40167 CreatePluginGlobals(nullptr /* ipc_task_runner */);
[email protected]b73c6592013-03-30 17:08:13168 // Some of the methods called during set-up check that the lock is held.
169 ProxyAutoLock lock;
[email protected]7ef6b79b2013-01-17 02:38:24170
[email protected]794d83cd2011-10-20 19:09:20171 resource_tracker().DidCreateInstance(pp_instance());
[email protected]f24448db2011-01-27 20:40:39172
[email protected]f24448db2011-01-27 20:40:39173 plugin_dispatcher_.reset(new PluginDispatcher(
[email protected]bc2eeb42012-05-02 22:35:53174 &MockGetInterface,
[email protected]195d4cde2012-10-02 18:12:41175 PpapiPermissions(),
[email protected]bc2eeb42012-05-02 22:35:53176 false));
[email protected]465faa22011-02-08 16:31:46177 plugin_dispatcher_->InitWithTestSink(&sink());
[email protected]d57fd382012-09-19 00:55:42178 // 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());
dmichaelfee3a512014-09-18 21:32:13184 PluginGlobals::Get()->SetPluginProxyDelegate(&plugin_delegate_mock_);
[email protected]22fdaa62012-11-30 01:55:44185 plugin_dispatcher_->DidCreateInstance(pp_instance());
[email protected]f24448db2011-01-27 20:40:39186}
187
[email protected]912f3d6c2011-06-29 18:26:36188void PluginProxyTestHarness::SetUpHarnessWithChannel(
189 const IPC::ChannelHandle& channel_handle,
skyostil23490d42015-06-12 12:54:26190 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:36191 base::WaitableEvent* shutdown_event,
192 bool is_client) {
193 // These must be first since the dispatcher set-up uses them.
skyostil23490d42015-06-12 12:54:26194 CreatePluginGlobals(ipc_task_runner);
[email protected]b73c6592013-03-30 17:08:13195 // Some of the methods called during set-up check that the lock is held.
196 ProxyAutoLock lock;
[email protected]7ef6b79b2013-01-17 02:38:24197
[email protected]794d83cd2011-10-20 19:09:20198 resource_tracker().DidCreateInstance(pp_instance());
skyostil23490d42015-06-12 12:54:26199 plugin_delegate_mock_.Init(ipc_task_runner, shutdown_event);
[email protected]912f3d6c2011-06-29 18:26:36200
201 plugin_dispatcher_.reset(new PluginDispatcher(
[email protected]bc2eeb42012-05-02 22:35:53202 &MockGetInterface,
[email protected]195d4cde2012-10-02 18:12:41203 PpapiPermissions(),
[email protected]bc2eeb42012-05-02 22:35:53204 false));
[email protected]912f3d6c2011-06-29 18:26:36205 plugin_dispatcher_->InitPluginWithChannel(&plugin_delegate_mock_,
[email protected]108fd342013-01-04 20:46:54206 base::kNullProcessId,
[email protected]912f3d6c2011-06-29 18:26:36207 channel_handle,
208 is_client);
[email protected]22fdaa62012-11-30 01:55:44209 plugin_delegate_mock_.set_browser_sender(plugin_dispatcher_.get());
dmichaelfee3a512014-09-18 21:32:13210 PluginGlobals::Get()->SetPluginProxyDelegate(&plugin_delegate_mock_);
[email protected]912f3d6c2011-06-29 18:26:36211 plugin_dispatcher_->DidCreateInstance(pp_instance());
212}
213
214void PluginProxyTestHarness::TearDownHarness() {
[email protected]b73c6592013-03-30 17:08:13215 {
216 // Some of the methods called during tear-down check that the lock is held.
217 ProxyAutoLock lock;
[email protected]f24448db2011-01-27 20:40:39218
[email protected]b73c6592013-03-30 17:08:13219 plugin_dispatcher_->DidDestroyInstance(pp_instance());
220 plugin_dispatcher_.reset();
221
222 resource_tracker().DidDeleteInstance(pp_instance());
223 }
[email protected]d57fd382012-09-19 00:55:42224 plugin_globals_.reset();
[email protected]f24448db2011-01-27 20:40:39225}
226
dmichaelb11ca7b2015-04-02 16:59:40227void PluginProxyTestHarness::CreatePluginGlobals(
228 const scoped_refptr<base::TaskRunner>& ipc_task_runner) {
[email protected]7ef6b79b2013-01-17 02:38:24229 if (globals_config_ == PER_THREAD_GLOBALS) {
dmichaelb11ca7b2015-04-02 16:59:40230 plugin_globals_.reset(new PluginGlobals(PpapiGlobals::PerThreadForTest(),
231 ipc_task_runner));
[email protected]7ef6b79b2013-01-17 02:38:24232 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
233 } else {
dmichaelb11ca7b2015-04-02 16:59:40234 plugin_globals_.reset(new PluginGlobals(ipc_task_runner));
[email protected]7ef6b79b2013-01-17 02:38:24235 }
236}
237
skyostil12262cf2015-05-21 14:49:31238base::SingleThreadTaskRunner*
239PluginProxyTestHarness::PluginDelegateMock::GetIPCTaskRunner() {
skyostil23490d42015-06-12 12:54:26240 return ipc_task_runner_;
[email protected]912f3d6c2011-06-29 18:26:36241}
242
243base::WaitableEvent*
244PluginProxyTestHarness::PluginDelegateMock::GetShutdownEvent() {
245 return shutdown_event_;
246}
247
[email protected]f0ecb552012-05-11 22:09:11248IPC::PlatformFileForTransit
249PluginProxyTestHarness::PluginDelegateMock::ShareHandleWithRemote(
250 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54251 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11252 bool should_close_source) {
erikchen09b40032016-04-06 18:51:55253 return IPC::GetPlatformFileForTransit(handle,
254 should_close_source);
[email protected]f0ecb552012-05-11 22:09:11255}
256
erikchen4fc32d52015-06-02 02:16:38257base::SharedMemoryHandle
258PluginProxyTestHarness::PluginDelegateMock::ShareSharedMemoryHandleWithRemote(
259 const base::SharedMemoryHandle& handle,
erikchenfc29ad992015-06-04 20:44:18260 base::ProcessId /* remote_pid */) {
261 return base::SharedMemory::DuplicateHandle(handle);
erikchen4fc32d52015-06-02 02:16:38262}
263
[email protected]912f3d6c2011-06-29 18:26:36264std::set<PP_Instance>*
265PluginProxyTestHarness::PluginDelegateMock::GetGloballySeenInstanceIDSet() {
266 return &instance_id_set_;
267}
268
avie029c4132015-12-23 06:45:22269uint32_t PluginProxyTestHarness::PluginDelegateMock::Register(
[email protected]6fc87e02011-12-20 19:18:45270 PluginDispatcher* plugin_dispatcher) {
271 return 0;
272}
273
274void PluginProxyTestHarness::PluginDelegateMock::Unregister(
avie029c4132015-12-23 06:45:22275 uint32_t plugin_dispatcher_id) {}
[email protected]6fc87e02011-12-20 19:18:45276
[email protected]93df81e2012-08-10 22:22:46277IPC::Sender* PluginProxyTestHarness::PluginDelegateMock::GetBrowserSender() {
[email protected]d57fd382012-09-19 00:55:42278 return browser_sender_;
[email protected]93df81e2012-08-10 22:22:46279}
280
[email protected]2306303a2012-06-11 18:10:37281std::string PluginProxyTestHarness::PluginDelegateMock::GetUILanguage() {
282 return std::string("en-US");
283}
284
mgiuca8ca59182015-07-08 02:10:21285void PluginProxyTestHarness::PluginDelegateMock::PreCacheFontForFlash(
[email protected]6fc87e02011-12-20 19:18:45286 const void* logfontw) {
[email protected]373a95a2011-07-01 16:58:14287}
[email protected]7f801d82011-07-08 23:30:11288
[email protected]72a10722012-06-27 19:30:58289void PluginProxyTestHarness::PluginDelegateMock::SetActiveURL(
290 const std::string& url) {
291}
292
[email protected]1d148062013-07-25 20:25:45293PP_Resource PluginProxyTestHarness::PluginDelegateMock::CreateBrowserFont(
294 Connection connection,
295 PP_Instance instance,
296 const PP_BrowserFont_Trusted_Description& desc,
297 const Preferences& prefs) {
298 return 0;
299}
300
[email protected]912f3d6c2011-06-29 18:26:36301// PluginProxyTest -------------------------------------------------------------
302
[email protected]7ef6b79b2013-01-17 02:38:24303PluginProxyTest::PluginProxyTest() : PluginProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]912f3d6c2011-06-29 18:26:36304}
305
306PluginProxyTest::~PluginProxyTest() {
307}
308
309void PluginProxyTest::SetUp() {
310 SetUpHarness();
311}
312
313void PluginProxyTest::TearDown() {
314 TearDownHarness();
315}
316
[email protected]7ef6b79b2013-01-17 02:38:24317// PluginProxyMultiThreadTest --------------------------------------------------
318
319PluginProxyMultiThreadTest::PluginProxyMultiThreadTest() {
320}
321
322PluginProxyMultiThreadTest::~PluginProxyMultiThreadTest() {
323}
324
325void PluginProxyMultiThreadTest::RunTest() {
skyostil23490d42015-06-12 12:54:26326 main_thread_task_runner_ = PpapiGlobals::Get()->GetMainThreadMessageLoop();
327 ASSERT_EQ(main_thread_task_runner_.get(),
328 base::ThreadTaskRunnerHandle::Get().get());
[email protected]7ef6b79b2013-01-17 02:38:24329 nested_main_thread_message_loop_.reset(new base::RunLoop());
330
331 secondary_thread_.reset(new base::DelegateSimpleThread(
332 this, "PluginProxyMultiThreadTest"));
333
334 {
335 ProxyAutoLock auto_lock;
336
337 // MessageLoopResource assumes that the proxy lock has been acquired.
338 secondary_thread_message_loop_ = new MessageLoopResource(pp_instance());
339
[email protected]7ef6b79b2013-01-17 02:38:24340 ASSERT_EQ(PP_OK,
341 secondary_thread_message_loop_->PostWork(
342 PP_MakeCompletionCallback(
343 &PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread,
344 this),
345 0));
346 }
347
348 SetUpTestOnMainThread();
349
350 secondary_thread_->Start();
351 nested_main_thread_message_loop_->Run();
352 secondary_thread_->Join();
353
354 {
355 ProxyAutoLock auto_lock;
356
357 // The destruction requires a valid PpapiGlobals instance, so we should
358 // explicitly release it.
359 secondary_thread_message_loop_ = NULL;
360 }
361
362 secondary_thread_.reset(NULL);
363 nested_main_thread_message_loop_.reset(NULL);
skyostil23490d42015-06-12 12:54:26364 main_thread_task_runner_ = NULL;
[email protected]7ef6b79b2013-01-17 02:38:24365}
366
367void PluginProxyMultiThreadTest::CheckOnThread(ThreadType thread_type) {
368 ProxyAutoLock auto_lock;
369 if (thread_type == MAIN_THREAD) {
370 ASSERT_TRUE(MessageLoopResource::GetCurrent()->is_main_thread_loop());
371 } else {
372 ASSERT_EQ(secondary_thread_message_loop_.get(),
373 MessageLoopResource::GetCurrent());
374 }
375}
376
377void PluginProxyMultiThreadTest::PostQuitForMainThread() {
skyostil23490d42015-06-12 12:54:26378 main_thread_task_runner_->PostTask(
379 FROM_HERE, base::Bind(&PluginProxyMultiThreadTest::QuitNestedLoop,
380 base::Unretained(this)));
[email protected]7ef6b79b2013-01-17 02:38:24381}
382
383void PluginProxyMultiThreadTest::PostQuitForSecondaryThread() {
384 ProxyAutoLock auto_lock;
385 secondary_thread_message_loop_->PostQuit(PP_TRUE);
386}
387
388void PluginProxyMultiThreadTest::Run() {
389 ProxyAutoLock auto_lock;
390 ASSERT_EQ(PP_OK, secondary_thread_message_loop_->AttachToCurrentThread());
391 ASSERT_EQ(PP_OK, secondary_thread_message_loop_->Run());
[email protected]1b777012013-07-15 20:42:38392 secondary_thread_message_loop_->DetachFromThread();
[email protected]7ef6b79b2013-01-17 02:38:24393}
394
395void PluginProxyMultiThreadTest::QuitNestedLoop() {
396 nested_main_thread_message_loop_->Quit();
397}
398
399// static
400void PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread(
401 void* user_data,
402 int32_t result) {
403 EXPECT_EQ(PP_OK, result);
404 PluginProxyMultiThreadTest* thiz =
405 static_cast<PluginProxyMultiThreadTest*>(user_data);
406 thiz->CheckOnThread(SECONDARY_THREAD);
407 thiz->SetUpTestOnSecondaryThread();
408}
409
[email protected]912f3d6c2011-06-29 18:26:36410// HostProxyTestHarness --------------------------------------------------------
411
[email protected]7ef6b79b2013-01-17 02:38:24412HostProxyTestHarness::HostProxyTestHarness(GlobalsConfiguration globals_config)
dmichael6b328f3d2014-09-29 23:49:02413 : globals_config_(globals_config) {
[email protected]912f3d6c2011-06-29 18:26:36414}
415
416HostProxyTestHarness::~HostProxyTestHarness() {
417}
418
[email protected]d57fd382012-09-19 00:55:42419PpapiGlobals* HostProxyTestHarness::GetGlobals() {
420 return host_globals_.get();
421}
422
[email protected]912f3d6c2011-06-29 18:26:36423Dispatcher* HostProxyTestHarness::GetDispatcher() {
424 return host_dispatcher_.get();
425}
426
427void HostProxyTestHarness::SetUpHarness() {
[email protected]73097562012-01-12 19:38:55428 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24429 CreateHostGlobals();
430
[email protected]912f3d6c2011-06-29 18:26:36431 host_dispatcher_.reset(new HostDispatcher(
[email protected]912f3d6c2011-06-29 18:26:36432 pp_module(),
[email protected]8be45842012-04-13 19:49:29433 &MockGetInterface,
[email protected]73e20db2012-12-12 22:27:06434 PpapiPermissions::AllPermissions()));
[email protected]912f3d6c2011-06-29 18:26:36435 host_dispatcher_->InitWithTestSink(&sink());
436 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
437}
438
439void HostProxyTestHarness::SetUpHarnessWithChannel(
440 const IPC::ChannelHandle& channel_handle,
skyostil23490d42015-06-12 12:54:26441 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:36442 base::WaitableEvent* shutdown_event,
443 bool is_client) {
[email protected]73097562012-01-12 19:38:55444 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24445 CreateHostGlobals();
446
skyostil23490d42015-06-12 12:54:26447 delegate_mock_.Init(ipc_task_runner, shutdown_event);
[email protected]73097562012-01-12 19:38:55448
[email protected]912f3d6c2011-06-29 18:26:36449 host_dispatcher_.reset(new HostDispatcher(
[email protected]912f3d6c2011-06-29 18:26:36450 pp_module(),
[email protected]8be45842012-04-13 19:49:29451 &MockGetInterface,
[email protected]73e20db2012-12-12 22:27:06452 PpapiPermissions::AllPermissions()));
[email protected]912f3d6c2011-06-29 18:26:36453 ppapi::Preferences preferences;
[email protected]108fd342013-01-04 20:46:54454 host_dispatcher_->InitHostWithChannel(&delegate_mock_,
455 base::kNullProcessId, channel_handle,
[email protected]912f3d6c2011-06-29 18:26:36456 is_client, preferences);
457 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
458}
459
460void HostProxyTestHarness::TearDownHarness() {
461 HostDispatcher::RemoveForInstance(pp_instance());
462 host_dispatcher_.reset();
[email protected]d57fd382012-09-19 00:55:42463 host_globals_.reset();
[email protected]912f3d6c2011-06-29 18:26:36464}
465
[email protected]7ef6b79b2013-01-17 02:38:24466void HostProxyTestHarness::CreateHostGlobals() {
dmichaelbd96c1e2015-02-12 00:58:08467 disable_locking_.reset(new ProxyLock::LockingDisablerForTest);
[email protected]7ef6b79b2013-01-17 02:38:24468 if (globals_config_ == PER_THREAD_GLOBALS) {
469 host_globals_.reset(new TestGlobals(PpapiGlobals::PerThreadForTest()));
470 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
471 } else {
472 host_globals_.reset(new TestGlobals());
473 }
474}
475
skyostil23490d42015-06-12 12:54:26476base::SingleThreadTaskRunner*
477HostProxyTestHarness::DelegateMock::GetIPCTaskRunner() {
478 return ipc_task_runner_;
[email protected]912f3d6c2011-06-29 18:26:36479}
480
481base::WaitableEvent* HostProxyTestHarness::DelegateMock::GetShutdownEvent() {
482 return shutdown_event_;
483}
484
[email protected]f0ecb552012-05-11 22:09:11485IPC::PlatformFileForTransit
486HostProxyTestHarness::DelegateMock::ShareHandleWithRemote(
487 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54488 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11489 bool should_close_source) {
erikchen09b40032016-04-06 18:51:55490 return IPC::GetPlatformFileForTransit(handle,
491 should_close_source);
[email protected]f0ecb552012-05-11 22:09:11492}
493
erikchen4fc32d52015-06-02 02:16:38494base::SharedMemoryHandle
495HostProxyTestHarness::DelegateMock::ShareSharedMemoryHandleWithRemote(
496 const base::SharedMemoryHandle& handle,
erikchenfc29ad992015-06-04 20:44:18497 base::ProcessId /*remote_pid*/) {
498 return base::SharedMemory::DuplicateHandle(handle);
erikchen4fc32d52015-06-02 02:16:38499}
[email protected]912f3d6c2011-06-29 18:26:36500
[email protected]465faa22011-02-08 16:31:46501// HostProxyTest ---------------------------------------------------------------
502
[email protected]7ef6b79b2013-01-17 02:38:24503HostProxyTest::HostProxyTest() : HostProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]465faa22011-02-08 16:31:46504}
505
506HostProxyTest::~HostProxyTest() {
507}
508
[email protected]465faa22011-02-08 16:31:46509void HostProxyTest::SetUp() {
[email protected]912f3d6c2011-06-29 18:26:36510 SetUpHarness();
[email protected]465faa22011-02-08 16:31:46511}
512
513void HostProxyTest::TearDown() {
[email protected]912f3d6c2011-06-29 18:26:36514 TearDownHarness();
[email protected]465faa22011-02-08 16:31:46515}
516
[email protected]912f3d6c2011-06-29 18:26:36517// TwoWayTest ---------------------------------------------------------------
518
519TwoWayTest::TwoWayTest(TwoWayTest::TwoWayTestMode test_mode)
520 : test_mode_(test_mode),
[email protected]7ef6b79b2013-01-17 02:38:24521 host_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
522 plugin_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
[email protected]912f3d6c2011-06-29 18:26:36523 io_thread_("TwoWayTest_IOThread"),
524 plugin_thread_("TwoWayTest_PluginThread"),
525 remote_harness_(NULL),
526 local_harness_(NULL),
gabf40c0a5e2016-06-01 20:10:46527 channel_created_(base::WaitableEvent::ResetPolicy::MANUAL,
528 base::WaitableEvent::InitialState::NOT_SIGNALED),
529 shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL,
530 base::WaitableEvent::InitialState::NOT_SIGNALED) {
[email protected]912f3d6c2011-06-29 18:26:36531 if (test_mode == TEST_PPP_INTERFACE) {
532 remote_harness_ = &plugin_;
533 local_harness_ = &host_;
534 } else {
535 remote_harness_ = &host_;
536 local_harness_ = &plugin_;
537 }
538}
539
540TwoWayTest::~TwoWayTest() {
541 shutdown_event_.Signal();
542}
543
544void TwoWayTest::SetUp() {
545 base::Thread::Options options;
[email protected]d2881d82013-05-06 19:23:08546 options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]912f3d6c2011-06-29 18:26:36547 io_thread_.StartWithOptions(options);
548 plugin_thread_.Start();
549
sammc9bf370c2016-11-14 03:29:08550 mojo::MessagePipe pipe;
gabf40c0a5e2016-06-01 20:10:46551 base::WaitableEvent remote_harness_set_up(
552 base::WaitableEvent::ResetPolicy::MANUAL,
553 base::WaitableEvent::InitialState::NOT_SIGNALED);
skyostil23490d42015-06-12 12:54:26554 plugin_thread_.task_runner()->PostTask(
sammc9bf370c2016-11-14 03:29:08555 FROM_HERE,
556 base::Bind(&SetUpRemoteHarness, remote_harness_, pipe.handle0.release(),
557 base::RetainedRef(io_thread_.task_runner()), &shutdown_event_,
558 &remote_harness_set_up));
[email protected]912f3d6c2011-06-29 18:26:36559 remote_harness_set_up.Wait();
skyostil23490d42015-06-12 12:54:26560 local_harness_->SetUpHarnessWithChannel(
sammc9bf370c2016-11-14 03:29:08561 pipe.handle1.release(), io_thread_.task_runner().get(), &shutdown_event_,
skyostil23490d42015-06-12 12:54:26562 true); // is_client
[email protected]912f3d6c2011-06-29 18:26:36563}
564
565void TwoWayTest::TearDown() {
gabf40c0a5e2016-06-01 20:10:46566 base::WaitableEvent remote_harness_torn_down(
567 base::WaitableEvent::ResetPolicy::MANUAL,
568 base::WaitableEvent::InitialState::NOT_SIGNALED);
skyostil23490d42015-06-12 12:54:26569 plugin_thread_.task_runner()->PostTask(
570 FROM_HERE, base::Bind(&TearDownRemoteHarness, remote_harness_,
571 &remote_harness_torn_down));
[email protected]912f3d6c2011-06-29 18:26:36572 remote_harness_torn_down.Wait();
573
574 local_harness_->TearDownHarness();
575
576 io_thread_.Stop();
577}
578
[email protected]d57fd382012-09-19 00:55:42579void TwoWayTest::PostTaskOnRemoteHarness(const base::Closure& task) {
gabf40c0a5e2016-06-01 20:10:46580 base::WaitableEvent task_complete(
581 base::WaitableEvent::ResetPolicy::MANUAL,
582 base::WaitableEvent::InitialState::NOT_SIGNALED);
skyostil23490d42015-06-12 12:54:26583 plugin_thread_.task_runner()->PostTask(
584 FROM_HERE, base::Bind(&RunTaskOnRemoteHarness, task, &task_complete));
[email protected]d57fd382012-09-19 00:55:42585 task_complete.Wait();
586}
587
588
[email protected]f24448db2011-01-27 20:40:39589} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02590} // namespace ppapi