blob: 86cc0b4972083bddb28e9d61813f8c17e4468e69 [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"
[email protected]7ccb7072013-06-10 20:56:2811#include "base/message_loop/message_loop_proxy.h"
[email protected]912f3d6c2011-06-29 18:26:3612#include "base/observer_list.h"
[email protected]b75673fe2012-10-17 17:59:1413#include "base/process_util.h"
[email protected]7ef6b79b2013-01-17 02:38:2414#include "base/run_loop.h"
[email protected]912f3d6c2011-06-29 18:26:3615#include "ipc/ipc_sync_channel.h"
[email protected]f24448db2011-01-27 20:40:3916#include "ppapi/c/pp_errors.h"
[email protected]912f3d6c2011-06-29 18:26:3617#include "ppapi/c/private/ppb_proxy_private.h"
[email protected]465faa22011-02-08 16:31:4618#include "ppapi/proxy/ppapi_messages.h"
[email protected]7ef6b79b2013-01-17 02:38:2419#include "ppapi/proxy/ppb_message_loop_proxy.h"
20#include "ppapi/shared_impl/proxy_lock.h"
[email protected]f24448db2011-01-27 20:40:3921
[email protected]4d2efd22011-08-18 21:58:0222namespace ppapi {
[email protected]f24448db2011-01-27 20:40:3923namespace proxy {
24
25namespace {
[email protected]912f3d6c2011-06-29 18:26:3626// HostDispatcher requires a PPB_Proxy_Private, so we always provide a fallback
27// do-nothing implementation.
28void PluginCrashed(PP_Module module) {
29 NOTREACHED();
30};
[email protected]f24448db2011-01-27 20:40:3931
[email protected]912f3d6c2011-06-29 18:26:3632PP_Instance GetInstanceForResource(PP_Resource resource) {
33 // If a test relies on this, we need to implement it.
34 NOTREACHED();
35 return 0;
36}
37
38void SetReserveInstanceIDCallback(PP_Module module,
39 PP_Bool (*is_seen)(PP_Module, PP_Instance)) {
40 // This function gets called in HostDispatcher's constructor. We simply don't
41 // worry about Instance uniqueness in tests, so we can ignore the call.
42}
43
[email protected]912f3d6c2011-06-29 18:26:3644void AddRefModule(PP_Module module) {}
45void ReleaseModule(PP_Module module) {}
[email protected]fa4dd2912011-10-17 21:23:2946PP_Bool IsInModuleDestructor(PP_Module module) { return PP_FALSE; }
[email protected]912f3d6c2011-06-29 18:26:3647
[email protected]fa4dd2912011-10-17 21:23:2948PPB_Proxy_Private ppb_proxy_private = {
49 &PluginCrashed,
50 &GetInstanceForResource,
51 &SetReserveInstanceIDCallback,
[email protected]fa4dd2912011-10-17 21:23:2952 &AddRefModule,
53 &ReleaseModule,
54 &IsInModuleDestructor
55};
[email protected]912f3d6c2011-06-29 18:26:3656
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.
65ObserverList<ProxyTestHarnessBase> get_interface_handlers_;
[email protected]465faa22011-02-08 16:31:4666
67const void* MockGetInterface(const char* name) {
[email protected]912f3d6c2011-06-29 18:26:3668 ObserverList<ProxyTestHarnessBase>::Iterator it =
69 get_interface_handlers_;
70 while (ProxyTestHarnessBase* observer = it.GetNext()) {
71 const void* interface = observer->GetInterface(name);
72 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,
82 base::MessageLoopProxy* ipc_message_loop_proxy,
83 base::WaitableEvent* shutdown_event,
84 base::WaitableEvent* harness_set_up) {
85 harness->SetUpHarnessWithChannel(handle, ipc_message_loop_proxy,
86 shutdown_event, false);
87 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
[email protected]912f3d6c2011-06-29 18:26:36124bool 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;
130 PpapiMsg_SupportsInterface msg(name, &unused_result);
131 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
139 TupleTypes<PpapiMsg_SupportsInterface::ReplyParam>::ValueTuple reply_data;
140 EXPECT_TRUE(PpapiMsg_SupportsInterface::ReadReplyParam(
141 reply_msg, &reply_data));
142
143 sink().ClearMessages();
144 return reply_data.a;
145}
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.
[email protected]7ef6b79b2013-01-17 02:38:24167 CreatePluginGlobals();
[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());
184 PluginGlobals::Get()->set_plugin_proxy_delegate(&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,
190 base::MessageLoopProxy* ipc_message_loop,
191 base::WaitableEvent* shutdown_event,
192 bool is_client) {
[email protected]912f3d6c2011-06-29 18:26:36193 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24194 CreatePluginGlobals();
[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());
[email protected]912f3d6c2011-06-29 18:26:36199 plugin_delegate_mock_.Init(ipc_message_loop, shutdown_event);
200
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());
210 PluginGlobals::Get()->set_plugin_proxy_delegate(&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
[email protected]7ef6b79b2013-01-17 02:38:24227void PluginProxyTestHarness::CreatePluginGlobals() {
228 if (globals_config_ == PER_THREAD_GLOBALS) {
229 plugin_globals_.reset(new PluginGlobals(PpapiGlobals::PerThreadForTest()));
230 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
231 } else {
232 plugin_globals_.reset(new PluginGlobals());
233 }
234}
235
[email protected]912f3d6c2011-06-29 18:26:36236base::MessageLoopProxy*
237PluginProxyTestHarness::PluginDelegateMock::GetIPCMessageLoop() {
238 return ipc_message_loop_;
239}
240
241base::WaitableEvent*
242PluginProxyTestHarness::PluginDelegateMock::GetShutdownEvent() {
243 return shutdown_event_;
244}
245
[email protected]f0ecb552012-05-11 22:09:11246IPC::PlatformFileForTransit
247PluginProxyTestHarness::PluginDelegateMock::ShareHandleWithRemote(
248 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54249 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11250 bool should_close_source) {
251 return IPC::GetFileHandleForProcess(handle,
252 base::Process::Current().handle(),
253 should_close_source);
254}
255
[email protected]912f3d6c2011-06-29 18:26:36256std::set<PP_Instance>*
257PluginProxyTestHarness::PluginDelegateMock::GetGloballySeenInstanceIDSet() {
258 return &instance_id_set_;
259}
260
[email protected]6fc87e02011-12-20 19:18:45261uint32 PluginProxyTestHarness::PluginDelegateMock::Register(
262 PluginDispatcher* plugin_dispatcher) {
263 return 0;
264}
265
266void PluginProxyTestHarness::PluginDelegateMock::Unregister(
267 uint32 plugin_dispatcher_id) {
268}
269
[email protected]93df81e2012-08-10 22:22:46270IPC::Sender* PluginProxyTestHarness::PluginDelegateMock::GetBrowserSender() {
[email protected]d57fd382012-09-19 00:55:42271 return browser_sender_;
[email protected]93df81e2012-08-10 22:22:46272}
273
[email protected]2306303a2012-06-11 18:10:37274std::string PluginProxyTestHarness::PluginDelegateMock::GetUILanguage() {
275 return std::string("en-US");
276}
277
[email protected]6fc87e02011-12-20 19:18:45278void PluginProxyTestHarness::PluginDelegateMock::PreCacheFont(
279 const void* logfontw) {
[email protected]373a95a2011-07-01 16:58:14280}
[email protected]7f801d82011-07-08 23:30:11281
[email protected]72a10722012-06-27 19:30:58282void PluginProxyTestHarness::PluginDelegateMock::SetActiveURL(
283 const std::string& url) {
284}
285
[email protected]912f3d6c2011-06-29 18:26:36286// PluginProxyTest -------------------------------------------------------------
287
[email protected]7ef6b79b2013-01-17 02:38:24288PluginProxyTest::PluginProxyTest() : PluginProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]912f3d6c2011-06-29 18:26:36289}
290
291PluginProxyTest::~PluginProxyTest() {
292}
293
294void PluginProxyTest::SetUp() {
295 SetUpHarness();
296}
297
298void PluginProxyTest::TearDown() {
299 TearDownHarness();
300}
301
[email protected]7ef6b79b2013-01-17 02:38:24302// PluginProxyMultiThreadTest --------------------------------------------------
303
304PluginProxyMultiThreadTest::PluginProxyMultiThreadTest() {
305}
306
307PluginProxyMultiThreadTest::~PluginProxyMultiThreadTest() {
308}
309
310void PluginProxyMultiThreadTest::RunTest() {
311 main_thread_message_loop_proxy_ =
312 PpapiGlobals::Get()->GetMainThreadMessageLoop();
313 ASSERT_EQ(main_thread_message_loop_proxy_.get(),
314 base::MessageLoopProxy::current());
315 nested_main_thread_message_loop_.reset(new base::RunLoop());
316
317 secondary_thread_.reset(new base::DelegateSimpleThread(
318 this, "PluginProxyMultiThreadTest"));
319
320 {
321 ProxyAutoLock auto_lock;
322
323 // MessageLoopResource assumes that the proxy lock has been acquired.
324 secondary_thread_message_loop_ = new MessageLoopResource(pp_instance());
325
[email protected]7ef6b79b2013-01-17 02:38:24326 ASSERT_EQ(PP_OK,
327 secondary_thread_message_loop_->PostWork(
328 PP_MakeCompletionCallback(
329 &PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread,
330 this),
331 0));
332 }
333
334 SetUpTestOnMainThread();
335
336 secondary_thread_->Start();
337 nested_main_thread_message_loop_->Run();
338 secondary_thread_->Join();
339
340 {
341 ProxyAutoLock auto_lock;
342
343 // The destruction requires a valid PpapiGlobals instance, so we should
344 // explicitly release it.
345 secondary_thread_message_loop_ = NULL;
346 }
347
348 secondary_thread_.reset(NULL);
349 nested_main_thread_message_loop_.reset(NULL);
350 main_thread_message_loop_proxy_ = NULL;
351}
352
353void PluginProxyMultiThreadTest::CheckOnThread(ThreadType thread_type) {
354 ProxyAutoLock auto_lock;
355 if (thread_type == MAIN_THREAD) {
356 ASSERT_TRUE(MessageLoopResource::GetCurrent()->is_main_thread_loop());
357 } else {
358 ASSERT_EQ(secondary_thread_message_loop_.get(),
359 MessageLoopResource::GetCurrent());
360 }
361}
362
363void PluginProxyMultiThreadTest::PostQuitForMainThread() {
364 main_thread_message_loop_proxy_->PostTask(
365 FROM_HERE,
366 base::Bind(&PluginProxyMultiThreadTest::QuitNestedLoop,
367 base::Unretained(this)));
368}
369
370void PluginProxyMultiThreadTest::PostQuitForSecondaryThread() {
371 ProxyAutoLock auto_lock;
372 secondary_thread_message_loop_->PostQuit(PP_TRUE);
373}
374
375void PluginProxyMultiThreadTest::Run() {
376 ProxyAutoLock auto_lock;
377 ASSERT_EQ(PP_OK, secondary_thread_message_loop_->AttachToCurrentThread());
378 ASSERT_EQ(PP_OK, secondary_thread_message_loop_->Run());
379}
380
381void PluginProxyMultiThreadTest::QuitNestedLoop() {
382 nested_main_thread_message_loop_->Quit();
383}
384
385// static
386void PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread(
387 void* user_data,
388 int32_t result) {
389 EXPECT_EQ(PP_OK, result);
390 PluginProxyMultiThreadTest* thiz =
391 static_cast<PluginProxyMultiThreadTest*>(user_data);
392 thiz->CheckOnThread(SECONDARY_THREAD);
393 thiz->SetUpTestOnSecondaryThread();
394}
395
[email protected]912f3d6c2011-06-29 18:26:36396// HostProxyTestHarness --------------------------------------------------------
397
[email protected]8be45842012-04-13 19:49:29398class HostProxyTestHarness::MockSyncMessageStatusReceiver
399 : public HostDispatcher::SyncMessageStatusReceiver {
400 public:
401 virtual void BeginBlockOnSyncMessage() OVERRIDE {}
402 virtual void EndBlockOnSyncMessage() OVERRIDE {}
403};
404
[email protected]7ef6b79b2013-01-17 02:38:24405HostProxyTestHarness::HostProxyTestHarness(GlobalsConfiguration globals_config)
406 : globals_config_(globals_config),
407 status_receiver_(new MockSyncMessageStatusReceiver) {
[email protected]912f3d6c2011-06-29 18:26:36408}
409
410HostProxyTestHarness::~HostProxyTestHarness() {
411}
412
[email protected]d57fd382012-09-19 00:55:42413PpapiGlobals* HostProxyTestHarness::GetGlobals() {
414 return host_globals_.get();
415}
416
[email protected]912f3d6c2011-06-29 18:26:36417Dispatcher* HostProxyTestHarness::GetDispatcher() {
418 return host_dispatcher_.get();
419}
420
421void HostProxyTestHarness::SetUpHarness() {
[email protected]73097562012-01-12 19:38:55422 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24423 CreateHostGlobals();
424
[email protected]912f3d6c2011-06-29 18:26:36425 host_dispatcher_.reset(new HostDispatcher(
[email protected]912f3d6c2011-06-29 18:26:36426 pp_module(),
[email protected]8be45842012-04-13 19:49:29427 &MockGetInterface,
[email protected]195d4cde2012-10-02 18:12:41428 status_receiver_.release(),
[email protected]73e20db2012-12-12 22:27:06429 PpapiPermissions::AllPermissions()));
[email protected]912f3d6c2011-06-29 18:26:36430 host_dispatcher_->InitWithTestSink(&sink());
431 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
432}
433
434void HostProxyTestHarness::SetUpHarnessWithChannel(
435 const IPC::ChannelHandle& channel_handle,
436 base::MessageLoopProxy* ipc_message_loop,
437 base::WaitableEvent* shutdown_event,
438 bool is_client) {
[email protected]73097562012-01-12 19:38:55439 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24440 CreateHostGlobals();
441
[email protected]912f3d6c2011-06-29 18:26:36442 delegate_mock_.Init(ipc_message_loop, shutdown_event);
[email protected]73097562012-01-12 19:38:55443
[email protected]912f3d6c2011-06-29 18:26:36444 host_dispatcher_.reset(new HostDispatcher(
[email protected]912f3d6c2011-06-29 18:26:36445 pp_module(),
[email protected]8be45842012-04-13 19:49:29446 &MockGetInterface,
[email protected]195d4cde2012-10-02 18:12:41447 status_receiver_.release(),
[email protected]73e20db2012-12-12 22:27:06448 PpapiPermissions::AllPermissions()));
[email protected]912f3d6c2011-06-29 18:26:36449 ppapi::Preferences preferences;
[email protected]108fd342013-01-04 20:46:54450 host_dispatcher_->InitHostWithChannel(&delegate_mock_,
451 base::kNullProcessId, channel_handle,
[email protected]912f3d6c2011-06-29 18:26:36452 is_client, preferences);
453 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() {
463 if (globals_config_ == PER_THREAD_GLOBALS) {
464 host_globals_.reset(new TestGlobals(PpapiGlobals::PerThreadForTest()));
465 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
466 } else {
467 host_globals_.reset(new TestGlobals());
468 }
469}
470
[email protected]912f3d6c2011-06-29 18:26:36471base::MessageLoopProxy*
472HostProxyTestHarness::DelegateMock::GetIPCMessageLoop() {
473 return ipc_message_loop_;
474}
475
476base::WaitableEvent* HostProxyTestHarness::DelegateMock::GetShutdownEvent() {
477 return shutdown_event_;
478}
479
[email protected]f0ecb552012-05-11 22:09:11480IPC::PlatformFileForTransit
481HostProxyTestHarness::DelegateMock::ShareHandleWithRemote(
482 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54483 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11484 bool should_close_source) {
485 return IPC::GetFileHandleForProcess(handle,
486 base::Process::Current().handle(),
487 should_close_source);
488}
489
[email protected]912f3d6c2011-06-29 18:26:36490
[email protected]465faa22011-02-08 16:31:46491// HostProxyTest ---------------------------------------------------------------
492
[email protected]7ef6b79b2013-01-17 02:38:24493HostProxyTest::HostProxyTest() : HostProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]465faa22011-02-08 16:31:46494}
495
496HostProxyTest::~HostProxyTest() {
497}
498
[email protected]465faa22011-02-08 16:31:46499void HostProxyTest::SetUp() {
[email protected]912f3d6c2011-06-29 18:26:36500 SetUpHarness();
[email protected]465faa22011-02-08 16:31:46501}
502
503void HostProxyTest::TearDown() {
[email protected]912f3d6c2011-06-29 18:26:36504 TearDownHarness();
[email protected]465faa22011-02-08 16:31:46505}
506
[email protected]912f3d6c2011-06-29 18:26:36507// TwoWayTest ---------------------------------------------------------------
508
509TwoWayTest::TwoWayTest(TwoWayTest::TwoWayTestMode test_mode)
510 : test_mode_(test_mode),
[email protected]7ef6b79b2013-01-17 02:38:24511 host_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
512 plugin_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
[email protected]912f3d6c2011-06-29 18:26:36513 io_thread_("TwoWayTest_IOThread"),
514 plugin_thread_("TwoWayTest_PluginThread"),
515 remote_harness_(NULL),
516 local_harness_(NULL),
517 channel_created_(true, false),
518 shutdown_event_(true, false) {
519 if (test_mode == TEST_PPP_INTERFACE) {
520 remote_harness_ = &plugin_;
521 local_harness_ = &host_;
522 } else {
523 remote_harness_ = &host_;
524 local_harness_ = &plugin_;
525 }
526}
527
528TwoWayTest::~TwoWayTest() {
529 shutdown_event_.Signal();
530}
531
532void TwoWayTest::SetUp() {
533 base::Thread::Options options;
[email protected]d2881d82013-05-06 19:23:08534 options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]912f3d6c2011-06-29 18:26:36535 io_thread_.StartWithOptions(options);
536 plugin_thread_.Start();
537
[email protected]b75673fe2012-10-17 17:59:14538 // Construct the IPC handle name using the process ID so we can safely run
539 // multiple |TwoWayTest|s concurrently.
540 std::ostringstream handle_name;
541 handle_name << "TwoWayTestChannel" << base::GetCurrentProcId();
542 IPC::ChannelHandle handle(handle_name.str());
[email protected]912f3d6c2011-06-29 18:26:36543 base::WaitableEvent remote_harness_set_up(true, false);
544 plugin_thread_.message_loop_proxy()->PostTask(
545 FROM_HERE,
[email protected]1f6581c2011-10-04 23:10:15546 base::Bind(&SetUpRemoteHarness,
547 remote_harness_,
548 handle,
549 io_thread_.message_loop_proxy(),
550 &shutdown_event_,
551 &remote_harness_set_up));
[email protected]912f3d6c2011-06-29 18:26:36552 remote_harness_set_up.Wait();
553 local_harness_->SetUpHarnessWithChannel(handle,
554 io_thread_.message_loop_proxy(),
555 &shutdown_event_,
556 true); // is_client
557}
558
559void TwoWayTest::TearDown() {
560 base::WaitableEvent remote_harness_torn_down(true, false);
561 plugin_thread_.message_loop_proxy()->PostTask(
562 FROM_HERE,
[email protected]1f6581c2011-10-04 23:10:15563 base::Bind(&TearDownRemoteHarness,
564 remote_harness_,
565 &remote_harness_torn_down));
[email protected]912f3d6c2011-06-29 18:26:36566 remote_harness_torn_down.Wait();
567
568 local_harness_->TearDownHarness();
569
570 io_thread_.Stop();
571}
572
[email protected]d57fd382012-09-19 00:55:42573void TwoWayTest::PostTaskOnRemoteHarness(const base::Closure& task) {
574 base::WaitableEvent task_complete(true, false);
575 plugin_thread_.message_loop_proxy()->PostTask(FROM_HERE,
576 base::Bind(&RunTaskOnRemoteHarness,
577 task,
578 &task_complete));
579 task_complete.Wait();
580}
581
582
[email protected]f24448db2011-01-27 20:40:39583} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02584} // namespace ppapi