blob: 46d7a4ada77f4b615c6589ba70c3f1c8753ed003 [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>
tzika0f614d2016-03-08 00:35:158#include <tuple>
[email protected]b75673fe2012-10-17 17:59:149
[email protected]1f6581c2011-10-04 23:10:1510#include "base/bind.h"
[email protected]7ef6b79b2013-01-17 02:38:2411#include "base/bind_helpers.h"
skyostil23490d42015-06-12 12:54:2612#include "base/location.h"
[email protected]912f3d6c2011-06-29 18:26:3613#include "base/observer_list.h"
rvargas079d1842014-10-17 22:32:1614#include "base/process/process_handle.h"
[email protected]7ef6b79b2013-01-17 02:38:2415#include "base/run_loop.h"
skyostil23490d42015-06-12 12:54:2616#include "base/single_thread_task_runner.h"
gabb19326e2016-05-11 18:10:3117#include "base/threading/thread_task_runner_handle.h"
[email protected]912f3d6c2011-06-29 18:26:3618#include "ipc/ipc_sync_channel.h"
dmichael6b328f3d2014-09-29 23:49:0219#include "ipc/message_filter.h"
[email protected]f24448db2011-01-27 20:40:3920#include "ppapi/c/pp_errors.h"
[email protected]912f3d6c2011-06-29 18:26:3621#include "ppapi/c/private/ppb_proxy_private.h"
[email protected]465faa22011-02-08 16:31:4622#include "ppapi/proxy/ppapi_messages.h"
[email protected]7ef6b79b2013-01-17 02:38:2423#include "ppapi/proxy/ppb_message_loop_proxy.h"
[email protected]f24448db2011-01-27 20:40:3924
[email protected]4d2efd22011-08-18 21:58:0225namespace ppapi {
[email protected]f24448db2011-01-27 20:40:3926namespace proxy {
27
28namespace {
[email protected]912f3d6c2011-06-29 18:26:3629// HostDispatcher requires a PPB_Proxy_Private, so we always provide a fallback
30// do-nothing implementation.
31void PluginCrashed(PP_Module module) {
32 NOTREACHED();
33};
[email protected]f24448db2011-01-27 20:40:3934
[email protected]912f3d6c2011-06-29 18:26:3635PP_Instance GetInstanceForResource(PP_Resource resource) {
36 // If a test relies on this, we need to implement it.
37 NOTREACHED();
38 return 0;
39}
40
41void SetReserveInstanceIDCallback(PP_Module module,
42 PP_Bool (*is_seen)(PP_Module, PP_Instance)) {
43 // This function gets called in HostDispatcher's constructor. We simply don't
44 // worry about Instance uniqueness in tests, so we can ignore the call.
45}
46
[email protected]912f3d6c2011-06-29 18:26:3647void AddRefModule(PP_Module module) {}
48void ReleaseModule(PP_Module module) {}
[email protected]fa4dd2912011-10-17 21:23:2949PP_Bool IsInModuleDestructor(PP_Module module) { return PP_FALSE; }
[email protected]912f3d6c2011-06-29 18:26:3650
[email protected]fa4dd2912011-10-17 21:23:2951PPB_Proxy_Private ppb_proxy_private = {
52 &PluginCrashed,
53 &GetInstanceForResource,
54 &SetReserveInstanceIDCallback,
[email protected]fa4dd2912011-10-17 21:23:2955 &AddRefModule,
56 &ReleaseModule,
57 &IsInModuleDestructor
58};
[email protected]912f3d6c2011-06-29 18:26:3659
60// We allow multiple harnesses at a time to respond to 'GetInterface' calls.
61// We assume that only 1 harness's GetInterface function will ever support a
62// given interface name. In practice, there will either be only 1 GetInterface
63// handler (for PluginProxyTest or HostProxyTest), or there will be only 2
64// GetInterface handlers (for TwoWayTest). In the latter case, one handler is
65// for the PluginProxyTestHarness and should only respond for PPP interfaces,
66// and the other handler is for the HostProxyTestHarness which should only
67// ever respond for PPB interfaces.
brettw236d3172015-06-03 16:31:4368base::ObserverList<ProxyTestHarnessBase> get_interface_handlers_;
[email protected]465faa22011-02-08 16:31:4669
70const void* MockGetInterface(const char* name) {
brettw236d3172015-06-03 16:31:4371 base::ObserverList<ProxyTestHarnessBase>::Iterator it(
72 &get_interface_handlers_);
[email protected]912f3d6c2011-06-29 18:26:3673 while (ProxyTestHarnessBase* observer = it.GetNext()) {
74 const void* interface = observer->GetInterface(name);
75 if (interface)
76 return interface;
[email protected]465faa22011-02-08 16:31:4677 }
[email protected]912f3d6c2011-06-29 18:26:3678 if (strcmp(name, PPB_PROXY_PRIVATE_INTERFACE) == 0)
79 return &ppb_proxy_private;
80 return NULL;
81}
82
83void SetUpRemoteHarness(ProxyTestHarnessBase* harness,
84 const IPC::ChannelHandle& handle,
skyostil23490d42015-06-12 12:54:2685 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:3686 base::WaitableEvent* shutdown_event,
87 base::WaitableEvent* harness_set_up) {
skyostil23490d42015-06-12 12:54:2688 harness->SetUpHarnessWithChannel(handle, ipc_task_runner, shutdown_event,
89 false);
[email protected]912f3d6c2011-06-29 18:26:3690 harness_set_up->Signal();
91}
92
93void TearDownRemoteHarness(ProxyTestHarnessBase* harness,
94 base::WaitableEvent* harness_torn_down) {
95 harness->TearDownHarness();
96 harness_torn_down->Signal();
[email protected]f24448db2011-01-27 20:40:3997}
98
[email protected]d57fd382012-09-19 00:55:4299void RunTaskOnRemoteHarness(const base::Closure& task,
100 base::WaitableEvent* task_complete) {
101 task.Run();
102 task_complete->Signal();
103}
104
[email protected]f24448db2011-01-27 20:40:39105} // namespace
106
[email protected]912f3d6c2011-06-29 18:26:36107// ProxyTestHarnessBase --------------------------------------------------------
[email protected]465faa22011-02-08 16:31:46108
[email protected]912f3d6c2011-06-29 18:26:36109ProxyTestHarnessBase::ProxyTestHarnessBase() : pp_module_(0x98765),
110 pp_instance_(0x12345) {
111 get_interface_handlers_.AddObserver(this);
[email protected]465faa22011-02-08 16:31:46112}
113
[email protected]912f3d6c2011-06-29 18:26:36114ProxyTestHarnessBase::~ProxyTestHarnessBase() {
115 get_interface_handlers_.RemoveObserver(this);
[email protected]465faa22011-02-08 16:31:46116}
117
[email protected]912f3d6c2011-06-29 18:26:36118const void* ProxyTestHarnessBase::GetInterface(const char* name) {
[email protected]465faa22011-02-08 16:31:46119 return registered_interfaces_[name];
120}
121
[email protected]912f3d6c2011-06-29 18:26:36122void ProxyTestHarnessBase::RegisterTestInterface(const char* name,
[email protected]99ff9932011-09-07 14:14:54123 const void* test_interface) {
124 registered_interfaces_[name] = test_interface;
[email protected]465faa22011-02-08 16:31:46125}
126
kareng1c62eeb2014-11-08 16:35:03127bool ProxyTestHarnessBase::SupportsInterface(const char* name) {
[email protected]465faa22011-02-08 16:31:46128 sink().ClearMessages();
129
130 // IPC doesn't actually write to this when we send a message manually
131 // not actually using IPC.
132 bool unused_result = false;
kareng1c62eeb2014-11-08 16:35:03133 PpapiMsg_SupportsInterface msg(name, &unused_result);
[email protected]465faa22011-02-08 16:31:46134 GetDispatcher()->OnMessageReceived(msg);
135
136 const IPC::Message* reply_msg =
137 sink().GetUniqueMessageMatching(IPC_REPLY_ID);
138 EXPECT_TRUE(reply_msg);
139 if (!reply_msg)
140 return false;
141
mdempsky390ab1e2016-03-07 22:18:35142 PpapiMsg_SupportsInterface::ReplyParam 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();
tzika0f614d2016-03-08 00:35:15147 return std::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) {
erikchen09b40032016-04-06 18:51:55256 return IPC::GetPlatformFileForTransit(handle,
257 should_close_source);
[email protected]f0ecb552012-05-11 22:09:11258}
259
erikchen4fc32d52015-06-02 02:16:38260base::SharedMemoryHandle
261PluginProxyTestHarness::PluginDelegateMock::ShareSharedMemoryHandleWithRemote(
262 const base::SharedMemoryHandle& handle,
erikchenfc29ad992015-06-04 20:44:18263 base::ProcessId /* remote_pid */) {
264 return base::SharedMemory::DuplicateHandle(handle);
erikchen4fc32d52015-06-02 02:16:38265}
266
[email protected]912f3d6c2011-06-29 18:26:36267std::set<PP_Instance>*
268PluginProxyTestHarness::PluginDelegateMock::GetGloballySeenInstanceIDSet() {
269 return &instance_id_set_;
270}
271
avie029c4132015-12-23 06:45:22272uint32_t PluginProxyTestHarness::PluginDelegateMock::Register(
[email protected]6fc87e02011-12-20 19:18:45273 PluginDispatcher* plugin_dispatcher) {
274 return 0;
275}
276
277void PluginProxyTestHarness::PluginDelegateMock::Unregister(
avie029c4132015-12-23 06:45:22278 uint32_t plugin_dispatcher_id) {}
[email protected]6fc87e02011-12-20 19:18:45279
[email protected]93df81e2012-08-10 22:22:46280IPC::Sender* PluginProxyTestHarness::PluginDelegateMock::GetBrowserSender() {
[email protected]d57fd382012-09-19 00:55:42281 return browser_sender_;
[email protected]93df81e2012-08-10 22:22:46282}
283
[email protected]2306303a2012-06-11 18:10:37284std::string PluginProxyTestHarness::PluginDelegateMock::GetUILanguage() {
285 return std::string("en-US");
286}
287
mgiuca8ca59182015-07-08 02:10:21288void PluginProxyTestHarness::PluginDelegateMock::PreCacheFontForFlash(
[email protected]6fc87e02011-12-20 19:18:45289 const void* logfontw) {
[email protected]373a95a2011-07-01 16:58:14290}
[email protected]7f801d82011-07-08 23:30:11291
[email protected]72a10722012-06-27 19:30:58292void PluginProxyTestHarness::PluginDelegateMock::SetActiveURL(
293 const std::string& url) {
294}
295
[email protected]1d148062013-07-25 20:25:45296PP_Resource PluginProxyTestHarness::PluginDelegateMock::CreateBrowserFont(
297 Connection connection,
298 PP_Instance instance,
299 const PP_BrowserFont_Trusted_Description& desc,
300 const Preferences& prefs) {
301 return 0;
302}
303
[email protected]912f3d6c2011-06-29 18:26:36304// PluginProxyTest -------------------------------------------------------------
305
[email protected]7ef6b79b2013-01-17 02:38:24306PluginProxyTest::PluginProxyTest() : PluginProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]912f3d6c2011-06-29 18:26:36307}
308
309PluginProxyTest::~PluginProxyTest() {
310}
311
312void PluginProxyTest::SetUp() {
313 SetUpHarness();
314}
315
316void PluginProxyTest::TearDown() {
317 TearDownHarness();
318}
319
[email protected]7ef6b79b2013-01-17 02:38:24320// PluginProxyMultiThreadTest --------------------------------------------------
321
322PluginProxyMultiThreadTest::PluginProxyMultiThreadTest() {
323}
324
325PluginProxyMultiThreadTest::~PluginProxyMultiThreadTest() {
326}
327
328void PluginProxyMultiThreadTest::RunTest() {
skyostil23490d42015-06-12 12:54:26329 main_thread_task_runner_ = PpapiGlobals::Get()->GetMainThreadMessageLoop();
330 ASSERT_EQ(main_thread_task_runner_.get(),
331 base::ThreadTaskRunnerHandle::Get().get());
[email protected]7ef6b79b2013-01-17 02:38:24332 nested_main_thread_message_loop_.reset(new base::RunLoop());
333
334 secondary_thread_.reset(new base::DelegateSimpleThread(
335 this, "PluginProxyMultiThreadTest"));
336
337 {
338 ProxyAutoLock auto_lock;
339
340 // MessageLoopResource assumes that the proxy lock has been acquired.
341 secondary_thread_message_loop_ = new MessageLoopResource(pp_instance());
342
[email protected]7ef6b79b2013-01-17 02:38:24343 ASSERT_EQ(PP_OK,
344 secondary_thread_message_loop_->PostWork(
345 PP_MakeCompletionCallback(
346 &PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread,
347 this),
348 0));
349 }
350
351 SetUpTestOnMainThread();
352
353 secondary_thread_->Start();
354 nested_main_thread_message_loop_->Run();
355 secondary_thread_->Join();
356
357 {
358 ProxyAutoLock auto_lock;
359
360 // The destruction requires a valid PpapiGlobals instance, so we should
361 // explicitly release it.
362 secondary_thread_message_loop_ = NULL;
363 }
364
365 secondary_thread_.reset(NULL);
366 nested_main_thread_message_loop_.reset(NULL);
skyostil23490d42015-06-12 12:54:26367 main_thread_task_runner_ = NULL;
[email protected]7ef6b79b2013-01-17 02:38:24368}
369
370void PluginProxyMultiThreadTest::CheckOnThread(ThreadType thread_type) {
371 ProxyAutoLock auto_lock;
372 if (thread_type == MAIN_THREAD) {
373 ASSERT_TRUE(MessageLoopResource::GetCurrent()->is_main_thread_loop());
374 } else {
375 ASSERT_EQ(secondary_thread_message_loop_.get(),
376 MessageLoopResource::GetCurrent());
377 }
378}
379
380void PluginProxyMultiThreadTest::PostQuitForMainThread() {
skyostil23490d42015-06-12 12:54:26381 main_thread_task_runner_->PostTask(
382 FROM_HERE, base::Bind(&PluginProxyMultiThreadTest::QuitNestedLoop,
383 base::Unretained(this)));
[email protected]7ef6b79b2013-01-17 02:38:24384}
385
386void PluginProxyMultiThreadTest::PostQuitForSecondaryThread() {
387 ProxyAutoLock auto_lock;
388 secondary_thread_message_loop_->PostQuit(PP_TRUE);
389}
390
391void PluginProxyMultiThreadTest::Run() {
392 ProxyAutoLock auto_lock;
393 ASSERT_EQ(PP_OK, secondary_thread_message_loop_->AttachToCurrentThread());
394 ASSERT_EQ(PP_OK, secondary_thread_message_loop_->Run());
[email protected]1b777012013-07-15 20:42:38395 secondary_thread_message_loop_->DetachFromThread();
[email protected]7ef6b79b2013-01-17 02:38:24396}
397
398void PluginProxyMultiThreadTest::QuitNestedLoop() {
399 nested_main_thread_message_loop_->Quit();
400}
401
402// static
403void PluginProxyMultiThreadTest::InternalSetUpTestOnSecondaryThread(
404 void* user_data,
405 int32_t result) {
406 EXPECT_EQ(PP_OK, result);
407 PluginProxyMultiThreadTest* thiz =
408 static_cast<PluginProxyMultiThreadTest*>(user_data);
409 thiz->CheckOnThread(SECONDARY_THREAD);
410 thiz->SetUpTestOnSecondaryThread();
411}
412
[email protected]912f3d6c2011-06-29 18:26:36413// HostProxyTestHarness --------------------------------------------------------
414
[email protected]7ef6b79b2013-01-17 02:38:24415HostProxyTestHarness::HostProxyTestHarness(GlobalsConfiguration globals_config)
dmichael6b328f3d2014-09-29 23:49:02416 : globals_config_(globals_config) {
[email protected]912f3d6c2011-06-29 18:26:36417}
418
419HostProxyTestHarness::~HostProxyTestHarness() {
420}
421
[email protected]d57fd382012-09-19 00:55:42422PpapiGlobals* HostProxyTestHarness::GetGlobals() {
423 return host_globals_.get();
424}
425
[email protected]912f3d6c2011-06-29 18:26:36426Dispatcher* HostProxyTestHarness::GetDispatcher() {
427 return host_dispatcher_.get();
428}
429
430void HostProxyTestHarness::SetUpHarness() {
[email protected]73097562012-01-12 19:38:55431 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24432 CreateHostGlobals();
433
[email protected]912f3d6c2011-06-29 18:26:36434 host_dispatcher_.reset(new HostDispatcher(
[email protected]912f3d6c2011-06-29 18:26:36435 pp_module(),
[email protected]8be45842012-04-13 19:49:29436 &MockGetInterface,
[email protected]73e20db2012-12-12 22:27:06437 PpapiPermissions::AllPermissions()));
[email protected]912f3d6c2011-06-29 18:26:36438 host_dispatcher_->InitWithTestSink(&sink());
439 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
440}
441
442void HostProxyTestHarness::SetUpHarnessWithChannel(
443 const IPC::ChannelHandle& channel_handle,
skyostil23490d42015-06-12 12:54:26444 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]912f3d6c2011-06-29 18:26:36445 base::WaitableEvent* shutdown_event,
446 bool is_client) {
[email protected]73097562012-01-12 19:38:55447 // These must be first since the dispatcher set-up uses them.
[email protected]7ef6b79b2013-01-17 02:38:24448 CreateHostGlobals();
449
skyostil23490d42015-06-12 12:54:26450 delegate_mock_.Init(ipc_task_runner, shutdown_event);
[email protected]73097562012-01-12 19:38:55451
[email protected]912f3d6c2011-06-29 18:26:36452 host_dispatcher_.reset(new HostDispatcher(
[email protected]912f3d6c2011-06-29 18:26:36453 pp_module(),
[email protected]8be45842012-04-13 19:49:29454 &MockGetInterface,
[email protected]73e20db2012-12-12 22:27:06455 PpapiPermissions::AllPermissions()));
[email protected]912f3d6c2011-06-29 18:26:36456 ppapi::Preferences preferences;
[email protected]108fd342013-01-04 20:46:54457 host_dispatcher_->InitHostWithChannel(&delegate_mock_,
458 base::kNullProcessId, channel_handle,
[email protected]912f3d6c2011-06-29 18:26:36459 is_client, preferences);
460 HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
461}
462
463void HostProxyTestHarness::TearDownHarness() {
464 HostDispatcher::RemoveForInstance(pp_instance());
465 host_dispatcher_.reset();
[email protected]d57fd382012-09-19 00:55:42466 host_globals_.reset();
[email protected]912f3d6c2011-06-29 18:26:36467}
468
[email protected]7ef6b79b2013-01-17 02:38:24469void HostProxyTestHarness::CreateHostGlobals() {
dmichaelbd96c1e2015-02-12 00:58:08470 disable_locking_.reset(new ProxyLock::LockingDisablerForTest);
[email protected]7ef6b79b2013-01-17 02:38:24471 if (globals_config_ == PER_THREAD_GLOBALS) {
472 host_globals_.reset(new TestGlobals(PpapiGlobals::PerThreadForTest()));
473 PpapiGlobals::SetPpapiGlobalsOnThreadForTest(GetGlobals());
474 } else {
475 host_globals_.reset(new TestGlobals());
476 }
477}
478
skyostil23490d42015-06-12 12:54:26479base::SingleThreadTaskRunner*
480HostProxyTestHarness::DelegateMock::GetIPCTaskRunner() {
481 return ipc_task_runner_;
[email protected]912f3d6c2011-06-29 18:26:36482}
483
484base::WaitableEvent* HostProxyTestHarness::DelegateMock::GetShutdownEvent() {
485 return shutdown_event_;
486}
487
[email protected]f0ecb552012-05-11 22:09:11488IPC::PlatformFileForTransit
489HostProxyTestHarness::DelegateMock::ShareHandleWithRemote(
490 base::PlatformFile handle,
[email protected]108fd342013-01-04 20:46:54491 base::ProcessId /* remote_pid */,
[email protected]f0ecb552012-05-11 22:09:11492 bool should_close_source) {
erikchen09b40032016-04-06 18:51:55493 return IPC::GetPlatformFileForTransit(handle,
494 should_close_source);
[email protected]f0ecb552012-05-11 22:09:11495}
496
erikchen4fc32d52015-06-02 02:16:38497base::SharedMemoryHandle
498HostProxyTestHarness::DelegateMock::ShareSharedMemoryHandleWithRemote(
499 const base::SharedMemoryHandle& handle,
erikchenfc29ad992015-06-04 20:44:18500 base::ProcessId /*remote_pid*/) {
501 return base::SharedMemory::DuplicateHandle(handle);
erikchen4fc32d52015-06-02 02:16:38502}
[email protected]912f3d6c2011-06-29 18:26:36503
[email protected]465faa22011-02-08 16:31:46504// HostProxyTest ---------------------------------------------------------------
505
[email protected]7ef6b79b2013-01-17 02:38:24506HostProxyTest::HostProxyTest() : HostProxyTestHarness(SINGLETON_GLOBALS) {
[email protected]465faa22011-02-08 16:31:46507}
508
509HostProxyTest::~HostProxyTest() {
510}
511
[email protected]465faa22011-02-08 16:31:46512void HostProxyTest::SetUp() {
[email protected]912f3d6c2011-06-29 18:26:36513 SetUpHarness();
[email protected]465faa22011-02-08 16:31:46514}
515
516void HostProxyTest::TearDown() {
[email protected]912f3d6c2011-06-29 18:26:36517 TearDownHarness();
[email protected]465faa22011-02-08 16:31:46518}
519
[email protected]912f3d6c2011-06-29 18:26:36520// TwoWayTest ---------------------------------------------------------------
521
522TwoWayTest::TwoWayTest(TwoWayTest::TwoWayTestMode test_mode)
523 : test_mode_(test_mode),
[email protected]7ef6b79b2013-01-17 02:38:24524 host_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
525 plugin_(ProxyTestHarnessBase::PER_THREAD_GLOBALS),
[email protected]912f3d6c2011-06-29 18:26:36526 io_thread_("TwoWayTest_IOThread"),
527 plugin_thread_("TwoWayTest_PluginThread"),
528 remote_harness_(NULL),
529 local_harness_(NULL),
gabf40c0a5e2016-06-01 20:10:46530 channel_created_(base::WaitableEvent::ResetPolicy::MANUAL,
531 base::WaitableEvent::InitialState::NOT_SIGNALED),
532 shutdown_event_(base::WaitableEvent::ResetPolicy::MANUAL,
533 base::WaitableEvent::InitialState::NOT_SIGNALED) {
[email protected]912f3d6c2011-06-29 18:26:36534 if (test_mode == TEST_PPP_INTERFACE) {
535 remote_harness_ = &plugin_;
536 local_harness_ = &host_;
537 } else {
538 remote_harness_ = &host_;
539 local_harness_ = &plugin_;
540 }
541}
542
543TwoWayTest::~TwoWayTest() {
544 shutdown_event_.Signal();
545}
546
547void TwoWayTest::SetUp() {
548 base::Thread::Options options;
[email protected]d2881d82013-05-06 19:23:08549 options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]912f3d6c2011-06-29 18:26:36550 io_thread_.StartWithOptions(options);
551 plugin_thread_.Start();
552
[email protected]b75673fe2012-10-17 17:59:14553 // Construct the IPC handle name using the process ID so we can safely run
554 // multiple |TwoWayTest|s concurrently.
555 std::ostringstream handle_name;
556 handle_name << "TwoWayTestChannel" << base::GetCurrentProcId();
557 IPC::ChannelHandle handle(handle_name.str());
gabf40c0a5e2016-06-01 20:10:46558 base::WaitableEvent remote_harness_set_up(
559 base::WaitableEvent::ResetPolicy::MANUAL,
560 base::WaitableEvent::InitialState::NOT_SIGNALED);
skyostil23490d42015-06-12 12:54:26561 plugin_thread_.task_runner()->PostTask(
562 FROM_HERE, base::Bind(&SetUpRemoteHarness, remote_harness_, handle,
vmpstra34d11322016-03-21 20:28:47563 base::RetainedRef(io_thread_.task_runner()),
564 &shutdown_event_, &remote_harness_set_up));
[email protected]912f3d6c2011-06-29 18:26:36565 remote_harness_set_up.Wait();
skyostil23490d42015-06-12 12:54:26566 local_harness_->SetUpHarnessWithChannel(
567 handle, io_thread_.task_runner().get(), &shutdown_event_,
568 true); // is_client
[email protected]912f3d6c2011-06-29 18:26:36569}
570
571void TwoWayTest::TearDown() {
gabf40c0a5e2016-06-01 20:10:46572 base::WaitableEvent remote_harness_torn_down(
573 base::WaitableEvent::ResetPolicy::MANUAL,
574 base::WaitableEvent::InitialState::NOT_SIGNALED);
skyostil23490d42015-06-12 12:54:26575 plugin_thread_.task_runner()->PostTask(
576 FROM_HERE, base::Bind(&TearDownRemoteHarness, remote_harness_,
577 &remote_harness_torn_down));
[email protected]912f3d6c2011-06-29 18:26:36578 remote_harness_torn_down.Wait();
579
580 local_harness_->TearDownHarness();
581
582 io_thread_.Stop();
583}
584
[email protected]d57fd382012-09-19 00:55:42585void TwoWayTest::PostTaskOnRemoteHarness(const base::Closure& task) {
gabf40c0a5e2016-06-01 20:10:46586 base::WaitableEvent task_complete(
587 base::WaitableEvent::ResetPolicy::MANUAL,
588 base::WaitableEvent::InitialState::NOT_SIGNALED);
skyostil23490d42015-06-12 12:54:26589 plugin_thread_.task_runner()->PostTask(
590 FROM_HERE, base::Bind(&RunTaskOnRemoteHarness, task, &task_complete));
[email protected]d57fd382012-09-19 00:55:42591 task_complete.Wait();
592}
593
594
[email protected]f24448db2011-01-27 20:40:39595} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02596} // namespace ppapi