Index: content/renderer/pepper/host_var_tracker_unittest.cc |
diff --git a/content/renderer/pepper/host_var_tracker_unittest.cc b/content/renderer/pepper/host_var_tracker_unittest.cc |
index 83fd3bdc5aa7fcb0212dba5ccfe8deec19981fb9..07b759dce69343f75b727db0582887d05eb45ca8 100644 |
--- a/content/renderer/pepper/host_var_tracker_unittest.cc |
+++ b/content/renderer/pepper/host_var_tracker_unittest.cc |
@@ -2,64 +2,56 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "base/basictypes.h" |
#include "base/memory/scoped_ptr.h" |
#include "content/renderer/pepper/host_globals.h" |
#include "content/renderer/pepper/host_var_tracker.h" |
#include "content/renderer/pepper/mock_resource.h" |
+#include "content/renderer/pepper/npapi_glue.h" |
+#include "content/renderer/pepper/npobject_var.h" |
#include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
-#include "content/renderer/pepper/pepper_try_catch.h" |
-#include "content/renderer/pepper/v8object_var.h" |
#include "content/test/ppapi_unittest.h" |
-#include "gin/handle.h" |
-#include "gin/wrappable.h" |
#include "ppapi/c/pp_var.h" |
#include "ppapi/c/ppp_instance.h" |
+#include "third_party/npapi/bindings/npruntime.h" |
#include "third_party/WebKit/public/web/WebBindings.h" |
-using ppapi::V8ObjectVar; |
+using ppapi::NPObjectVar; |
namespace content { |
namespace { |
-int g_v8objects_alive = 0; |
+// Tracked NPObjects ----------------------------------------------------------- |
-class MyObject : public gin::Wrappable<MyObject> { |
- public: |
- static gin::WrapperInfo kWrapperInfo; |
+int g_npobjects_alive = 0; |
- static v8::Handle<v8::Value> Create(v8::Isolate* isolate) { |
- return gin::CreateHandle(isolate, new MyObject()).ToV8(); |
- } |
+void TrackedClassDeallocate(NPObject* npobject) { |
+ g_npobjects_alive--; |
+ delete npobject; |
+} |
- private: |
- MyObject() { ++g_v8objects_alive; } |
- virtual ~MyObject() { --g_v8objects_alive; } |
+NPClass g_tracked_npclass = { |
+ NP_CLASS_STRUCT_VERSION, NULL, &TrackedClassDeallocate, NULL, NULL, NULL, |
+ NULL, NULL, NULL, NULL, NULL, NULL, }; |
- DISALLOW_COPY_AND_ASSIGN(MyObject); |
+// Returns a new tracked NPObject with a refcount of 1. You'll want to put this |
+// in a NPObjectReleaser to free this ref when the test completes. |
+NPObject* NewTrackedNPObject() { |
+ NPObject* object = new NPObject; |
+ object->_class = &g_tracked_npclass; |
+ object->referenceCount = 1; |
+ |
+ g_npobjects_alive++; |
+ return object; |
+} |
+ |
+struct ReleaseNPObject { |
+ void operator()(NPObject* o) const { blink::WebBindings::releaseObject(o); } |
}; |
-gin::WrapperInfo MyObject::kWrapperInfo = {gin::kEmbedderNativeGin}; |
- |
-class PepperTryCatchForTest : public PepperTryCatch { |
- public: |
- explicit PepperTryCatchForTest(PepperPluginInstanceImpl* instance) |
- : PepperTryCatch(instance, V8VarConverter::kAllowObjectVars), |
- handle_scope_(instance->GetIsolate()), |
- context_scope_(v8::Context::New(instance->GetIsolate())) {} |
- |
- virtual void SetException(const char* message) OVERRIDE { NOTREACHED(); } |
- virtual v8::Handle<v8::Context> GetContext() OVERRIDE { |
- return instance_->GetIsolate()->GetCurrentContext(); |
- } |
- |
- private: |
- v8::HandleScope handle_scope_; |
- v8::Context::Scope context_scope_; |
- |
- DISALLOW_COPY_AND_ASSIGN(PepperTryCatchForTest); |
-}; |
+// Handles automatically releasing a reference to the NPObject on destruction. |
+// It's assumed the input has a ref already taken. |
+typedef scoped_ptr<NPObject, ReleaseNPObject> NPObjectReleaser; |
} // namespace |
@@ -67,68 +59,58 @@ |
public: |
HostVarTrackerTest() {} |
- virtual void TearDown() OVERRIDE { |
- v8::Isolate::GetCurrent()->RequestGarbageCollectionForTesting( |
- v8::Isolate::kFullGarbageCollection); |
- EXPECT_EQ(0, g_v8objects_alive); |
- PpapiUnittest::TearDown(); |
- } |
- |
HostVarTracker& tracker() { return *HostGlobals::Get()->host_var_tracker(); } |
}; |
TEST_F(HostVarTrackerTest, DeleteObjectVarWithInstance) { |
- v8::Isolate* test_isolate = v8::Isolate::GetCurrent(); |
- |
// Make a second instance (the test harness already creates & manages one). |
scoped_refptr<PepperPluginInstanceImpl> instance2( |
PepperPluginInstanceImpl::Create(NULL, module(), NULL, GURL())); |
PP_Instance pp_instance2 = instance2->pp_instance(); |
- { |
- PepperTryCatchForTest try_catch(instance2.get()); |
- // Make an object var. |
- ppapi::ScopedPPVar var = try_catch.FromV8(MyObject::Create(test_isolate)); |
- EXPECT_EQ(1, g_v8objects_alive); |
- EXPECT_EQ(1, tracker().GetLiveV8ObjectVarsForTest(pp_instance2)); |
- // Purposely leak the var. |
- var.Release(); |
- } |
+ // Make an object var. |
+ NPObjectReleaser npobject(NewTrackedNPObject()); |
+ NPObjectToPPVarForTest(instance2.get(), npobject.get()); |
+ |
+ EXPECT_EQ(1, g_npobjects_alive); |
+ EXPECT_EQ(1, tracker().GetLiveNPObjectVarsForInstance(pp_instance2)); |
// Free the instance, this should release the ObjectVar. |
instance2 = NULL; |
- EXPECT_EQ(0, tracker().GetLiveV8ObjectVarsForTest(pp_instance2)); |
+ EXPECT_EQ(0, tracker().GetLiveNPObjectVarsForInstance(pp_instance2)); |
} |
// Make sure that using the same NPObject should give the same PP_Var |
// each time. |
TEST_F(HostVarTrackerTest, ReuseVar) { |
- PepperTryCatchForTest try_catch(instance()); |
+ NPObjectReleaser npobject(NewTrackedNPObject()); |
- v8::Handle<v8::Value> v8_object = MyObject::Create(v8::Isolate::GetCurrent()); |
- ppapi::ScopedPPVar pp_object1 = try_catch.FromV8(v8_object); |
- ppapi::ScopedPPVar pp_object2 = try_catch.FromV8(v8_object); |
+ PP_Var pp_object1 = NPObjectToPPVarForTest(instance(), npobject.get()); |
+ PP_Var pp_object2 = NPObjectToPPVarForTest(instance(), npobject.get()); |
// The two results should be the same. |
- EXPECT_EQ(pp_object1.get().value.as_id, pp_object2.get().value.as_id); |
+ EXPECT_EQ(pp_object1.value.as_id, pp_object2.value.as_id); |
- // The objects should be able to get us back to the associated v8 object. |
+ // The objects should be able to get us back to the associated NPObject. |
+ // This ObjectVar must be released before we do NPObjectToPPVarForTest again |
+ // below so it gets freed and we get a new identifier. |
{ |
- scoped_refptr<V8ObjectVar> check_object( |
- V8ObjectVar::FromPPVar(pp_object1.get())); |
+ scoped_refptr<NPObjectVar> check_object(NPObjectVar::FromPPVar(pp_object1)); |
ASSERT_TRUE(check_object.get()); |
- EXPECT_EQ(instance(), check_object->instance()); |
- EXPECT_EQ(v8_object, check_object->GetHandle()); |
+ EXPECT_EQ(instance()->pp_instance(), check_object->pp_instance()); |
+ EXPECT_EQ(npobject.get(), check_object->np_object()); |
} |
// Remove both of the refs we made above. |
- pp_object1 = ppapi::ScopedPPVar(); |
- pp_object2 = ppapi::ScopedPPVar(); |
+ ppapi::VarTracker* var_tracker = ppapi::PpapiGlobals::Get()->GetVarTracker(); |
+ var_tracker->ReleaseVar(static_cast<int32_t>(pp_object2.value.as_id)); |
+ var_tracker->ReleaseVar(static_cast<int32_t>(pp_object1.value.as_id)); |
// Releasing the resource should free the internal ref, and so making a new |
// one now should generate a new ID. |
- ppapi::ScopedPPVar pp_object3 = try_catch.FromV8(v8_object); |
- EXPECT_NE(pp_object1.get().value.as_id, pp_object3.get().value.as_id); |
+ PP_Var pp_object3 = NPObjectToPPVarForTest(instance(), npobject.get()); |
+ EXPECT_NE(pp_object1.value.as_id, pp_object3.value.as_id); |
+ var_tracker->ReleaseVar(static_cast<int32_t>(pp_object3.value.as_id)); |
} |
} // namespace content |