1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <stddef.h>
#include <stdint.h>
#include "base/compiler_specific.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/proxy/connection.h"
#include "ppapi/proxy/device_enumeration_resource_helper.h"
#include "ppapi/proxy/plugin_message_filter.h"
#include "ppapi/proxy/plugin_resource.h"
#include "ppapi/proxy/plugin_resource_tracker.h"
#include "ppapi/proxy/plugin_var_tracker.h"
#include "ppapi/proxy/ppapi_message_utils.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/ppapi_proxy_test.h"
#include "ppapi/shared_impl/ppb_device_ref_shared.h"
#include "ppapi/shared_impl/proxy_lock.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_device_ref_api.h"
#include "ppapi/thunk/thunk.h"
namespace ppapi {
namespace proxy {
namespace {
typedef PluginProxyTest DeviceEnumerationResourceHelperTest;
Connection GetConnection(PluginProxyTestHarness* harness) {
CHECK(harness->GetGlobals()->IsPluginGlobals());
return Connection(
static_cast<PluginGlobals*>(harness->GetGlobals())->GetBrowserSender(),
harness->plugin_dispatcher(), 0);
}
bool CompareDeviceRef(PluginVarTracker* var_tracker,
PP_Resource resource,
const DeviceRefData& expected) {
thunk::EnterResourceNoLock<thunk::PPB_DeviceRef_API> enter(resource, true);
if (enter.failed())
return false;
if (expected.type != enter.object()->GetType())
return false;
PP_Var name_pp_var = enter.object()->GetName();
bool result = false;
do {
Var* name_var = var_tracker->GetVar(name_pp_var);
if (!name_var)
break;
StringVar* name_string_var = name_var->AsStringVar();
if (!name_string_var)
break;
if (expected.name != name_string_var->value())
break;
result = true;
} while (false);
var_tracker->ReleaseVar(name_pp_var);
return result;
}
class TestResource : public PluginResource {
public:
TestResource(Connection connection, PP_Instance instance)
: PluginResource(connection, instance),
device_enumeration_(this) {
}
TestResource(const TestResource&) = delete;
TestResource& operator=(const TestResource&) = delete;
~TestResource() override {}
void OnReplyReceived(const ResourceMessageReplyParams& params,
const IPC::Message& msg) override {
if (!device_enumeration_.HandleReply(params, msg))
PluginResource::OnReplyReceived(params, msg);
}
DeviceEnumerationResourceHelper& device_enumeration() {
return device_enumeration_;
}
private:
DeviceEnumerationResourceHelper device_enumeration_;
};
class TestCallback {
public:
TestCallback() : called_(false), result_(PP_ERROR_FAILED) {
}
TestCallback(const TestCallback&) = delete;
TestCallback& operator=(const TestCallback&) = delete;
~TestCallback() {
CHECK(called_);
}
PP_CompletionCallback MakeCompletionCallback() {
return PP_MakeCompletionCallback(&CompletionCallbackBody, this);
}
bool called() const { return called_; }
int32_t result() const { return result_; }
private:
static void CompletionCallbackBody(void* user_data, int32_t result) {
TestCallback* callback = static_cast<TestCallback*>(user_data);
CHECK(!callback->called_);
callback->called_ = true;
callback->result_ = result;
}
bool called_;
int32_t result_;
};
class TestArrayOutput {
public:
explicit TestArrayOutput(PluginResourceTracker* resource_tracker)
: data_(NULL),
count_(0),
resource_tracker_(resource_tracker) {
}
TestArrayOutput(const TestArrayOutput&) = delete;
TestArrayOutput& operator=(const TestArrayOutput&) = delete;
~TestArrayOutput() {
if (count_ > 0) {
for (size_t i = 0; i < count_; ++i)
resource_tracker_->ReleaseResource(data_[i]);
delete [] data_;
}
}
PP_ArrayOutput MakeArrayOutput() {
PP_ArrayOutput array_output = { &GetDataBuffer, this };
return array_output;
}
const PP_Resource* data() const { return data_; }
uint32_t count() const { return count_; }
private:
static void* GetDataBuffer(void* user_data,
uint32_t element_count,
uint32_t element_size) {
CHECK_EQ(element_size, sizeof(PP_Resource));
TestArrayOutput* output = static_cast<TestArrayOutput*>(user_data);
CHECK(!output->data_);
output->count_ = element_count;
if (element_count > 0)
output->data_ = new PP_Resource[element_count];
else
output->data_ = NULL;
return output->data_;
}
PP_Resource* data_;
uint32_t count_;
PluginResourceTracker* resource_tracker_;
};
class TestMonitorDeviceChange {
public:
explicit TestMonitorDeviceChange(PluginVarTracker* var_tracker)
: called_(false),
same_as_expected_(false),
var_tracker_(var_tracker) {
}
TestMonitorDeviceChange(const TestMonitorDeviceChange&) = delete;
TestMonitorDeviceChange& operator=(const TestMonitorDeviceChange&) = delete;
~TestMonitorDeviceChange() {}
void SetExpectedResult(const std::vector<DeviceRefData>& expected) {
called_ = false;
same_as_expected_ = false;
expected_ = expected;
}
bool called() const { return called_; }
bool same_as_expected() const { return same_as_expected_; }
static void MonitorDeviceChangeCallback(void* user_data,
uint32_t device_count,
const PP_Resource devices[]) {
ProxyAutoLock lock;
TestMonitorDeviceChange* helper =
static_cast<TestMonitorDeviceChange*>(user_data);
CHECK(!helper->called_);
helper->called_ = true;
helper->same_as_expected_ = false;
if (device_count != helper->expected_.size())
return;
for (size_t i = 0; i < device_count; ++i) {
if (!CompareDeviceRef(helper->var_tracker_, devices[i],
helper->expected_[i])) {
return;
}
}
helper->same_as_expected_ = true;
}
private:
bool called_;
bool same_as_expected_;
std::vector<DeviceRefData> expected_;
PluginVarTracker* var_tracker_;
};
} // namespace
TEST_F(DeviceEnumerationResourceHelperTest, EnumerateDevices) {
ProxyAutoLock lock;
scoped_refptr<TestResource> resource(
new TestResource(GetConnection(this), pp_instance()));
DeviceEnumerationResourceHelper& device_enumeration =
resource->device_enumeration();
TestArrayOutput output(&resource_tracker());
TestCallback callback;
scoped_refptr<TrackedCallback> tracked_callback(
new TrackedCallback(resource.get(), callback.MakeCompletionCallback()));
int32_t result = device_enumeration.EnumerateDevices(output.MakeArrayOutput(),
tracked_callback);
ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
// Should have sent an EnumerateDevices message.
ResourceMessageCallParams params;
IPC::Message msg;
ASSERT_TRUE(sink().GetFirstResourceCallMatching(
PpapiHostMsg_DeviceEnumeration_EnumerateDevices::ID, ¶ms, &msg));
// Synthesize a response.
ResourceMessageReplyParams reply_params(params.pp_resource(),
params.sequence());
reply_params.set_result(PP_OK);
std::vector<DeviceRefData> data;
DeviceRefData data_item;
data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE;
data_item.name = "name_1";
data_item.id = "id_1";
data.push_back(data_item);
data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE;
data_item.name = "name_2";
data_item.id = "id_2";
data.push_back(data_item);
{
ProxyAutoUnlock unlock;
PluginMessageFilter::DispatchResourceReplyForTest(
reply_params,
PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply(data));
}
EXPECT_TRUE(callback.called());
EXPECT_EQ(PP_OK, callback.result());
EXPECT_EQ(2U, output.count());
for (size_t i = 0; i < output.count(); ++i)
EXPECT_TRUE(CompareDeviceRef(&var_tracker(), output.data()[i], data[i]));
}
TEST_F(DeviceEnumerationResourceHelperTest, MonitorDeviceChange) {
ProxyAutoLock lock;
scoped_refptr<TestResource> resource(
new TestResource(GetConnection(this), pp_instance()));
DeviceEnumerationResourceHelper& device_enumeration =
resource->device_enumeration();
TestMonitorDeviceChange helper(&var_tracker());
int32_t result = device_enumeration.MonitorDeviceChange(
&TestMonitorDeviceChange::MonitorDeviceChangeCallback, &helper);
ASSERT_EQ(PP_OK, result);
// Should have sent a MonitorDeviceChange message.
ResourceMessageCallParams params;
IPC::Message msg;
ASSERT_TRUE(sink().GetFirstResourceCallMatching(
PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange::ID, ¶ms, &msg));
sink().ClearMessages();
uint32_t callback_id = 0;
ASSERT_TRUE(UnpackMessage<PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange>(
msg, &callback_id));
ResourceMessageReplyParams reply_params(params.pp_resource(), 0);
reply_params.set_result(PP_OK);
std::vector<DeviceRefData> data;
helper.SetExpectedResult(data);
{
ProxyAutoUnlock unlock;
// Synthesize a response with no device.
PluginMessageFilter::DispatchResourceReplyForTest(
reply_params,
PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(
callback_id, data));
}
EXPECT_TRUE(helper.called() && helper.same_as_expected());
DeviceRefData data_item;
data_item.type = PP_DEVICETYPE_DEV_AUDIOCAPTURE;
data_item.name = "name_1";
data_item.id = "id_1";
data.push_back(data_item);
data_item.type = PP_DEVICETYPE_DEV_VIDEOCAPTURE;
data_item.name = "name_2";
data_item.id = "id_2";
data.push_back(data_item);
helper.SetExpectedResult(data);
{
ProxyAutoUnlock unlock;
// Synthesize a response with some devices.
PluginMessageFilter::DispatchResourceReplyForTest(
reply_params,
PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(
callback_id, data));
}
EXPECT_TRUE(helper.called() && helper.same_as_expected());
TestMonitorDeviceChange helper2(&var_tracker());
result = device_enumeration.MonitorDeviceChange(
&TestMonitorDeviceChange::MonitorDeviceChangeCallback, &helper2);
ASSERT_EQ(PP_OK, result);
// Should have sent another MonitorDeviceChange message.
ResourceMessageCallParams params2;
IPC::Message msg2;
ASSERT_TRUE(sink().GetFirstResourceCallMatching(
PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange::ID, ¶ms2, &msg2));
sink().ClearMessages();
uint32_t callback_id2 = 0;
ASSERT_TRUE(UnpackMessage<PpapiHostMsg_DeviceEnumeration_MonitorDeviceChange>(
msg2, &callback_id2));
helper.SetExpectedResult(data);
helper2.SetExpectedResult(data);
{
ProxyAutoUnlock unlock;
// |helper2| should receive the result while |helper| shouldn't.
PluginMessageFilter::DispatchResourceReplyForTest(
reply_params,
PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(
callback_id2, data));
}
EXPECT_TRUE(helper2.called() && helper2.same_as_expected());
EXPECT_FALSE(helper.called());
helper.SetExpectedResult(data);
helper2.SetExpectedResult(data);
{
ProxyAutoUnlock unlock;
// Even if a message with |callback_id| arrives. |helper| shouldn't receive
// the result.
PluginMessageFilter::DispatchResourceReplyForTest(
reply_params,
PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(
callback_id, data));
}
EXPECT_FALSE(helper2.called());
EXPECT_FALSE(helper.called());
result = device_enumeration.MonitorDeviceChange(NULL, NULL);
ASSERT_EQ(PP_OK, result);
// Should have sent a StopMonitoringDeviceChange message.
ResourceMessageCallParams params3;
IPC::Message msg3;
ASSERT_TRUE(sink().GetFirstResourceCallMatching(
PpapiHostMsg_DeviceEnumeration_StopMonitoringDeviceChange::ID,
¶ms3, &msg3));
sink().ClearMessages();
helper2.SetExpectedResult(data);
{
ProxyAutoUnlock unlock;
// |helper2| shouldn't receive any result any more.
PluginMessageFilter::DispatchResourceReplyForTest(
reply_params,
PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(
callback_id2, data));
}
EXPECT_FALSE(helper2.called());
}
} // namespace proxy
} // namespace ppapi
|