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
|
// 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.
#include <stdint.h>
#include "base/functional/bind.h"
#include "base/test/test_timeouts.h"
#include "base/time/time.h"
#include "ppapi/c/dev/ppb_var_deprecated.h"
#include "ppapi/c/dev/ppp_class_deprecated.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/c/ppb_var.h"
#include "ppapi/c/ppp_instance.h"
#include "ppapi/c/private/ppp_instance_private.h"
#include "ppapi/proxy/host_dispatcher.h"
#include "ppapi/proxy/interface_list.h"
#include "ppapi/proxy/ppapi_proxy_test.h"
#include "ppapi/shared_impl/ppapi_permissions.h"
#include "ppapi/shared_impl/ppb_var_shared.h"
#include "ppapi/shared_impl/var.h"
namespace ppapi {
// A fake version of V8ObjectVar for testing.
class V8ObjectVar : public ppapi::Var {
public:
V8ObjectVar(const PPP_Class_Deprecated* ppp_class, void* ppp_class_data)
: ppp_class_(ppp_class), ppp_class_data_(ppp_class_data) {}
~V8ObjectVar() override { ppp_class_->Deallocate(ppp_class_data_); }
// Var overrides.
V8ObjectVar* AsV8ObjectVar() override { return this; }
PP_VarType GetType() const override { return PP_VARTYPE_OBJECT; }
private:
const PPP_Class_Deprecated* ppp_class_;
void* ppp_class_data_;
};
namespace proxy {
namespace {
const PP_Instance kInstance = 0xdeadbeef;
PP_Var GetPPVarNoAddRef(Var* var) {
PP_Var var_to_return = var->GetPPVar();
PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var_to_return);
return var_to_return;
}
PluginDispatcher* plugin_dispatcher = NULL;
// Return the plugin-side proxy for PPB_Var_Deprecated.
const PPB_Var_Deprecated* plugin_var_deprecated_if() {
// The test code must set the plugin dispatcher.
CHECK(plugin_dispatcher);
// Grab the plugin-side proxy for PPB_Var_Deprecated (for CreateObject).
return static_cast<const PPB_Var_Deprecated*>(
plugin_dispatcher->GetBrowserInterface(
PPB_VAR_DEPRECATED_INTERFACE));
}
// Mock PPP_Instance_Private.
PP_Var instance_obj;
PP_Var GetInstanceObject(PP_Instance /*instance*/) {
// The 1 ref we got from CreateObject will be passed to the host. We want to
// have a ref of our own.
printf("GetInstanceObject called\n");
plugin_var_deprecated_if()->AddRef(instance_obj);
return instance_obj;
}
PPP_Instance_Private ppp_instance_private_mock = {
&GetInstanceObject
};
// We need to pass in a |PPP_Class_Deprecated| to
// |PPB_Var_Deprecated->CreateObject| for a mock |Deallocate| method.
void Deallocate(void* object) {
}
const PPP_Class_Deprecated ppp_class_deprecated_mock = {
NULL, // HasProperty
NULL, // HasMethod
NULL, // GetProperty
NULL, // GetAllPropertyNames
NULL, // SetProperty
NULL, // RemoveProperty
NULL, // Call
NULL, // Construct
&Deallocate
};
// We need to mock PPP_Instance, so that we can create and destroy the pretend
// instance that PPP_Instance_Private uses.
PP_Bool DidCreate(PP_Instance /*instance*/, uint32_t /*argc*/,
const char* /*argn*/[], const char* /*argv*/[]) {
// Create an object var. This should exercise the typical path for creating
// instance objects.
instance_obj =
plugin_var_deprecated_if()->CreateObject(kInstance,
&ppp_class_deprecated_mock,
NULL);
return PP_TRUE;
}
void DidDestroy(PP_Instance /*instance*/) {
// Decrement the reference count for our instance object. It should be
// deleted.
plugin_var_deprecated_if()->Release(instance_obj);
}
PPP_Instance_1_0 ppp_instance_mock = { &DidCreate, &DidDestroy };
// Mock PPB_Var_Deprecated, so that we can emulate creating an Object Var.
PP_Var CreateObject(PP_Instance /*instance*/,
const PPP_Class_Deprecated* ppp_class,
void* ppp_class_data) {
V8ObjectVar* obj_var = new V8ObjectVar(ppp_class, ppp_class_data);
return obj_var->GetPPVar();
}
const PPB_Var_Deprecated ppb_var_deprecated_mock = {
PPB_Var_Shared::GetVarInterface1_0()->AddRef,
PPB_Var_Shared::GetVarInterface1_0()->Release,
PPB_Var_Shared::GetVarInterface1_0()->VarFromUtf8,
PPB_Var_Shared::GetVarInterface1_0()->VarToUtf8,
NULL, // HasProperty
NULL, // HasMethod
NULL, // GetProperty
NULL, // EnumerateProperties
NULL, // SetProperty
NULL, // RemoveProperty
NULL, // Call
NULL, // Construct
NULL, // IsInstanceOf
&CreateObject
};
class PPP_Instance_Private_ProxyTest : public TwoWayTest {
public:
PPP_Instance_Private_ProxyTest()
: TwoWayTest(TwoWayTest::TEST_PPP_INTERFACE) {
plugin().RegisterTestInterface(PPP_INSTANCE_PRIVATE_INTERFACE,
&ppp_instance_private_mock);
plugin().RegisterTestInterface(PPP_INSTANCE_INTERFACE_1_0,
&ppp_instance_mock);
host().RegisterTestInterface(PPB_VAR_DEPRECATED_INTERFACE,
&ppb_var_deprecated_mock);
}
};
} // namespace
TEST_F(PPP_Instance_Private_ProxyTest, PPPInstancePrivate) {
// This test controls its own instance; we can't use the one that
// PluginProxyTestHarness provides.
ASSERT_NE(kInstance, pp_instance());
HostDispatcher::SetForInstance(kInstance, host().host_dispatcher());
// Requires dev interfaces.
InterfaceList::SetProcessGlobalPermissions(
PpapiPermissions::AllPermissions());
// This file-local global is used by the PPP_Instance mock above in order to
// access PPB_Var_Deprecated.
plugin_dispatcher = plugin().plugin_dispatcher();
// Grab the host-side proxy for PPP_Instance and PPP_Instance_Private.
const PPP_Instance_Private* ppp_instance_private =
static_cast<const PPP_Instance_Private*>(
host().host_dispatcher()->GetProxiedInterface(
PPP_INSTANCE_PRIVATE_INTERFACE));
const PPP_Instance_1_1* ppp_instance = static_cast<const PPP_Instance_1_1*>(
host().host_dispatcher()->GetProxiedInterface(
PPP_INSTANCE_INTERFACE_1_1));
// Initialize an Instance, so that the plugin-side machinery will work
// properly.
EXPECT_EQ(PP_TRUE, ppp_instance->DidCreate(kInstance, 0, NULL, NULL));
// Check the plugin-side reference count.
EXPECT_EQ(1, plugin().var_tracker().GetRefCountForObject(instance_obj));
// Check the host-side var exists with the expected id and has 1 refcount (the
// refcount on behalf of the plugin).
int32_t expected_host_id =
plugin().var_tracker().GetHostObject(instance_obj).value.as_id;
Var* host_var = host().var_tracker().GetVar(expected_host_id);
ASSERT_TRUE(host_var);
EXPECT_EQ(
1,
host().var_tracker().GetRefCountForObject(GetPPVarNoAddRef(host_var)));
// Call from the browser side to get the instance object.
PP_Var host_pp_var = ppp_instance_private->GetInstanceObject(kInstance);
EXPECT_EQ(instance_obj.type, host_pp_var.type);
EXPECT_EQ(host_pp_var.value.as_id, expected_host_id);
EXPECT_EQ(1, plugin().var_tracker().GetRefCountForObject(instance_obj));
// A reference is passed to the browser, which we consume here.
host().var_tracker().ReleaseVar(host_pp_var);
EXPECT_EQ(1, host().var_tracker().GetRefCountForObject(host_pp_var));
// The plugin is going away; generally, so will all references to its instance
// object.
host().var_tracker().ReleaseVar(host_pp_var);
// Destroy the instance. DidDestroy above decrements the reference count for
// instance_obj, so it should also be destroyed.
ppp_instance->DidDestroy(kInstance);
EXPECT_EQ(-1, plugin().var_tracker().GetRefCountForObject(instance_obj));
EXPECT_EQ(-1, host().var_tracker().GetRefCountForObject(host_pp_var));
}
} // namespace proxy
} // namespace ppapi
|