blob: 364a02c97cde7ea44f6683bba6d25147a1deea5d [file] [log] [blame]
Avi Drissmandb497b32022-09-15 19:47:281// Copyright 2012 The Chromium Authors
[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
Peter Boströmfb60ea02021-04-05 21:06:127#include <memory>
tzika0f614d2016-03-08 00:35:158#include <tuple>
[email protected]b75673fe2012-10-17 17:59:149
Avi Drissman821ca3092023-01-11 22:42:1510#include "base/functional/bind.h"
11#include "base/functional/callback_helpers.h"
skyostil23490d42015-06-12 12:54:2612#include "base/location.h"
Carlos Caballerodd8bf7b042019-07-30 14:14:1513#include "base/message_loop/message_pump_type.h"
[email protected]912f3d6c2011-06-29 18:26:3614#include "base/observer_list.h"
rvargas079d1842014-10-17 22:32:1615#include "base/process/process_handle.h"
[email protected]7ef6b79b2013-01-17 02:38:2416#include "base/run_loop.h"
Patrick Monette643cdf62021-10-15 19:13:4217#include "base/task/single_thread_task_runner.h"
[email protected]912f3d6c2011-06-29 18:26:3618#include "ipc/ipc_sync_channel.h"
dmichael6b328f3d2014-09-29 23:49:0219#include "ipc/message_filter.h"
[email protected]f24448db2011-01-27 20:40:3920#include "ppapi/c/pp_errors.h"
[email protected]912f3d6c2011-06-29 18:26:3621#include "ppapi/c/private/ppb_proxy_private.h"
[email protected]465faa22011-02-08 16:31:4622#include "ppapi/proxy/ppapi_messages.h"
[email protected]7ef6b79b2013-01-17 02:38:2423#include "ppapi/proxy/ppb_message_loop_proxy.h"
[email protected]f24448db2011-01-27 20:40:3924
[email protected]4d2efd22011-08-18 21:58:0225namespace ppapi {
[email protected]f24448db2011-01-27 20:40:3926namespace proxy {
27
28namespace {
[email protected]912f3d6c2011-06-29 18:26:3629// HostDispatcher requires a PPB_Proxy_Private, so we always provide a fallback
30// do-nothing implementation.
31void PluginCrashed(PP_Module module) {
32 NOTREACHED();
Nico Webere779c822019-02-12 20:19:0833}
[email protected]f24448db2011-01-27 20:40:3934
[email protected]912f3d6c2011-06-29 18:26:3635PP_Instance GetInstanceForResource(PP_Resource resource) {
36 // If a test relies on this, we need to implement it.
37 NOTREACHED();
38 return 0;
39}
40
41void SetReserveInstanceIDCallback(PP_Module module,
42 PP_Bool (*is_seen)(PP_Module, PP_Instance)) {
43 // This function gets called in HostDispatcher's constructor. We simply don't
44 // worry about Instance uniqueness in tests, so we can ignore the call.
45}
46
[email protected]912f3d6c2011-06-29 18:26:3647void AddRefModule(PP_Module module) {}
48void ReleaseModule(PP_Module module) {}
[email protected]fa4dd2912011-10-17 21:23:2949PP_Bool IsInModuleDestructor(PP_Module module) { return PP_FALSE; }
[email protected]912f3d6c2011-06-29 18:26:3650
[email protected]fa4dd2912011-10-17 21:23:2951PPB_Proxy_Private ppb_proxy_private = {
52 &PluginCrashed,
53 &GetInstanceForResource,
54 &SetReserveInstanceIDCallback,
[email protected]fa4dd2912011-10-17 21:23:2955 &AddRefModule,
56 &ReleaseModule,
57 &IsInModuleDestructor
58};
[email protected]912f3d6c2011-06-29 18:26:3659
60// We allow multiple harnesses at a time to respond to 'GetInterface' calls.
61// We assume that only 1 harness's GetInterface function will ever support a
62// given interface name. In practice, there will either be only 1 GetInterface
63// handler (for PluginProxyTest or HostProxyTest), or there will be only 2
64// GetInterface handlers (for TwoWayTest). In the latter case, one handler is
65// for the PluginProxyTestHarness and should only respond for PPP interfaces,
66// and the other handler is for the HostProxyTestHarness which should only
67// ever respond for PPB interfaces.
Trent Apteda250ec3ab2018-08-19 08:52:1968base::ObserverList<ProxyTestHarnessBase>::Unchecked get_interface_handlers_;
[email protected]465faa22011-02-08 16:31:4669
70const void* MockGetInterface(const char* name) {
dcheng9fae50222016-10-14 03:21:0471 for (auto& observer : get_interface_handlers_) {
72 const void* interface = observer.GetInterface(name);
[email protected]912f3d6c2011-06-29 18:26:3673 if (interface)
74 return interface;
[email protected]465faa22011-02-08 16:31:4675 }
[email protected]912f3d6c2011-06-29 18:26:3676 if (strcmp(name, PPB_PROXY_PRIVATE_INTERFACE) == 0)
77 return &ppb_proxy_private;
78 return NULL;
79}
80
81void SetUpRemoteHarness(ProxyTestHarnessBase* harness,
82 const IPC::ChannelHandle& handle,
skyostil23490d42015-06-12 12:54:2683 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:3684 base::WaitableEvent* shutdown_event,
85 base::WaitableEvent* harness_set_up) {
skyostil23490d42015-06-12 12:54:2686 harness->SetUpHarnessWithChannel(handle, ipc_task_runner, shutdown_event,
87 false);
[email protected]912f3d6c2011-06-29 18:26:3688 harness_set_up->Signal();
89}
90
91void TearDownRemoteHarness(ProxyTestHarnessBase* harness,
92 base::WaitableEvent* harness_torn_down) {
93 harness->TearDownHarness();
94 harness_torn_down->Signal();
[email protected]f24448db2011-01-27 20:40:3995}
96
Ayu Ishii54b618752021-02-08 17:54:2697void RunTaskOnRemoteHarness(base::OnceClosure task,
[email protected]d57fd382012-09-19 00:55:4298 base::WaitableEvent* task_complete) {
Ayu Ishii54b618752021-02-08 17:54:2699 std::move(task).Run();
100 task_complete->Signal();
[email protected]d57fd382012-09-19 00:55:42101}
102
[email protected]f24448db2011-01-27 20:40:39103} // namespace
104
[email protected]912f3d6c2011-06-29 18:26:36105// ProxyTestHarnessBase --------------------------------------------------------
[email protected]465faa22011-02-08 16:31:46106
[email protected]912f3d6c2011-06-29 18:26:36107ProxyTestHarnessBase::ProxyTestHarnessBase() : pp_module_(0x98765),
108 pp_instance_(0x12345) {
109 get_interface_handlers_.AddObserver(this);
[email protected]465faa22011-02-08 16:31:46110}
111
[email protected]912f3d6c2011-06-29 18:26:36112ProxyTestHarnessBase::~ProxyTestHarnessBase() {
113 get_interface_handlers_.RemoveObserver(this);
[email protected]465faa22011-02-08 16:31:46114}
115
[email protected]912f3d6c2011-06-29 18:26:36116const void* ProxyTestHarnessBase::GetInterface(const char* name) {
[email protected]465faa22011-02-08 16:31:46117 return registered_interfaces_[name];
118}
119
[email protected]912f3d6c2011-06-29 18:26:36120void ProxyTestHarnessBase::RegisterTestInterface(const char* name,
[email protected]99ff9932011-09-07 14:14:54121 const void* test_interface) {
122 registered_interfaces_[name] = test_interface;
[email protected]465faa22011-02-08 16:31:46123}
124
kareng1c62eeb2014-11-08 16:35:03125bool ProxyTestHarnessBase::SupportsInterface(const char* name) {
[email protected]465faa22011-02-08 16:31:46126 sink().ClearMessages();
127
128 // IPC doesn't actually write to this when we send a message manually
129 // not actually using IPC.
130 bool unused_result = false;
kareng1c62eeb2014-11-08 16:35:03131 PpapiMsg_SupportsInterface msg(name, &unused_result);
[email protected]465faa22011-02-08 16:31:46132 GetDispatcher()->OnMessageReceived(msg);
133
134 const IPC::Message* reply_msg =
135 sink().GetUniqueMessageMatching(IPC_REPLY_ID);
136 EXPECT_TRUE(reply_msg);
137 if (!reply_msg)
138 return false;
139
mdempsky390ab1e2016-03-07 22:18:35140 PpapiMsg_SupportsInterface::ReplyParam reply_data;
kareng1c62eeb2014-11-08 16:35:03141 EXPECT_TRUE(PpapiMsg_SupportsInterface::ReadReplyParam(
[email protected]465faa22011-02-08 16:31:46142 reply_msg, &reply_data));
143
144 sink().ClearMessages();
tzika0f614d2016-03-08 00:35:15145 return std::get<0>(reply_data);
[email protected]465faa22011-02-08 16:31:46146}
147
[email protected]912f3d6c2011-06-29 18:26:36148// PluginProxyTestHarness ------------------------------------------------------
[email protected]465faa22011-02-08 16:31:46149
[email protected]7ef6b79b2013-01-17 02:38:24150PluginProxyTestHarness::PluginProxyTestHarness(
151 GlobalsConfiguration globals_config)
152 : globals_config_(globals_config) {
[email protected]f24448db2011-01-27 20:40:39153}
154
[email protected]912f3d6c2011-06-29 18:26:36155PluginProxyTestHarness::~PluginProxyTestHarness() {
[email protected]f24448db2011-01-27 20:40:39156}
157
[email protected]d57fd382012-09-19 00:55:42158PpapiGlobals* PluginProxyTestHarness::GetGlobals() {
159 return plugin_globals_.get();
160}
161
[email protected]912f3d6c2011-06-29 18:26:36162Dispatcher* PluginProxyTestHarness::GetDispatcher() {
[email protected]465faa22011-02-08 16:31:46163 return plugin_dispatcher_.get();
164}
165
[email protected]912f3d6c2011-06-29 18:26:36166void PluginProxyTestHarness::SetUpHarness() {
[email protected]f24448db2011-01-27 20:40:39167 // These must be first since the dispatcher set-up uses them.
dmichaelb11ca7b2015-04-02 16:59:40168 CreatePluginGlobals(nullptr /* ipc_task_runner */);
[email protected]b73c6592013-03-30 17:08:13169 // Some of the methods called during set-up check that the lock is held.
170 ProxyAutoLock lock;
[email protected]7ef6b79b2013-01-17 02:38:24171
[email protected]794d83cd2011-10-20 19:09:20172 resource_tracker().DidCreateInstance(pp_instance());
[email protected]f24448db2011-01-27 20:40:39173
Peter Boströmfb60ea02021-04-05 21:06:12174 plugin_dispatcher_ = std::make_unique<PluginDispatcher>(
175 &MockGetInterface, PpapiPermissions(), false);
[email protected]465faa22011-02-08 16:31:46176 plugin_dispatcher_->InitWithTestSink(&sink());
[email protected]d57fd382012-09-19 00:55:42177 // The plugin proxy delegate is needed for
178 // |PluginProxyDelegate::GetBrowserSender| which is used
179 // in |ResourceCreationProxy::GetConnection| to get the channel to the
180 // browser. In this case we just use the |plugin_dispatcher_| as the channel
181 // for test purposes.
182 plugin_delegate_mock_.set_browser_sender(plugin_dispatcher_.get());
dmichaelfee3a512014-09-18 21:32:13183 PluginGlobals::Get()->SetPluginProxyDelegate(&plugin_delegate_mock_);
[email protected]22fdaa62012-11-30 01:55:44184 plugin_dispatcher_->DidCreateInstance(pp_instance());
[email protected]f24448db2011-01-27 20:40:39185}
186
[email protected]912f3d6c2011-06-29 18:26:36187void PluginProxyTestHarness::SetUpHarnessWithChannel(
188 const IPC::ChannelHandle& channel_handle,
skyostil23490d42015-06-12 12:54:26189 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:36190 base::WaitableEvent* shutdown_event,
191 bool is_client) {
192 // These must be first since the dispatcher set-up uses them.
skyostil23490d42015-06-12 12:54:26193 CreatePluginGlobals(ipc_task_runner);
[email protected]b73c6592013-03-30 17:08:13194 // Some of the methods called during set-up check that the lock is held.
195 ProxyAutoLock lock;
[email protected]7ef6b79b2013-01-17 02:38:24196
[email protected]794d83cd2011-10-20 19:09:20197 resource_tracker().DidCreateInstance(pp_instance());
skyostil23490d42015-06-12 12:54:26198 plugin_delegate_mock_.Init(ipc_task_runner, shutdown_event);
[email protected]912f3d6c2011-06-29 18:26:36199
Peter Boströmfb60ea02021-04-05 21:06:12200 plugin_dispatcher_ = std::make_unique<PluginDispatcher>(
201 &MockGetInterface, PpapiPermissions(), false);
[email protected]912f3d6c2011-06-29 18:26:36202 plugin_dispatcher_->InitPluginWithChannel(&plugin_delegate_mock_,
[email protected]108fd342013-01-04 20:46:54203 base::kNullProcessId,
[email protected]912f3d6c2011-06-29 18:26:36204 channel_handle,
205 is_client);
[email protected]22fdaa62012-11-30 01:55:44206 plugin_delegate_mock_.set_browser_sender(plugin_dispatcher_.get());
dmichaelfee3a512014-09-18 21:32:13207 PluginGlobals::Get()->SetPluginProxyDelegate(&plugin_delegate_mock_);
[email protected]912f3d6c2011-06-29 18:26:36208 plugin_dispatcher_->DidCreateInstance(pp_instance());
209}
210
211void PluginProxyTestHarness::TearDownHarness() {
[email protected]b73c6592013-03-30 17:08:13212 {
213 // Some of the methods called during tear-down check that the lock is held.
214 ProxyAutoLock lock;
[email protected]f24448db2011-01-27 20:40:39215
[email protected]b73c6592013-03-30 17:08:13216 plugin_dispatcher_->DidDestroyInstance(pp_instance());
217 plugin_dispatcher_.reset();
218
219 resource_tracker().DidDeleteInstance(pp_instance());
220 }
[email protected]d57fd382012-09-19 00:55:42221 plugin_globals_.reset();
[email protected]f24448db2011-01-27 20:40:39222}
223
dmichaelb11ca7b2015-04-02 16:59:40224void PluginProxyTestHarness::CreatePluginGlobals(
Gabriel Charettee926fc12019-12-16 19:00:02225 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) {
[email protected]7ef6b79b2013-01-17 02:38:24226 if (globals_config_ == PER_THREAD_GLOBALS) {
Peter Boströmfb60ea02021-04-05 21:06:12227 plugin_globals_ = std::make_unique<PluginGlobals>(
228 PpapiGlobals::PerThreadForTest(), ipc_task_runner);
[email protected]7ef6b79b2013-01-17 02:38:24229 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
230 } else {
Peter Boströmfb60ea02021-04-05 21:06:12231 plugin_globals_ = std::make_unique<PluginGlobals>(ipc_task_runner);
[email protected]7ef6b79b2013-01-17 02:38:24232 }
233}
234
skyostil12262cf2015-05-21 14:49:31235base::SingleThreadTaskRunner*
236PluginProxyTestHarness::PluginDelegateMock::GetIPCTaskRunner() {
skyostil23490d42015-06-12 12:54:26237 return ipc_task_runner_;
[email protected]912f3d6c2011-06-29 18:26:36238}
239
240base::WaitableEvent*
241PluginProxyTestHarness::PluginDelegateMock::GetShutdownEvent() {
242 return shutdown_event_;
243}
244
[email protected]f0ecb552012-05-11 22:09:11245IPC::PlatformFileForTransit
246PluginProxyTestHarness::PluginDelegateMock::ShareHandleWithRemote(
247 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54248 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11249 bool should_close_source) {
erikchen09b40032016-04-06 18:51:55250 return IPC::GetPlatformFileForTransit(handle,
251 should_close_source);
[email protected]f0ecb552012-05-11 22:09:11252}
253
Alexandr Ilinc7d975f2018-06-01 09:25:41254base::UnsafeSharedMemoryRegion PluginProxyTestHarness::PluginDelegateMock::
255 ShareUnsafeSharedMemoryRegionWithRemote(
256 const base::UnsafeSharedMemoryRegion& region,
257 base::ProcessId /* remote_pid */) {
258 return region.Duplicate();
259}
260
261base::ReadOnlySharedMemoryRegion PluginProxyTestHarness::PluginDelegateMock::
262 ShareReadOnlySharedMemoryRegionWithRemote(
263 const base::ReadOnlySharedMemoryRegion& region,
264 base::ProcessId /* remote_pid */) {
265 return region.Duplicate();
266}
267
[email protected]912f3d6c2011-06-29 18:26:36268std::set<PP_Instance>*
269PluginProxyTestHarness::PluginDelegateMock::GetGloballySeenInstanceIDSet() {
270 return &instance_id_set_;
271}
272
avie029c4132015-12-23 06:45:22273uint32_t PluginProxyTestHarness::PluginDelegateMock::Register(
[email protected]6fc87e02011-12-20 19:18:45274 PluginDispatcher* plugin_dispatcher) {
275 return 0;
276}
277
278void PluginProxyTestHarness::PluginDelegateMock::Unregister(
avie029c4132015-12-23 06:45:22279 uint32_t plugin_dispatcher_id) {}
[email protected]6fc87e02011-12-20 19:18:45280
[email protected]93df81e2012-08-10 22:22:46281IPC::Sender* PluginProxyTestHarness::PluginDelegateMock::GetBrowserSender() {
[email protected]d57fd382012-09-19 00:55:42282 return browser_sender_;
[email protected]93df81e2012-08-10 22:22:46283}
284
[email protected]2306303a2012-06-11 18:10:37285std::string PluginProxyTestHarness::PluginDelegateMock::GetUILanguage() {
286 return std::string("en-US");
287}
288
[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(),
Sean Maher5b9af51f2022-11-21 15:32:47328 base::SingleThreadTaskRunner::GetCurrentDefault().get());
Peter Boströmfb60ea02021-04-05 21:06:12329 nested_main_thread_message_loop_ = std::make_unique<base::RunLoop>();
[email protected]7ef6b79b2013-01-17 02:38:24330
Peter Boströmfb60ea02021-04-05 21:06:12331 secondary_thread_ = std::make_unique<base::DelegateSimpleThread>(
332 this, "PluginProxyMultiThreadTest");
[email protected]7ef6b79b2013-01-17 02:38:24333
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.
kylechar37594cb2019-11-15 16:41:17359 secondary_thread_message_loop_.reset();
[email protected]7ef6b79b2013-01-17 02:38:24360 }
361
362 secondary_thread_.reset(NULL);
363 nested_main_thread_message_loop_.reset(NULL);
kylechar37594cb2019-11-15 16:41:17364 main_thread_task_runner_.reset();
[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(
kylechar444859b2019-02-20 04:32:19379 FROM_HERE, base::BindOnce(&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
Peter Boströmfb60ea02021-04-05 21:06:12431 host_dispatcher_ = std::make_unique<HostDispatcher>(
432 pp_module(), &MockGetInterface, PpapiPermissions::AllPermissions());
[email protected]912f3d6c2011-06-29 18:26:36433 host_dispatcher_->InitWithTestSink(&sink());
434 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
435}
436
437void HostProxyTestHarness::SetUpHarnessWithChannel(
438 const IPC::ChannelHandle& channel_handle,
skyostil23490d42015-06-12 12:54:26439 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:36440 base::WaitableEvent* shutdown_event,
441 bool is_client) {
[email protected]73097562012-01-12 19:38:55442 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24443 CreateHostGlobals();
444
skyostil23490d42015-06-12 12:54:26445 delegate_mock_.Init(ipc_task_runner, shutdown_event);
[email protected]73097562012-01-12 19:38:55446
Peter Boströmfb60ea02021-04-05 21:06:12447 host_dispatcher_ = std::make_unique<HostDispatcher>(
448 pp_module(), &MockGetInterface, PpapiPermissions::AllPermissions());
[email protected]912f3d6c2011-06-29 18:26:36449 ppapi::Preferences preferences;
Sean Maher5b9af51f2022-11-21 15:32:47450 host_dispatcher_->InitHostWithChannel(
451 &delegate_mock_, base::kNullProcessId, channel_handle, is_client,
452 preferences, base::SingleThreadTaskRunner::GetCurrentDefault());
[email protected]912f3d6c2011-06-29 18:26:36453 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
454}
455
456void HostProxyTestHarness::TearDownHarness() {
457 HostDispatcher::RemoveForInstance(pp_instance());
458 host_dispatcher_.reset();
[email protected]d57fd382012-09-19 00:55:42459 host_globals_.reset();
[email protected]912f3d6c2011-06-29 18:26:36460}
461
[email protected]7ef6b79b2013-01-17 02:38:24462void HostProxyTestHarness::CreateHostGlobals() {
Peter Boströmfb60ea02021-04-05 21:06:12463 disable_locking_ = std::make_unique<ProxyLock::LockingDisablerForTest>();
[email protected]7ef6b79b2013-01-17 02:38:24464 if (globals_config_ == PER_THREAD_GLOBALS) {
Peter Boströmfb60ea02021-04-05 21:06:12465 host_globals_ =
466 std::make_unique<TestGlobals>(PpapiGlobals::PerThreadForTest());
[email protected]7ef6b79b2013-01-17 02:38:24467 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
468 } else {
Peter Boströmfb60ea02021-04-05 21:06:12469 host_globals_ = std::make_unique<TestGlobals>();
[email protected]7ef6b79b2013-01-17 02:38:24470 }
471}
472
skyostil23490d42015-06-12 12:54:26473base::SingleThreadTaskRunner*
474HostProxyTestHarness::DelegateMock::GetIPCTaskRunner() {
475 return ipc_task_runner_;
[email protected]912f3d6c2011-06-29 18:26:36476}
477
478base::WaitableEvent* HostProxyTestHarness::DelegateMock::GetShutdownEvent() {
479 return shutdown_event_;
480}
481
[email protected]f0ecb552012-05-11 22:09:11482IPC::PlatformFileForTransit
483HostProxyTestHarness::DelegateMock::ShareHandleWithRemote(
484 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54485 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11486 bool should_close_source) {
erikchen09b40032016-04-06 18:51:55487 return IPC::GetPlatformFileForTransit(handle,
488 should_close_source);
[email protected]f0ecb552012-05-11 22:09:11489}
490
Alexandr Ilinc7d975f2018-06-01 09:25:41491base::UnsafeSharedMemoryRegion
492HostProxyTestHarness::DelegateMock::ShareUnsafeSharedMemoryRegionWithRemote(
493 const base::UnsafeSharedMemoryRegion& region,
494 base::ProcessId /*remote_pid*/) {
495 return region.Duplicate();
496}
497
498base::ReadOnlySharedMemoryRegion
499HostProxyTestHarness::DelegateMock::ShareReadOnlySharedMemoryRegionWithRemote(
500 const base::ReadOnlySharedMemoryRegion& region,
501 base::ProcessId /*remote_pid*/) {
502 return region.Duplicate();
503}
504
[email protected]465faa22011-02-08 16:31:46505// HostProxyTest ---------------------------------------------------------------
506
[email protected]7ef6b79b2013-01-17 02:38:24507HostProxyTest::HostProxyTest() : HostProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]465faa22011-02-08 16:31:46508}
509
510HostProxyTest::~HostProxyTest() {
511}
512
[email protected]465faa22011-02-08 16:31:46513void HostProxyTest::SetUp() {
[email protected]912f3d6c2011-06-29 18:26:36514 SetUpHarness();
[email protected]465faa22011-02-08 16:31:46515}
516
517void HostProxyTest::TearDown() {
[email protected]912f3d6c2011-06-29 18:26:36518 TearDownHarness();
[email protected]465faa22011-02-08 16:31:46519}
520
[email protected]912f3d6c2011-06-29 18:26:36521// TwoWayTest ---------------------------------------------------------------
522
523TwoWayTest::TwoWayTest(TwoWayTest::TwoWayTestMode test_mode)
524 : test_mode_(test_mode),
[email protected]7ef6b79b2013-01-17 02:38:24525 host_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
526 plugin_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
[email protected]912f3d6c2011-06-29 18:26:36527 io_thread_("TwoWayTest_IOThread"),
528 plugin_thread_("TwoWayTest_PluginThread"),
529 remote_harness_(NULL),
530 local_harness_(NULL),
gabf40c0a5e2016-06-01 20:10:46531 channel_created_(base::WaitableEvent::ResetPolicy::MANUAL,
532 base::WaitableEvent::InitialState::NOT_SIGNALED),
533 shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL,
534 base::WaitableEvent::InitialState::NOT_SIGNALED) {
[email protected]912f3d6c2011-06-29 18:26:36535 if (test_mode == TEST_PPP_INTERFACE) {
536 remote_harness_ = &plugin_;
537 local_harness_ = &host_;
538 } else {
539 remote_harness_ = &host_;
540 local_harness_ = &plugin_;
541 }
542}
543
544TwoWayTest::~TwoWayTest() {
545 shutdown_event_.Signal();
546}
547
548void TwoWayTest::SetUp() {
549 base::Thread::Options options;
Carlos Caballerodd8bf7b042019-07-30 14:14:15550 options.message_pump_type = base::MessagePumpType::IO;
Olivier Li769afe962021-05-13 00:27:10551 io_thread_.StartWithOptions(std::move(options));
[email protected]912f3d6c2011-06-29 18:26:36552 plugin_thread_.Start();
553
sammc9bf370c2016-11-14 03:29:08554 mojo::MessagePipe pipe;
gabf40c0a5e2016-06-01 20:10:46555 base::WaitableEvent remote_harness_set_up(
556 base::WaitableEvent::ResetPolicy::MANUAL,
557 base::WaitableEvent::InitialState::NOT_SIGNALED);
skyostil23490d42015-06-12 12:54:26558 plugin_thread_.task_runner()->PostTask(
kylechar444859b2019-02-20 04:32:19559 FROM_HERE, base::BindOnce(&SetUpRemoteHarness, remote_harness_,
560 pipe.handle0.release(),
561 base::RetainedRef(io_thread_.task_runner()),
562 &shutdown_event_, &remote_harness_set_up));
[email protected]912f3d6c2011-06-29 18:26:36563 remote_harness_set_up.Wait();
skyostil23490d42015-06-12 12:54:26564 local_harness_->SetUpHarnessWithChannel(
sammc9bf370c2016-11-14 03:29:08565 pipe.handle1.release(), io_thread_.task_runner().get(), &shutdown_event_,
skyostil23490d42015-06-12 12:54:26566 true); // is_client
[email protected]912f3d6c2011-06-29 18:26:36567}
568
569void TwoWayTest::TearDown() {
gabf40c0a5e2016-06-01 20:10:46570 base::WaitableEvent remote_harness_torn_down(
571 base::WaitableEvent::ResetPolicy::MANUAL,
572 base::WaitableEvent::InitialState::NOT_SIGNALED);
skyostil23490d42015-06-12 12:54:26573 plugin_thread_.task_runner()->PostTask(
kylechar444859b2019-02-20 04:32:19574 FROM_HERE, base::BindOnce(&TearDownRemoteHarness, remote_harness_,
575 &remote_harness_torn_down));
[email protected]912f3d6c2011-06-29 18:26:36576 remote_harness_torn_down.Wait();
577
578 local_harness_->TearDownHarness();
579
580 io_thread_.Stop();
581}
582
Ayu Ishii54b618752021-02-08 17:54:26583void TwoWayTest::PostTaskOnRemoteHarness(base::OnceClosure task) {
gabf40c0a5e2016-06-01 20:10:46584 base::WaitableEvent task_complete(
585 base::WaitableEvent::ResetPolicy::MANUAL,
586 base::WaitableEvent::InitialState::NOT_SIGNALED);
skyostil23490d42015-06-12 12:54:26587 plugin_thread_.task_runner()->PostTask(
Ayu Ishii54b618752021-02-08 17:54:26588 FROM_HERE,
589 base::BindOnce(&RunTaskOnRemoteHarness, std::move(task), &task_complete));
[email protected]d57fd382012-09-19 00:55:42590 task_complete.Wait();
591}
592
[email protected]f24448db2011-01-27 20:40:39593} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02594} // namespace ppapi