| 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/proxy/plugin_var_tracker.h" | 7 #include "ppapi/proxy/plugin_var_tracker.h" |
| 7 #include "ppapi/proxy/ppapi_messages.h" | 8 #include "ppapi/proxy/ppapi_messages.h" |
| 8 #include "ppapi/proxy/ppapi_proxy_test.h" | 9 #include "ppapi/proxy/ppapi_proxy_test.h" |
| 10 #include "ppapi/proxy/proxy_object_var.h" |
| 9 | 11 |
| 10 namespace ppapi { | 12 namespace ppapi { |
| 11 namespace proxy { | 13 namespace proxy { |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 15 PP_Var MakeObject(int32 object_id) { | 17 PP_Var MakeObject(int32 object_id) { |
| 16 PP_Var ret; | 18 PP_Var ret; |
| 17 ret.type = PP_VARTYPE_OBJECT; | 19 ret.type = PP_VARTYPE_OBJECT; |
| 18 ret.value.as_id = object_id; | 20 ret.value.as_id = object_id; |
| 19 return ret; | 21 return ret; |
| 20 } | 22 } |
| 21 | 23 |
| 24 // A Deallocate() function for PPP_Class that just writes 1 to the given |
| 25 // pointer so we know when Deallocate was called. |
| 26 void MarkOnDeallocate(void* object) { |
| 27 *static_cast<int*>(object) = 1; |
| 28 } |
| 29 |
| 30 // A class that just implements MarkOnDeallocate on destruction. |
| 31 PPP_Class_Deprecated mark_on_deallocate_class = { |
| 32 NULL, // HasProperty, |
| 33 NULL, // HasMethod, |
| 34 NULL, // GetProperty, |
| 35 NULL, // GetAllPropertyNames, |
| 36 NULL, // SetProperty, |
| 37 NULL, // RemoveProperty, |
| 38 NULL, // Call, |
| 39 NULL, // Construct, |
| 40 &MarkOnDeallocate |
| 41 }; |
| 42 |
| 22 } // namespace | 43 } // namespace |
| 23 | 44 |
| 24 class PluginVarTrackerTest : public PluginProxyTest { | 45 class PluginVarTrackerTest : public PluginProxyTest { |
| 25 public: | 46 public: |
| 26 PluginVarTrackerTest() {} | 47 PluginVarTrackerTest() {} |
| 27 | 48 |
| 28 protected: | 49 protected: |
| 29 // Asserts that there is a unique "release object" IPC message in the test | 50 // Asserts that there is a unique "release object" IPC message in the test |
| 30 // sink. This will return the var ID from the message or -1 if none found. | 51 // sink. This will return the var ID from the message or -1 if none found. |
| 31 int32 GetObjectIDForUniqueReleaseObject() { | 52 int32 GetObjectIDForUniqueReleaseObject() { |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 174 |
| 154 // Now release those tracked items, the reference should be freed. | 175 // Now release those tracked items, the reference should be freed. |
| 155 var_tracker().StopTrackingObjectWithNoReference(plugin_var); | 176 var_tracker().StopTrackingObjectWithNoReference(plugin_var); |
| 156 EXPECT_EQ(1, | 177 EXPECT_EQ(1, |
| 157 var_tracker().GetTrackedWithNoReferenceCountForObject(plugin_var)); | 178 var_tracker().GetTrackedWithNoReferenceCountForObject(plugin_var)); |
| 158 var_tracker().StopTrackingObjectWithNoReference(plugin_var); | 179 var_tracker().StopTrackingObjectWithNoReference(plugin_var); |
| 159 EXPECT_EQ(-1, | 180 EXPECT_EQ(-1, |
| 160 var_tracker().GetTrackedWithNoReferenceCountForObject(plugin_var)); | 181 var_tracker().GetTrackedWithNoReferenceCountForObject(plugin_var)); |
| 161 } | 182 } |
| 162 | 183 |
| 184 // Tests that objects implemented by the plugin that have no references by |
| 185 // the plugin get their Deallocate function called on destruction. |
| 186 TEST_F(PluginVarTrackerTest, PluginObjectInstanceDeleted) { |
| 187 PP_Var host_object = MakeObject(12345); |
| 188 PP_Instance pp_instance = 0x12345; |
| 189 |
| 190 int deallocate_called = 0; |
| 191 void* user_data = &deallocate_called; |
| 192 |
| 193 // Make a var with one reference. |
| 194 scoped_refptr<ProxyObjectVar> object( |
| 195 new ProxyObjectVar(plugin_dispatcher(), host_object.value.as_id)); |
| 196 PP_Var plugin_var = MakeObject(var_tracker().AddVar(object)); |
| 197 var_tracker().PluginImplementedObjectCreated(pp_instance, |
| 198 plugin_var, |
| 199 &mark_on_deallocate_class, |
| 200 user_data); |
| 201 |
| 202 // Release the plugin ref to the var. WebKit hasn't called destroy so |
| 203 // we won't get a destroy call. |
| 204 object = NULL; |
| 205 var_tracker().ReleaseVar(plugin_var); |
| 206 EXPECT_FALSE(deallocate_called); |
| 207 |
| 208 // Synthesize an instance destuction, this should call Deallocate. |
| 209 var_tracker().DidDeleteInstance(pp_instance); |
| 210 EXPECT_TRUE(deallocate_called); |
| 211 } |
| 212 |
| 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 |
| 215 // the plugin releases its last ref. |
| 216 TEST_F(PluginVarTrackerTest, PluginObjectLeaked) { |
| 217 PP_Var host_object = MakeObject(12345); |
| 218 PP_Instance pp_instance = 0x12345; |
| 219 |
| 220 int deallocate_called = 0; |
| 221 void* user_data = &deallocate_called; |
| 222 |
| 223 // Make a var with one reference. |
| 224 scoped_refptr<ProxyObjectVar> object( |
| 225 new ProxyObjectVar(plugin_dispatcher(), host_object.value.as_id)); |
| 226 PP_Var plugin_var = MakeObject(var_tracker().AddVar(object)); |
| 227 var_tracker().PluginImplementedObjectCreated(pp_instance, |
| 228 plugin_var, |
| 229 &mark_on_deallocate_class, |
| 230 user_data); |
| 231 |
| 232 // Destroy the innstance. This should not call deallocate since the plugin |
| 233 // still has a ref. |
| 234 var_tracker().DidDeleteInstance(pp_instance); |
| 235 EXPECT_FALSE(deallocate_called); |
| 236 |
| 237 // Release the plugin ref to the var. Since the instance is gone this should |
| 238 // call deallocate. |
| 239 object = NULL; |
| 240 var_tracker().ReleaseVar(plugin_var); |
| 241 EXPECT_TRUE(deallocate_called); |
| 242 } |
| 243 |
| 163 } // namespace proxy | 244 } // namespace proxy |
| 164 } // namespace ppapi | 245 } // namespace ppapi |
| OLD | NEW |