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