blob: 39a1a34330510b2d5b67d3ae04156618ffea5bb5 [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
[email protected]b75673fe2012-10-17 17:59:147#include <sstream>
8
[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"
16#include "base/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) {
brettw236d3172015-06-03 16:31:4370 base::ObserverList<ProxyTestHarnessBase>::Iterator it(
71 &get_interface_handlers_);
[email protected]912f3d6c2011-06-29 18:26:3672 while (ProxyTestHarnessBase* observer = it.GetNext()) {
73 const void* interface = observer->GetInterface(name);
74 if (interface)
75 return interface;
[email protected]465faa22011-02-08 16:31:4676 }
[email protected]912f3d6c2011-06-29 18:26:3677 if (strcmp(name, PPB_PROXY_PRIVATE_INTERFACE) == 0)
78 return &ppb_proxy_private;
79 return NULL;
80}
81
82void SetUpRemoteHarness(ProxyTestHarnessBase* harness,
83 const IPC::ChannelHandle& handle,
skyostil23490d42015-06-12 12:54:2684 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:3685 base::WaitableEvent* shutdown_event,
86 base::WaitableEvent* harness_set_up) {
skyostil23490d42015-06-12 12:54:2687 harness->SetUpHarnessWithChannel(handle, ipc_task_runner, shutdown_event,
88 false);
[email protected]912f3d6c2011-06-29 18:26:3689 harness_set_up->Signal();
90}
91
92void TearDownRemoteHarness(ProxyTestHarnessBase* harness,
93 base::WaitableEvent* harness_torn_down) {
94 harness->TearDownHarness();
95 harness_torn_down->Signal();
[email protected]f24448db2011-01-27 20:40:3996}
97
[email protected]d57fd382012-09-19 00:55:4298void RunTaskOnRemoteHarness(const base::Closure& task,
99 base::WaitableEvent* task_complete) {
100 task.Run();
101 task_complete->Signal();
102}
103
[email protected]f24448db2011-01-27 20:40:39104} // namespace
105
[email protected]912f3d6c2011-06-29 18:26:36106// ProxyTestHarnessBase --------------------------------------------------------
[email protected]465faa22011-02-08 16:31:46107
[email protected]912f3d6c2011-06-29 18:26:36108ProxyTestHarnessBase::ProxyTestHarnessBase() : pp_module_(0x98765),
109 pp_instance_(0x12345) {
110 get_interface_handlers_.AddObserver(this);
[email protected]465faa22011-02-08 16:31:46111}
112
[email protected]912f3d6c2011-06-29 18:26:36113ProxyTestHarnessBase::~ProxyTestHarnessBase() {
114 get_interface_handlers_.RemoveObserver(this);
[email protected]465faa22011-02-08 16:31:46115}
116
[email protected]912f3d6c2011-06-29 18:26:36117const void* ProxyTestHarnessBase::GetInterface(const char* name) {
[email protected]465faa22011-02-08 16:31:46118 return registered_interfaces_[name];
119}
120
[email protected]912f3d6c2011-06-29 18:26:36121void ProxyTestHarnessBase::RegisterTestInterface(const char* name,
[email protected]99ff9932011-09-07 14:14:54122 const void* test_interface) {
123 registered_interfaces_[name] = test_interface;
[email protected]465faa22011-02-08 16:31:46124}
125
kareng1c62eeb2014-11-08 16:35:03126bool ProxyTestHarnessBase::SupportsInterface(const char* name) {
[email protected]465faa22011-02-08 16:31:46127 sink().ClearMessages();
128
129 // IPC doesn't actually write to this when we send a message manually
130 // not actually using IPC.
131 bool unused_result = false;
kareng1c62eeb2014-11-08 16:35:03132 PpapiMsg_SupportsInterface msg(name, &unused_result);
[email protected]465faa22011-02-08 16:31:46133 GetDispatcher()->OnMessageReceived(msg);
134
135 const IPC::Message* reply_msg =
136 sink().GetUniqueMessageMatching(IPC_REPLY_ID);
137 EXPECT_TRUE(reply_msg);
138 if (!reply_msg)
139 return false;
140
brettwd5ca2bc2015-05-29 22:15:47141 base::TupleTypes<PpapiMsg_SupportsInterface::ReplyParam>::ValueTuple
142 reply_data;
kareng1c62eeb2014-11-08 16:35:03143 EXPECT_TRUE(PpapiMsg_SupportsInterface::ReadReplyParam(
[email protected]465faa22011-02-08 16:31:46144 reply_msg, &reply_data));
145
146 sink().ClearMessages();
brettwd5ca2bc2015-05-29 22:15:47147 return base::get<0>(reply_data);
[email protected]465faa22011-02-08 16:31:46148}
149
[email protected]912f3d6c2011-06-29 18:26:36150// PluginProxyTestHarness ------------------------------------------------------
[email protected]465faa22011-02-08 16:31:46151
[email protected]7ef6b79b2013-01-17 02:38:24152PluginProxyTestHarness::PluginProxyTestHarness(
153 GlobalsConfiguration globals_config)
154 : globals_config_(globals_config) {
[email protected]f24448db2011-01-27 20:40:39155}
156
[email protected]912f3d6c2011-06-29 18:26:36157PluginProxyTestHarness::~PluginProxyTestHarness() {
[email protected]f24448db2011-01-27 20:40:39158}
159
[email protected]d57fd382012-09-19 00:55:42160PpapiGlobals* PluginProxyTestHarness::GetGlobals() {
161 return plugin_globals_.get();
162}
163
[email protected]912f3d6c2011-06-29 18:26:36164Dispatcher* PluginProxyTestHarness::GetDispatcher() {
[email protected]465faa22011-02-08 16:31:46165 return plugin_dispatcher_.get();
166}
167
[email protected]912f3d6c2011-06-29 18:26:36168void PluginProxyTestHarness::SetUpHarness() {
[email protected]f24448db2011-01-27 20:40:39169 // These must be first since the dispatcher set-up uses them.
dmichaelb11ca7b2015-04-02 16:59:40170 CreatePluginGlobals(nullptr /* ipc_task_runner */);
[email protected]b73c6592013-03-30 17:08:13171 // Some of the methods called during set-up check that the lock is held.
172 ProxyAutoLock lock;
[email protected]7ef6b79b2013-01-17 02:38:24173
[email protected]794d83cd2011-10-20 19:09:20174 resource_tracker().DidCreateInstance(pp_instance());
[email protected]f24448db2011-01-27 20:40:39175
[email protected]f24448db2011-01-27 20:40:39176 plugin_dispatcher_.reset(new PluginDispatcher(
[email protected]bc2eeb42012-05-02 22:35:53177 &MockGetInterface,
[email protected]195d4cde2012-10-02 18:12:41178 PpapiPermissions(),
[email protected]bc2eeb42012-05-02 22:35:53179 false));
[email protected]465faa22011-02-08 16:31:46180 plugin_dispatcher_->InitWithTestSink(&sink());
[email protected]d57fd382012-09-19 00:55:42181 // The plugin proxy delegate is needed for
182 // |PluginProxyDelegate::GetBrowserSender| which is used
183 // in |ResourceCreationProxy::GetConnection| to get the channel to the
184 // browser. In this case we just use the |plugin_dispatcher_| as the channel
185 // for test purposes.
186 plugin_delegate_mock_.set_browser_sender(plugin_dispatcher_.get());
dmichaelfee3a512014-09-18 21:32:13187 PluginGlobals::Get()->SetPluginProxyDelegate(&plugin_delegate_mock_);
[email protected]22fdaa62012-11-30 01:55:44188 plugin_dispatcher_->DidCreateInstance(pp_instance());
[email protected]f24448db2011-01-27 20:40:39189}
190
[email protected]912f3d6c2011-06-29 18:26:36191void PluginProxyTestHarness::SetUpHarnessWithChannel(
192 const IPC::ChannelHandle& channel_handle,
skyostil23490d42015-06-12 12:54:26193 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:36194 base::WaitableEvent* shutdown_event,
195 bool is_client) {
196 // These must be first since the dispatcher set-up uses them.
skyostil23490d42015-06-12 12:54:26197 CreatePluginGlobals(ipc_task_runner);
[email protected]b73c6592013-03-30 17:08:13198 // Some of the methods called during set-up check that the lock is held.
199 ProxyAutoLock lock;
[email protected]7ef6b79b2013-01-17 02:38:24200
[email protected]794d83cd2011-10-20 19:09:20201 resource_tracker().DidCreateInstance(pp_instance());
skyostil23490d42015-06-12 12:54:26202 plugin_delegate_mock_.Init(ipc_task_runner, shutdown_event);
[email protected]912f3d6c2011-06-29 18:26:36203
204 plugin_dispatcher_.reset(new PluginDispatcher(
[email protected]bc2eeb42012-05-02 22:35:53205 &MockGetInterface,
[email protected]195d4cde2012-10-02 18:12:41206 PpapiPermissions(),
[email protected]bc2eeb42012-05-02 22:35:53207 false));
[email protected]912f3d6c2011-06-29 18:26:36208 plugin_dispatcher_->InitPluginWithChannel(&plugin_delegate_mock_,
[email protected]108fd342013-01-04 20:46:54209 base::kNullProcessId,
[email protected]912f3d6c2011-06-29 18:26:36210 channel_handle,
211 is_client);
[email protected]22fdaa62012-11-30 01:55:44212 plugin_delegate_mock_.set_browser_sender(plugin_dispatcher_.get());
dmichaelfee3a512014-09-18 21:32:13213 PluginGlobals::Get()->SetPluginProxyDelegate(&plugin_delegate_mock_);
[email protected]912f3d6c2011-06-29 18:26:36214 plugin_dispatcher_->DidCreateInstance(pp_instance());
215}
216
217void PluginProxyTestHarness::TearDownHarness() {
[email protected]b73c6592013-03-30 17:08:13218 {
219 // Some of the methods called during tear-down check that the lock is held.
220 ProxyAutoLock lock;
[email protected]f24448db2011-01-27 20:40:39221
[email protected]b73c6592013-03-30 17:08:13222 plugin_dispatcher_->DidDestroyInstance(pp_instance());
223 plugin_dispatcher_.reset();
224
225 resource_tracker().DidDeleteInstance(pp_instance());
226 }
[email protected]d57fd382012-09-19 00:55:42227 plugin_globals_.reset();
[email protected]f24448db2011-01-27 20:40:39228}
229
dmichaelb11ca7b2015-04-02 16:59:40230void PluginProxyTestHarness::CreatePluginGlobals(
231 const scoped_refptr<base::TaskRunner>& ipc_task_runner) {
[email protected]7ef6b79b2013-01-17 02:38:24232 if (globals_config_ == PER_THREAD_GLOBALS) {
dmichaelb11ca7b2015-04-02 16:59:40233 plugin_globals_.reset(new PluginGlobals(PpapiGlobals::PerThreadForTest(),
234 ipc_task_runner));
[email protected]7ef6b79b2013-01-17 02:38:24235 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
236 } else {
dmichaelb11ca7b2015-04-02 16:59:40237 plugin_globals_.reset(new PluginGlobals(ipc_task_runner));
[email protected]7ef6b79b2013-01-17 02:38:24238 }
239}
240
skyostil12262cf2015-05-21 14:49:31241base::SingleThreadTaskRunner*
242PluginProxyTestHarness::PluginDelegateMock::GetIPCTaskRunner() {
skyostil23490d42015-06-12 12:54:26243 return ipc_task_runner_;
[email protected]912f3d6c2011-06-29 18:26:36244}
245
246base::WaitableEvent*
247PluginProxyTestHarness::PluginDelegateMock::GetShutdownEvent() {
248 return shutdown_event_;
249}
250
[email protected]f0ecb552012-05-11 22:09:11251IPC::PlatformFileForTransit
252PluginProxyTestHarness::PluginDelegateMock::ShareHandleWithRemote(
253 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54254 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11255 bool should_close_source) {
256 return IPC::GetFileHandleForProcess(handle,
rvargas079d1842014-10-17 22:32:16257 base::GetCurrentProcessHandle(),
[email protected]f0ecb552012-05-11 22:09:11258 should_close_source);
259}
260
erikchen4fc32d52015-06-02 02:16:38261base::SharedMemoryHandle
262PluginProxyTestHarness::PluginDelegateMock::ShareSharedMemoryHandleWithRemote(
263 const base::SharedMemoryHandle& handle,
erikchenfc29ad992015-06-04 20:44:18264 base::ProcessId /* remote_pid */) {
265 return base::SharedMemory::DuplicateHandle(handle);
erikchen4fc32d52015-06-02 02:16:38266}
267
[email protected]912f3d6c2011-06-29 18:26:36268std::set<PP_Instance>*
269PluginProxyTestHarness::PluginDelegateMock::GetGloballySeenInstanceIDSet() {
270 return &instance_id_set_;
271}
272
[email protected]6fc87e02011-12-20 19:18:45273uint32 PluginProxyTestHarness::PluginDelegateMock::Register(
274 PluginDispatcher* plugin_dispatcher) {
275 return 0;
276}
277
278void PluginProxyTestHarness::PluginDelegateMock::Unregister(
279 uint32 plugin_dispatcher_id) {
280}
281
[email protected]93df81e2012-08-10 22:22:46282IPC::Sender* PluginProxyTestHarness::PluginDelegateMock::GetBrowserSender() {
[email protected]d57fd382012-09-19 00:55:42283 return browser_sender_;
[email protected]93df81e2012-08-10 22:22:46284}
285
[email protected]2306303a2012-06-11 18:10:37286std::string PluginProxyTestHarness::PluginDelegateMock::GetUILanguage() {
287 return std::string("en-US");
288}
289
[email protected]6fc87e02011-12-20 19:18:45290void PluginProxyTestHarness::PluginDelegateMock::PreCacheFont(
291 const void* logfontw) {
[email protected]373a95a2011-07-01 16:58:14292}
[email protected]7f801d82011-07-08 23:30:11293
[email protected]72a10722012-06-27 19:30:58294void PluginProxyTestHarness::PluginDelegateMock::SetActiveURL(
295 const std::string& url) {
296}
297
[email protected]1d148062013-07-25 20:25:45298PP_Resource PluginProxyTestHarness::PluginDelegateMock::CreateBrowserFont(
299 Connection connection,
300 PP_Instance instance,
301 const PP_BrowserFont_Trusted_Description& desc,
302 const Preferences& prefs) {
303 return 0;
304}
305
[email protected]912f3d6c2011-06-29 18:26:36306// PluginProxyTest -------------------------------------------------------------
307
[email protected]7ef6b79b2013-01-17 02:38:24308PluginProxyTest::PluginProxyTest() : PluginProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]912f3d6c2011-06-29 18:26:36309}
310
311PluginProxyTest::~PluginProxyTest() {
312}
313
314void PluginProxyTest::SetUp() {
315 SetUpHarness();
316}
317
318void PluginProxyTest::TearDown() {
319 TearDownHarness();
320}
321
[email protected]7ef6b79b2013-01-17 02:38:24322// PluginProxyMultiThreadTest --------------------------------------------------
323
324PluginProxyMultiThreadTest::PluginProxyMultiThreadTest() {
325}
326
327PluginProxyMultiThreadTest::~PluginProxyMultiThreadTest() {
328}
329
330void PluginProxyMultiThreadTest::RunTest() {
skyostil23490d42015-06-12 12:54:26331 main_thread_task_runner_ = PpapiGlobals::Get()->GetMainThreadMessageLoop();
332 ASSERT_EQ(main_thread_task_runner_.get(),
333 base::ThreadTaskRunnerHandle::Get().get());
[email protected]7ef6b79b2013-01-17 02:38:24334 nested_main_thread_message_loop_.reset(new base::RunLoop());
335
336 secondary_thread_.reset(new base::DelegateSimpleThread(
337 this, "PluginProxyMultiThreadTest"));
338
339 {
340 ProxyAutoLock auto_lock;
341
342 // MessageLoopResource assumes that the proxy lock has been acquired.
343 secondary_thread_message_loop_ = new MessageLoopResource(pp_instance());
344
[email protected]7ef6b79b2013-01-17 02:38:24345 ASSERT_EQ(PP_OK,
346 secondary_thread_message_loop_->PostWork(
347 PP_MakeCompletionCallback(
348 &PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread,
349 this),
350 0));
351 }
352
353 SetUpTestOnMainThread();
354
355 secondary_thread_->Start();
356 nested_main_thread_message_loop_->Run();
357 secondary_thread_->Join();
358
359 {
360 ProxyAutoLock auto_lock;
361
362 // The destruction requires a valid PpapiGlobals instance, so we should
363 // explicitly release it.
364 secondary_thread_message_loop_ = NULL;
365 }
366
367 secondary_thread_.reset(NULL);
368 nested_main_thread_message_loop_.reset(NULL);
skyostil23490d42015-06-12 12:54:26369 main_thread_task_runner_ = NULL;
[email protected]7ef6b79b2013-01-17 02:38:24370}
371
372void PluginProxyMultiThreadTest::CheckOnThread(ThreadType thread_type) {
373 ProxyAutoLock auto_lock;
374 if (thread_type == MAIN_THREAD) {
375 ASSERT_TRUE(MessageLoopResource::GetCurrent()->is_main_thread_loop());
376 } else {
377 ASSERT_EQ(secondary_thread_message_loop_.get(),
378 MessageLoopResource::GetCurrent());
379 }
380}
381
382void PluginProxyMultiThreadTest::PostQuitForMainThread() {
skyostil23490d42015-06-12 12:54:26383 main_thread_task_runner_->PostTask(
384 FROM_HERE, base::Bind(&PluginProxyMultiThreadTest::QuitNestedLoop,
385 base::Unretained(this)));
[email protected]7ef6b79b2013-01-17 02:38:24386}
387
388void PluginProxyMultiThreadTest::PostQuitForSecondaryThread() {
389 ProxyAutoLock auto_lock;
390 secondary_thread_message_loop_->PostQuit(PP_TRUE);
391}
392
393void PluginProxyMultiThreadTest::Run() {
394 ProxyAutoLock auto_lock;
395 ASSERT_EQ(PP_OK, secondary_thread_message_loop_->AttachToCurrentThread());
396 ASSERT_EQ(PP_OK, secondary_thread_message_loop_->Run());
[email protected]1b777012013-07-15 20:42:38397 secondary_thread_message_loop_->DetachFromThread();
[email protected]7ef6b79b2013-01-17 02:38:24398}
399
400void PluginProxyMultiThreadTest::QuitNestedLoop() {
401 nested_main_thread_message_loop_->Quit();
402}
403
404// static
405void PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread(
406 void* user_data,
407 int32_t result) {
408 EXPECT_EQ(PP_OK, result);
409 PluginProxyMultiThreadTest* thiz =
410 static_cast<PluginProxyMultiThreadTest*>(user_data);
411 thiz->CheckOnThread(SECONDARY_THREAD);
412 thiz->SetUpTestOnSecondaryThread();
413}
414
[email protected]912f3d6c2011-06-29 18:26:36415// HostProxyTestHarness --------------------------------------------------------
416
[email protected]7ef6b79b2013-01-17 02:38:24417HostProxyTestHarness::HostProxyTestHarness(GlobalsConfiguration globals_config)
dmichael6b328f3d2014-09-29 23:49:02418 : globals_config_(globals_config) {
[email protected]912f3d6c2011-06-29 18:26:36419}
420
421HostProxyTestHarness::~HostProxyTestHarness() {
422}
423
[email protected]d57fd382012-09-19 00:55:42424PpapiGlobals* HostProxyTestHarness::GetGlobals() {
425 return host_globals_.get();
426}
427
[email protected]912f3d6c2011-06-29 18:26:36428Dispatcher* HostProxyTestHarness::GetDispatcher() {
429 return host_dispatcher_.get();
430}
431
432void HostProxyTestHarness::SetUpHarness() {
[email protected]73097562012-01-12 19:38:55433 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24434 CreateHostGlobals();
435
[email protected]912f3d6c2011-06-29 18:26:36436 host_dispatcher_.reset(new HostDispatcher(
[email protected]912f3d6c2011-06-29 18:26:36437 pp_module(),
[email protected]8be45842012-04-13 19:49:29438 &MockGetInterface,
[email protected]73e20db2012-12-12 22:27:06439 PpapiPermissions::AllPermissions()));
[email protected]912f3d6c2011-06-29 18:26:36440 host_dispatcher_->InitWithTestSink(&sink());
441 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
442}
443
444void HostProxyTestHarness::SetUpHarnessWithChannel(
445 const IPC::ChannelHandle& channel_handle,
skyostil23490d42015-06-12 12:54:26446 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:36447 base::WaitableEvent* shutdown_event,
448 bool is_client) {
[email protected]73097562012-01-12 19:38:55449 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24450 CreateHostGlobals();
451
skyostil23490d42015-06-12 12:54:26452 delegate_mock_.Init(ipc_task_runner, shutdown_event);
[email protected]73097562012-01-12 19:38:55453
[email protected]912f3d6c2011-06-29 18:26:36454 host_dispatcher_.reset(new HostDispatcher(
[email protected]912f3d6c2011-06-29 18:26:36455 pp_module(),
[email protected]8be45842012-04-13 19:49:29456 &MockGetInterface,
[email protected]73e20db2012-12-12 22:27:06457 PpapiPermissions::AllPermissions()));
[email protected]912f3d6c2011-06-29 18:26:36458 ppapi::Preferences preferences;
[email protected]108fd342013-01-04 20:46:54459 host_dispatcher_->InitHostWithChannel(&delegate_mock_,
460 base::kNullProcessId, channel_handle,
[email protected]912f3d6c2011-06-29 18:26:36461 is_client, preferences);
462 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
463}
464
465void HostProxyTestHarness::TearDownHarness() {
466 HostDispatcher::RemoveForInstance(pp_instance());
467 host_dispatcher_.reset();
[email protected]d57fd382012-09-19 00:55:42468 host_globals_.reset();
[email protected]912f3d6c2011-06-29 18:26:36469}
470
[email protected]7ef6b79b2013-01-17 02:38:24471void HostProxyTestHarness::CreateHostGlobals() {
dmichaelbd96c1e2015-02-12 00:58:08472 disable_locking_.reset(new ProxyLock::LockingDisablerForTest);
[email protected]7ef6b79b2013-01-17 02:38:24473 if (globals_config_ == PER_THREAD_GLOBALS) {
474 host_globals_.reset(new TestGlobals(PpapiGlobals::PerThreadForTest()));
475 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
476 } else {
477 host_globals_.reset(new TestGlobals());
478 }
479}
480
skyostil23490d42015-06-12 12:54:26481base::SingleThreadTaskRunner*
482HostProxyTestHarness::DelegateMock::GetIPCTaskRunner() {
483 return ipc_task_runner_;
[email protected]912f3d6c2011-06-29 18:26:36484}
485
486base::WaitableEvent* HostProxyTestHarness::DelegateMock::GetShutdownEvent() {
487 return shutdown_event_;
488}
489
[email protected]f0ecb552012-05-11 22:09:11490IPC::PlatformFileForTransit
491HostProxyTestHarness::DelegateMock::ShareHandleWithRemote(
492 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54493 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11494 bool should_close_source) {
495 return IPC::GetFileHandleForProcess(handle,
rvargas079d1842014-10-17 22:32:16496 base::GetCurrentProcessHandle(),
[email protected]f0ecb552012-05-11 22:09:11497 should_close_source);
498}
499
erikchen4fc32d52015-06-02 02:16:38500base::SharedMemoryHandle
501HostProxyTestHarness::DelegateMock::ShareSharedMemoryHandleWithRemote(
502 const base::SharedMemoryHandle& handle,
erikchenfc29ad992015-06-04 20:44:18503 base::ProcessId /*remote_pid*/) {
504 return base::SharedMemory::DuplicateHandle(handle);
erikchen4fc32d52015-06-02 02:16:38505}
[email protected]912f3d6c2011-06-29 18:26:36506
[email protected]465faa22011-02-08 16:31:46507// HostProxyTest ---------------------------------------------------------------
508
[email protected]7ef6b79b2013-01-17 02:38:24509HostProxyTest::HostProxyTest() : HostProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]465faa22011-02-08 16:31:46510}
511
512HostProxyTest::~HostProxyTest() {
513}
514
[email protected]465faa22011-02-08 16:31:46515void HostProxyTest::SetUp() {
[email protected]912f3d6c2011-06-29 18:26:36516 SetUpHarness();
[email protected]465faa22011-02-08 16:31:46517}
518
519void HostProxyTest::TearDown() {
[email protected]912f3d6c2011-06-29 18:26:36520 TearDownHarness();
[email protected]465faa22011-02-08 16:31:46521}
522
[email protected]912f3d6c2011-06-29 18:26:36523// TwoWayTest ---------------------------------------------------------------
524
525TwoWayTest::TwoWayTest(TwoWayTest::TwoWayTestMode test_mode)
526 : test_mode_(test_mode),
[email protected]7ef6b79b2013-01-17 02:38:24527 host_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
528 plugin_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
[email protected]912f3d6c2011-06-29 18:26:36529 io_thread_("TwoWayTest_IOThread"),
530 plugin_thread_("TwoWayTest_PluginThread"),
531 remote_harness_(NULL),
532 local_harness_(NULL),
533 channel_created_(true, false),
534 shutdown_event_(true, false) {
535 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;
[email protected]d2881d82013-05-06 19:23:08550 options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]912f3d6c2011-06-29 18:26:36551 io_thread_.StartWithOptions(options);
552 plugin_thread_.Start();
553
[email protected]b75673fe2012-10-17 17:59:14554 // Construct the IPC handle name using the process ID so we can safely run
555 // multiple |TwoWayTest|s concurrently.
556 std::ostringstream handle_name;
557 handle_name << "TwoWayTestChannel" << base::GetCurrentProcId();
558 IPC::ChannelHandle handle(handle_name.str());
[email protected]912f3d6c2011-06-29 18:26:36559 base::WaitableEvent remote_harness_set_up(true, false);
skyostil23490d42015-06-12 12:54:26560 plugin_thread_.task_runner()->PostTask(
561 FROM_HERE, base::Bind(&SetUpRemoteHarness, remote_harness_, handle,
562 io_thread_.task_runner(), &shutdown_event_,
563 &remote_harness_set_up));
[email protected]912f3d6c2011-06-29 18:26:36564 remote_harness_set_up.Wait();
skyostil23490d42015-06-12 12:54:26565 local_harness_->SetUpHarnessWithChannel(
566 handle, io_thread_.task_runner().get(), &shutdown_event_,
567 true); // is_client
[email protected]912f3d6c2011-06-29 18:26:36568}
569
570void TwoWayTest::TearDown() {
571 base::WaitableEvent remote_harness_torn_down(true, false);
skyostil23490d42015-06-12 12:54:26572 plugin_thread_.task_runner()->PostTask(
573 FROM_HERE, base::Bind(&TearDownRemoteHarness, remote_harness_,
574 &remote_harness_torn_down));
[email protected]912f3d6c2011-06-29 18:26:36575 remote_harness_torn_down.Wait();
576
577 local_harness_->TearDownHarness();
578
579 io_thread_.Stop();
580}
581
[email protected]d57fd382012-09-19 00:55:42582void TwoWayTest::PostTaskOnRemoteHarness(const base::Closure& task) {
583 base::WaitableEvent task_complete(true, false);
skyostil23490d42015-06-12 12:54:26584 plugin_thread_.task_runner()->PostTask(
585 FROM_HERE, base::Bind(&RunTaskOnRemoteHarness, task, &task_complete));
[email protected]d57fd382012-09-19 00:55:42586 task_complete.Wait();
587}
588
589
[email protected]f24448db2011-01-27 20:40:39590} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02591} // namespace ppapi