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

Side by Side Diff: ppapi/proxy/ppp_instance_private_proxy_unittest.cc

Issue 9034035: Make it possible to have 1 PpapiGlobals per thread. Update unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review comments. Created 8 years, 11 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 | « ppapi/proxy/ppb_var_unittest.cc ('k') | ppapi/proxy/ppp_instance_proxy_unittest.cc » ('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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/bind.h"
6 #include "base/message_loop.h"
7 #include "base/test/test_timeouts.h"
8 #include "base/time.h"
5 #include "ppapi/c/dev/ppb_var_deprecated.h" 9 #include "ppapi/c/dev/ppb_var_deprecated.h"
6 #include "ppapi/c/dev/ppp_class_deprecated.h" 10 #include "ppapi/c/dev/ppp_class_deprecated.h"
7 #include "ppapi/c/pp_var.h" 11 #include "ppapi/c/pp_var.h"
8 #include "ppapi/c/ppb_var.h" 12 #include "ppapi/c/ppb_var.h"
9 #include "ppapi/c/ppp_instance.h" 13 #include "ppapi/c/ppp_instance.h"
10 #include "ppapi/c/private/ppp_instance_private.h" 14 #include "ppapi/c/private/ppp_instance_private.h"
11 #include "ppapi/proxy/host_dispatcher.h" 15 #include "ppapi/proxy/host_dispatcher.h"
12 #include "ppapi/proxy/ppapi_proxy_test.h" 16 #include "ppapi/proxy/ppapi_proxy_test.h"
17 #include "ppapi/shared_impl/ppb_var_shared.h"
18 #include "ppapi/shared_impl/var.h"
13 19
14 namespace ppapi { 20 namespace ppapi {
21
22 // A fake version of NPObjectVar for testing.
23 class NPObjectVar : public ppapi::Var {
24 public:
25 NPObjectVar() {}
26 virtual ~NPObjectVar() {}
27
28 // Var overrides.
29 virtual NPObjectVar* AsNPObjectVar() OVERRIDE { return this; }
30 virtual PP_VarType GetType() const OVERRIDE { return PP_VARTYPE_OBJECT; }
31 };
32
15 namespace proxy { 33 namespace proxy {
16 34
17 namespace { 35 namespace {
18 const PP_Instance kInstance = 0xdeadbeef; 36 const PP_Instance kInstance = 0xdeadbeef;
19 37
20 PP_Var MakeObjectVar(int64_t object_id) { 38 PP_Var GetPPVarNoAddRef(Var* var) {
21 PP_Var ret; 39 PP_Var var_to_return = var->GetPPVar();
22 ret.type = PP_VARTYPE_OBJECT; 40 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var_to_return);
23 ret.value.as_id = object_id; 41 return var_to_return;
24 return ret;
25 } 42 }
26 43
27 PluginDispatcher* plugin_dispatcher = NULL; 44 PluginDispatcher* plugin_dispatcher = NULL;
28 // Return the plugin-side proxy for PPB_Var_Deprecated. 45 // Return the plugin-side proxy for PPB_Var_Deprecated.
29 const PPB_Var_Deprecated* plugin_var_deprecated_if() { 46 const PPB_Var_Deprecated* plugin_var_deprecated_if() {
30 // The test code must set the plugin dispatcher. 47 // The test code must set the plugin dispatcher.
31 CHECK(plugin_dispatcher); 48 CHECK(plugin_dispatcher);
32 // Grab the plugin-side proxy for PPB_Var_Deprecated (for CreateObject). 49 // Grab the plugin-side proxy for PPB_Var_Deprecated (for CreateObject).
33 return static_cast<const PPB_Var_Deprecated*>( 50 return static_cast<const PPB_Var_Deprecated*>(
34 plugin_dispatcher->GetBrowserInterface( 51 plugin_dispatcher->GetBrowserInterface(
35 PPB_VAR_DEPRECATED_INTERFACE)); 52 PPB_VAR_DEPRECATED_INTERFACE));
36 } 53 }
37 54
38 // Mock PPP_Instance_Private. 55 // Mock PPP_Instance_Private.
39 PP_Var instance_obj; 56 PP_Var instance_obj;
40 PP_Var GetInstanceObject(PP_Instance /*instance*/) { 57 PP_Var GetInstanceObject(PP_Instance /*instance*/) {
41 // The 1 ref we got from CreateObject will be passed to the host. We want to 58 // The 1 ref we got from CreateObject will be passed to the host. We want to
42 // have a ref of our own. 59 // have a ref of our own.
43 plugin_var_deprecated_if()->AddRef(instance_obj); 60 printf("GetInstanceObject called\n");
61 PpapiGlobals::Get()->GetVarTracker()->AddRefVar(instance_obj);
44 return instance_obj; 62 return instance_obj;
45 } 63 }
46 64
47 PPP_Instance_Private ppp_instance_private_mock = { 65 PPP_Instance_Private ppp_instance_private_mock = {
48 &GetInstanceObject 66 &GetInstanceObject
49 }; 67 };
50 68
51 // We need to mock PPP_Instance, so that we can create and destroy the pretend 69 // We need to mock PPP_Instance, so that we can create and destroy the pretend
52 // instance that PPP_Instance_Private uses. 70 // instance that PPP_Instance_Private uses.
53 PP_Bool DidCreate(PP_Instance /*instance*/, uint32_t /*argc*/, 71 PP_Bool DidCreate(PP_Instance /*instance*/, uint32_t /*argc*/,
54 const char* /*argn*/[], const char* /*argv*/[]) { 72 const char* /*argn*/[], const char* /*argv*/[]) {
55 // Create an object var. This should exercise the typical path for creating 73 // Create an object var. This should exercise the typical path for creating
56 // instance objects. 74 // instance objects.
57 instance_obj = 75 instance_obj =
58 plugin_var_deprecated_if()->CreateObject(kInstance, NULL, NULL); 76 plugin_var_deprecated_if()->CreateObject(kInstance, NULL, NULL);
59 return PP_TRUE; 77 return PP_TRUE;
60 } 78 }
61 79
62 void DidDestroy(PP_Instance /*instance*/) { 80 void DidDestroy(PP_Instance /*instance*/) {
63 // Decrement the reference count for our instance object. It should be 81 // Decrement the reference count for our instance object. It should be
64 // deleted. 82 // deleted.
65 plugin_var_deprecated_if()->Release(instance_obj); 83 plugin_var_deprecated_if()->Release(instance_obj);
66 } 84 }
67 85
68 PPP_Instance_1_0 ppp_instance_mock = { &DidCreate, &DidDestroy }; 86 PPP_Instance_1_0 ppp_instance_mock = { &DidCreate, &DidDestroy };
69 87
70 } // namespace
71
72 // Mock PPB_Var_Deprecated, so that we can emulate creating an Object Var. 88 // Mock PPB_Var_Deprecated, so that we can emulate creating an Object Var.
73 std::map<int64_t, int> id_refcount_map; 89 PP_Var CreateObject(PP_Instance /*instance*/,
74 void AddRefVar(PP_Var var) {
75 CHECK(var.type >= PP_VARTYPE_STRING); // Must be a ref-counted type.
76 CHECK(id_refcount_map.find(var.value.as_id) != id_refcount_map.end());
77 ++id_refcount_map[var.value.as_id];
78 }
79
80 void ReleaseVar(PP_Var var) {
81 CHECK(var.type >= PP_VARTYPE_STRING); // Must be a ref-counted type.
82 std::map<int64_t, int>::iterator iter = id_refcount_map.find(var.value.as_id);
83 CHECK(iter != id_refcount_map.end());
84 CHECK(iter->second > 0);
85 if (--(iter->second) == 0)
86 id_refcount_map.erase(iter);
87 }
88
89 PP_Var CreateObject(PP_Instance instance,
90 const PPP_Class_Deprecated* /*ppp_class*/, 90 const PPP_Class_Deprecated* /*ppp_class*/,
91 void* /*ppp_class_data*/) { 91 void* /*ppp_class_data*/) {
92 static int64_t last_id = 0; 92 NPObjectVar* obj_var = new NPObjectVar;
93 ++last_id; 93 return obj_var->GetPPVar();
94 // Set the refcount to 0. It should really be 1, but that ref gets passed to
95 // the plugin immediately (and we don't emulate all of that behavior here).
96 id_refcount_map[last_id] = 0;
97 return MakeObjectVar(last_id);
98 } 94 }
99 95
100 const PPB_Var_Deprecated ppb_var_deprecated_mock = { 96 const PPB_Var_Deprecated ppb_var_deprecated_mock = {
101 &AddRefVar, 97 PPB_Var_Shared::GetVarInterface1_0()->AddRef,
102 &ReleaseVar, 98 PPB_Var_Shared::GetVarInterface1_0()->Release,
103 NULL, // VarFromUtf8 99 PPB_Var_Shared::GetVarInterface1_0()->VarFromUtf8,
104 NULL, // VarToUtf8 100 PPB_Var_Shared::GetVarInterface1_0()->VarToUtf8,
105 NULL, // HasProperty 101 NULL, // HasProperty
106 NULL, // HasMethod 102 NULL, // HasMethod
107 NULL, // GetProperty 103 NULL, // GetProperty
108 NULL, // EnumerateProperties 104 NULL, // EnumerateProperties
109 NULL, // SetProperty 105 NULL, // SetProperty
110 NULL, // RemoveProperty 106 NULL, // RemoveProperty
111 NULL, // Call 107 NULL, // Call
112 NULL, // Construct 108 NULL, // Construct
113 NULL, // IsInstanceOf 109 NULL, // IsInstanceOf
114 &CreateObject 110 &CreateObject
115 }; 111 };
116 112
117 const PPB_Var ppb_var_mock = { &AddRefVar, &ReleaseVar }; 113 } // namespace
118 114
119 class PPP_Instance_Private_ProxyTest : public TwoWayTest { 115 class PPP_Instance_Private_ProxyTest : public TwoWayTest {
120 public: 116 public:
121 PPP_Instance_Private_ProxyTest() 117 PPP_Instance_Private_ProxyTest()
122 : TwoWayTest(TwoWayTest::TEST_PPP_INTERFACE) { 118 : TwoWayTest(TwoWayTest::TEST_PPP_INTERFACE) {
123 plugin().RegisterTestInterface(PPP_INSTANCE_PRIVATE_INTERFACE, 119 plugin().RegisterTestInterface(PPP_INSTANCE_PRIVATE_INTERFACE,
124 &ppp_instance_private_mock); 120 &ppp_instance_private_mock);
125 plugin().RegisterTestInterface(PPP_INSTANCE_INTERFACE_1_0, 121 plugin().RegisterTestInterface(PPP_INSTANCE_INTERFACE_1_0,
126 &ppp_instance_mock); 122 &ppp_instance_mock);
127 host().RegisterTestInterface(PPB_VAR_DEPRECATED_INTERFACE, 123 host().RegisterTestInterface(PPB_VAR_DEPRECATED_INTERFACE,
128 &ppb_var_deprecated_mock); 124 &ppb_var_deprecated_mock);
129 host().RegisterTestInterface(PPB_VAR_INTERFACE,
130 &ppb_var_mock);
131 } 125 }
132 }; 126 };
133 127
134 TEST_F(PPP_Instance_Private_ProxyTest, PPPInstancePrivate) { 128 TEST_F(PPP_Instance_Private_ProxyTest, PPPInstancePrivate) {
135 // This test controls its own instance; we can't use the one that 129 // This test controls its own instance; we can't use the one that
136 // PluginProxyTestHarness provides. 130 // PluginProxyTestHarness provides.
137 ASSERT_NE(kInstance, pp_instance()); 131 ASSERT_NE(kInstance, pp_instance());
138 HostDispatcher::SetForInstance(kInstance, host().host_dispatcher()); 132 HostDispatcher::SetForInstance(kInstance, host().host_dispatcher());
139 133
140 // This file-local global is used by the PPP_Instance mock above in order to 134 // This file-local global is used by the PPP_Instance mock above in order to
141 // access PPB_Var_Deprecated. 135 // access PPB_Var_Deprecated.
142 plugin_dispatcher = plugin().plugin_dispatcher(); 136 plugin_dispatcher = plugin().plugin_dispatcher();
143 137
144 // Grab the host-side proxy for PPP_Instance and PPP_Instance_Private. 138 // Grab the host-side proxy for PPP_Instance and PPP_Instance_Private.
145 const PPP_Instance_Private* ppp_instance_private = 139 const PPP_Instance_Private* ppp_instance_private =
146 static_cast<const PPP_Instance_Private*>( 140 static_cast<const PPP_Instance_Private*>(
147 host().host_dispatcher()->GetProxiedInterface( 141 host().host_dispatcher()->GetProxiedInterface(
148 PPP_INSTANCE_PRIVATE_INTERFACE)); 142 PPP_INSTANCE_PRIVATE_INTERFACE));
149 const PPP_Instance_1_0* ppp_instance = static_cast<const PPP_Instance_1_0*>( 143 const PPP_Instance_1_1* ppp_instance = static_cast<const PPP_Instance_1_1*>(
150 host().host_dispatcher()->GetProxiedInterface( 144 host().host_dispatcher()->GetProxiedInterface(
151 PPP_INSTANCE_INTERFACE_1_0)); 145 PPP_INSTANCE_INTERFACE_1_1));
152 146
153 // Initialize an Instance, so that the plugin-side machinery will work 147 // Initialize an Instance, so that the plugin-side machinery will work
154 // properly. 148 // properly.
155 EXPECT_EQ(PP_TRUE, ppp_instance->DidCreate(kInstance, 0, NULL, NULL)); 149 EXPECT_EQ(PP_TRUE, ppp_instance->DidCreate(kInstance, 0, NULL, NULL));
156 150
157 // Now instance_obj is valid and should have a ref-count of 1.
158 PluginVarTracker& plugin_var_tracker =
159 *PluginGlobals::Get()->plugin_var_tracker();
160 // Check the plugin-side reference count. 151 // Check the plugin-side reference count.
161 EXPECT_EQ(1, plugin_var_tracker.GetRefCountForObject(instance_obj)); 152 EXPECT_EQ(1, plugin().var_tracker().GetRefCountForObject(instance_obj));
162 // Check the host-side var and reference count. 153 // Check the host-side var exists with the expected id and has 1 refcount (the
163 ASSERT_EQ(1u, id_refcount_map.size()); 154 // refcount on behalf of the plugin).
164 EXPECT_EQ(plugin_var_tracker.GetHostObject(instance_obj).value.as_id, 155 int32 expected_host_id =
165 id_refcount_map.begin()->first); 156 plugin().var_tracker().GetHostObject(instance_obj).value.as_id;
166 EXPECT_EQ(0, id_refcount_map.begin()->second); 157 Var* host_var = host().var_tracker().GetVar(expected_host_id);
158 ASSERT_TRUE(host_var);
159 EXPECT_EQ(
160 1,
161 host().var_tracker().GetRefCountForObject(GetPPVarNoAddRef(host_var)));
167 162
168 // Call from the browser side to get the instance object. 163 // Call from the browser side to get the instance object.
169 PP_Var host_obj = ppp_instance_private->GetInstanceObject(kInstance); 164 PP_Var host_pp_var = ppp_instance_private->GetInstanceObject(kInstance);
170 EXPECT_EQ(instance_obj.type, host_obj.type); 165 EXPECT_EQ(instance_obj.type, host_pp_var.type);
171 EXPECT_EQ(host_obj.value.as_id, 166 EXPECT_EQ(host_pp_var.value.as_id, expected_host_id);
172 plugin_var_tracker.GetHostObject(instance_obj).value.as_id); 167 EXPECT_EQ(1, plugin().var_tracker().GetRefCountForObject(instance_obj));
173 EXPECT_EQ(1, plugin_var_tracker.GetRefCountForObject(instance_obj)); 168 // A reference is passed to the browser, which we consume here.
174 ASSERT_EQ(1u, id_refcount_map.size()); 169 host().var_tracker().ReleaseVar(host_pp_var);
175 // The browser should be passed a reference. 170 EXPECT_EQ(1, host().var_tracker().GetRefCountForObject(host_pp_var));
176 EXPECT_EQ(1, id_refcount_map.begin()->second);
177 171
178 // The plugin is going away; generally, so will all references to its instance 172 // The plugin is going away; generally, so will all references to its instance
179 // object. 173 // object.
180 ReleaseVar(host_obj); 174 host().var_tracker().ReleaseVar(host_pp_var);
181 // Destroy the instance. DidDestroy above decrements the reference count for 175 // Destroy the instance. DidDestroy above decrements the reference count for
182 // instance_obj, so it should also be destroyed. 176 // instance_obj, so it should also be destroyed.
183 ppp_instance->DidDestroy(kInstance); 177 ppp_instance->DidDestroy(kInstance);
184 EXPECT_EQ(-1, plugin_var_tracker.GetRefCountForObject(instance_obj)); 178 EXPECT_EQ(-1, plugin().var_tracker().GetRefCountForObject(instance_obj));
185 // Check the host-side reference count. 179 EXPECT_EQ(-1, host().var_tracker().GetRefCountForObject(host_pp_var));
186 EXPECT_EQ(0u, id_refcount_map.size());
187 } 180 }
188 181
189 } // namespace proxy 182 } // namespace proxy
190 } // namespace ppapi 183 } // namespace ppapi
191 184
OLDNEW
« no previous file with comments | « ppapi/proxy/ppb_var_unittest.cc ('k') | ppapi/proxy/ppp_instance_proxy_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698