Dispose of NPOject in npobject_identity_test.cc
[chromium-blink-merge.git] / webkit / plugins / npapi / test / plugin_npobject_identity_test.cc
blob1c6bbd22c477fd84d904698dfe3993bffe236701
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.
5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h"
8 #include "webkit/plugins/npapi/test/plugin_npobject_identity_test.h"
10 namespace {
12 class NPThingy : public NPObject {
13 public:
14 NPThingy() : NPObject() {}
16 static NPObject* Allocate(NPP npp, NPClass* npclass) {
17 return new NPThingy();
20 static void Deallocate(NPObject* npobject) {
21 delete static_cast<NPThingy*>(npobject);
25 NPClass* GetNPThingyClass() {
26 static NPClass plugin_class = {
27 NP_CLASS_STRUCT_VERSION,
28 NPThingy::Allocate,
29 NPThingy::Deallocate,
30 NULL, // Invalidate
31 NULL, // HasMethod
32 NULL, // Invoke
33 NULL, // InvokeDefault
34 NULL, // HasProperty
35 NULL, // GetProperty
36 NULL, // SetProperty
37 NULL, // RemoveProperty
39 return &plugin_class;
43 } // namespace
45 namespace NPAPIClient {
47 NPObjectIdentityTest::NPObjectIdentityTest(NPP id,
48 NPNetscapeFuncs *host_functions)
49 : PluginTest(id, host_functions) {
52 NPError NPObjectIdentityTest::SetWindow(NPWindow* pNPWindow) {
53 if (pNPWindow->window == NULL)
54 return NPERR_NO_ERROR;
56 NPIdentifier are_these_the_same_id = HostFunctions()->getstringidentifier(
57 "areTheseTheSame");
59 // Get a function from window.areTheseTheSame.
60 NPObject* window;
61 HostFunctions()->getvalue(id(), NPNVWindowNPObject, &window);
62 NPVariant func_var;
63 HostFunctions()->getproperty(id(), window, are_these_the_same_id, &func_var);
64 NPObject* func = NPVARIANT_TO_OBJECT(func_var);
66 // Create a custom NPObject and pass it in both arguments to areTheseTheSame.
67 NPObject* thingy = HostFunctions()->createobject(id(), GetNPThingyClass());
68 NPVariant func_args[2];
69 OBJECT_TO_NPVARIANT(thingy, func_args[0]);
70 OBJECT_TO_NPVARIANT(thingy, func_args[1]);
71 NPVariant were_the_same_var;
72 HostFunctions()->invokeDefault(id(), func, (const NPVariant*)&func_args, 2,
73 &were_the_same_var);
75 // Confirm that JavaScript could see that the objects were the same.
76 bool were_the_same = NPVARIANT_TO_BOOLEAN(were_the_same_var);
77 if (!were_the_same)
78 SetError("Identity was lost in passing from NPAPI into JavaScript.");
80 HostFunctions()->releaseobject(thingy);
82 // If this test failed, then we'd have crashed by now.
83 SignalTestCompleted();
85 return NPERR_NO_ERROR;
88 } // namespace NPAPIClient