| OLD | NEW |
| 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 "ipc/ipc_test_sink.h" | 5 #include "ipc/ipc_test_sink.h" |
| 6 #include "ppapi/c/dev/ppp_class_deprecated.h" | 6 #include "ppapi/c/dev/ppp_class_deprecated.h" |
| 7 #include "ppapi/proxy/plugin_var_tracker.h" | 7 #include "ppapi/proxy/plugin_var_tracker.h" |
| 8 #include "ppapi/proxy/ppapi_messages.h" | 8 #include "ppapi/proxy/ppapi_messages.h" |
| 9 #include "ppapi/proxy/ppapi_proxy_test.h" | 9 #include "ppapi/proxy/ppapi_proxy_test.h" |
| 10 #include "ppapi/proxy/proxy_object_var.h" | 10 #include "ppapi/proxy/proxy_object_var.h" |
| 11 | 11 |
| 12 namespace ppapi { | 12 namespace ppapi { |
| 13 namespace proxy { | 13 namespace proxy { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 PP_Var MakeObject(int32 object_id) { | 17 PP_Var MakeObject(int32 object_id) { |
| 18 PP_Var ret; | 18 PP_Var ret; |
| 19 ret.type = PP_VARTYPE_OBJECT; | 19 ret.type = PP_VARTYPE_OBJECT; |
| 20 ret.value.as_id = object_id; | 20 ret.value.as_id = object_id; |
| 21 return ret; | 21 return ret; |
| 22 } | 22 } |
| 23 | 23 |
| 24 // A Deallocate() function for PPP_Class that just writes 1 to the given | 24 // A Deallocate() function for PPP_Class that just increments the integer |
| 25 // pointer so we know when Deallocate was called. | 25 // referenced by the pointer so we know how often Deallocate was called. |
| 26 void MarkOnDeallocate(void* object) { | 26 void MarkOnDeallocate(void* object) { |
| 27 *static_cast<int*>(object) = 1; | 27 (*static_cast<int*>(object))++; |
| 28 } | 28 } |
| 29 | 29 |
| 30 // A class that just implements MarkOnDeallocate on destruction. | 30 // A class that just implements MarkOnDeallocate on destruction. |
| 31 PPP_Class_Deprecated mark_on_deallocate_class = { | 31 PPP_Class_Deprecated mark_on_deallocate_class = { |
| 32 NULL, // HasProperty, | 32 NULL, // HasProperty, |
| 33 NULL, // HasMethod, | 33 NULL, // HasMethod, |
| 34 NULL, // GetProperty, | 34 NULL, // GetProperty, |
| 35 NULL, // GetAllPropertyNames, | 35 NULL, // GetAllPropertyNames, |
| 36 NULL, // SetProperty, | 36 NULL, // SetProperty, |
| 37 NULL, // RemoveProperty, | 37 NULL, // RemoveProperty, |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 PP_Var plugin_var = MakeObject(var_tracker().AddVar(object)); | 196 PP_Var plugin_var = MakeObject(var_tracker().AddVar(object)); |
| 197 var_tracker().PluginImplementedObjectCreated(pp_instance, | 197 var_tracker().PluginImplementedObjectCreated(pp_instance, |
| 198 plugin_var, | 198 plugin_var, |
| 199 &mark_on_deallocate_class, | 199 &mark_on_deallocate_class, |
| 200 user_data); | 200 user_data); |
| 201 | 201 |
| 202 // Release the plugin ref to the var. WebKit hasn't called destroy so | 202 // Release the plugin ref to the var. WebKit hasn't called destroy so |
| 203 // we won't get a destroy call. | 203 // we won't get a destroy call. |
| 204 object = NULL; | 204 object = NULL; |
| 205 var_tracker().ReleaseVar(plugin_var); | 205 var_tracker().ReleaseVar(plugin_var); |
| 206 EXPECT_FALSE(deallocate_called); | 206 EXPECT_EQ(0, deallocate_called); |
| 207 | 207 |
| 208 // Synthesize an instance destuction, this should call Deallocate. | 208 // Synthesize an instance destuction, this should call Deallocate. |
| 209 var_tracker().DidDeleteInstance(pp_instance); | 209 var_tracker().DidDeleteInstance(pp_instance); |
| 210 EXPECT_TRUE(deallocate_called); | 210 EXPECT_EQ(1, deallocate_called); |
| 211 } | 211 } |
| 212 | 212 |
| 213 // Tests what happens when a plugin keeps a ref to a plugin-implemented | 213 // Tests what happens when a plugin keeps a ref to a plugin-implemented |
| 214 // object var longer than the instance. We should not call the destructor until | 214 // object var longer than the instance. We should not call the destructor until |
| 215 // the plugin releases its last ref. | 215 // the plugin releases its last ref. |
| 216 TEST_F(PluginVarTrackerTest, PluginObjectLeaked) { | 216 TEST_F(PluginVarTrackerTest, PluginObjectLeaked) { |
| 217 PP_Var host_object = MakeObject(12345); | 217 PP_Var host_object = MakeObject(12345); |
| 218 PP_Instance pp_instance = 0x12345; | 218 PP_Instance pp_instance = 0x12345; |
| 219 | 219 |
| 220 int deallocate_called = 0; | 220 int deallocate_called = 0; |
| 221 void* user_data = &deallocate_called; | 221 void* user_data = &deallocate_called; |
| 222 | 222 |
| 223 // Make a var with one reference. | 223 // Make a var with one reference. |
| 224 scoped_refptr<ProxyObjectVar> object( | 224 scoped_refptr<ProxyObjectVar> object( |
| 225 new ProxyObjectVar(plugin_dispatcher(), host_object.value.as_id)); | 225 new ProxyObjectVar(plugin_dispatcher(), host_object.value.as_id)); |
| 226 PP_Var plugin_var = MakeObject(var_tracker().AddVar(object)); | 226 PP_Var plugin_var = MakeObject(var_tracker().AddVar(object)); |
| 227 var_tracker().PluginImplementedObjectCreated(pp_instance, | 227 var_tracker().PluginImplementedObjectCreated(pp_instance, |
| 228 plugin_var, | 228 plugin_var, |
| 229 &mark_on_deallocate_class, | 229 &mark_on_deallocate_class, |
| 230 user_data); | 230 user_data); |
| 231 | 231 |
| 232 // Destroy the innstance. This should not call deallocate since the plugin | 232 // Destroy the instance. This should not call deallocate since the plugin |
| 233 // still has a ref. | 233 // still has a ref. |
| 234 var_tracker().DidDeleteInstance(pp_instance); | 234 var_tracker().DidDeleteInstance(pp_instance); |
| 235 EXPECT_FALSE(deallocate_called); | 235 EXPECT_EQ(0, deallocate_called); |
| 236 | 236 |
| 237 // Release the plugin ref to the var. Since the instance is gone this should | 237 // Release the plugin ref to the var. Since the instance is gone this should |
| 238 // call deallocate. | 238 // call deallocate. |
| 239 object = NULL; | 239 object = NULL; |
| 240 var_tracker().ReleaseVar(plugin_var); | 240 var_tracker().ReleaseVar(plugin_var); |
| 241 EXPECT_TRUE(deallocate_called); | 241 EXPECT_EQ(1, deallocate_called); |
| 242 } | 242 } |
| 243 | 243 |
| 244 } // namespace proxy | 244 } // namespace proxy |
| 245 } // namespace ppapi | 245 } // namespace ppapi |
| OLD | NEW |