Home | History | Annotate | Download | only in pepper
      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,
     35   NULL,
     36   &TrackedClassDeallocate,
     37   NULL,
     38   NULL,
     39   NULL,
     40   NULL,
     41   NULL,
     42   NULL,
     43   NULL,
     44   NULL,
     45   NULL,
     46 };
     47 
     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;
     54 
     55   g_npobjects_alive++;
     56   return object;
     57 }
     58 
     59 class ReleaseNPObject {
     60  public:
     61   void operator()(NPObject* o) const {
     62     blink::WebBindings::releaseObject(o);
     63   }
     64 };
     65 
     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;
     69 
     70 }  // namespace
     71 
     72 class HostVarTrackerTest : public PpapiUnittest {
     73  public:
     74   HostVarTrackerTest() {
     75   }
     76 
     77   HostVarTracker& tracker() {
     78     return *HostGlobals::Get()->host_var_tracker();
     79   }
     80 };
     81 
     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();
     87 
     88   // Make an object var.
     89   NPObjectReleaser npobject(NewTrackedNPObject());
     90   NPObjectToPPVarForTest(instance2.get(), npobject.get());
     91 
     92   EXPECT_EQ(1, g_npobjects_alive);
     93   EXPECT_EQ(1, tracker().GetLiveNPObjectVarsForInstance(pp_instance2));
     94 
     95   // Free the instance, this should release the ObjectVar.
     96   instance2 = NULL;
     97   EXPECT_EQ(0, tracker().GetLiveNPObjectVarsForInstance(pp_instance2));
     98 }
     99 
    100 // Make sure that using the same NPObject should give the same PP_Var
    101 // each time.
    102 TEST_F(HostVarTrackerTest, ReuseVar) {
    103   NPObjectReleaser npobject(NewTrackedNPObject());
    104 
    105   PP_Var pp_object1 = NPObjectToPPVarForTest(instance(), npobject.get());
    106   PP_Var pp_object2 = NPObjectToPPVarForTest(instance(), npobject.get());
    107 
    108   // The two results should be the same.
    109   EXPECT_EQ(pp_object1.value.as_id, pp_object2.value.as_id);
    110 
    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.
    114   {
    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());
    119   }
    120 
    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));
    126 
    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));
    132 }
    133 
    134 }  // namespace content
    135