1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "content/renderer/pepper/host_globals.h"
7 #include "content/renderer/pepper/host_var_tracker.h"
8 #include "content/renderer/pepper/mock_resource.h"
9 #include "content/renderer/pepper/npapi_glue.h"
10 #include "content/renderer/pepper/npobject_var.h"
11 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
12 #include "content/test/ppapi_unittest.h"
13 #include "ppapi/c/pp_var.h"
14 #include "ppapi/c/ppp_instance.h"
15 #include "third_party/npapi/bindings/npruntime.h"
16 #include "third_party/WebKit/public/web/WebBindings.h"
18 using ppapi::NPObjectVar;
24 // Tracked NPObjects -----------------------------------------------------------
26 int g_npobjects_alive = 0;
28 void TrackedClassDeallocate(NPObject* npobject) {
33 NPClass g_tracked_npclass = {
34 NP_CLASS_STRUCT_VERSION,
36 &TrackedClassDeallocate,
48 // Returns a new tracked NPObject with a refcount of 1. You'll want to put this
49 // in a NPObjectReleaser to free this ref when the test completes.
50 NPObject* NewTrackedNPObject() {
51 NPObject* object = new NPObject;
52 object->_class = &g_tracked_npclass;
53 object->referenceCount = 1;
59 class ReleaseNPObject {
61 void operator()(NPObject* o) const {
62 blink::WebBindings::releaseObject(o);
66 // Handles automatically releasing a reference to the NPObject on destruction.
67 // It's assumed the input has a ref already taken.
68 typedef scoped_ptr_malloc<NPObject, ReleaseNPObject> NPObjectReleaser;
72 class HostVarTrackerTest : public PpapiUnittest {
74 HostVarTrackerTest() {
77 HostVarTracker& tracker() {
78 return *HostGlobals::Get()->host_var_tracker();
82 TEST_F(HostVarTrackerTest, DeleteObjectVarWithInstance) {
83 // Make a second instance (the test harness already creates & manages one).
84 scoped_refptr<PepperPluginInstanceImpl> instance2(
85 PepperPluginInstanceImpl::Create(NULL, module(), NULL, GURL()));
86 PP_Instance pp_instance2 = instance2->pp_instance();
88 // Make an object var.
89 NPObjectReleaser npobject(NewTrackedNPObject());
90 NPObjectToPPVarForTest(instance2.get(), npobject.get());
92 EXPECT_EQ(1, g_npobjects_alive);
93 EXPECT_EQ(1, tracker().GetLiveNPObjectVarsForInstance(pp_instance2));
95 // Free the instance, this should release the ObjectVar.
97 EXPECT_EQ(0, tracker().GetLiveNPObjectVarsForInstance(pp_instance2));
100 // Make sure that using the same NPObject should give the same PP_Var
102 TEST_F(HostVarTrackerTest, ReuseVar) {
103 NPObjectReleaser npobject(NewTrackedNPObject());
105 PP_Var pp_object1 = NPObjectToPPVarForTest(instance(), npobject.get());
106 PP_Var pp_object2 = NPObjectToPPVarForTest(instance(), npobject.get());
108 // The two results should be the same.
109 EXPECT_EQ(pp_object1.value.as_id, pp_object2.value.as_id);
111 // The objects should be able to get us back to the associated NPObject.
112 // This ObjectVar must be released before we do NPObjectToPPVarForTest again
113 // below so it gets freed and we get a new identifier.
115 scoped_refptr<NPObjectVar> check_object(NPObjectVar::FromPPVar(pp_object1));
116 ASSERT_TRUE(check_object.get());
117 EXPECT_EQ(instance()->pp_instance(), check_object->pp_instance());
118 EXPECT_EQ(npobject.get(), check_object->np_object());
121 // Remove both of the refs we made above.
122 ppapi::VarTracker* var_tracker =
123 ppapi::PpapiGlobals::Get()->GetVarTracker();
124 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object2.value.as_id));
125 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object1.value.as_id));
127 // Releasing the resource should free the internal ref, and so making a new
128 // one now should generate a new ID.
129 PP_Var pp_object3 = NPObjectToPPVarForTest(instance(), npobject.get());
130 EXPECT_NE(pp_object1.value.as_id, pp_object3.value.as_id);
131 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object3.value.as_id));
134 } // namespace content