1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/proxy/ppapi_proxy_test.h"
10 #include "base/bind_helpers.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/observer_list.h"
13 #include "base/process/process_handle.h"
14 #include "base/run_loop.h"
15 #include "ipc/ipc_sync_channel.h"
16 #include "ipc/message_filter.h"
17 #include "ppapi/c/pp_errors.h"
18 #include "ppapi/c/private/ppb_proxy_private.h"
19 #include "ppapi/proxy/ppapi_messages.h"
20 #include "ppapi/proxy/ppb_message_loop_proxy.h"
26 // HostDispatcher requires a PPB_Proxy_Private, so we always provide a fallback
27 // do-nothing implementation.
28 void PluginCrashed(PP_Module module
) {
32 PP_Instance
GetInstanceForResource(PP_Resource resource
) {
33 // If a test relies on this, we need to implement it.
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.
44 void AddRefModule(PP_Module module
) {}
45 void ReleaseModule(PP_Module module
) {}
46 PP_Bool
IsInModuleDestructor(PP_Module module
) { return PP_FALSE
; }
48 PPB_Proxy_Private ppb_proxy_private
= {
50 &GetInstanceForResource
,
51 &SetReserveInstanceIDCallback
,
57 // We allow multiple harnesses at a time to respond to 'GetInterface' calls.
58 // We assume that only 1 harness's GetInterface function will ever support a
59 // given interface name. In practice, there will either be only 1 GetInterface
60 // handler (for PluginProxyTest or HostProxyTest), or there will be only 2
61 // GetInterface handlers (for TwoWayTest). In the latter case, one handler is
62 // for the PluginProxyTestHarness and should only respond for PPP interfaces,
63 // and the other handler is for the HostProxyTestHarness which should only
64 // ever respond for PPB interfaces.
65 ObserverList
<ProxyTestHarnessBase
> get_interface_handlers_
;
67 const void* MockGetInterface(const char* name
) {
68 ObserverList
<ProxyTestHarnessBase
>::Iterator
it(&get_interface_handlers_
);
69 while (ProxyTestHarnessBase
* observer
= it
.GetNext()) {
70 const void* interface
= observer
->GetInterface(name
);
74 if (strcmp(name
, PPB_PROXY_PRIVATE_INTERFACE
) == 0)
75 return &ppb_proxy_private
;
79 void SetUpRemoteHarness(ProxyTestHarnessBase
* harness
,
80 const IPC::ChannelHandle
& handle
,
81 base::MessageLoopProxy
* ipc_message_loop_proxy
,
82 base::WaitableEvent
* shutdown_event
,
83 base::WaitableEvent
* harness_set_up
) {
84 harness
->SetUpHarnessWithChannel(handle
, ipc_message_loop_proxy
,
85 shutdown_event
, false);
86 harness_set_up
->Signal();
89 void TearDownRemoteHarness(ProxyTestHarnessBase
* harness
,
90 base::WaitableEvent
* harness_torn_down
) {
91 harness
->TearDownHarness();
92 harness_torn_down
->Signal();
95 void RunTaskOnRemoteHarness(const base::Closure
& task
,
96 base::WaitableEvent
* task_complete
) {
98 task_complete
->Signal();
103 // ProxyTestHarnessBase --------------------------------------------------------
105 ProxyTestHarnessBase::ProxyTestHarnessBase() : pp_module_(0x98765),
106 pp_instance_(0x12345) {
107 get_interface_handlers_
.AddObserver(this);
110 ProxyTestHarnessBase::~ProxyTestHarnessBase() {
111 get_interface_handlers_
.RemoveObserver(this);
114 const void* ProxyTestHarnessBase::GetInterface(const char* name
) {
115 return registered_interfaces_
[name
];
118 void ProxyTestHarnessBase::RegisterTestInterface(const char* name
,
119 const void* test_interface
) {
120 registered_interfaces_
[name
] = test_interface
;
123 bool ProxyTestHarnessBase::SupportsInterface(const char* name
) {
124 sink().ClearMessages();
126 // IPC doesn't actually write to this when we send a message manually
127 // not actually using IPC.
128 bool unused_result
= false;
129 PpapiMsg_SupportsInterface
msg(name
, &unused_result
);
130 GetDispatcher()->OnMessageReceived(msg
);
132 const IPC::Message
* reply_msg
=
133 sink().GetUniqueMessageMatching(IPC_REPLY_ID
);
134 EXPECT_TRUE(reply_msg
);
138 TupleTypes
<PpapiMsg_SupportsInterface::ReplyParam
>::ValueTuple reply_data
;
139 EXPECT_TRUE(PpapiMsg_SupportsInterface::ReadReplyParam(
140 reply_msg
, &reply_data
));
142 sink().ClearMessages();
143 return get
<0>(reply_data
);
146 // PluginProxyTestHarness ------------------------------------------------------
148 PluginProxyTestHarness::PluginProxyTestHarness(
149 GlobalsConfiguration globals_config
)
150 : globals_config_(globals_config
) {
153 PluginProxyTestHarness::~PluginProxyTestHarness() {
156 PpapiGlobals
* PluginProxyTestHarness::GetGlobals() {
157 return plugin_globals_
.get();
160 Dispatcher
* PluginProxyTestHarness::GetDispatcher() {
161 return plugin_dispatcher_
.get();
164 void PluginProxyTestHarness::SetUpHarness() {
165 // These must be first since the dispatcher set-up uses them.
166 CreatePluginGlobals();
167 // Some of the methods called during set-up check that the lock is held.
170 resource_tracker().DidCreateInstance(pp_instance());
172 plugin_dispatcher_
.reset(new PluginDispatcher(
176 plugin_dispatcher_
->InitWithTestSink(&sink());
177 // 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());
183 PluginGlobals::Get()->SetPluginProxyDelegate(&plugin_delegate_mock_
);
184 plugin_dispatcher_
->DidCreateInstance(pp_instance());
187 void PluginProxyTestHarness::SetUpHarnessWithChannel(
188 const IPC::ChannelHandle
& channel_handle
,
189 base::MessageLoopProxy
* ipc_message_loop
,
190 base::WaitableEvent
* shutdown_event
,
192 // These must be first since the dispatcher set-up uses them.
193 CreatePluginGlobals();
194 // Some of the methods called during set-up check that the lock is held.
197 resource_tracker().DidCreateInstance(pp_instance());
198 plugin_delegate_mock_
.Init(ipc_message_loop
, shutdown_event
);
200 plugin_dispatcher_
.reset(new PluginDispatcher(
204 plugin_dispatcher_
->InitPluginWithChannel(&plugin_delegate_mock_
,
205 base::kNullProcessId
,
208 plugin_delegate_mock_
.set_browser_sender(plugin_dispatcher_
.get());
209 PluginGlobals::Get()->SetPluginProxyDelegate(&plugin_delegate_mock_
);
210 plugin_dispatcher_
->DidCreateInstance(pp_instance());
213 void PluginProxyTestHarness::TearDownHarness() {
215 // Some of the methods called during tear-down check that the lock is held.
218 plugin_dispatcher_
->DidDestroyInstance(pp_instance());
219 plugin_dispatcher_
.reset();
221 resource_tracker().DidDeleteInstance(pp_instance());
223 plugin_globals_
.reset();
226 void PluginProxyTestHarness::CreatePluginGlobals() {
227 if (globals_config_
== PER_THREAD_GLOBALS
) {
228 plugin_globals_
.reset(new PluginGlobals(PpapiGlobals::PerThreadForTest()));
229 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
231 plugin_globals_
.reset(new PluginGlobals());
235 base::MessageLoopProxy
*
236 PluginProxyTestHarness::PluginDelegateMock::GetIPCMessageLoop() {
237 return ipc_message_loop_
;
241 PluginProxyTestHarness::PluginDelegateMock::GetShutdownEvent() {
242 return shutdown_event_
;
245 IPC::PlatformFileForTransit
246 PluginProxyTestHarness::PluginDelegateMock::ShareHandleWithRemote(
247 base::PlatformFile handle
,
248 base::ProcessId
/* remote_pid */,
249 bool should_close_source
) {
250 return IPC::GetFileHandleForProcess(handle
,
251 base::GetCurrentProcessHandle(),
252 should_close_source
);
255 std::set
<PP_Instance
>*
256 PluginProxyTestHarness::PluginDelegateMock::GetGloballySeenInstanceIDSet() {
257 return &instance_id_set_
;
260 uint32
PluginProxyTestHarness::PluginDelegateMock::Register(
261 PluginDispatcher
* plugin_dispatcher
) {
265 void PluginProxyTestHarness::PluginDelegateMock::Unregister(
266 uint32 plugin_dispatcher_id
) {
269 IPC::Sender
* PluginProxyTestHarness::PluginDelegateMock::GetBrowserSender() {
270 return browser_sender_
;
273 std::string
PluginProxyTestHarness::PluginDelegateMock::GetUILanguage() {
274 return std::string("en-US");
277 void PluginProxyTestHarness::PluginDelegateMock::PreCacheFont(
278 const void* logfontw
) {
281 void PluginProxyTestHarness::PluginDelegateMock::SetActiveURL(
282 const std::string
& url
) {
285 PP_Resource
PluginProxyTestHarness::PluginDelegateMock::CreateBrowserFont(
286 Connection connection
,
287 PP_Instance instance
,
288 const PP_BrowserFont_Trusted_Description
& desc
,
289 const Preferences
& prefs
) {
293 // PluginProxyTest -------------------------------------------------------------
295 PluginProxyTest::PluginProxyTest() : PluginProxyTestHarness(SINGLETON_GLOBALS
) {
298 PluginProxyTest::~PluginProxyTest() {
301 void PluginProxyTest::SetUp() {
305 void PluginProxyTest::TearDown() {
309 // PluginProxyMultiThreadTest --------------------------------------------------
311 PluginProxyMultiThreadTest::PluginProxyMultiThreadTest() {
314 PluginProxyMultiThreadTest::~PluginProxyMultiThreadTest() {
317 void PluginProxyMultiThreadTest::RunTest() {
318 main_thread_message_loop_proxy_
=
319 PpapiGlobals::Get()->GetMainThreadMessageLoop();
320 ASSERT_EQ(main_thread_message_loop_proxy_
.get(),
321 base::MessageLoopProxy::current().get());
322 nested_main_thread_message_loop_
.reset(new base::RunLoop());
324 secondary_thread_
.reset(new base::DelegateSimpleThread(
325 this, "PluginProxyMultiThreadTest"));
328 ProxyAutoLock auto_lock
;
330 // MessageLoopResource assumes that the proxy lock has been acquired.
331 secondary_thread_message_loop_
= new MessageLoopResource(pp_instance());
334 secondary_thread_message_loop_
->PostWork(
335 PP_MakeCompletionCallback(
336 &PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread
,
341 SetUpTestOnMainThread();
343 secondary_thread_
->Start();
344 nested_main_thread_message_loop_
->Run();
345 secondary_thread_
->Join();
348 ProxyAutoLock auto_lock
;
350 // The destruction requires a valid PpapiGlobals instance, so we should
351 // explicitly release it.
352 secondary_thread_message_loop_
= NULL
;
355 secondary_thread_
.reset(NULL
);
356 nested_main_thread_message_loop_
.reset(NULL
);
357 main_thread_message_loop_proxy_
= NULL
;
360 void PluginProxyMultiThreadTest::CheckOnThread(ThreadType thread_type
) {
361 ProxyAutoLock auto_lock
;
362 if (thread_type
== MAIN_THREAD
) {
363 ASSERT_TRUE(MessageLoopResource::GetCurrent()->is_main_thread_loop());
365 ASSERT_EQ(secondary_thread_message_loop_
.get(),
366 MessageLoopResource::GetCurrent());
370 void PluginProxyMultiThreadTest::PostQuitForMainThread() {
371 main_thread_message_loop_proxy_
->PostTask(
373 base::Bind(&PluginProxyMultiThreadTest::QuitNestedLoop
,
374 base::Unretained(this)));
377 void PluginProxyMultiThreadTest::PostQuitForSecondaryThread() {
378 ProxyAutoLock auto_lock
;
379 secondary_thread_message_loop_
->PostQuit(PP_TRUE
);
382 void PluginProxyMultiThreadTest::Run() {
383 ProxyAutoLock auto_lock
;
384 ASSERT_EQ(PP_OK
, secondary_thread_message_loop_
->AttachToCurrentThread());
385 ASSERT_EQ(PP_OK
, secondary_thread_message_loop_
->Run());
386 secondary_thread_message_loop_
->DetachFromThread();
389 void PluginProxyMultiThreadTest::QuitNestedLoop() {
390 nested_main_thread_message_loop_
->Quit();
394 void PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread(
397 EXPECT_EQ(PP_OK
, result
);
398 PluginProxyMultiThreadTest
* thiz
=
399 static_cast<PluginProxyMultiThreadTest
*>(user_data
);
400 thiz
->CheckOnThread(SECONDARY_THREAD
);
401 thiz
->SetUpTestOnSecondaryThread();
404 // HostProxyTestHarness --------------------------------------------------------
406 HostProxyTestHarness::HostProxyTestHarness(GlobalsConfiguration globals_config
)
407 : globals_config_(globals_config
) {
410 HostProxyTestHarness::~HostProxyTestHarness() {
413 PpapiGlobals
* HostProxyTestHarness::GetGlobals() {
414 return host_globals_
.get();
417 Dispatcher
* HostProxyTestHarness::GetDispatcher() {
418 return host_dispatcher_
.get();
421 void HostProxyTestHarness::SetUpHarness() {
422 // These must be first since the dispatcher set-up uses them.
425 host_dispatcher_
.reset(new HostDispatcher(
428 PpapiPermissions::AllPermissions()));
429 host_dispatcher_
->InitWithTestSink(&sink());
430 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_
.get());
433 void HostProxyTestHarness::SetUpHarnessWithChannel(
434 const IPC::ChannelHandle
& channel_handle
,
435 base::MessageLoopProxy
* ipc_message_loop
,
436 base::WaitableEvent
* shutdown_event
,
438 // These must be first since the dispatcher set-up uses them.
441 delegate_mock_
.Init(ipc_message_loop
, shutdown_event
);
443 host_dispatcher_
.reset(new HostDispatcher(
446 PpapiPermissions::AllPermissions()));
447 ppapi::Preferences preferences
;
448 host_dispatcher_
->InitHostWithChannel(&delegate_mock_
,
449 base::kNullProcessId
, channel_handle
,
450 is_client
, preferences
);
451 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_
.get());
454 void HostProxyTestHarness::TearDownHarness() {
455 HostDispatcher::RemoveForInstance(pp_instance());
456 host_dispatcher_
.reset();
457 host_globals_
.reset();
460 void HostProxyTestHarness::CreateHostGlobals() {
461 disable_locking_
.reset(new ProxyLock::LockingDisablerForTest
);
462 if (globals_config_
== PER_THREAD_GLOBALS
) {
463 host_globals_
.reset(new TestGlobals(PpapiGlobals::PerThreadForTest()));
464 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
466 host_globals_
.reset(new TestGlobals());
470 base::MessageLoopProxy
*
471 HostProxyTestHarness::DelegateMock::GetIPCMessageLoop() {
472 return ipc_message_loop_
;
475 base::WaitableEvent
* HostProxyTestHarness::DelegateMock::GetShutdownEvent() {
476 return shutdown_event_
;
479 IPC::PlatformFileForTransit
480 HostProxyTestHarness::DelegateMock::ShareHandleWithRemote(
481 base::PlatformFile handle
,
482 base::ProcessId
/* remote_pid */,
483 bool should_close_source
) {
484 return IPC::GetFileHandleForProcess(handle
,
485 base::GetCurrentProcessHandle(),
486 should_close_source
);
490 // HostProxyTest ---------------------------------------------------------------
492 HostProxyTest::HostProxyTest() : HostProxyTestHarness(SINGLETON_GLOBALS
) {
495 HostProxyTest::~HostProxyTest() {
498 void HostProxyTest::SetUp() {
502 void HostProxyTest::TearDown() {
506 // TwoWayTest ---------------------------------------------------------------
508 TwoWayTest::TwoWayTest(TwoWayTest::TwoWayTestMode test_mode
)
509 : test_mode_(test_mode
),
510 host_(ProxyTestHarnessBase::PER_THREAD_GLOBALS
),
511 plugin_(ProxyTestHarnessBase::PER_THREAD_GLOBALS
),
512 io_thread_("TwoWayTest_IOThread"),
513 plugin_thread_("TwoWayTest_PluginThread"),
514 remote_harness_(NULL
),
515 local_harness_(NULL
),
516 channel_created_(true, false),
517 shutdown_event_(true, false) {
518 if (test_mode
== TEST_PPP_INTERFACE
) {
519 remote_harness_
= &plugin_
;
520 local_harness_
= &host_
;
522 remote_harness_
= &host_
;
523 local_harness_
= &plugin_
;
527 TwoWayTest::~TwoWayTest() {
528 shutdown_event_
.Signal();
531 void TwoWayTest::SetUp() {
532 base::Thread::Options options
;
533 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
534 io_thread_
.StartWithOptions(options
);
535 plugin_thread_
.Start();
537 // Construct the IPC handle name using the process ID so we can safely run
538 // multiple |TwoWayTest|s concurrently.
539 std::ostringstream handle_name
;
540 handle_name
<< "TwoWayTestChannel" << base::GetCurrentProcId();
541 IPC::ChannelHandle
handle(handle_name
.str());
542 base::WaitableEvent
remote_harness_set_up(true, false);
543 plugin_thread_
.message_loop_proxy()->PostTask(
545 base::Bind(&SetUpRemoteHarness
,
548 io_thread_
.message_loop_proxy(),
550 &remote_harness_set_up
));
551 remote_harness_set_up
.Wait();
552 local_harness_
->SetUpHarnessWithChannel(handle
,
553 io_thread_
.message_loop_proxy().get(),
558 void TwoWayTest::TearDown() {
559 base::WaitableEvent
remote_harness_torn_down(true, false);
560 plugin_thread_
.message_loop_proxy()->PostTask(
562 base::Bind(&TearDownRemoteHarness
,
564 &remote_harness_torn_down
));
565 remote_harness_torn_down
.Wait();
567 local_harness_
->TearDownHarness();
572 void TwoWayTest::PostTaskOnRemoteHarness(const base::Closure
& task
) {
573 base::WaitableEvent
task_complete(true, false);
574 plugin_thread_
.message_loop_proxy()->PostTask(FROM_HERE
,
575 base::Bind(&RunTaskOnRemoteHarness
,
578 task_complete
.Wait();