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. 4 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" 17 18 using ppapi::NPObjectVar; 19 20 namespace content { 21 22 namespace { 23 24 // Tracked NPObjects ----------------------------------------------------------- 25 26 int g_npobjects_alive = 0; 27 28 void TrackedClassDeallocate(NPObject* npobject) { 29 g_npobjects_alive--; 30 delete npobject; 31 } 32 33 NPClass g_tracked_npclass = { 34 NP_CLASS_STRUCT_VERSION, NULL, &TrackedClassDeallocate, NULL, NULL, NULL, 35 NULL, NULL, NULL, NULL, NULL, NULL, }; 36 37 // Returns a new tracked NPObject with a refcount of 1. You'll want to put this 38 // in a NPObjectReleaser to free this ref when the test completes. 39 NPObject* NewTrackedNPObject() { 40 NPObject* object = new NPObject; 41 object->_class = &g_tracked_npclass; 42 object->referenceCount = 1; 43 44 g_npobjects_alive++; 45 return object; 46 } 47 48 struct ReleaseNPObject { 49 void operator()(NPObject* o) const { blink::WebBindings::releaseObject(o); } 50 }; 51 52 // Handles automatically releasing a reference to the NPObject on destruction. 53 // It's assumed the input has a ref already taken. 54 typedef scoped_ptr<NPObject, ReleaseNPObject> NPObjectReleaser; 55 56 } // namespace 57 58 class HostVarTrackerTest : public PpapiUnittest { 59 public: 60 HostVarTrackerTest() {} 61 62 HostVarTracker& tracker() { return *HostGlobals::Get()->host_var_tracker(); } 63 }; 64 65 TEST_F(HostVarTrackerTest, DeleteObjectVarWithInstance) { 66 // Make a second instance (the test harness already creates & manages one). 67 scoped_refptr<PepperPluginInstanceImpl> instance2( 68 PepperPluginInstanceImpl::Create(NULL, module(), NULL, GURL())); 69 PP_Instance pp_instance2 = instance2->pp_instance(); 70 71 // Make an object var. 72 NPObjectReleaser npobject(NewTrackedNPObject()); 73 NPObjectToPPVarForTest(instance2.get(), npobject.get()); 74 75 EXPECT_EQ(1, g_npobjects_alive); 76 EXPECT_EQ(1, tracker().GetLiveNPObjectVarsForInstance(pp_instance2)); 77 78 // Free the instance, this should release the ObjectVar. 79 instance2 = NULL; 80 EXPECT_EQ(0, tracker().GetLiveNPObjectVarsForInstance(pp_instance2)); 81 } 82 83 // Make sure that using the same NPObject should give the same PP_Var 84 // each time. 85 TEST_F(HostVarTrackerTest, ReuseVar) { 86 NPObjectReleaser npobject(NewTrackedNPObject()); 87 88 PP_Var pp_object1 = NPObjectToPPVarForTest(instance(), npobject.get()); 89 PP_Var pp_object2 = NPObjectToPPVarForTest(instance(), npobject.get()); 90 91 // The two results should be the same. 92 EXPECT_EQ(pp_object1.value.as_id, pp_object2.value.as_id); 93 94 // The objects should be able to get us back to the associated NPObject. 95 // This ObjectVar must be released before we do NPObjectToPPVarForTest again 96 // below so it gets freed and we get a new identifier. 97 { 98 scoped_refptr<NPObjectVar> check_object(NPObjectVar::FromPPVar(pp_object1)); 99 ASSERT_TRUE(check_object.get()); 100 EXPECT_EQ(instance()->pp_instance(), check_object->pp_instance()); 101 EXPECT_EQ(npobject.get(), check_object->np_object()); 102 } 103 104 // Remove both of the refs we made above. 105 ppapi::VarTracker* var_tracker = ppapi::PpapiGlobals::Get()->GetVarTracker(); 106 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object2.value.as_id)); 107 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object1.value.as_id)); 108 109 // Releasing the resource should free the internal ref, and so making a new 110 // one now should generate a new ID. 111 PP_Var pp_object3 = NPObjectToPPVarForTest(instance(), npobject.get()); 112 EXPECT_NE(pp_object1.value.as_id, pp_object3.value.as_id); 113 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object3.value.as_id)); 114 } 115 116 } // namespace content 117