OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/plugins/npapi/test/plugin_delete_plugin_in_deallocate_test.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 class DeletePluginInDeallocateTestNPObject : public NPObject { |
| 13 public: |
| 14 DeletePluginInDeallocateTestNPObject() |
| 15 : NPObject(), id_(NULL), host_functions_(NULL), deallocate_count_(0) {} |
| 16 |
| 17 static NPObject* Allocate(NPP npp, NPClass* npclass) { |
| 18 return new DeletePluginInDeallocateTestNPObject(); |
| 19 } |
| 20 |
| 21 static void Deallocate(NPObject* npobject) { |
| 22 DeletePluginInDeallocateTestNPObject* object = |
| 23 reinterpret_cast<DeletePluginInDeallocateTestNPObject*>(npobject); |
| 24 ++object->deallocate_count_; |
| 25 |
| 26 // Call window.deletePlugin to tear-down our plugin from inside deallocate. |
| 27 if (object->deallocate_count_ == 1) { |
| 28 NPIdentifier delete_id = |
| 29 object->host_functions_->getstringidentifier("deletePlugin"); |
| 30 NPObject* window_obj = NULL; |
| 31 object->host_functions_->getvalue(object->id_, NPNVWindowNPObject, |
| 32 &window_obj); |
| 33 NPVariant rv; |
| 34 object->host_functions_->invoke(object->id_, window_obj, delete_id, NULL, |
| 35 0, &rv); |
| 36 } |
| 37 } |
| 38 |
| 39 NPP id_; |
| 40 NPNetscapeFuncs* host_functions_; |
| 41 int deallocate_count_; |
| 42 }; |
| 43 |
| 44 NPClass* GetDeletePluginInDeallocateTestClass() { |
| 45 static NPClass plugin_class = { |
| 46 NP_CLASS_STRUCT_VERSION, |
| 47 DeletePluginInDeallocateTestNPObject::Allocate, |
| 48 DeletePluginInDeallocateTestNPObject::Deallocate, |
| 49 NULL, // Invalidate |
| 50 NULL, // HasMethod |
| 51 NULL, // Invoke |
| 52 NULL, // InvokeDefault |
| 53 NULL, // HasProperty |
| 54 NULL, // GetProperty |
| 55 NULL, // SetProperty |
| 56 NULL, // RemoveProperty |
| 57 }; |
| 58 return &plugin_class; |
| 59 } |
| 60 |
| 61 } // namespace |
| 62 |
| 63 namespace NPAPIClient { |
| 64 |
| 65 DeletePluginInDeallocateTest::DeletePluginInDeallocateTest( |
| 66 NPP id, NPNetscapeFuncs *host_functions) |
| 67 : PluginTest(id, host_functions), npobject_(NULL) { |
| 68 } |
| 69 |
| 70 NPError DeletePluginInDeallocateTest::SetWindow(NPWindow* pNPWindow) { |
| 71 if (pNPWindow->window == NULL) |
| 72 return NPERR_NO_ERROR; |
| 73 |
| 74 NPObject* window = NULL; |
| 75 HostFunctions()->getvalue(id(), NPNVWindowNPObject, &window); |
| 76 |
| 77 // Create a custom NPObject and give our Id and the Netscape function table. |
| 78 npobject_ = HostFunctions()->createobject( |
| 79 id(), GetDeletePluginInDeallocateTestClass()); |
| 80 DeletePluginInDeallocateTestNPObject* test_object = |
| 81 reinterpret_cast<DeletePluginInDeallocateTestNPObject*>(npobject_); |
| 82 test_object->id_ = id(); |
| 83 test_object->host_functions_ = HostFunctions(); |
| 84 |
| 85 // Pass it to the window.setTestObject function, which will later release it. |
| 86 NPIdentifier set_test_object_id = |
| 87 HostFunctions()->getstringidentifier("setTestObject"); |
| 88 NPVariant func_var; |
| 89 HostFunctions()->getproperty(id(), window, set_test_object_id, &func_var); |
| 90 NPObject* func = NPVARIANT_TO_OBJECT(func_var); |
| 91 NPVariant func_arg; |
| 92 OBJECT_TO_NPVARIANT(npobject_, func_arg); |
| 93 NPVariant func_result; |
| 94 HostFunctions()->invokeDefault(id(), func, &func_arg, 1, |
| 95 &func_result); |
| 96 |
| 97 // Release the object - the page's reference should keep it alive. |
| 98 HostFunctions()->releaseobject(npobject_); |
| 99 |
| 100 return NPERR_NO_ERROR; |
| 101 } |
| 102 |
| 103 NPError DeletePluginInDeallocateTest::Destroy() { |
| 104 if (!npobject_) { |
| 105 SetError("SetWindow was not invoked."); |
| 106 } else { |
| 107 // Verify that our object was deallocated exactly once. |
| 108 DeletePluginInDeallocateTestNPObject* test_object = |
| 109 reinterpret_cast<DeletePluginInDeallocateTestNPObject*>(npobject_); |
| 110 if (test_object->deallocate_count_ != 1) |
| 111 SetError("Object was not deallocated exactly once."); |
| 112 delete test_object; |
| 113 } |
| 114 |
| 115 SignalTestCompleted(); |
| 116 |
| 117 return NPERR_NO_ERROR; |
| 118 } |
| 119 |
| 120 } // namespace NPAPIClient |
OLD | NEW |