blob: 9034d8fa75cd636069be3703b91e86858af72425 [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]912f3d6c2011-06-29 18:26:3611#include "base/message_loop_proxy.h"
12#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
44int32_t GetURLLoaderBufferedBytes(PP_Resource url_loader) {
45 NOTREACHED();
46 return 0;
47}
48
49void AddRefModule(PP_Module module) {}
50void ReleaseModule(PP_Module module) {}
[email protected]fa4dd2912011-10-17 21:23:2951PP_Bool IsInModuleDestructor(PP_Module module) { return PP_FALSE; }
[email protected]912f3d6c2011-06-29 18:26:3652
[email protected]fa4dd2912011-10-17 21:23:2953PPB_Proxy_Private ppb_proxy_private = {
54 &PluginCrashed,
55 &GetInstanceForResource,
56 &SetReserveInstanceIDCallback,
57 &GetURLLoaderBufferedBytes,
58 &AddRefModule,
59 &ReleaseModule,
60 &IsInModuleDestructor
61};
[email protected]912f3d6c2011-06-29 18:26:3662
63// We allow multiple harnesses at a time to respond to 'GetInterface' calls.
64// We assume that only 1 harness's GetInterface function will ever support a
65// given interface name. In practice, there will either be only 1 GetInterface
66// handler (for PluginProxyTest or HostProxyTest), or there will be only 2
67// GetInterface handlers (for TwoWayTest). In the latter case, one handler is
68// for the PluginProxyTestHarness and should only respond for PPP interfaces,
69// and the other handler is for the HostProxyTestHarness which should only
70// ever respond for PPB interfaces.
71ObserverList<ProxyTestHarnessBase> get_interface_handlers_;
[email protected]465faa22011-02-08 16:31:4672
73const void* MockGetInterface(const char* name) {
[email protected]912f3d6c2011-06-29 18:26:3674 ObserverList<ProxyTestHarnessBase>::Iterator it =
75 get_interface_handlers_;
76 while (ProxyTestHarnessBase* observer = it.GetNext()) {
77 const void* interface = observer->GetInterface(name);
78 if (interface)
79 return interface;
[email protected]465faa22011-02-08 16:31:4680 }
[email protected]912f3d6c2011-06-29 18:26:3681 if (strcmp(name, PPB_PROXY_PRIVATE_INTERFACE) == 0)
82 return &ppb_proxy_private;
83 return NULL;
84}
85
86void SetUpRemoteHarness(ProxyTestHarnessBase* harness,
87 const IPC::ChannelHandle& handle,
88 base::MessageLoopProxy* ipc_message_loop_proxy,
89 base::WaitableEvent* shutdown_event,
90 base::WaitableEvent* harness_set_up) {
91 harness->SetUpHarnessWithChannel(handle, ipc_message_loop_proxy,
92 shutdown_event, false);
93 harness_set_up->Signal();
94}
95
96void TearDownRemoteHarness(ProxyTestHarnessBase* harness,
97 base::WaitableEvent* harness_torn_down) {
98 harness->TearDownHarness();
99 harness_torn_down->Signal();
[email protected]f24448db2011-01-27 20:40:39100}
101
[email protected]d57fd382012-09-19 00:55:42102void RunTaskOnRemoteHarness(const base::Closure& task,
103 base::WaitableEvent* task_complete) {
104 task.Run();
105 task_complete->Signal();
106}
107
[email protected]f24448db2011-01-27 20:40:39108} // namespace
109
[email protected]912f3d6c2011-06-29 18:26:36110// ProxyTestHarnessBase --------------------------------------------------------
[email protected]465faa22011-02-08 16:31:46111
[email protected]912f3d6c2011-06-29 18:26:36112ProxyTestHarnessBase::ProxyTestHarnessBase() : pp_module_(0x98765),
113 pp_instance_(0x12345) {
114 get_interface_handlers_.AddObserver(this);
[email protected]465faa22011-02-08 16:31:46115}
116
[email protected]912f3d6c2011-06-29 18:26:36117ProxyTestHarnessBase::~ProxyTestHarnessBase() {
118 get_interface_handlers_.RemoveObserver(this);
[email protected]465faa22011-02-08 16:31:46119}
120
[email protected]912f3d6c2011-06-29 18:26:36121const void* ProxyTestHarnessBase::GetInterface(const char* name) {
[email protected]465faa22011-02-08 16:31:46122 return registered_interfaces_[name];
123}
124
[email protected]912f3d6c2011-06-29 18:26:36125void ProxyTestHarnessBase::RegisterTestInterface(const char* name,
[email protected]99ff9932011-09-07 14:14:54126 const void* test_interface) {
127 registered_interfaces_[name] = test_interface;
[email protected]465faa22011-02-08 16:31:46128}
129
[email protected]912f3d6c2011-06-29 18:26:36130bool ProxyTestHarnessBase::SupportsInterface(const char* name) {
[email protected]465faa22011-02-08 16:31:46131 sink().ClearMessages();
132
133 // IPC doesn't actually write to this when we send a message manually
134 // not actually using IPC.
135 bool unused_result = false;
136 PpapiMsg_SupportsInterface msg(name, &unused_result);
137 GetDispatcher()->OnMessageReceived(msg);
138
139 const IPC::Message* reply_msg =
140 sink().GetUniqueMessageMatching(IPC_REPLY_ID);
141 EXPECT_TRUE(reply_msg);
142 if (!reply_msg)
143 return false;
144
145 TupleTypes<PpapiMsg_SupportsInterface::ReplyParam>::ValueTuple reply_data;
146 EXPECT_TRUE(PpapiMsg_SupportsInterface::ReadReplyParam(
147 reply_msg, &reply_data));
148
149 sink().ClearMessages();
150 return reply_data.a;
151}
152
[email protected]912f3d6c2011-06-29 18:26:36153// PluginProxyTestHarness ------------------------------------------------------
[email protected]465faa22011-02-08 16:31:46154
[email protected]7ef6b79b2013-01-17 02:38:24155PluginProxyTestHarness::PluginProxyTestHarness(
156 GlobalsConfiguration globals_config)
157 : globals_config_(globals_config) {
[email protected]f24448db2011-01-27 20:40:39158}
159
[email protected]912f3d6c2011-06-29 18:26:36160PluginProxyTestHarness::~PluginProxyTestHarness() {
[email protected]f24448db2011-01-27 20:40:39161}
162
[email protected]d57fd382012-09-19 00:55:42163PpapiGlobals* PluginProxyTestHarness::GetGlobals() {
164 return plugin_globals_.get();
165}
166
[email protected]912f3d6c2011-06-29 18:26:36167Dispatcher* PluginProxyTestHarness::GetDispatcher() {
[email protected]465faa22011-02-08 16:31:46168 return plugin_dispatcher_.get();
169}
170
[email protected]912f3d6c2011-06-29 18:26:36171void PluginProxyTestHarness::SetUpHarness() {
[email protected]f24448db2011-01-27 20:40:39172 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24173 CreatePluginGlobals();
[email protected]b73c6592013-03-30 17:08:13174 // Some of the methods called during set-up check that the lock is held.
175 ProxyAutoLock lock;
[email protected]7ef6b79b2013-01-17 02:38:24176
[email protected]794d83cd2011-10-20 19:09:20177 resource_tracker().DidCreateInstance(pp_instance());
[email protected]f24448db2011-01-27 20:40:39178
[email protected]f24448db2011-01-27 20:40:39179 plugin_dispatcher_.reset(new PluginDispatcher(
[email protected]bc2eeb42012-05-02 22:35:53180 &MockGetInterface,
[email protected]195d4cde2012-10-02 18:12:41181 PpapiPermissions(),
[email protected]bc2eeb42012-05-02 22:35:53182 false));
[email protected]465faa22011-02-08 16:31:46183 plugin_dispatcher_->InitWithTestSink(&sink());
[email protected]d57fd382012-09-19 00:55:42184 // The plugin proxy delegate is needed for
185 // |PluginProxyDelegate::GetBrowserSender| which is used
186 // in |ResourceCreationProxy::GetConnection| to get the channel to the
187 // browser. In this case we just use the |plugin_dispatcher_| as the channel
188 // for test purposes.
189 plugin_delegate_mock_.set_browser_sender(plugin_dispatcher_.get());
190 PluginGlobals::Get()->set_plugin_proxy_delegate(&plugin_delegate_mock_);
[email protected]22fdaa62012-11-30 01:55:44191 plugin_dispatcher_->DidCreateInstance(pp_instance());
[email protected]f24448db2011-01-27 20:40:39192}
193
[email protected]912f3d6c2011-06-29 18:26:36194void PluginProxyTestHarness::SetUpHarnessWithChannel(
195 const IPC::ChannelHandle& channel_handle,
196 base::MessageLoopProxy* ipc_message_loop,
197 base::WaitableEvent* shutdown_event,
198 bool is_client) {
[email protected]912f3d6c2011-06-29 18:26:36199 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24200 CreatePluginGlobals();
[email protected]b73c6592013-03-30 17:08:13201 // Some of the methods called during set-up check that the lock is held.
202 ProxyAutoLock lock;
[email protected]7ef6b79b2013-01-17 02:38:24203
[email protected]794d83cd2011-10-20 19:09:20204 resource_tracker().DidCreateInstance(pp_instance());
[email protected]912f3d6c2011-06-29 18:26:36205 plugin_delegate_mock_.Init(ipc_message_loop, shutdown_event);
206
207 plugin_dispatcher_.reset(new PluginDispatcher(
[email protected]bc2eeb42012-05-02 22:35:53208 &MockGetInterface,
[email protected]195d4cde2012-10-02 18:12:41209 PpapiPermissions(),
[email protected]bc2eeb42012-05-02 22:35:53210 false));
[email protected]912f3d6c2011-06-29 18:26:36211 plugin_dispatcher_->InitPluginWithChannel(&plugin_delegate_mock_,
[email protected]108fd342013-01-04 20:46:54212 base::kNullProcessId,
[email protected]912f3d6c2011-06-29 18:26:36213 channel_handle,
214 is_client);
[email protected]22fdaa62012-11-30 01:55:44215 plugin_delegate_mock_.set_browser_sender(plugin_dispatcher_.get());
216 PluginGlobals::Get()->set_plugin_proxy_delegate(&plugin_delegate_mock_);
[email protected]912f3d6c2011-06-29 18:26:36217 plugin_dispatcher_->DidCreateInstance(pp_instance());
218}
219
220void PluginProxyTestHarness::TearDownHarness() {
[email protected]b73c6592013-03-30 17:08:13221 {
222 // Some of the methods called during tear-down check that the lock is held.
223 ProxyAutoLock lock;
[email protected]f24448db2011-01-27 20:40:39224
[email protected]b73c6592013-03-30 17:08:13225 plugin_dispatcher_->DidDestroyInstance(pp_instance());
226 plugin_dispatcher_.reset();
227
228 resource_tracker().DidDeleteInstance(pp_instance());
229 }
[email protected]d57fd382012-09-19 00:55:42230 plugin_globals_.reset();
[email protected]f24448db2011-01-27 20:40:39231}
232
[email protected]7ef6b79b2013-01-17 02:38:24233void PluginProxyTestHarness::CreatePluginGlobals() {
234 if (globals_config_ == PER_THREAD_GLOBALS) {
235 plugin_globals_.reset(new PluginGlobals(PpapiGlobals::PerThreadForTest()));
236 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
237 } else {
238 plugin_globals_.reset(new PluginGlobals());
239 }
240}
241
[email protected]912f3d6c2011-06-29 18:26:36242base::MessageLoopProxy*
243PluginProxyTestHarness::PluginDelegateMock::GetIPCMessageLoop() {
244 return ipc_message_loop_;
245}
246
247base::WaitableEvent*
248PluginProxyTestHarness::PluginDelegateMock::GetShutdownEvent() {
249 return shutdown_event_;
250}
251
[email protected]f0ecb552012-05-11 22:09:11252IPC::PlatformFileForTransit
253PluginProxyTestHarness::PluginDelegateMock::ShareHandleWithRemote(
254 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54255 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11256 bool should_close_source) {
257 return IPC::GetFileHandleForProcess(handle,
258 base::Process::Current().handle(),
259 should_close_source);
260}
261
[email protected]912f3d6c2011-06-29 18:26:36262std::set<PP_Instance>*
263PluginProxyTestHarness::PluginDelegateMock::GetGloballySeenInstanceIDSet() {
264 return &instance_id_set_;
265}
266
[email protected]6fc87e02011-12-20 19:18:45267uint32 PluginProxyTestHarness::PluginDelegateMock::Register(
268 PluginDispatcher* plugin_dispatcher) {
269 return 0;
270}
271
272void PluginProxyTestHarness::PluginDelegateMock::Unregister(
273 uint32 plugin_dispatcher_id) {
274}
275
[email protected]93df81e2012-08-10 22:22:46276IPC::Sender* PluginProxyTestHarness::PluginDelegateMock::GetBrowserSender() {
[email protected]d57fd382012-09-19 00:55:42277 return browser_sender_;
[email protected]93df81e2012-08-10 22:22:46278}
279
[email protected]2306303a2012-06-11 18:10:37280std::string PluginProxyTestHarness::PluginDelegateMock::GetUILanguage() {
281 return std::string("en-US");
282}
283
[email protected]6fc87e02011-12-20 19:18:45284void PluginProxyTestHarness::PluginDelegateMock::PreCacheFont(
285 const void* logfontw) {
[email protected]373a95a2011-07-01 16:58:14286}
[email protected]7f801d82011-07-08 23:30:11287
[email protected]72a10722012-06-27 19:30:58288void PluginProxyTestHarness::PluginDelegateMock::SetActiveURL(
289 const std::string& url) {
290}
291
[email protected]912f3d6c2011-06-29 18:26:36292// PluginProxyTest -------------------------------------------------------------
293
[email protected]7ef6b79b2013-01-17 02:38:24294PluginProxyTest::PluginProxyTest() : PluginProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]912f3d6c2011-06-29 18:26:36295}
296
297PluginProxyTest::~PluginProxyTest() {
298}
299
300void PluginProxyTest::SetUp() {
301 SetUpHarness();
302}
303
304void PluginProxyTest::TearDown() {
305 TearDownHarness();
306}
307
[email protected]7ef6b79b2013-01-17 02:38:24308// PluginProxyMultiThreadTest --------------------------------------------------
309
310PluginProxyMultiThreadTest::PluginProxyMultiThreadTest() {
311}
312
313PluginProxyMultiThreadTest::~PluginProxyMultiThreadTest() {
314}
315
316void PluginProxyMultiThreadTest::RunTest() {
317 main_thread_message_loop_proxy_ =
318 PpapiGlobals::Get()->GetMainThreadMessageLoop();
319 ASSERT_EQ(main_thread_message_loop_proxy_.get(),
320 base::MessageLoopProxy::current());
321 nested_main_thread_message_loop_.reset(new base::RunLoop());
322
323 secondary_thread_.reset(new base::DelegateSimpleThread(
324 this, "PluginProxyMultiThreadTest"));
325
326 {
327 ProxyAutoLock auto_lock;
328
329 // MessageLoopResource assumes that the proxy lock has been acquired.
330 secondary_thread_message_loop_ = new MessageLoopResource(pp_instance());
331
[email protected]7ef6b79b2013-01-17 02:38:24332 ASSERT_EQ(PP_OK,
333 secondary_thread_message_loop_->PostWork(
334 PP_MakeCompletionCallback(
335 &PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread,
336 this),
337 0));
338 }
339
340 SetUpTestOnMainThread();
341
342 secondary_thread_->Start();
343 nested_main_thread_message_loop_->Run();
344 secondary_thread_->Join();
345
346 {
347 ProxyAutoLock auto_lock;
348
349 // The destruction requires a valid PpapiGlobals instance, so we should
350 // explicitly release it.
351 secondary_thread_message_loop_ = NULL;
352 }
353
354 secondary_thread_.reset(NULL);
355 nested_main_thread_message_loop_.reset(NULL);
356 main_thread_message_loop_proxy_ = NULL;
357}
358
359void PluginProxyMultiThreadTest::CheckOnThread(ThreadType thread_type) {
360 ProxyAutoLock auto_lock;
361 if (thread_type == MAIN_THREAD) {
362 ASSERT_TRUE(MessageLoopResource::GetCurrent()->is_main_thread_loop());
363 } else {
364 ASSERT_EQ(secondary_thread_message_loop_.get(),
365 MessageLoopResource::GetCurrent());
366 }
367}
368
369void PluginProxyMultiThreadTest::PostQuitForMainThread() {
370 main_thread_message_loop_proxy_->PostTask(
371 FROM_HERE,
372 base::Bind(&PluginProxyMultiThreadTest::QuitNestedLoop,
373 base::Unretained(this)));
374}
375
376void PluginProxyMultiThreadTest::PostQuitForSecondaryThread() {
377 ProxyAutoLock auto_lock;
378 secondary_thread_message_loop_->PostQuit(PP_TRUE);
379}
380
381void PluginProxyMultiThreadTest::Run() {
382 ProxyAutoLock auto_lock;
383 ASSERT_EQ(PP_OK, secondary_thread_message_loop_->AttachToCurrentThread());
384 ASSERT_EQ(PP_OK, secondary_thread_message_loop_->Run());
385}
386
387void PluginProxyMultiThreadTest::QuitNestedLoop() {
388 nested_main_thread_message_loop_->Quit();
389}
390
391// static
392void PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread(
393 void* user_data,
394 int32_t result) {
395 EXPECT_EQ(PP_OK, result);
396 PluginProxyMultiThreadTest* thiz =
397 static_cast<PluginProxyMultiThreadTest*>(user_data);
398 thiz->CheckOnThread(SECONDARY_THREAD);
399 thiz->SetUpTestOnSecondaryThread();
400}
401
[email protected]912f3d6c2011-06-29 18:26:36402// HostProxyTestHarness --------------------------------------------------------
403
[email protected]8be45842012-04-13 19:49:29404class HostProxyTestHarness::MockSyncMessageStatusReceiver
405 : public HostDispatcher::SyncMessageStatusReceiver {
406 public:
407 virtual void BeginBlockOnSyncMessage() OVERRIDE {}
408 virtual void EndBlockOnSyncMessage() OVERRIDE {}
409};
410
[email protected]7ef6b79b2013-01-17 02:38:24411HostProxyTestHarness::HostProxyTestHarness(GlobalsConfiguration globals_config)
412 : globals_config_(globals_config),
413 status_receiver_(new MockSyncMessageStatusReceiver) {
[email protected]912f3d6c2011-06-29 18:26:36414}
415
416HostProxyTestHarness::~HostProxyTestHarness() {
417}
418
[email protected]d57fd382012-09-19 00:55:42419PpapiGlobals* HostProxyTestHarness::GetGlobals() {
420 return host_globals_.get();
421}
422
[email protected]912f3d6c2011-06-29 18:26:36423Dispatcher* HostProxyTestHarness::GetDispatcher() {
424 return host_dispatcher_.get();
425}
426
427void HostProxyTestHarness::SetUpHarness() {
[email protected]73097562012-01-12 19:38:55428 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24429 CreateHostGlobals();
430
[email protected]912f3d6c2011-06-29 18:26:36431 host_dispatcher_.reset(new HostDispatcher(
[email protected]912f3d6c2011-06-29 18:26:36432 pp_module(),
[email protected]8be45842012-04-13 19:49:29433 &MockGetInterface,
[email protected]195d4cde2012-10-02 18:12:41434 status_receiver_.release(),
[email protected]73e20db2012-12-12 22:27:06435 PpapiPermissions::AllPermissions()));
[email protected]912f3d6c2011-06-29 18:26:36436 host_dispatcher_->InitWithTestSink(&sink());
437 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
438}
439
440void HostProxyTestHarness::SetUpHarnessWithChannel(
441 const IPC::ChannelHandle& channel_handle,
442 base::MessageLoopProxy* ipc_message_loop,
443 base::WaitableEvent* shutdown_event,
444 bool is_client) {
[email protected]73097562012-01-12 19:38:55445 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24446 CreateHostGlobals();
447
[email protected]912f3d6c2011-06-29 18:26:36448 delegate_mock_.Init(ipc_message_loop, shutdown_event);
[email protected]73097562012-01-12 19:38:55449
[email protected]912f3d6c2011-06-29 18:26:36450 host_dispatcher_.reset(new HostDispatcher(
[email protected]912f3d6c2011-06-29 18:26:36451 pp_module(),
[email protected]8be45842012-04-13 19:49:29452 &MockGetInterface,
[email protected]195d4cde2012-10-02 18:12:41453 status_receiver_.release(),
[email protected]73e20db2012-12-12 22:27:06454 PpapiPermissions::AllPermissions()));
[email protected]912f3d6c2011-06-29 18:26:36455 ppapi::Preferences preferences;
[email protected]108fd342013-01-04 20:46:54456 host_dispatcher_->InitHostWithChannel(&delegate_mock_,
457 base::kNullProcessId, channel_handle,
[email protected]912f3d6c2011-06-29 18:26:36458 is_client, preferences);
459 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
460}
461
462void HostProxyTestHarness::TearDownHarness() {
463 HostDispatcher::RemoveForInstance(pp_instance());
464 host_dispatcher_.reset();
[email protected]d57fd382012-09-19 00:55:42465 host_globals_.reset();
[email protected]912f3d6c2011-06-29 18:26:36466}
467
[email protected]7ef6b79b2013-01-17 02:38:24468void HostProxyTestHarness::CreateHostGlobals() {
469 if (globals_config_ == PER_THREAD_GLOBALS) {
470 host_globals_.reset(new TestGlobals(PpapiGlobals::PerThreadForTest()));
471 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
472 } else {
473 host_globals_.reset(new TestGlobals());
474 }
475}
476
[email protected]912f3d6c2011-06-29 18:26:36477base::MessageLoopProxy*
478HostProxyTestHarness::DelegateMock::GetIPCMessageLoop() {
479 return ipc_message_loop_;
480}
481
482base::WaitableEvent* HostProxyTestHarness::DelegateMock::GetShutdownEvent() {
483 return shutdown_event_;
484}
485
[email protected]f0ecb552012-05-11 22:09:11486IPC::PlatformFileForTransit
487HostProxyTestHarness::DelegateMock::ShareHandleWithRemote(
488 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54489 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11490 bool should_close_source) {
491 return IPC::GetFileHandleForProcess(handle,
492 base::Process::Current().handle(),
493 should_close_source);
494}
495
[email protected]912f3d6c2011-06-29 18:26:36496
[email protected]465faa22011-02-08 16:31:46497// HostProxyTest ---------------------------------------------------------------
498
[email protected]7ef6b79b2013-01-17 02:38:24499HostProxyTest::HostProxyTest() : HostProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]465faa22011-02-08 16:31:46500}
501
502HostProxyTest::~HostProxyTest() {
503}
504
[email protected]465faa22011-02-08 16:31:46505void HostProxyTest::SetUp() {
[email protected]912f3d6c2011-06-29 18:26:36506 SetUpHarness();
[email protected]465faa22011-02-08 16:31:46507}
508
509void HostProxyTest::TearDown() {
[email protected]912f3d6c2011-06-29 18:26:36510 TearDownHarness();
[email protected]465faa22011-02-08 16:31:46511}
512
[email protected]912f3d6c2011-06-29 18:26:36513// TwoWayTest ---------------------------------------------------------------
514
515TwoWayTest::TwoWayTest(TwoWayTest::TwoWayTestMode test_mode)
516 : test_mode_(test_mode),
[email protected]7ef6b79b2013-01-17 02:38:24517 host_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
518 plugin_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
[email protected]912f3d6c2011-06-29 18:26:36519 io_thread_("TwoWayTest_IOThread"),
520 plugin_thread_("TwoWayTest_PluginThread"),
521 remote_harness_(NULL),
522 local_harness_(NULL),
523 channel_created_(true, false),
524 shutdown_event_(true, false) {
525 if (test_mode == TEST_PPP_INTERFACE) {
526 remote_harness_ = &plugin_;
527 local_harness_ = &host_;
528 } else {
529 remote_harness_ = &host_;
530 local_harness_ = &plugin_;
531 }
532}
533
534TwoWayTest::~TwoWayTest() {
535 shutdown_event_.Signal();
536}
537
538void TwoWayTest::SetUp() {
539 base::Thread::Options options;
540 options.message_loop_type = MessageLoop::TYPE_IO;
541 io_thread_.StartWithOptions(options);
542 plugin_thread_.Start();
543
[email protected]b75673fe2012-10-17 17:59:14544 // Construct the IPC handle name using the process ID so we can safely run
545 // multiple |TwoWayTest|s concurrently.
546 std::ostringstream handle_name;
547 handle_name << "TwoWayTestChannel" << base::GetCurrentProcId();
548 IPC::ChannelHandle handle(handle_name.str());
[email protected]912f3d6c2011-06-29 18:26:36549 base::WaitableEvent remote_harness_set_up(true, false);
550 plugin_thread_.message_loop_proxy()->PostTask(
551 FROM_HERE,
[email protected]1f6581c2011-10-04 23:10:15552 base::Bind(&SetUpRemoteHarness,
553 remote_harness_,
554 handle,
555 io_thread_.message_loop_proxy(),
556 &shutdown_event_,
557 &remote_harness_set_up));
[email protected]912f3d6c2011-06-29 18:26:36558 remote_harness_set_up.Wait();
559 local_harness_->SetUpHarnessWithChannel(handle,
560 io_thread_.message_loop_proxy(),
561 &shutdown_event_,
562 true); // is_client
563}
564
565void TwoWayTest::TearDown() {
566 base::WaitableEvent remote_harness_torn_down(true, false);
567 plugin_thread_.message_loop_proxy()->PostTask(
568 FROM_HERE,
[email protected]1f6581c2011-10-04 23:10:15569 base::Bind(&TearDownRemoteHarness,
570 remote_harness_,
571 &remote_harness_torn_down));
[email protected]912f3d6c2011-06-29 18:26:36572 remote_harness_torn_down.Wait();
573
574 local_harness_->TearDownHarness();
575
576 io_thread_.Stop();
577}
578
[email protected]d57fd382012-09-19 00:55:42579void TwoWayTest::PostTaskOnRemoteHarness(const base::Closure& task) {
580 base::WaitableEvent task_complete(true, false);
581 plugin_thread_.message_loop_proxy()->PostTask(FROM_HERE,
582 base::Bind(&RunTaskOnRemoteHarness,
583 task,
584 &task_complete));
585 task_complete.Wait();
586}
587
588
[email protected]f24448db2011-01-27 20:40:39589} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02590} // namespace ppapi