Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(694)

Side by Side Diff: content/renderer/pepper/host_var_tracker_unittest.cc

Issue 459553003: Replace NPObject usage in ppapi with gin (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/renderer/pepper/host_var_tracker.cc ('k') | content/renderer/pepper/message_channel.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h"
5 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
6 #include "content/renderer/pepper/host_globals.h" 7 #include "content/renderer/pepper/host_globals.h"
7 #include "content/renderer/pepper/host_var_tracker.h" 8 #include "content/renderer/pepper/host_var_tracker.h"
8 #include "content/renderer/pepper/mock_resource.h" 9 #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" 10 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
11 #include "content/renderer/pepper/pepper_try_catch.h"
12 #include "content/renderer/pepper/v8object_var.h"
12 #include "content/test/ppapi_unittest.h" 13 #include "content/test/ppapi_unittest.h"
14 #include "gin/handle.h"
15 #include "gin/wrappable.h"
13 #include "ppapi/c/pp_var.h" 16 #include "ppapi/c/pp_var.h"
14 #include "ppapi/c/ppp_instance.h" 17 #include "ppapi/c/ppp_instance.h"
15 #include "third_party/npapi/bindings/npruntime.h"
16 #include "third_party/WebKit/public/web/WebBindings.h" 18 #include "third_party/WebKit/public/web/WebBindings.h"
17 19
18 using ppapi::NPObjectVar; 20 using ppapi::V8ObjectVar;
19 21
20 namespace content { 22 namespace content {
21 23
22 namespace { 24 namespace {
23 25
24 // Tracked NPObjects ----------------------------------------------------------- 26 int g_v8objects_alive = 0;
25 27
26 int g_npobjects_alive = 0; 28 class MyObject : public gin::Wrappable<MyObject> {
29 public:
30 static gin::WrapperInfo kWrapperInfo;
27 31
28 void TrackedClassDeallocate(NPObject* npobject) { 32 static v8::Handle<v8::Value> Create(v8::Isolate* isolate) {
29 g_npobjects_alive--; 33 return gin::CreateHandle(isolate, new MyObject()).ToV8();
30 delete npobject; 34 }
31 }
32 35
33 NPClass g_tracked_npclass = { 36 private:
34 NP_CLASS_STRUCT_VERSION, NULL, &TrackedClassDeallocate, NULL, NULL, NULL, 37 MyObject() { ++g_v8objects_alive; }
35 NULL, NULL, NULL, NULL, NULL, NULL, }; 38 virtual ~MyObject() { --g_v8objects_alive; }
36 39
37 // Returns a new tracked NPObject with a refcount of 1. You'll want to put this 40 DISALLOW_COPY_AND_ASSIGN(MyObject);
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 }; 41 };
51 42
52 // Handles automatically releasing a reference to the NPObject on destruction. 43 gin::WrapperInfo MyObject::kWrapperInfo = {gin::kEmbedderNativeGin};
53 // It's assumed the input has a ref already taken. 44
54 typedef scoped_ptr<NPObject, ReleaseNPObject> NPObjectReleaser; 45 class PepperTryCatchForTest : public PepperTryCatch {
46 public:
47 explicit PepperTryCatchForTest(PepperPluginInstanceImpl* instance)
48 : PepperTryCatch(instance, V8VarConverter::kAllowObjectVars),
49 handle_scope_(instance->GetIsolate()),
50 context_scope_(v8::Context::New(instance->GetIsolate())) {}
51
52 virtual void SetException(const char* message) OVERRIDE { NOTREACHED(); }
53 virtual bool HasException() OVERRIDE { return false; }
54 virtual v8::Handle<v8::Context> GetContext() OVERRIDE {
55 return instance_->GetIsolate()->GetCurrentContext();
56 }
57
58 private:
59 v8::HandleScope handle_scope_;
60 v8::Context::Scope context_scope_;
61
62 DISALLOW_COPY_AND_ASSIGN(PepperTryCatchForTest);
63 };
55 64
56 } // namespace 65 } // namespace
57 66
58 class HostVarTrackerTest : public PpapiUnittest { 67 class HostVarTrackerTest : public PpapiUnittest {
59 public: 68 public:
60 HostVarTrackerTest() {} 69 HostVarTrackerTest() {}
61 70
71 virtual void TearDown() OVERRIDE {
72 v8::Isolate::GetCurrent()->RequestGarbageCollectionForTesting(
73 v8::Isolate::kFullGarbageCollection);
74 EXPECT_EQ(0, g_v8objects_alive);
75 PpapiUnittest::TearDown();
76 }
77
62 HostVarTracker& tracker() { return *HostGlobals::Get()->host_var_tracker(); } 78 HostVarTracker& tracker() { return *HostGlobals::Get()->host_var_tracker(); }
63 }; 79 };
64 80
65 TEST_F(HostVarTrackerTest, DeleteObjectVarWithInstance) { 81 TEST_F(HostVarTrackerTest, DeleteObjectVarWithInstance) {
82 v8::Isolate* test_isolate = v8::Isolate::GetCurrent();
83
66 // Make a second instance (the test harness already creates & manages one). 84 // Make a second instance (the test harness already creates & manages one).
67 scoped_refptr<PepperPluginInstanceImpl> instance2( 85 scoped_refptr<PepperPluginInstanceImpl> instance2(
68 PepperPluginInstanceImpl::Create(NULL, module(), NULL, GURL())); 86 PepperPluginInstanceImpl::Create(NULL, module(), NULL, GURL()));
69 PP_Instance pp_instance2 = instance2->pp_instance(); 87 PP_Instance pp_instance2 = instance2->pp_instance();
70 88
71 // Make an object var. 89 {
72 NPObjectReleaser npobject(NewTrackedNPObject()); 90 PepperTryCatchForTest try_catch(instance2.get());
73 NPObjectToPPVarForTest(instance2.get(), npobject.get()); 91 // Make an object var.
74 92 ppapi::ScopedPPVar var = try_catch.FromV8(MyObject::Create(test_isolate));
75 EXPECT_EQ(1, g_npobjects_alive); 93 EXPECT_EQ(1, g_v8objects_alive);
76 EXPECT_EQ(1, tracker().GetLiveNPObjectVarsForInstance(pp_instance2)); 94 EXPECT_EQ(1, tracker().GetLiveV8ObjectVarsForTest(pp_instance2));
95 // Purposely leak the var.
96 var.Release();
97 }
77 98
78 // Free the instance, this should release the ObjectVar. 99 // Free the instance, this should release the ObjectVar.
79 instance2 = NULL; 100 instance2 = NULL;
80 EXPECT_EQ(0, tracker().GetLiveNPObjectVarsForInstance(pp_instance2)); 101 EXPECT_EQ(0, tracker().GetLiveV8ObjectVarsForTest(pp_instance2));
81 } 102 }
82 103
83 // Make sure that using the same NPObject should give the same PP_Var 104 // Make sure that using the same NPObject should give the same PP_Var
84 // each time. 105 // each time.
85 TEST_F(HostVarTrackerTest, ReuseVar) { 106 TEST_F(HostVarTrackerTest, ReuseVar) {
86 NPObjectReleaser npobject(NewTrackedNPObject()); 107 PepperTryCatchForTest try_catch(instance());
87 108
88 PP_Var pp_object1 = NPObjectToPPVarForTest(instance(), npobject.get()); 109 v8::Handle<v8::Value> v8_object = MyObject::Create(v8::Isolate::GetCurrent());
89 PP_Var pp_object2 = NPObjectToPPVarForTest(instance(), npobject.get()); 110 ppapi::ScopedPPVar pp_object1 = try_catch.FromV8(v8_object);
111 ppapi::ScopedPPVar pp_object2 = try_catch.FromV8(v8_object);
90 112
91 // The two results should be the same. 113 // The two results should be the same.
92 EXPECT_EQ(pp_object1.value.as_id, pp_object2.value.as_id); 114 EXPECT_EQ(pp_object1.get().value.as_id, pp_object2.get().value.as_id);
93 115
94 // The objects should be able to get us back to the associated NPObject. 116 // The objects should be able to get us back to the associated v8 object.
95 // This ObjectVar must be released before we do NPObjectToPPVarForTest again
96 // below so it gets freed and we get a new identifier.
97 { 117 {
98 scoped_refptr<NPObjectVar> check_object(NPObjectVar::FromPPVar(pp_object1)); 118 scoped_refptr<V8ObjectVar> check_object(
119 V8ObjectVar::FromPPVar(pp_object1.get()));
99 ASSERT_TRUE(check_object.get()); 120 ASSERT_TRUE(check_object.get());
100 EXPECT_EQ(instance()->pp_instance(), check_object->pp_instance()); 121 EXPECT_EQ(instance(), check_object->instance());
101 EXPECT_EQ(npobject.get(), check_object->np_object()); 122 EXPECT_EQ(v8_object, check_object->GetHandle());
102 } 123 }
103 124
104 // Remove both of the refs we made above. 125 // Remove both of the refs we made above.
105 ppapi::VarTracker* var_tracker = ppapi::PpapiGlobals::Get()->GetVarTracker(); 126 pp_object1 = ppapi::ScopedPPVar();
106 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object2.value.as_id)); 127 pp_object2 = ppapi::ScopedPPVar();
107 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object1.value.as_id));
108 128
109 // Releasing the resource should free the internal ref, and so making a new 129 // Releasing the resource should free the internal ref, and so making a new
110 // one now should generate a new ID. 130 // one now should generate a new ID.
111 PP_Var pp_object3 = NPObjectToPPVarForTest(instance(), npobject.get()); 131 ppapi::ScopedPPVar pp_object3 = try_catch.FromV8(v8_object);
112 EXPECT_NE(pp_object1.value.as_id, pp_object3.value.as_id); 132 EXPECT_NE(pp_object1.get().value.as_id, pp_object3.get().value.as_id);
113 var_tracker->ReleaseVar(static_cast<int32_t>(pp_object3.value.as_id));
114 } 133 }
115 134
116 } // namespace content 135 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/pepper/host_var_tracker.cc ('k') | content/renderer/pepper/message_channel.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698