diff options
author | apatrick@google.com <apatrick@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-02 00:55:39 +0000 |
---|---|---|
committer | apatrick@google.com <apatrick@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-02 00:55:39 +0000 |
commit | 261e60d51c33dfa14a225203bb7cfe0dc0ec88a2 (patch) | |
tree | 059a0de9dda5abaa70a74e79e1a7c5542e14c48b /o3d | |
parent | 2408380ce8f8b90dd577a0403958bb292aec0d68 (diff) | |
download | chromium_src-261e60d51c33dfa14a225203bb7cfe0dc0ec88a2.zip chromium_src-261e60d51c33dfa14a225203bb7cfe0dc0ec88a2.tar.gz chromium_src-261e60d51c33dfa14a225203bb7cfe0dc0ec88a2.tar.bz2 |
GPU plugin object can create a command buffer via a V8 extension in Chromium and return it via NPAPI.
Introduced NPBrowser class to abstract functions provided by browser to plugin, including Chromium extensions. NPBrowser is stubbed for tests.
TEST=none
BUG=none
Review URL: http://codereview.chromium.org/177060
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25135 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d')
35 files changed, 1707 insertions, 962 deletions
diff --git a/o3d/gpu_plugin/gpu_plugin.cc b/o3d/gpu_plugin/gpu_plugin.cc index ff21b8b..7e487b7 100644 --- a/o3d/gpu_plugin/gpu_plugin.cc +++ b/o3d/gpu_plugin/gpu_plugin.cc @@ -3,9 +3,10 @@ // found in the LICENSE file. #include "o3d/gpu_plugin/gpu_plugin.h" -#include "o3d/gpu_plugin/plugin_object.h" -#include "o3d/gpu_plugin/plugin_object_factory.h" -#include "o3d/gpu_plugin/np_utils/npn_funcs.h" +#include "o3d/gpu_plugin/gpu_plugin_object_factory.h" +#include "o3d/gpu_plugin/np_utils/np_browser.h" +#include "o3d/gpu_plugin/np_utils/np_plugin_object.h" +#include "o3d/gpu_plugin/np_utils/np_plugin_object_factory.h" #include "webkit/glue/plugins/nphostapi.h" namespace o3d { @@ -14,8 +15,8 @@ namespace gpu_plugin { // Definitions of NPAPI plugin entry points. namespace { -bool g_initialized; -PluginObjectFactory* g_plugin_object_factory = new PluginObjectFactory; +NPBrowser* g_browser; +GPUPluginObjectFactory g_plugin_object_factory; NPError NPP_New(NPMIMEType plugin_type, NPP instance, uint16 mode, int16 argc, char* argn[], @@ -23,14 +24,19 @@ NPError NPP_New(NPMIMEType plugin_type, NPP instance, if (!instance) return NPERR_INVALID_INSTANCE_ERROR; - PluginObject* plugin_object = g_plugin_object_factory->CreatePluginObject( - instance, plugin_type); + PluginObject* plugin_object = + NPPluginObjectFactory::get()->CreatePluginObject(instance, plugin_type); if (!plugin_object) return NPERR_GENERIC_ERROR; instance->pdata = plugin_object; - return plugin_object->New(plugin_type, argc, argn, argv, saved); + NPError error = plugin_object->New(plugin_type, argc, argn, argv, saved); + if (error != NPERR_NO_ERROR) { + plugin_object->Release(); + } + + return error; } NPError NPP_Destroy(NPP instance, NPSavedData** saved) { @@ -38,7 +44,13 @@ NPError NPP_Destroy(NPP instance, NPSavedData** saved) { return NPERR_INVALID_INSTANCE_ERROR; PluginObject* plugin_object = static_cast<PluginObject*>(instance->pdata); - return plugin_object->Destroy(saved); + NPError error = plugin_object->Destroy(saved); + + if (error == NPERR_NO_ERROR) { + plugin_object->Release(); + } + + return error; } NPError NPP_SetWindow(NPP instance, NPWindow* window) { @@ -63,9 +75,9 @@ NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) { PluginObject* plugin_object = static_cast<PluginObject*>(instance->pdata); switch (variable) { - case NPPVpluginScriptableInstance: + case NPPVpluginScriptableNPObject: *reinterpret_cast<NPObject**>(value) = - plugin_object->GetScriptableInstance(); + plugin_object->GetScriptableNPObject(); return NPERR_NO_ERROR; default: return NPERR_GENERIC_ERROR; @@ -77,12 +89,6 @@ NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value) { } } -PluginObjectFactory* SetPluginObjectFactory(PluginObjectFactory* factory) { - PluginObjectFactory* previous_factory = g_plugin_object_factory; - g_plugin_object_factory = factory; - return previous_factory; -} - NPError API_CALL NP_GetEntryPoints(NPPluginFuncs* funcs) { funcs->newp = NPP_New; funcs->destroy = NPP_Destroy; @@ -102,28 +108,26 @@ NPError API_CALL NP_Initialize(NPNetscapeFuncs *browser_funcs) { if (!browser_funcs) return NPERR_INVALID_FUNCTABLE_ERROR; - if (g_initialized) + if (g_browser) return NPERR_GENERIC_ERROR; #if defined(OS_LINUX) NP_GetEntryPoints(plugin_funcs); #endif - SetBrowserFuncs(browser_funcs); - g_initialized = true; + g_browser = new NPBrowser(browser_funcs); return NPERR_NO_ERROR; } NPError API_CALL NP_Shutdown() { - if (!g_initialized) + if (!g_browser) return NPERR_GENERIC_ERROR; - SetBrowserFuncs(NULL); - g_initialized = false; + delete g_browser; + g_browser = NULL; return NPERR_NO_ERROR; } - } // namespace gpu_plugin } // namespace o3d diff --git a/o3d/gpu_plugin/gpu_plugin.gyp b/o3d/gpu_plugin/gpu_plugin.gyp index 6ee016e..1c442ac8 100644 --- a/o3d/gpu_plugin/gpu_plugin.gyp +++ b/o3d/gpu_plugin/gpu_plugin.gyp @@ -23,20 +23,22 @@ 'gpu_plugin_object.cc', 'gpu_plugin_object.h', 'gpu_plugin_object_win.cc', - 'plugin_object_factory.cc', - 'plugin_object.h', - 'plugin_object_factory.h', + 'gpu_plugin_object_factory.cc', + 'gpu_plugin_object_factory.h', 'np_utils/base_np_object.cc', 'np_utils/base_np_object.h', 'np_utils/dispatched_np_object.cc', 'np_utils/dispatched_np_object.h', + 'np_utils/np_browser.cc', + 'np_utils/np_browser.h', 'np_utils/np_dispatcher.h', 'np_utils/np_dispatcher_specializations.h', 'np_utils/np_object_pointer.h', - 'np_utils/np_variant_utils.cc', - 'np_utils/np_variant_utils.h', - 'np_utils/npn_funcs.cc', - 'np_utils/npn_funcs.h', + 'np_utils/np_plugin_object.h', + 'np_utils/np_plugin_object_factory.cc', + 'np_utils/np_plugin_object_factory.h', + 'np_utils/np_utils.cc', + 'np_utils/np_utils.h', ], }, @@ -58,17 +60,17 @@ 'sources': [ 'gpu_plugin_unittest.cc', 'gpu_plugin_object_unittest.cc', - 'plugin_object_factory_mock.h', - 'plugin_object_factory_unittest.cc', - 'plugin_object_mock.h', + 'gpu_plugin_object_factory_unittest.cc', 'np_utils/base_np_object_mock.cc', 'np_utils/base_np_object_mock.h', 'np_utils/base_np_object_unittest.cc', + 'np_utils/np_browser_stub.h', + 'np_utils/np_browser_stub.cc', 'np_utils/dispatched_np_object_unittest.cc', 'np_utils/np_object_pointer_unittest.cc', - 'np_utils/np_variant_utils_unittest.cc', - 'np_utils/npn_test_stub.h', - 'np_utils/npn_test_stub.cc', + 'np_utils/np_plugin_object_factory_mock.h', + 'np_utils/np_plugin_object_mock.h', + 'np_utils/np_utils_unittest.cc', ], }, ] diff --git a/o3d/gpu_plugin/gpu_plugin.h b/o3d/gpu_plugin/gpu_plugin.h index 269e053..748a69e 100644 --- a/o3d/gpu_plugin/gpu_plugin.h +++ b/o3d/gpu_plugin/gpu_plugin.h @@ -14,11 +14,6 @@ typedef struct _NPNetscapeFuncs NPNetscapeFuncs; namespace o3d { namespace gpu_plugin { -class PluginObjectFactory; - -// Replaces the plugin object factory and returns the previous one. -PluginObjectFactory* SetPluginObjectFactory(PluginObjectFactory* factory); - // Declarations of NPAPI plugin entry points. NPError API_CALL NP_GetEntryPoints(NPPluginFuncs* funcs); diff --git a/o3d/gpu_plugin/gpu_plugin_object.cc b/o3d/gpu_plugin/gpu_plugin_object.cc index e6b7647..8191e0a 100644 --- a/o3d/gpu_plugin/gpu_plugin_object.cc +++ b/o3d/gpu_plugin/gpu_plugin_object.cc @@ -4,6 +4,7 @@ #include <stdlib.h> +#include "base/logging.h" #include "o3d/gpu_plugin/gpu_plugin_object.h" namespace o3d { @@ -12,15 +13,23 @@ namespace gpu_plugin { const NPUTF8 GPUPluginObject::kPluginType[] = "application/vnd.google.chrome.gpu-plugin"; +GPUPluginObject::GPUPluginObject(NPP npp) + : DispatchedNPObject(npp), + status_(CREATED), + shared_memory_(NULL) { + memset(&window_, 0, sizeof(window_)); +} + NPError GPUPluginObject::New(NPMIMEType plugin_type, - int16 argc, - char* argn[], - char* argv[], - NPSavedData* saved) { + int16 argc, + char* argn[], + char* argv[], + NPSavedData* saved) { if (status_ != CREATED) return NPERR_GENERIC_ERROR; status_ = INITIALIZED; + return NPERR_NO_ERROR; } @@ -46,12 +55,59 @@ NPError GPUPluginObject::Destroy(NPSavedData** saved) { if (status_ != INITIALIZED) return NPERR_GENERIC_ERROR; + if (shared_memory_) { + NPBrowser::get()->UnmapSharedMemory(npp(), shared_memory_); + } + + command_buffer_object_ = NPObjectPointer<NPObject>(); + status_ = DESTROYED; + return NPERR_NO_ERROR; } -NPObject* GPUPluginObject::GetScriptableInstance() { - return NULL; +void GPUPluginObject::Release() { + DCHECK(status_ != INITIALIZED); + NPBrowser::get()->ReleaseObject(this); +} + +NPObject*GPUPluginObject::GetScriptableNPObject() { + NPBrowser::get()->RetainObject(this); + return this; +} + +NPObjectPointer<NPObject> GPUPluginObject::OpenCommandBuffer() { + if (command_buffer_object_.Get()) + return command_buffer_object_; + + NPObjectPointer<NPObject> window = NPObjectPointer<NPObject>::FromReturned( + NPBrowser::get()->GetWindowNPObject(npp())); + if (!window.Get()) + return NPObjectPointer<NPObject>(); + + NPObjectPointer<NPObject> chromium; + if (!NPGetProperty(npp(), window, "chromium", &chromium)) { + return NPObjectPointer<NPObject>(); + } + + NPObjectPointer<NPObject> system; + if (!NPGetProperty(npp(), chromium, "system", &system)) { + return NPObjectPointer<NPObject>(); + } + + if (!NPInvoke(npp(), system, "createSharedMemory", 1024, + &command_buffer_object_)) { + return NPObjectPointer<NPObject>(); + } + + shared_memory_ = NPBrowser::get()->MapSharedMemory( + npp(), command_buffer_object_.Get(), 1024, false); + if (!shared_memory_) { + command_buffer_object_ = NPObjectPointer<NPObject>(); + return NPObjectPointer<NPObject>(); + } + + return command_buffer_object_; } } // namespace gpu_plugin diff --git a/o3d/gpu_plugin/gpu_plugin_object.h b/o3d/gpu_plugin/gpu_plugin_object.h index d3c6576..d939982 100644 --- a/o3d/gpu_plugin/gpu_plugin_object.h +++ b/o3d/gpu_plugin/gpu_plugin_object.h @@ -5,11 +5,17 @@ #ifndef O3D_GPU_PLUGIN_GPU_PLUGIN_OBJECT_H_ #define O3D_GPU_PLUGIN_GPU_PLUGIN_OBJECT_H_ +#include <string> + #include "o3d/gpu_plugin/np_utils/dispatched_np_object.h" -#include "o3d/gpu_plugin/plugin_object.h" +#include "o3d/gpu_plugin/np_utils/np_dispatcher.h" +#include "o3d/gpu_plugin/np_utils/np_plugin_object.h" +#include "o3d/gpu_plugin/np_utils/np_utils.h" #include "third_party/npapi/bindings/npapi.h" #include "third_party/npapi/bindings/npruntime.h" +struct NaClDesc; + namespace o3d { namespace gpu_plugin { @@ -18,10 +24,7 @@ class GPUPluginObject : public DispatchedNPObject, public PluginObject { public: static const NPUTF8 kPluginType[]; - explicit GPUPluginObject(NPP npp) - : DispatchedNPObject(npp), - status_(CREATED) { - } + explicit GPUPluginObject(NPP npp); virtual NPError New(NPMIMEType plugin_type, int16 argc, @@ -36,7 +39,16 @@ class GPUPluginObject : public DispatchedNPObject, public PluginObject { virtual NPError Destroy(NPSavedData** saved); - virtual NPObject* GetScriptableInstance(); + virtual void Release(); + + virtual NPObject* GetScriptableNPObject(); + + NPObjectPointer<NPObject> OpenCommandBuffer(); + + protected: + NP_UTILS_BEGIN_DISPATCHER_CHAIN(GPUPluginObject, DispatchedNPObject) + NP_UTILS_DISPATCHER(OpenCommandBuffer, NPObjectPointer<NPObject>()) + NP_UTILS_END_DISPATCHER_CHAIN private: NPError PlatformSpecificSetWindow(NPWindow* new_window); @@ -49,6 +61,8 @@ class GPUPluginObject : public DispatchedNPObject, public PluginObject { Status status_; NPWindow window_; + NPObjectPointer<NPObject> command_buffer_object_; + NPSharedMemory* shared_memory_; }; } // namespace gpu_plugin diff --git a/o3d/gpu_plugin/gpu_plugin_object_factory.cc b/o3d/gpu_plugin/gpu_plugin_object_factory.cc new file mode 100644 index 0000000..7a2e8d4 --- /dev/null +++ b/o3d/gpu_plugin/gpu_plugin_object_factory.cc @@ -0,0 +1,28 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "o3d/gpu_plugin/gpu_plugin_object.h" +#include "o3d/gpu_plugin/gpu_plugin_object_factory.h" + +namespace o3d { +namespace gpu_plugin { + +GPUPluginObjectFactory::GPUPluginObjectFactory() { +} + +GPUPluginObjectFactory::~GPUPluginObjectFactory() { +} + +PluginObject* GPUPluginObjectFactory::CreatePluginObject( + NPP npp, + NPMIMEType plugin_type) { + if (strcmp(plugin_type, GPUPluginObject::kPluginType) == 0) { + return NPCreateObject<GPUPluginObject>(npp).ToReturned(); + } + + return NULL; +} + +} // namespace gpu_plugin +} // namespace o3d diff --git a/o3d/gpu_plugin/plugin_object_factory_unittest.cc b/o3d/gpu_plugin/gpu_plugin_object_factory_unittest.cc index a5fc3d2..a3cd3bd 100644 --- a/o3d/gpu_plugin/plugin_object_factory_unittest.cc +++ b/o3d/gpu_plugin/gpu_plugin_object_factory_unittest.cc @@ -3,8 +3,8 @@ // found in the LICENSE file. #include "o3d/gpu_plugin/gpu_plugin_object.h" -#include "o3d/gpu_plugin/plugin_object_factory.h" -#include "o3d/gpu_plugin/np_utils/npn_test_stub.h" +#include "o3d/gpu_plugin/gpu_plugin_object_factory.h" +#include "o3d/gpu_plugin/np_utils/np_browser_stub.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -14,16 +14,15 @@ namespace gpu_plugin { class PluginObjectFactoryTest : public testing::Test { protected: virtual void SetUp() { - InitializeNPNTestStub(); - factory_ = new PluginObjectFactory; + factory_ = new GPUPluginObjectFactory; } virtual void TearDown() { delete factory_; - ShutdownNPNTestStub(); } - PluginObjectFactory* factory_; + StubNPBrowser stub_browser_; + GPUPluginObjectFactory* factory_; }; TEST_F(PluginObjectFactoryTest, ReturnsNullForUnknownMimeType) { diff --git a/o3d/gpu_plugin/gpu_plugin_object_unittest.cc b/o3d/gpu_plugin/gpu_plugin_object_unittest.cc index 2c4024f..3cac993 100644 --- a/o3d/gpu_plugin/gpu_plugin_object_unittest.cc +++ b/o3d/gpu_plugin/gpu_plugin_object_unittest.cc @@ -3,33 +3,30 @@ // found in the LICENSE file. #include "o3d/gpu_plugin/gpu_plugin_object.h" +#include "o3d/gpu_plugin/np_utils/base_np_object_mock.h" +#include "o3d/gpu_plugin/np_utils/np_browser_mock.h" #include "o3d/gpu_plugin/np_utils/np_object_pointer.h" -#include "o3d/gpu_plugin/np_utils/npn_test_stub.h" #include "testing/gtest/include/gtest/gtest.h" +#include "testing/gmock/include/gmock/gmock.h" #include "webkit/glue/plugins/nphostapi.h" +using testing::_; +using testing::DoAll; +using testing::Return; +using testing::SetArgumentPointee; +using testing::StrictMock; + namespace o3d { namespace gpu_plugin { class GPUPluginObjectTest : public testing::Test { protected: virtual void SetUp() { - InitializeNPNTestStub(); - - np_class_ = const_cast<NPClass*>( - BaseNPObject::GetNPClass<GPUPluginObject>()); - - plugin_object_ = static_cast<GPUPluginObject*>( - gpu_plugin::NPN_CreateObject(NULL, np_class_)); - } - - virtual void TearDown() { - gpu_plugin::NPN_ReleaseObject(plugin_object_); - ShutdownNPNTestStub(); + plugin_object_ = NPCreateObject<GPUPluginObject>(NULL); } - NPClass* np_class_; - GPUPluginObject* plugin_object_; + MockNPBrowser mock_browser_; + NPObjectPointer<GPUPluginObject> plugin_object_; }; TEST_F(GPUPluginObjectTest, CanInitializeAndDestroyPluginObject) { @@ -56,6 +53,7 @@ TEST_F(GPUPluginObjectTest, NewFailsIfAlreadyInitialized) { NULL, NULL, NULL)); + EXPECT_EQ(NPERR_NO_ERROR, plugin_object_->Destroy(NULL)); } TEST_F(GPUPluginObjectTest, NewFailsIfObjectHasPreviouslyBeenDestroyed) { @@ -72,6 +70,11 @@ TEST_F(GPUPluginObjectTest, NewFailsIfObjectHasPreviouslyBeenDestroyed) { NULL)); } +TEST_F(GPUPluginObjectTest, WindowIsNullBeforeSetWindowCalled) { + NPWindow window = plugin_object_->GetWindow(); + EXPECT_EQ(NULL, window.window); +} + TEST_F(GPUPluginObjectTest, CanSetWindow) { EXPECT_EQ(NPERR_NO_ERROR, plugin_object_->New("application/foo", 0, @@ -94,5 +97,8 @@ TEST_F(GPUPluginObjectTest, SetWindowFailsIfNotInitialized) { EXPECT_EQ(NPERR_GENERIC_ERROR, plugin_object_->SetWindow(&window)); } +TEST_F(GPUPluginObjectTest, CanGetScriptableNPObject) { + EXPECT_EQ(plugin_object_.Get(), plugin_object_->GetScriptableNPObject()); +} } // namespace gpu_plugin } // namespace o3d diff --git a/o3d/gpu_plugin/gpu_plugin_unittest.cc b/o3d/gpu_plugin/gpu_plugin_unittest.cc index ab462c4..028f1c2 100644 --- a/o3d/gpu_plugin/gpu_plugin_unittest.cc +++ b/o3d/gpu_plugin/gpu_plugin_unittest.cc @@ -4,8 +4,9 @@ #include "o3d/gpu_plugin/gpu_plugin.h" #include "o3d/gpu_plugin/gpu_plugin_object.h" -#include "o3d/gpu_plugin/plugin_object_factory_mock.h" -#include "o3d/gpu_plugin/plugin_object_mock.h" +#include "o3d/gpu_plugin/np_utils/base_np_object_mock.h" +#include "o3d/gpu_plugin/np_utils/np_plugin_object_factory_mock.h" +#include "o3d/gpu_plugin/np_utils/np_plugin_object_mock.h" #include "testing/gtest/include/gtest/gtest.h" #include "webkit/glue/plugins/nphostapi.h" @@ -31,13 +32,13 @@ class GPUPluginTest : public testing::Test { memset(&npp_, 0, sizeof(npp_)); memset(&browser_funcs_, 0, sizeof(browser_funcs_)); memset(&plugin_funcs_, 0, sizeof(plugin_funcs_)); + plugin_object_factory_ = new StrictMock<MockPluginObjectFactory>; - previous_plugin_object_factory_ = SetPluginObjectFactory( - plugin_object_factory_); + + np_class_ = BaseNPObject::GetNPClass<StrictMock<MockBaseNPObject> >(); } virtual void TearDown() { - SetPluginObjectFactory(previous_plugin_object_factory_); delete plugin_object_factory_; } @@ -45,7 +46,7 @@ class GPUPluginTest : public testing::Test { NPNetscapeFuncs browser_funcs_; NPPluginFuncs plugin_funcs_; MockPluginObjectFactory* plugin_object_factory_; - PluginObjectFactory* previous_plugin_object_factory_; + const NPClass* np_class_; }; TEST_F(GPUPluginTest, GetEntryPointsSetsNeededFunctionPointers) { @@ -144,19 +145,21 @@ TEST_F(GPUPluginTest, NewCreatesAPluginObjectAndInitializesIt) { &npp_, const_cast<NPMIMEType>(GPUPluginObject::kPluginType))) .WillOnce(Return(&plugin_object)); - NPObject scriptable_instance; + NPObject scriptable_object; EXPECT_CALL(plugin_object, New( const_cast<NPMIMEType>(GPUPluginObject::kPluginType), 0, NULL, NULL, NULL)) .WillOnce(Return(NPERR_NO_ERROR)); - EXPECT_CALL(plugin_object, GetScriptableInstance()) - .WillOnce(Return(&scriptable_instance)); + EXPECT_CALL(plugin_object, GetScriptableNPObject()) + .WillOnce(Return(&scriptable_object)); EXPECT_CALL(plugin_object, Destroy(static_cast<NPSavedData**>(NULL))) .WillOnce(Return(NPERR_NO_ERROR)); + EXPECT_CALL(plugin_object, Release()); + NP_GetEntryPoints(&plugin_funcs_); NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS); @@ -164,10 +167,10 @@ TEST_F(GPUPluginTest, NewCreatesAPluginObjectAndInitializesIt) { const_cast<NPMIMEType>(GPUPluginObject::kPluginType), &npp_, 0, 0, NULL, NULL, NULL)); - NPObject* result = NULL; + NPObject* result; EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.getvalue( - &npp_, NPPVpluginScriptableInstance, &result)); - EXPECT_EQ(&scriptable_instance, result); + &npp_, NPPVpluginScriptableNPObject, &result)); + EXPECT_EQ(&scriptable_object, result); EXPECT_EQ(NPERR_NO_ERROR, plugin_funcs_.destroy(&npp_, NULL)); @@ -209,6 +212,8 @@ TEST_F(GPUPluginTest, SetWindowForwardsToPluginObject) { EXPECT_CALL(plugin_object, Destroy(static_cast<NPSavedData**>(NULL))) .WillOnce(Return(NPERR_NO_ERROR)); + EXPECT_CALL(plugin_object, Release()); + NP_GetEntryPoints(&plugin_funcs_); NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS); @@ -243,6 +248,8 @@ TEST_F(GPUPluginTest, HandleEventForwardsToPluginObject) { EXPECT_CALL(plugin_object, Destroy(static_cast<NPSavedData**>(NULL))) .WillOnce(Return(NPERR_NO_ERROR)); + EXPECT_CALL(plugin_object, Release()); + NP_GetEntryPoints(&plugin_funcs_); NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS); @@ -272,6 +279,8 @@ TEST_F(GPUPluginTest, GetValueReturnsErrorForUnknownVariable) { EXPECT_CALL(plugin_object, Destroy(static_cast<NPSavedData**>(NULL))) .WillOnce(Return(NPERR_NO_ERROR)); + EXPECT_CALL(plugin_object, Release()); + NP_GetEntryPoints(&plugin_funcs_); NP_Initialize(&browser_funcs_ INITIALIZE_PLUGIN_FUNCS); diff --git a/o3d/gpu_plugin/np_utils/base_np_object.h b/o3d/gpu_plugin/np_utils/base_np_object.h index 6944d72..b7815b8 100644 --- a/o3d/gpu_plugin/np_utils/base_np_object.h +++ b/o3d/gpu_plugin/np_utils/base_np_object.h @@ -16,7 +16,7 @@ namespace gpu_plugin { class BaseNPObject : public NPObject { public: // Returns the NPClass for the concrete BaseNPObject subclass T. - template <typename T> + template <typename NPObjectType> static const NPClass* GetNPClass(); NPP npp() const { @@ -60,9 +60,9 @@ class BaseNPObject : public NPObject { private: // This template version of the NPClass allocate function creates a subclass // of BaseNPObject. - template <typename T> + template <typename NPObjectType> static NPObject* AllocateImpl(NPP npp, NPClass*) { - return new T(npp); + return new NPObjectType(npp); } // These implementations of the NPClass functions forward to the virtual @@ -110,11 +110,11 @@ class BaseNPObject : public NPObject { DISALLOW_COPY_AND_ASSIGN(BaseNPObject); }; -template <typename T> +template <typename NPObjectType> const NPClass* BaseNPObject::GetNPClass() { static NPClass np_class = { NP_CLASS_STRUCT_VERSION, - AllocateImpl<T>, + AllocateImpl<NPObjectType>, DeallocateImpl, InvalidateImpl, HasMethodImpl, @@ -129,7 +129,6 @@ const NPClass* BaseNPObject::GetNPClass() { }; return &np_class; }; - } // namespace gpu_plugin } // namespace o3d diff --git a/o3d/gpu_plugin/np_utils/base_np_object_mock.h b/o3d/gpu_plugin/np_utils/base_np_object_mock.h index 7b4ae1f..80ccb4c 100644 --- a/o3d/gpu_plugin/np_utils/base_np_object_mock.h +++ b/o3d/gpu_plugin/np_utils/base_np_object_mock.h @@ -13,6 +13,14 @@ namespace gpu_plugin { class MockBaseNPObject : public BaseNPObject { public: + explicit MockBaseNPObject(NPP npp) : BaseNPObject(npp) { + ++count_; + } + + ~MockBaseNPObject() { + --count_; + } + static int count() { return count_; } @@ -29,15 +37,6 @@ class MockBaseNPObject : public BaseNPObject { MOCK_METHOD2(Enumerate, bool(NPIdentifier**, uint32_t*)); MOCK_METHOD3(Construct, bool(const NPVariant*, uint32_t, NPVariant*)); - protected: - explicit MockBaseNPObject(NPP npp) : BaseNPObject(npp) { - ++count_; - } - - ~MockBaseNPObject() { - --count_; - } - private: static int count_; }; diff --git a/o3d/gpu_plugin/np_utils/base_np_object_unittest.cc b/o3d/gpu_plugin/np_utils/base_np_object_unittest.cc index 26a5c61..b7e1e9f 100644 --- a/o3d/gpu_plugin/np_utils/base_np_object_unittest.cc +++ b/o3d/gpu_plugin/np_utils/base_np_object_unittest.cc @@ -15,8 +15,7 @@ namespace gpu_plugin { class BaseNPObjectTest : public testing::Test { protected: virtual void SetUp() { - np_class = const_cast<NPClass*>( - BaseNPObject::GetNPClass<StrictMock<MockBaseNPObject> >()); + np_class = BaseNPObject::GetNPClass<StrictMock<MockBaseNPObject> >(); // Dummy identifier is never used with real NPAPI so it can point to // anything. @@ -32,7 +31,7 @@ class BaseNPObjectTest : public testing::Test { } NPP_t npp_; - NPClass* np_class; + const NPClass* np_class; NPIdentifier identifier; NPVariant args[3]; NPVariant result; @@ -43,7 +42,7 @@ TEST_F(BaseNPObjectTest, AllocateAndDeallocateObject) { EXPECT_EQ(0, MockBaseNPObject::count()); MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); EXPECT_TRUE(NULL != object); EXPECT_EQ(1, MockBaseNPObject::count()); @@ -54,7 +53,7 @@ TEST_F(BaseNPObjectTest, AllocateAndDeallocateObject) { TEST_F(BaseNPObjectTest, InvalidateForwards) { MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); EXPECT_CALL(*object, Invalidate()); np_class->invalidate(object); @@ -64,7 +63,7 @@ TEST_F(BaseNPObjectTest, InvalidateForwards) { TEST_F(BaseNPObjectTest, HasMethodForwards) { MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); EXPECT_CALL(*object, HasMethod(identifier)); np_class->hasMethod(object, identifier); @@ -74,7 +73,7 @@ TEST_F(BaseNPObjectTest, HasMethodForwards) { TEST_F(BaseNPObjectTest, InvokeForwards) { MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); EXPECT_CALL(*object, Invoke(identifier, args, 3, &result)); np_class->invoke(object, identifier, args, 3, &result); @@ -84,7 +83,7 @@ TEST_F(BaseNPObjectTest, InvokeForwards) { TEST_F(BaseNPObjectTest, InvokeDefaultForwards) { MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); EXPECT_CALL(*object, InvokeDefault(args, 3, &result)); np_class->invokeDefault(object, args, 3, &result); @@ -94,7 +93,7 @@ TEST_F(BaseNPObjectTest, InvokeDefaultForwards) { TEST_F(BaseNPObjectTest, HasPropertyForwards) { MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); EXPECT_CALL(*object, HasProperty(identifier)); np_class->hasProperty(object, identifier); @@ -104,7 +103,7 @@ TEST_F(BaseNPObjectTest, HasPropertyForwards) { TEST_F(BaseNPObjectTest, GetPropertyForwards) { MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); EXPECT_CALL(*object, GetProperty(identifier, &result)); np_class->getProperty(object, identifier, &result); @@ -114,7 +113,7 @@ TEST_F(BaseNPObjectTest, GetPropertyForwards) { TEST_F(BaseNPObjectTest, SetPropertyForwards) { MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); EXPECT_CALL(*object, SetProperty(identifier, &result)); np_class->setProperty(object, identifier, &result); @@ -124,7 +123,7 @@ TEST_F(BaseNPObjectTest, SetPropertyForwards) { TEST_F(BaseNPObjectTest, RemovePropertyForwards) { MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); EXPECT_CALL(*object, RemoveProperty(identifier)); np_class->removeProperty(object, identifier); @@ -134,7 +133,7 @@ TEST_F(BaseNPObjectTest, RemovePropertyForwards) { TEST_F(BaseNPObjectTest, EnumerateForwards) { MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); NPIdentifier* identifier = NULL; uint32_t count; @@ -146,7 +145,7 @@ TEST_F(BaseNPObjectTest, EnumerateForwards) { TEST_F(BaseNPObjectTest, ConstructForwards) { MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - np_class->allocate(&npp_, np_class)); + np_class->allocate(&npp_, const_cast<NPClass*>(np_class))); EXPECT_CALL(*object, Construct(args, 3, &result)); np_class->construct(object, args, 3, &result); diff --git a/o3d/gpu_plugin/np_utils/dispatched_np_object.cc b/o3d/gpu_plugin/np_utils/dispatched_np_object.cc index ef04a21..9f82e31 100644 --- a/o3d/gpu_plugin/np_utils/dispatched_np_object.cc +++ b/o3d/gpu_plugin/np_utils/dispatched_np_object.cc @@ -52,7 +52,7 @@ bool DispatchedNPObject::Enumerate(NPIdentifier** names, uint32_t* num_names) { // Copy names into the array. *names = static_cast<NPIdentifier*>( - NPN_MemAlloc((*num_names) * sizeof(**names))); + NPBrowser::get()->MemAlloc((*num_names) * sizeof(**names))); int i = 0; for (BaseNPDispatcher* dispatcher = GetDynamicDispatcherChain(); dispatcher; diff --git a/o3d/gpu_plugin/np_utils/dispatched_np_object_unittest.cc b/o3d/gpu_plugin/np_utils/dispatched_np_object_unittest.cc index b9edd67..26a79a5 100644 --- a/o3d/gpu_plugin/np_utils/dispatched_np_object_unittest.cc +++ b/o3d/gpu_plugin/np_utils/dispatched_np_object_unittest.cc @@ -5,8 +5,8 @@ #include <string> #include "o3d/gpu_plugin/np_utils/dispatched_np_object.h" +#include "o3d/gpu_plugin/np_utils/np_browser_stub.h" #include "o3d/gpu_plugin/np_utils/np_dispatcher.h" -#include "o3d/gpu_plugin/np_utils/npn_test_stub.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -21,8 +21,8 @@ namespace gpu_plugin { // corresponding mocked member function. class MockDispatchedNPObject : public DispatchedNPObject { public: - MockDispatchedNPObject() - : DispatchedNPObject(NULL) { + explicit MockDispatchedNPObject(NPP npp) + : DispatchedNPObject(npp) { } MOCK_METHOD0(VoidReturnNoParams, void()); @@ -31,7 +31,7 @@ class MockDispatchedNPObject : public DispatchedNPObject { MOCK_METHOD1(VoidReturnFloatParam, void(float)); MOCK_METHOD1(VoidReturnDoubleParam, void(double)); MOCK_METHOD1(VoidReturnStringParam, void(std::string)); - MOCK_METHOD1(VoidReturnObjectParam, void(NPObject*)); + MOCK_METHOD1(VoidReturnObjectParam, void(NPObjectPointer<BaseNPObject>)); MOCK_METHOD2(VoidReturnTwoParams, void(bool, int)); MOCK_METHOD0(Overloaded, void()); MOCK_METHOD1(Overloaded, void(bool)); @@ -41,7 +41,7 @@ class MockDispatchedNPObject : public DispatchedNPObject { MOCK_METHOD0(FloatReturn, float()); MOCK_METHOD0(DoubleReturn, double()); MOCK_METHOD0(StringReturn, std::string()); - MOCK_METHOD0(ObjectReturn, NPObject*()); + MOCK_METHOD0(ObjectReturn, NPObjectPointer<BaseNPObject>()); protected: NP_UTILS_BEGIN_DISPATCHER_CHAIN(MockDispatchedNPObject, DispatchedNPObject) @@ -51,7 +51,8 @@ class MockDispatchedNPObject : public DispatchedNPObject { NP_UTILS_DISPATCHER(VoidReturnFloatParam, void(float)) NP_UTILS_DISPATCHER(VoidReturnDoubleParam, void(double)) NP_UTILS_DISPATCHER(VoidReturnStringParam, void(std::string)) - NP_UTILS_DISPATCHER(VoidReturnObjectParam, void(NPObject*)) + NP_UTILS_DISPATCHER(VoidReturnObjectParam, + void(NPObjectPointer<BaseNPObject>)) NP_UTILS_DISPATCHER(VoidReturnTwoParams, void(bool, int)) NP_UTILS_DISPATCHER(Overloaded, void()) NP_UTILS_DISPATCHER(Overloaded, void(bool)) @@ -61,16 +62,15 @@ class MockDispatchedNPObject : public DispatchedNPObject { NP_UTILS_DISPATCHER(FloatReturn, float()) NP_UTILS_DISPATCHER(DoubleReturn, double()) NP_UTILS_DISPATCHER(StringReturn, std::string()) - NP_UTILS_DISPATCHER(ObjectReturn, NPObject*()); + NP_UTILS_DISPATCHER(ObjectReturn, NPObjectPointer<BaseNPObject>()); NP_UTILS_END_DISPATCHER_CHAIN }; class NPObjectDispatcherTest : public testing::Test { protected: virtual void SetUp() { - InitializeNPNTestStub(); - - object_ = new StrictMock<MockDispatchedNPObject>; + object_ = NPCreateObject<StrictMock<MockDispatchedNPObject> >(NULL); + passed_object_ = NPCreateObject<BaseNPObject>(NULL); for (int i = 0; i != arraysize(args_); ++i) { NULL_TO_NPVARIANT(args_[i]); @@ -78,40 +78,40 @@ class NPObjectDispatcherTest : public testing::Test { NULL_TO_NPVARIANT(result_); } - virtual void TearDown() { - delete object_; - ShutdownNPNTestStub(); - } - + StubNPBrowser stub_browser_; NPVariant args_[3]; NPVariant result_; - MockDispatchedNPObject* object_; + NPObjectPointer<MockDispatchedNPObject> object_; + NPObjectPointer<BaseNPObject> passed_object_; }; TEST_F(NPObjectDispatcherTest, CannotInvokeMissingFunction) { - EXPECT_FALSE(object_->Invoke(NPN_GetStringIdentifier("missing"), - NULL, - 0, - &result_)); + EXPECT_FALSE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("missing"), + NULL, + 0, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnNoParams) { EXPECT_CALL(*object_, VoidReturnNoParams()); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnNoParams"), - NULL, - 0, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnNoParams"), + NULL, + 0, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } TEST_F(NPObjectDispatcherTest, CannotInvokeVoidReturnNoParamsWithTooManyParams) { - EXPECT_FALSE(object_->Invoke(NPN_GetStringIdentifier("voidReturnNoParams"), - args_, - 1, - &result_)); + EXPECT_FALSE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnNoParams"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } @@ -120,10 +120,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnIntParam) { INT32_TO_NPVARIANT(7, args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnIntParam"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnIntParam"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } @@ -132,10 +133,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnBoolParam) { BOOLEAN_TO_NPVARIANT(true, args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnBoolParam"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnBoolParam"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } @@ -144,10 +146,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnFloatParamWithDoubleParam) { DOUBLE_TO_NPVARIANT(7.0, args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnFloatParam"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnFloatParam"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } @@ -156,10 +159,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnFloatParamWithIntParam) { INT32_TO_NPVARIANT(7, args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnFloatParam"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnFloatParam"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } @@ -168,10 +172,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnDoubleParamWithDoubleParam) { DOUBLE_TO_NPVARIANT(7.0, args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnDoubleParam"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnDoubleParam"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } @@ -180,10 +185,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnDoubleParamWithIntParam) { INT32_TO_NPVARIANT(7, args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnDoubleParam"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnDoubleParam"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } @@ -192,34 +198,39 @@ TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnStringParam) { STRINGZ_TO_NPVARIANT("hello", args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnStringParam"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnStringParam"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnObjectParamWithObject) { - EXPECT_CALL(*object_, VoidReturnObjectParam(object_)); + EXPECT_CALL(*object_, VoidReturnObjectParam(passed_object_)); - OBJECT_TO_NPVARIANT(object_, args_[0]); + OBJECT_TO_NPVARIANT(passed_object_.Get(), args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnObjectParam"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnObjectParam"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnObjectParamWithNull) { - EXPECT_CALL(*object_, VoidReturnObjectParam(NULL)); + EXPECT_CALL( + *object_, + VoidReturnObjectParam(NPObjectPointer<BaseNPObject>())); NULL_TO_NPVARIANT(args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnObjectParam"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnObjectParam"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } @@ -229,20 +240,22 @@ TEST_F(NPObjectDispatcherTest, CanInvokeVoidReturnTwoParams) { BOOLEAN_TO_NPVARIANT(false, args_[0]); INT32_TO_NPVARIANT(7, args_[1]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("voidReturnTwoParams"), - args_, - 2, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("voidReturnTwoParams"), + args_, + 2, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } TEST_F(NPObjectDispatcherTest, CanInvokeOverloadedWithNoParams) { EXPECT_CALL(*object_, Overloaded()); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("overloaded"), - NULL, - 0, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("overloaded"), + NULL, + 0, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } @@ -251,10 +264,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeOverloadedWithOneStringParam) { STRINGZ_TO_NPVARIANT("hello", args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("overloaded"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("overloaded"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } @@ -263,20 +277,22 @@ TEST_F(NPObjectDispatcherTest, CanInvokeOverloadedWithOneBoolParam) { BOOLEAN_TO_NPVARIANT(true, args_[0]); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("overloaded"), - args_, - 1, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("overloaded"), + args_, + 1, + &result_)); EXPECT_TRUE(NPVARIANT_IS_VOID(result_)); } TEST_F(NPObjectDispatcherTest, CanInvokeBoolReturn) { EXPECT_CALL(*object_, BoolReturn()).WillOnce(Return(true)); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("boolReturn"), - NULL, - 0, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("boolReturn"), + NULL, + 0, + &result_)); EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(result_)); EXPECT_TRUE(NPVARIANT_TO_BOOLEAN(result_)); } @@ -284,10 +300,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeBoolReturn) { TEST_F(NPObjectDispatcherTest, CanInvokeIntReturn) { EXPECT_CALL(*object_, IntReturn()).WillOnce(Return(7)); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("intReturn"), - NULL, - 0, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("intReturn"), + NULL, + 0, + &result_)); EXPECT_TRUE(NPVARIANT_IS_INT32(result_)); EXPECT_EQ(7, NPVARIANT_TO_INT32(result_)); } @@ -295,10 +312,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeIntReturn) { TEST_F(NPObjectDispatcherTest, CanInvokeFloatReturn) { EXPECT_CALL(*object_, FloatReturn()).WillOnce(Return(7.0f)); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("floatReturn"), - NULL, - 0, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("floatReturn"), + NULL, + 0, + &result_)); EXPECT_TRUE(NPVARIANT_IS_DOUBLE(result_)); EXPECT_EQ(7.0, NPVARIANT_TO_DOUBLE(result_)); } @@ -306,10 +324,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeFloatReturn) { TEST_F(NPObjectDispatcherTest, CanInvokeDoubleReturn) { EXPECT_CALL(*object_, DoubleReturn()).WillOnce(Return(7.0)); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("doubleReturn"), - NULL, - 0, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("doubleReturn"), + NULL, + 0, + &result_)); EXPECT_TRUE(NPVARIANT_IS_DOUBLE(result_)); EXPECT_EQ(7.0, NPVARIANT_TO_DOUBLE(result_)); } @@ -317,10 +336,11 @@ TEST_F(NPObjectDispatcherTest, CanInvokeDoubleReturn) { TEST_F(NPObjectDispatcherTest, CanInvokeStringReturn) { EXPECT_CALL(*object_, StringReturn()).WillOnce(Return(std::string("hello"))); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("stringReturn"), - NULL, - 0, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("stringReturn"), + NULL, + 0, + &result_)); EXPECT_TRUE(NPVARIANT_IS_STRING(result_)); NPString& str = NPVARIANT_TO_STRING(result_); @@ -328,37 +348,43 @@ TEST_F(NPObjectDispatcherTest, CanInvokeStringReturn) { std::string(str.UTF8Characters, str.UTF8Length)); // Callee is responsible for releasing string. - gpu_plugin::NPN_ReleaseVariantValue(&result_); + NPBrowser::get()->ReleaseVariantValue(&result_); } TEST_F(NPObjectDispatcherTest, CanInvokeObjectReturnWithObject) { - EXPECT_CALL(*object_, ObjectReturn()).WillOnce(Return(object_)); + EXPECT_CALL(*object_, ObjectReturn()).WillOnce(Return(passed_object_)); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("objectReturn"), - NULL, - 0, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("objectReturn"), + NULL, + 0, + &result_)); EXPECT_TRUE(NPVARIANT_IS_OBJECT(result_)); - EXPECT_EQ(object_, NPVARIANT_TO_OBJECT(result_)); + EXPECT_EQ(passed_object_.Get(), NPVARIANT_TO_OBJECT(result_)); + + NPBrowser::get()->ReleaseVariantValue(&result_); } TEST_F(NPObjectDispatcherTest, CanInvokeObjectReturnWithNull) { EXPECT_CALL(*object_, ObjectReturn()) - .WillOnce(Return(static_cast<NPObject*>(NULL))); + .WillOnce(Return(NPObjectPointer<BaseNPObject>())); - EXPECT_TRUE(object_->Invoke(NPN_GetStringIdentifier("objectReturn"), - NULL, - 0, - &result_)); + EXPECT_TRUE(object_->Invoke( + NPBrowser::get()->GetStringIdentifier("objectReturn"), + NULL, + 0, + &result_)); EXPECT_TRUE(NPVARIANT_IS_NULL(result_)); } TEST_F(NPObjectDispatcherTest, HasMethodReturnsTrueIfMatchingMemberVariable) { - EXPECT_TRUE(object_->HasMethod(NPN_GetStringIdentifier("objectReturn"))); + EXPECT_TRUE(object_->HasMethod( + NPBrowser::get()->GetStringIdentifier("objectReturn"))); } TEST_F(NPObjectDispatcherTest, HasMethodReturnsTrueIfNoMatchingMemberVariable) { - EXPECT_FALSE(object_->HasMethod(NPN_GetStringIdentifier("missing"))); + EXPECT_FALSE(object_->HasMethod( + NPBrowser::get()->GetStringIdentifier("missing"))); } TEST_F(NPObjectDispatcherTest, EnumeratesAllAvailableMethods) { @@ -371,10 +397,10 @@ TEST_F(NPObjectDispatcherTest, EnumeratesAllAvailableMethods) { // registered (last in the dispatcher chain) and that more than one is // returned. EXPECT_GT(num_names, 1u); - EXPECT_EQ(NPN_GetStringIdentifier("voidReturnNoParams"), - names[num_names - 1]); + EXPECT_EQ(NPBrowser::get()->GetStringIdentifier("voidReturnNoParams"), + names[num_names - 1]); - NPN_MemFree(names); + NPBrowser::get()->MemFree(names); } } // namespace gpu_plugin diff --git a/o3d/gpu_plugin/np_utils/np_browser.cc b/o3d/gpu_plugin/np_utils/np_browser.cc new file mode 100644 index 0000000..6dcbc3d --- /dev/null +++ b/o3d/gpu_plugin/np_utils/np_browser.cc @@ -0,0 +1,114 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "o3d/gpu_plugin/np_utils/np_browser.h" +#include "base/logging.h" +#include "webkit/glue/plugins/nphostapi.h" + +namespace o3d { +namespace gpu_plugin { + +NPBrowser* NPBrowser::browser_; + +NPBrowser::NPBrowser(NPNetscapeFuncs* funcs) + : netscape_funcs_(funcs), + chromium_funcs_(NULL) { + // Attempt to get the Chromium functions. + if (netscape_funcs_ && netscape_funcs_->getvalue) { + netscape_funcs_->getvalue(NULL, NPNVchromiumFuncs, &chromium_funcs_); + } + + // Make this the first browser in the linked list. + previous_browser_ = browser_; + browser_ = this; +} + +NPBrowser::~NPBrowser() { + // Remove this browser from the linked list. + DCHECK(browser_ == this); + browser_ = previous_browser_; +} + +NPIdentifier NPBrowser::GetStringIdentifier(const NPUTF8* name) { + return netscape_funcs_->getstringidentifier(name); +} + +void* NPBrowser::MemAlloc(size_t size) { + return netscape_funcs_->memalloc(size); +} + +void NPBrowser::MemFree(void* p) { + netscape_funcs_->memfree(p); +} + +NPObject* NPBrowser::CreateObject(NPP npp, const NPClass* cl) { + return netscape_funcs_->createobject(npp, const_cast<NPClass*>(cl)); +} + +NPObject* NPBrowser::RetainObject(NPObject* object) { + return netscape_funcs_->retainobject(object); +} + +void NPBrowser::ReleaseObject(NPObject* object) { + netscape_funcs_->releaseobject(object); +} + +void NPBrowser::ReleaseVariantValue(NPVariant* variant) { + netscape_funcs_->releasevariantvalue(variant); +} + +bool NPBrowser::HasProperty(NPP npp, + NPObject* object, + NPIdentifier name) { + return netscape_funcs_->hasproperty(npp, object, name); +} + +bool NPBrowser::GetProperty(NPP npp, + NPObject* object, + NPIdentifier name, + NPVariant* result) { + return netscape_funcs_->getproperty(npp, object, name, result); +} + +bool NPBrowser::HasMethod(NPP npp, + NPObject* object, + NPIdentifier name) { + return netscape_funcs_->hasmethod(npp, object, name); +} + +bool NPBrowser::Invoke(NPP npp, + NPObject* object, + NPIdentifier name, + const NPVariant* args, + uint32_t num_args, + NPVariant* result) { + return netscape_funcs_->invoke(npp, object, name, args, num_args, result); +} + +NPObject* NPBrowser::GetWindowNPObject(NPP npp) { + NPObject* window; + if (NPERR_NO_ERROR == netscape_funcs_->getvalue(npp, + NPNVWindowNPObject, + &window)) { + return window; + } else { + return NULL; + } +} + +NPSharedMemory* NPBrowser::MapSharedMemory(NPP id, + NPObject* object, + size_t size, + bool read_only) { + DCHECK(chromium_funcs_); + return chromium_funcs_->mapsharedmemory(id, object, size, read_only); +} + +void NPBrowser::UnmapSharedMemory(NPP id, + NPSharedMemory* shared_memory) { + DCHECK(chromium_funcs_); + chromium_funcs_->unmapsharedmemory(id, shared_memory); +} +} // namespace gpu_plugin +} // namespace o3d diff --git a/o3d/gpu_plugin/np_utils/np_browser.h b/o3d/gpu_plugin/np_utils/np_browser.h new file mode 100644 index 0000000..6ea8655 --- /dev/null +++ b/o3d/gpu_plugin/np_utils/np_browser.h @@ -0,0 +1,86 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_BROWSER_H_ +#define O3D_GPU_PLUGIN_NP_UTILS_NP_BROWSER_H_ + +#include "third_party/npapi/bindings/npapi.h" +#include "third_party/npapi/bindings/npruntime.h" + +typedef struct _NPNetscapeFuncs NPNetscapeFuncs; +typedef struct _NPChromiumFuncs NPChromiumFuncs; + +namespace o3d { +namespace gpu_plugin { + +// This class exposes the functions provided by the browser to a plugin (the +// ones prefixed NPN_). +class NPBrowser { + public: + explicit NPBrowser(NPNetscapeFuncs* funcs); + virtual ~NPBrowser(); + + static NPBrowser* get() { + return browser_; + } + + // Standard functions from NPNetscapeFuncs. + + virtual NPIdentifier GetStringIdentifier(const NPUTF8* name); + + virtual void* MemAlloc(size_t size); + + virtual void MemFree(void* p); + + virtual NPObject* CreateObject(NPP npp, const NPClass* cl); + + virtual NPObject* RetainObject(NPObject* object); + + virtual void ReleaseObject(NPObject* object); + + virtual void ReleaseVariantValue(NPVariant* variant); + + virtual bool HasProperty(NPP npp, + NPObject* object, + NPIdentifier name); + + virtual bool GetProperty(NPP npp, + NPObject* object, + NPIdentifier name, + NPVariant* result); + + virtual bool HasMethod(NPP npp, + NPObject* object, + NPIdentifier name); + + virtual bool Invoke(NPP npp, + NPObject* object, + NPIdentifier name, + const NPVariant* args, + uint32_t num_args, + NPVariant* result); + + virtual NPObject* GetWindowNPObject(NPP npp); + + // Chromium specific additions. + virtual NPSharedMemory* MapSharedMemory(NPP id, + NPObject* object, + size_t size, + bool read_only); + + virtual void UnmapSharedMemory(NPP id, + NPSharedMemory* shared_memory); + + private: + static NPBrowser* browser_; + NPBrowser* previous_browser_; + NPNetscapeFuncs* netscape_funcs_; + NPChromiumFuncs* chromium_funcs_; + DISALLOW_COPY_AND_ASSIGN(NPBrowser); +}; + +} // namespace gpu_plugin +} // namespace o3d + +#endif // O3D_GPU_PLUGIN_NP_UTILS_NP_BROWSER_H_ diff --git a/o3d/gpu_plugin/np_utils/np_browser_stub.cc b/o3d/gpu_plugin/np_utils/np_browser_stub.cc new file mode 100644 index 0000000..b074cbe --- /dev/null +++ b/o3d/gpu_plugin/np_utils/np_browser_stub.cc @@ -0,0 +1,106 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "o3d/gpu_plugin/np_utils/np_browser_stub.h" +#include "base/logging.h" + +namespace o3d { +namespace gpu_plugin { + +StubNPBrowser::StubNPBrowser() : NPBrowser(NULL) { +} + +StubNPBrowser::~StubNPBrowser() { +} + +NPIdentifier StubNPBrowser::GetStringIdentifier(const NPUTF8* name) { + static std::set<std::string> names; + std::set<std::string>::iterator it = names.find(name); + if (it == names.end()) { + it = names.insert(name).first; + } + return const_cast<NPUTF8*>((*it).c_str()); +} + +void* StubNPBrowser::MemAlloc(size_t size) { + return malloc(size); +} + +void StubNPBrowser::MemFree(void* p) { + free(p); +} + +NPObject* StubNPBrowser::CreateObject(NPP npp, const NPClass* cl) { + NPObject* object = cl->allocate(npp, const_cast<NPClass*>(cl)); + object->referenceCount = 1; + object->_class = const_cast<NPClass*>(cl); + return object; +} + +NPObject* StubNPBrowser::RetainObject(NPObject* object) { + ++object->referenceCount; + return object; +} + +void StubNPBrowser::ReleaseObject(NPObject* object) { + DCHECK_GE(object->referenceCount, 0u); + --object->referenceCount; + if (object->referenceCount == 0) { + object->_class->deallocate(object); + } +} + +void StubNPBrowser::ReleaseVariantValue(NPVariant* variant) { + if (NPVARIANT_IS_STRING(*variant)) { + MemFree(const_cast<NPUTF8*>(variant->value.stringValue.UTF8Characters)); + } else if (NPVARIANT_IS_OBJECT(*variant)) { + ReleaseObject(NPVARIANT_TO_OBJECT(*variant)); + } +} + +bool StubNPBrowser::HasProperty(NPP npp, + NPObject* object, + NPIdentifier name) { + return object->_class->hasProperty(object, name); +} + +bool StubNPBrowser::GetProperty(NPP npp, + NPObject* object, + NPIdentifier name, + NPVariant* result) { + return object->_class->getProperty(object, name, result); +} + +bool StubNPBrowser::HasMethod(NPP npp, + NPObject* object, + NPIdentifier name) { + return object->_class->hasMethod(object, name); +} + +bool StubNPBrowser::Invoke(NPP npp, + NPObject* object, + NPIdentifier name, + const NPVariant* args, + uint32_t num_args, + NPVariant* result) { + return object->_class->invoke(object, name, args, num_args, result); +} + +NPObject* StubNPBrowser::GetWindowNPObject(NPP npp) { + return NULL; +} + + +NPSharedMemory* StubNPBrowser::MapSharedMemory(NPP id, + NPObject* object, + size_t size, + bool read_only) { + return NULL; +} + +void StubNPBrowser::UnmapSharedMemory(NPP id, + NPSharedMemory* shared_memory) { +} +} // namespace gpu_plugin +} // namespace o3d diff --git a/o3d/gpu_plugin/np_utils/np_browser_stub.h b/o3d/gpu_plugin/np_utils/np_browser_stub.h new file mode 100644 index 0000000..1391c55 --- /dev/null +++ b/o3d/gpu_plugin/np_utils/np_browser_stub.h @@ -0,0 +1,77 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_BROWSER_STUB_H_ +#define O3D_GPU_PLUGIN_NP_UTILS_NP_BROWSER_STUB_H_ + +#include <set> +#include <string> + +#include "o3d/gpu_plugin/np_utils/np_browser.h" +#include "third_party/npapi/bindings/npapi.h" +#include "third_party/npapi/bindings/npruntime.h" + +namespace o3d { +namespace gpu_plugin { + +// Simple implementation of subset of the NPN functions for testing. +class StubNPBrowser : public NPBrowser { + public: + StubNPBrowser(); + virtual ~StubNPBrowser(); + + // Standard functions from NPNetscapeFuncs. + + virtual NPIdentifier GetStringIdentifier(const NPUTF8* name); + + virtual void* MemAlloc(size_t size); + + virtual void MemFree(void* p); + + virtual NPObject* CreateObject(NPP npp, const NPClass* cl); + + virtual NPObject* RetainObject(NPObject* object); + + virtual void ReleaseObject(NPObject* object); + + virtual void ReleaseVariantValue(NPVariant* variant); + + virtual bool HasProperty(NPP npp, + NPObject* object, + NPIdentifier name); + + virtual bool GetProperty(NPP npp, + NPObject* object, + NPIdentifier name, + NPVariant* result); + + virtual bool HasMethod(NPP npp, + NPObject* object, + NPIdentifier name); + virtual bool Invoke(NPP npp, + NPObject* object, + NPIdentifier name, + const NPVariant* args, + uint32_t num_args, + NPVariant* result); + + virtual NPObject* GetWindowNPObject(NPP npp); + + // Chromium specific additions. + virtual NPSharedMemory* MapSharedMemory(NPP id, + NPObject* object, + size_t size, + bool read_only); + + virtual void UnmapSharedMemory(NPP id, + NPSharedMemory* shared_memory); + + private: + DISALLOW_COPY_AND_ASSIGN(StubNPBrowser); +}; + +} // namespace gpu_plugin +} // namespace o3d + +#endif // O3D_GPU_PLUGIN_NP_UTILS_NP_BROWSER_STUB_H_ diff --git a/o3d/gpu_plugin/np_utils/np_dispatcher.h b/o3d/gpu_plugin/np_utils/np_dispatcher.h index 7408e64..0f55d09 100644 --- a/o3d/gpu_plugin/np_utils/np_dispatcher.h +++ b/o3d/gpu_plugin/np_utils/np_dispatcher.h @@ -7,7 +7,7 @@ #include <string> -#include "o3d/gpu_plugin/np_utils/np_variant_utils.h" +#include "o3d/gpu_plugin/np_utils/np_utils.h" #include "third_party/npapi/bindings/npapi.h" #include "third_party/npapi/bindings/npruntime.h" @@ -53,7 +53,8 @@ class BaseNPDispatcher { java_script_style_name[0] = tolower(java_script_style_name[0]); } - name_ = NPN_GetStringIdentifier(java_script_style_name.c_str()); + name_ = NPBrowser::get()->GetStringIdentifier( + java_script_style_name.c_str()); } virtual ~BaseNPDispatcher() { diff --git a/o3d/gpu_plugin/np_utils/np_object_pointer.h b/o3d/gpu_plugin/np_utils/np_object_pointer.h index 045bc57..b6e299c 100644 --- a/o3d/gpu_plugin/np_utils/np_object_pointer.h +++ b/o3d/gpu_plugin/np_utils/np_object_pointer.h @@ -5,7 +5,8 @@ #ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_OBJECT_POINTER_H_ #define O3D_GPU_PLUGIN_NP_UTILS_NP_OBJECT_POINTER_H_ -#include "o3d/gpu_plugin/np_utils/npn_funcs.h" +#include "base/logging.h" +#include "o3d/gpu_plugin/np_utils/np_browser.h" #include "third_party/npapi/bindings/npapi.h" #include "third_party/npapi/bindings/npruntime.h" @@ -19,44 +20,62 @@ class NPObjectPointer { NPObjectPointer() : object_(NULL) {} NPObjectPointer(const NPObjectPointer& rhs) : object_(rhs.object_) { - if (object_) { - gpu_plugin::NPN_RetainObject(object_); - } + Retain(); } explicit NPObjectPointer(NPObjectType* p) : object_(p) { - if (object_) { - gpu_plugin::NPN_RetainObject(object_); - } + Retain(); + } + + template <typename RHS> + NPObjectPointer(const NPObjectPointer<RHS>& rhs) : object_(rhs.Get()) { + Retain(); } ~NPObjectPointer() { - if (object_) { - gpu_plugin::NPN_ReleaseObject(object_); - } + Release(); } NPObjectPointer& operator=(const NPObjectPointer& rhs) { - if (object_) { - gpu_plugin::NPN_ReleaseObject(object_); - } + Release(); object_ = rhs.object_; - if (object_) { - gpu_plugin::NPN_RetainObject(object_); - } + Retain(); return *this; } + template <typename RHS> + NPObjectPointer& operator=(const NPObjectPointer<RHS>& rhs) { + Release(); + object_ = rhs.Get(); + Retain(); + return *this; + } + + template <class RHS> + bool operator==(const NPObjectPointer<RHS>& rhs) const { + return object_ == rhs.Get(); + } + + template <class RHS> + bool operator!=(const NPObjectPointer<RHS>& rhs) const { + return object_ != rhs.Get(); + } + // The NPObject convention for returning an NPObject pointer from a function // is that the caller is responsible for releasing the reference count. static NPObjectPointer FromReturned(NPObjectType* p) { NPObjectPointer pointer(p); - if (p) { - gpu_plugin::NPN_ReleaseObject(p); - } + pointer.Release(); return pointer; } + // The NPObject convention for returning an NPObject pointer from a function + // is that the caller is responsible for releasing the reference count. + NPObjectType* ToReturned() const { + Retain(); + return object_; + } + NPObjectType* Get() const { return object_; } @@ -65,10 +84,32 @@ class NPObjectPointer { return object_; } + NPObjectType& operator*() const { + return *object_; + } + private: + void Retain() const { + if (object_) { + NPBrowser::get()->RetainObject(object_); + } + } + + void Release() const { + if (object_) { + NPBrowser::get()->ReleaseObject(object_); + } + } + NPObjectType* object_; }; +// For test diagnostics. +template <typename NPObjectType> +std::ostream& operator<<(std::ostream& stream, + const NPObjectPointer<NPObjectType>& pointer) { + return stream << pointer.Get(); +} } // namespace gpu_plugin } // namespace o3d diff --git a/o3d/gpu_plugin/np_utils/np_object_pointer_unittest.cc b/o3d/gpu_plugin/np_utils/np_object_pointer_unittest.cc index 7daf218..344e07b 100644 --- a/o3d/gpu_plugin/np_utils/np_object_pointer_unittest.cc +++ b/o3d/gpu_plugin/np_utils/np_object_pointer_unittest.cc @@ -3,8 +3,8 @@ // found in the LICENSE file. #include "o3d/gpu_plugin/np_utils/base_np_object_mock.h" +#include "o3d/gpu_plugin/np_utils/np_browser_stub.h" #include "o3d/gpu_plugin/np_utils/np_object_pointer.h" -#include "o3d/gpu_plugin/np_utils/npn_test_stub.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -14,32 +14,39 @@ using testing::StrictMock; namespace o3d { namespace gpu_plugin { +class DerivedNPObject : public MockBaseNPObject { + public: + explicit DerivedNPObject(NPP npp) : MockBaseNPObject(npp) { + } +}; + class NPObjectPointerTest : public testing::Test { protected: virtual void SetUp() { - InitializeNPNTestStub(); - - np_class_ = const_cast<NPClass*>( - BaseNPObject::GetNPClass<StrictMock<MockBaseNPObject> >()); + np_class_ = BaseNPObject::GetNPClass<StrictMock<MockBaseNPObject> >(); // Make sure no MockBaseNPObject objects exist before test. ASSERT_EQ(0, MockBaseNPObject::count()); raw_pointer_ = static_cast<MockBaseNPObject*>( - gpu_plugin::NPN_CreateObject(NULL, np_class_)); + NPBrowser::get()->CreateObject(NULL, np_class_)); + + raw_derived_pointer_ = static_cast<DerivedNPObject*>( + NPBrowser::get()->CreateObject(NULL, np_class_)); } virtual void TearDown() { - gpu_plugin::NPN_ReleaseObject(raw_pointer_); + NPBrowser::get()->ReleaseObject(raw_pointer_); + NPBrowser::get()->ReleaseObject(raw_derived_pointer_); // Make sure no MockBaseNPObject leaked an object. ASSERT_EQ(0, MockBaseNPObject::count()); - - ShutdownNPNTestStub(); } - NPClass* np_class_; + StubNPBrowser stub_browser_; + const NPClass* np_class_; MockBaseNPObject* raw_pointer_; + DerivedNPObject* raw_derived_pointer_; }; TEST_F(NPObjectPointerTest, PointerIsNullByDefault) { @@ -74,6 +81,17 @@ TEST_F(NPObjectPointerTest, PointerCanBeCopyConstructed) { EXPECT_EQ(2, raw_pointer_->referenceCount); } +TEST_F(NPObjectPointerTest, PointerCanBeConstructedFromDerived) { + NPObjectPointer<DerivedNPObject> p1(raw_derived_pointer_); + EXPECT_EQ(2, raw_derived_pointer_->referenceCount); + { + NPObjectPointer<MockBaseNPObject> p2(p1); + ASSERT_TRUE(raw_derived_pointer_ == p2.Get()); + EXPECT_EQ(3, raw_derived_pointer_->referenceCount); + } + EXPECT_EQ(2, raw_derived_pointer_->referenceCount); +} + TEST_F(NPObjectPointerTest, PointerCanBeCopyConstructedFromNull) { NPObjectPointer<MockBaseNPObject> p(NULL); @@ -100,8 +118,46 @@ TEST_F(NPObjectPointerTest, PointerCanBeAssigned) { EXPECT_EQ(2, raw_pointer_->referenceCount); } +TEST_F(NPObjectPointerTest, PointerCanBeAssignedDerived) { + NPObjectPointer<DerivedNPObject> p1(raw_derived_pointer_); + EXPECT_EQ(2, raw_derived_pointer_->referenceCount); + { + NPObjectPointer<MockBaseNPObject> p2; + p2 = p1; + ASSERT_TRUE(raw_derived_pointer_ == p2.Get()); + EXPECT_EQ(3, raw_derived_pointer_->referenceCount); + + p2 = NPObjectPointer<MockBaseNPObject>(); + ASSERT_TRUE(NULL == p2.Get()); + EXPECT_EQ(2, raw_derived_pointer_->referenceCount); + + p2 = p1; + ASSERT_TRUE(raw_derived_pointer_ == p2.Get()); + EXPECT_EQ(3, raw_derived_pointer_->referenceCount); + } + EXPECT_EQ(2, raw_derived_pointer_->referenceCount); +} + +TEST_F(NPObjectPointerTest, CanComparePointersForEqual) { + NPObjectPointer<MockBaseNPObject> p1(raw_pointer_); + NPObjectPointer<DerivedNPObject> p2(raw_derived_pointer_); + EXPECT_TRUE(p1 == p1); + EXPECT_FALSE(p1 == p2); + EXPECT_FALSE(p2 == p1); + EXPECT_FALSE(p1 == NPObjectPointer<MockBaseNPObject>()); +} + +TEST_F(NPObjectPointerTest, CanComparePointersForNotEqual) { + NPObjectPointer<MockBaseNPObject> p1(raw_pointer_); + NPObjectPointer<DerivedNPObject> p2(raw_derived_pointer_); + EXPECT_FALSE(p1 != p1); + EXPECT_TRUE(p1 != p2); + EXPECT_TRUE(p2 != p1); + EXPECT_TRUE(p1 != NPObjectPointer<MockBaseNPObject>()); +} + TEST_F(NPObjectPointerTest, ArrowOperatorCanBeUsedToAccessNPObjectMembers) { - NPIdentifier name = NPN_GetStringIdentifier("hello"); + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("hello"); EXPECT_CALL(*raw_pointer_, HasProperty(name)).WillOnce(Return(true)); @@ -109,8 +165,13 @@ TEST_F(NPObjectPointerTest, ArrowOperatorCanBeUsedToAccessNPObjectMembers) { EXPECT_TRUE(p->HasProperty(name)); } +TEST_F(NPObjectPointerTest, StarOperatorReturnsNPObjectReference) { + NPObjectPointer<MockBaseNPObject> p(raw_pointer_); + EXPECT_EQ(raw_pointer_, &*p); +} + TEST_F(NPObjectPointerTest, PointerCanBeConstructedFromReturnedNPObject) { - gpu_plugin::NPN_RetainObject(raw_pointer_); + NPBrowser::get()->RetainObject(raw_pointer_); EXPECT_EQ(2, raw_pointer_->referenceCount); { NPObjectPointer<MockBaseNPObject> p( @@ -126,5 +187,20 @@ TEST_F(NPObjectPointerTest, PointerCanBeConstructedFromReturnedNullNPObject) { EXPECT_TRUE(NULL == p.Get()); } +TEST_F(NPObjectPointerTest, PointerCanBeReturnedAsARawNPObject) { + NPObjectPointer<MockBaseNPObject> p(raw_pointer_); + EXPECT_EQ(raw_pointer_, p.ToReturned()); + + // Check reference count is incremented before return for caller. + EXPECT_EQ(3, raw_pointer_->referenceCount); + + NPBrowser::get()->ReleaseObject(raw_pointer_); +} + +TEST_F(NPObjectPointerTest, NULLPointerCanBeReturnedAsARawNPObject) { + NPObjectPointer<MockBaseNPObject> p; + EXPECT_TRUE(NULL == p.ToReturned()); +} + } // namespace gpu_plugin } // namespace o3d diff --git a/o3d/gpu_plugin/plugin_object.h b/o3d/gpu_plugin/np_utils/np_plugin_object.h index d04dd07..c87b6e1 100644 --- a/o3d/gpu_plugin/plugin_object.h +++ b/o3d/gpu_plugin/np_utils/np_plugin_object.h @@ -2,9 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef O3D_GPU_PLUGIN_PLUGIN_OBJECT_H_ -#define O3D_GPU_PLUGIN_PLUGIN_OBJECT_H_ +#ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_H_ +#define O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_H_ +#include "o3d/gpu_plugin/np_utils/np_object_pointer.h" #include "third_party/npapi/bindings/npapi.h" #include "third_party/npapi/bindings/npruntime.h" @@ -15,12 +16,7 @@ namespace gpu_plugin { // of this interface. class PluginObject { public: - PluginObject() { - } - - virtual ~PluginObject() { - } - + // Initialize this object. virtual NPError New(NPMIMEType plugin_type, int16 argc, char* argn[], @@ -31,9 +27,21 @@ class PluginObject { virtual int16 HandleEvent(NPEvent* event) = 0; + // Uninitialize but do not deallocate the object. Release will be called to + // deallocate if Destroy succeeds. virtual NPError Destroy(NPSavedData** saved) = 0; - virtual NPObject* GetScriptableInstance() = 0; + // Deallocate this object. This object is invalid after this returns. + virtual void Release() = 0; + + virtual NPObject* GetScriptableNPObject() = 0; + + protected: + PluginObject() { + } + + virtual ~PluginObject() { + } private: DISALLOW_COPY_AND_ASSIGN(PluginObject); @@ -42,4 +50,4 @@ class PluginObject { } // namespace gpu_plugin } // namespace o3d -#endif // O3D_GPU_PLUGIN_PLUGIN_OBJECT_H_ +#endif // O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_H_ diff --git a/o3d/gpu_plugin/np_utils/np_plugin_object_factory.cc b/o3d/gpu_plugin/np_utils/np_plugin_object_factory.cc new file mode 100644 index 0000000..5f47430 --- /dev/null +++ b/o3d/gpu_plugin/np_utils/np_plugin_object_factory.cc @@ -0,0 +1,32 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "o3d/gpu_plugin/gpu_plugin_object_factory.h" +#include "base/logging.h" + +namespace o3d { +namespace gpu_plugin { + +NPPluginObjectFactory* NPPluginObjectFactory::factory_; + +PluginObject* NPPluginObjectFactory::CreatePluginObject( + NPP npp, + NPMIMEType plugin_type) { + return NULL; +} + +NPPluginObjectFactory::NPPluginObjectFactory() { + // Make this the first factory in the linked list. + previous_factory_ = factory_; + factory_ = this; +} + +NPPluginObjectFactory::~NPPluginObjectFactory() { + // Remove this factory from the linked list. + DCHECK(factory_ == this); + factory_ = previous_factory_; +} + +} // namespace gpu_plugin +} // namespace o3d diff --git a/o3d/gpu_plugin/np_utils/np_plugin_object_factory.h b/o3d/gpu_plugin/np_utils/np_plugin_object_factory.h new file mode 100644 index 0000000..3671542 --- /dev/null +++ b/o3d/gpu_plugin/np_utils/np_plugin_object_factory.h @@ -0,0 +1,39 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_FACTORY_H_ +#define O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_FACTORY_H_ + +#include "third_party/npapi/bindings/npapi.h" +#include "third_party/npapi/bindings/npruntime.h" + +namespace o3d { +namespace gpu_plugin { + +class PluginObject; + +// Mockable factory base class used to create instances of PluginObject based on +// plugin mime type. +class NPPluginObjectFactory { + public: + virtual PluginObject* CreatePluginObject(NPP npp, NPMIMEType plugin_type); + + static NPPluginObjectFactory* get() { + return factory_; + } + + protected: + NPPluginObjectFactory(); + virtual ~NPPluginObjectFactory(); + + private: + static NPPluginObjectFactory* factory_; + NPPluginObjectFactory* previous_factory_; + DISALLOW_COPY_AND_ASSIGN(NPPluginObjectFactory); +}; + +} // namespace gpu_plugin +} // namespace o3d + +#endif // O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_FACTORY_H_ diff --git a/o3d/gpu_plugin/plugin_object_factory_mock.h b/o3d/gpu_plugin/np_utils/np_plugin_object_factory_mock.h index 8640c7d..304f4c3 100644 --- a/o3d/gpu_plugin/plugin_object_factory_mock.h +++ b/o3d/gpu_plugin/np_utils/np_plugin_object_factory_mock.h @@ -2,10 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef O3D_GPU_PLUGIN_PLUGIN_OBJECT_FACTORY_MOCK_H_ -#define O3D_GPU_PLUGIN_PLUGIN_OBJECT_FACTORY_MOCK_H_ +#ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_FACTORY_MOCK_H_ +#define O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_FACTORY_MOCK_H_ -#include "o3d/gpu_plugin/plugin_object_factory.h" +#include "o3d/gpu_plugin/np_utils/np_plugin_object_factory.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -14,7 +14,7 @@ namespace gpu_plugin { // Mockable factory used to create instances of PluginObject based on plugin // mime type. -class MockPluginObjectFactory : public PluginObjectFactory { +class MockPluginObjectFactory : public NPPluginObjectFactory { public: MOCK_METHOD2(CreatePluginObject, PluginObject*(NPP, NPMIMEType)); }; @@ -22,4 +22,4 @@ class MockPluginObjectFactory : public PluginObjectFactory { } // namespace gpu_plugin } // namespace o3d -#endif // O3D_GPU_PLUGIN_PLUGIN_OBJECT_FACTORY_MOCK_H_ +#endif // O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_FACTORY_MOCK_H_ diff --git a/o3d/gpu_plugin/plugin_object_mock.h b/o3d/gpu_plugin/np_utils/np_plugin_object_mock.h index 904e757..83ccee5 100644 --- a/o3d/gpu_plugin/plugin_object_mock.h +++ b/o3d/gpu_plugin/np_utils/np_plugin_object_mock.h @@ -2,10 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef O3D_GPU_PLUGIN_PLUGIN_OBJECT_MOCK_H_ -#define O3D_GPU_PLUGIN_PLUGIN_OBJECT_MOCK_H_ +#ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_MOCK_H_ +#define O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_MOCK_H_ -#include "o3d/gpu_plugin/plugin_object.h" +#include "o3d/gpu_plugin/np_utils/np_plugin_object.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -18,10 +18,11 @@ class MockPluginObject : public PluginObject { MOCK_METHOD1(SetWindow, NPError(NPWindow*)); MOCK_METHOD1(HandleEvent, int16(NPEvent*)); MOCK_METHOD1(Destroy, NPError(NPSavedData**)); - MOCK_METHOD0(GetScriptableInstance, NPObject*()); + MOCK_METHOD0(Release, void()); + MOCK_METHOD0(GetScriptableNPObject, NPObject*()); }; } // namespace gpu_plugin } // namespace o3d -#endif // O3D_GPU_PLUGIN_PLUGIN_OBJECT_MOCK_H_ +#endif // O3D_GPU_PLUGIN_NP_UTILS_NP_PLUGIN_OBJECT_MOCK_H_ diff --git a/o3d/gpu_plugin/np_utils/np_variant_utils.cc b/o3d/gpu_plugin/np_utils/np_utils.cc index 3a7332e..33c0d00 100644 --- a/o3d/gpu_plugin/np_utils/np_variant_utils.cc +++ b/o3d/gpu_plugin/np_utils/np_utils.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "o3d/gpu_plugin/np_utils/np_variant_utils.h" +#include "o3d/gpu_plugin/np_utils/np_utils.h" #include "third_party/npapi/bindings/npapi.h" #include "third_party/npapi/bindings/npruntime.h" @@ -61,18 +61,6 @@ bool NPVariantToValue(std::string* value, const NPVariant& variant) { return false; } -bool NPVariantToValue(NPObject** value, const NPVariant& variant) { - if (NPVARIANT_IS_NULL(variant)) { - *value = NULL; - return true; - } else if (NPVARIANT_IS_OBJECT(variant)) { - *value = NPVARIANT_TO_OBJECT(variant); - return true; - } - - return false; -} - void ValueToNPVariant(bool value, NPVariant* variant) { BOOLEAN_TO_NPVARIANT(value, *variant); } @@ -90,19 +78,68 @@ void ValueToNPVariant(double value, NPVariant* variant) { } void ValueToNPVariant(const std::string& value, NPVariant* variant) { - NPUTF8* p = static_cast<NPUTF8*>(NPN_MemAlloc(value.length())); + NPUTF8* p = static_cast<NPUTF8*>(NPBrowser::get()->MemAlloc(value.length())); memcpy(p, value.c_str(), value.length()); STRINGN_TO_NPVARIANT(p, value.length(), *variant); } -void ValueToNPVariant(NPObject* value, NPVariant* variant) { - if (value) { - gpu_plugin::NPN_RetainObject(value); - OBJECT_TO_NPVARIANT(value, *variant); +SmartNPVariant::SmartNPVariant() { + VOID_TO_NPVARIANT(*this); +} + +SmartNPVariant::SmartNPVariant(const SmartNPVariant& rhs) { + Copy(rhs); +} + +SmartNPVariant::~SmartNPVariant() { + Release(); +} + +SmartNPVariant& SmartNPVariant::operator=(const SmartNPVariant& rhs) { + Release(); + Copy(rhs); + return *this; +} + +void SmartNPVariant::Release() { + NPBrowser::get()->ReleaseVariantValue(this); + VOID_TO_NPVARIANT(*this); +} + +void SmartNPVariant::Copy(const NPVariant& rhs) { + if (NPVARIANT_IS_OBJECT(rhs)) { + NPObject* object = NPVARIANT_TO_OBJECT(rhs); + OBJECT_TO_NPVARIANT(object, *this); + NPBrowser::get()->RetainObject(object); + } else if (NPVARIANT_IS_STRING(rhs)) { + NPUTF8* copy = static_cast<NPUTF8*>(NPBrowser::get()->MemAlloc( + rhs.value.stringValue.UTF8Length)); + memcpy(copy, + rhs.value.stringValue.UTF8Characters, + rhs.value.stringValue.UTF8Length); + STRINGN_TO_NPVARIANT(copy, rhs.value.stringValue.UTF8Length, *this); } else { - NULL_TO_NPVARIANT(*variant); + memcpy(this, &rhs, sizeof(rhs)); } } +bool NPHasMethod(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name) { + return NPBrowser::get()->HasMethod( + npp, + object.Get(), + NPBrowser::get()->GetStringIdentifier(name)); +} + +bool NPHasProperty(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name) { + return NPBrowser::get()->HasProperty( + npp, + object.Get(), + NPBrowser::get()->GetStringIdentifier(name)); +} + } // namespace gpu_plugin } // namespace o3d diff --git a/o3d/gpu_plugin/np_utils/np_utils.h b/o3d/gpu_plugin/np_utils/np_utils.h new file mode 100644 index 0000000..f2ec2ed --- /dev/null +++ b/o3d/gpu_plugin/np_utils/np_utils.h @@ -0,0 +1,248 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_VARIANT_UTILS_H_ +#define O3D_GPU_PLUGIN_NP_UTILS_NP_VARIANT_UTILS_H_ + +#include <string> + +#include "o3d/gpu_plugin/np_utils/np_browser.h" +#include "o3d/gpu_plugin/np_utils/np_object_pointer.h" +#include "third_party/npapi/bindings/npapi.h" +#include "third_party/npapi/bindings/npruntime.h" + +namespace o3d { +namespace gpu_plugin { + +// Convert NPVariant to C++ type. Returns whether the conversion was successful. +bool NPVariantToValue(bool* value, const NPVariant& variant); +bool NPVariantToValue(int* value, const NPVariant& variant); +bool NPVariantToValue(float* value, const NPVariant& variant); +bool NPVariantToValue(double* value, const NPVariant& variant); +bool NPVariantToValue(std::string* value, const NPVariant& variant); + +template <typename T> +bool NPVariantToValue(NPObjectPointer<T>* value, + const NPVariant& variant) { + if (NPVARIANT_IS_NULL(variant)) { + *value = NPObjectPointer<T>(); + return true; + } else if (NPVARIANT_IS_OBJECT(variant)) { + NPObject* object = NPVARIANT_TO_OBJECT(variant); + if (object->_class == BaseNPObject::GetNPClass<T>()) { + *value = NPObjectPointer<T>(static_cast<T*>( + NPVARIANT_TO_OBJECT(variant))); + return true; + } + } + + return false; +} + +// Specialization for NPObject does not check for mismatched NPClass. +template <> +inline bool NPVariantToValue(NPObjectPointer<NPObject>* value, + const NPVariant& variant) { + if (NPVARIANT_IS_NULL(variant)) { + *value = NPObjectPointer<NPObject>(); + return true; + } else if (NPVARIANT_IS_OBJECT(variant)) { + *value = NPObjectPointer<NPObject>(NPVARIANT_TO_OBJECT(variant)); + return true; + } + + return false; +} + +// Convert C++ type to NPVariant. +void ValueToNPVariant(bool value, NPVariant* variant); +void ValueToNPVariant(int value, NPVariant* variant); +void ValueToNPVariant(float value, NPVariant* variant); +void ValueToNPVariant(double value, NPVariant* variant); +void ValueToNPVariant(const std::string& value, NPVariant* variant); + +template <typename T> +void ValueToNPVariant(const NPObjectPointer<T>& value, + NPVariant* variant) { + if (value.Get()) { + NPBrowser::get()->RetainObject(value.Get()); + OBJECT_TO_NPVARIANT(value.Get(), *variant); + } else { + NULL_TO_NPVARIANT(*variant); + } +} + +// NPVariant that autmatically releases in destructor. +class SmartNPVariant : public NPVariant { + public: + SmartNPVariant(); + SmartNPVariant(const SmartNPVariant& rhs); + + template <typename T> + explicit SmartNPVariant(const T& v) { + ValueToNPVariant(v, this); + } + + ~SmartNPVariant(); + + SmartNPVariant& operator=(const SmartNPVariant& rhs); + + template <typename T> + bool GetValue(T* v) const { + return NPVariantToValue(v, *this); + } + + template <typename T> + void SetValue(const T& v) { + Release(); + ValueToNPVariant(v, this); + } + + void Release(); + +private: + void Copy(const NPVariant& rhs); +}; + +// These allow a method to be invoked with automatic conversion of C++ +// types to variants for arguments and return values. + +bool NPHasMethod(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name); + +inline bool NPInvokeVoid(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name) { + SmartNPVariant result; + return NPBrowser::get()->Invoke( + npp, + object.Get(), + NPBrowser::get()->GetStringIdentifier(name), + NULL, 0, + &result); +} + +template<typename R> +bool NPInvoke(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name, + R* r) { + SmartNPVariant result; + if (NPBrowser::get()->Invoke( + npp, + object.Get(), + NPBrowser::get()->GetStringIdentifier(name), + NULL, 0, + &result)) { + return result.GetValue(r); + } + return false; +} + +template<typename P0> +bool NPInvokeVoid(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name, + P0 p0) { + SmartNPVariant args[1]; + args[0].SetValue(p0); + SmartNPVariant result; + return NPBrowser::get()->Invoke( + npp, + object.Get(), + NPBrowser::get()->GetStringIdentifier(name), + &args[0], 1, + &result); +} + +template<typename R, typename P0> +bool NPInvoke(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name, + P0 p0, + R* r) { + SmartNPVariant args[1]; + args[0].SetValue(p0); + SmartNPVariant result; + if (NPBrowser::get()->Invoke( + npp, + object.Get(), + NPBrowser::get()->GetStringIdentifier(name), + &args[0], 1, + &result)) { + return result.GetValue(r); + } + return false; +} + +template<typename P0, typename P1> +bool NPInvokeVoid(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name, + P0 p0, P1 p1) { + SmartNPVariant args[2]; + args[0].SetValue(p0); + args[1].SetValue(p1); + SmartNPVariant result; + return NPBrowser::get()->Invoke( + npp, + object.Get(), + NPBrowser::get()->GetStringIdentifier(name), + &args[0], 2, + &result); +} + +template<typename R, typename P0, typename P1> +bool NPInvoke(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name, + P0 p0, P1 p1, + R* r) { + SmartNPVariant args[2]; + args[0].SetValue(p0); + args[1].SetValue(p1); + SmartNPVariant result; + if (NPBrowser::get()->Invoke( + npp, + object.Get(), + NPBrowser::get()->GetStringIdentifier(name), + &args[0], 2, + &result)) { + return result.GetValue(r); + } + return false; +} + +bool NPHasProperty(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name); + +template <typename T> +bool NPGetProperty(NPP npp, + const NPObjectPointer<NPObject>& object, + const NPUTF8* name, + T* value) { + SmartNPVariant result; + if (NPBrowser::get()->GetProperty(npp, + object.Get(), + NPBrowser::get()->GetStringIdentifier(name), + &result)) { + return result.GetValue(value); + } + return false; +} + +template <typename NPObjectType> +NPObjectPointer<NPObjectType> NPCreateObject(NPP npp) { + const NPClass* np_class = BaseNPObject::GetNPClass<NPObjectType>(); + NPObjectType* object = static_cast<NPObjectType*>( + NPBrowser::get()->CreateObject(npp, np_class)); + return NPObjectPointer<NPObjectType>::FromReturned(object); +} + +} // namespace gpu_plugin +} // namespace o3d + +#endif // O3D_GPU_PLUGIN_NP_UTILS_NP_VARIANT_UTILS_H_ diff --git a/o3d/gpu_plugin/np_utils/np_utils_unittest.cc b/o3d/gpu_plugin/np_utils/np_utils_unittest.cc new file mode 100644 index 0000000..9fdeaf9 --- /dev/null +++ b/o3d/gpu_plugin/np_utils/np_utils_unittest.cc @@ -0,0 +1,396 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "o3d/gpu_plugin/np_utils/base_np_object_mock.h" +#include "o3d/gpu_plugin/np_utils/np_browser_stub.h" +#include "o3d/gpu_plugin/np_utils/np_utils.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +using testing::_; +using testing::DoAll; +using testing::MakeMatcher; +using testing::Matcher; +using testing::Pointee; +using testing::Return; +using testing::SetArgumentPointee; +using testing::StrictMock; + +namespace o3d { +namespace gpu_plugin { + +class NPUtilsTest : public testing::Test { + protected: + virtual void SetUp() { + // Make sure no MockBaseNPObject objects exist before test. + ASSERT_EQ(0, MockBaseNPObject::count()); + } + + virtual void TearDown() { + // Make sure no MockBaseNPObject leaked an object. + ASSERT_EQ(0, MockBaseNPObject::count()); + } + + StubNPBrowser stub_browser_; + NPP_t npp_; + NPVariant variant_; +}; + +TEST_F(NPUtilsTest, TestBoolNPVariantToValue) { + bool v; + + BOOLEAN_TO_NPVARIANT(false, variant_); + EXPECT_TRUE(NPVariantToValue(&v, variant_)); + EXPECT_FALSE(v); + + BOOLEAN_TO_NPVARIANT(true, variant_); + EXPECT_TRUE(NPVariantToValue(&v, variant_)); + EXPECT_TRUE(v); + + INT32_TO_NPVARIANT(7, variant_); + EXPECT_FALSE(NPVariantToValue(&v, variant_)); +} + +TEST_F(NPUtilsTest, TestIntNPVariantToValue) { + INT32_TO_NPVARIANT(7, variant_); + + int v1; + EXPECT_TRUE(NPVariantToValue(&v1, variant_)); + EXPECT_EQ(7, v1); + + float v2; + EXPECT_TRUE(NPVariantToValue(&v2, variant_)); + EXPECT_EQ(7.0f, v2); + + double v3; + EXPECT_TRUE(NPVariantToValue(&v3, variant_)); + EXPECT_EQ(7.0, v3); + + BOOLEAN_TO_NPVARIANT(false, variant_); + EXPECT_FALSE(NPVariantToValue(&v1, variant_)); +} + +TEST_F(NPUtilsTest, TestFloatNPVariantToValue) { + float v; + + DOUBLE_TO_NPVARIANT(7.0, variant_); + EXPECT_TRUE(NPVariantToValue(&v, variant_)); + EXPECT_EQ(7.0f, v); + + BOOLEAN_TO_NPVARIANT(false, variant_); + EXPECT_FALSE(NPVariantToValue(&v, variant_)); +} + +TEST_F(NPUtilsTest, TestDoubleNPVariantToValue) { + double v; + + DOUBLE_TO_NPVARIANT(7.0, variant_); + EXPECT_TRUE(NPVariantToValue(&v, variant_)); + EXPECT_EQ(7.0, v); + + BOOLEAN_TO_NPVARIANT(false, variant_); + EXPECT_FALSE(NPVariantToValue(&v, variant_)); +} + +TEST_F(NPUtilsTest, TestStringNPVariantToValue) { + std::string v; + + STRINGZ_TO_NPVARIANT("hello", variant_); + EXPECT_TRUE(NPVariantToValue(&v, variant_)); + EXPECT_EQ(std::string("hello"), v); + + BOOLEAN_TO_NPVARIANT(false, variant_); + EXPECT_FALSE(NPVariantToValue(&v, variant_)); +} + +TEST_F(NPUtilsTest, TestObjectNPVariantToValue) { + NPObjectPointer<NPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL); + NPObjectPointer<NPObject> v; + + OBJECT_TO_NPVARIANT(object.Get(), variant_); + EXPECT_TRUE(NPVariantToValue(&v, variant_)); + EXPECT_EQ(object, v); + + BOOLEAN_TO_NPVARIANT(false, variant_); + EXPECT_FALSE(NPVariantToValue(&v, variant_)); +} + +TEST_F(NPUtilsTest, TestNullNPVariantToValue) { + NPObjectPointer<NPObject> v; + + NULL_TO_NPVARIANT(variant_); + EXPECT_TRUE(NPVariantToValue(&v, variant_)); + EXPECT_TRUE(NPObjectPointer<NPObject>() == v); + + BOOLEAN_TO_NPVARIANT(false, variant_); + EXPECT_FALSE(NPVariantToValue(&v, variant_)); +} + +TEST_F(NPUtilsTest, TestDerivedObjectNPVariantToValue) { + NPObjectPointer<NPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL); + NPObjectPointer<StrictMock<MockBaseNPObject> > v; + + OBJECT_TO_NPVARIANT(object.Get(), variant_); + EXPECT_TRUE(NPVariantToValue(&v, variant_)); + EXPECT_EQ(object, v); +} + +TEST_F(NPUtilsTest, + TestDerivedObjectNPVariantToValueFailsIfValueHasDifferentType) { + NPObjectPointer<NPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL); + NPObjectPointer<MockBaseNPObject> v; + + OBJECT_TO_NPVARIANT(object.Get(), variant_); + EXPECT_FALSE(NPVariantToValue(&v, variant_)); +} + +TEST_F(NPUtilsTest, TestBoolValueToNPVariant) { + ValueToNPVariant(true, &variant_); + EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(variant_)); + EXPECT_TRUE(NPVARIANT_TO_BOOLEAN(variant_)); + + ValueToNPVariant(false, &variant_); + EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(variant_)); + EXPECT_FALSE(NPVARIANT_TO_BOOLEAN(variant_)); +} + +TEST_F(NPUtilsTest, TestIntValueToNPVariant) { + ValueToNPVariant(7, &variant_); + EXPECT_TRUE(NPVARIANT_IS_INT32(variant_)); + EXPECT_EQ(7, NPVARIANT_TO_INT32(variant_)); +} + +TEST_F(NPUtilsTest, TestFloatValueToNPVariant) { + ValueToNPVariant(7.0f, &variant_); + EXPECT_TRUE(NPVARIANT_IS_DOUBLE(variant_)); + EXPECT_EQ(7.0, NPVARIANT_TO_DOUBLE(variant_)); +} + +TEST_F(NPUtilsTest, TestDoubleValueToNPVariant) { + ValueToNPVariant(7.0, &variant_); + EXPECT_TRUE(NPVARIANT_IS_DOUBLE(variant_)); + EXPECT_EQ(7.0, NPVARIANT_TO_DOUBLE(variant_)); +} + +TEST_F(NPUtilsTest, TestStringValueToNPVariant) { + ValueToNPVariant(std::string("hello"), &variant_); + EXPECT_TRUE(NPVARIANT_IS_STRING(variant_)); + EXPECT_EQ(std::string("hello"), + std::string(variant_.value.stringValue.UTF8Characters, + variant_.value.stringValue.UTF8Length)); +} + +TEST_F(NPUtilsTest, TestObjectValueToNPVariant) { + NPObjectPointer<NPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL); + + ValueToNPVariant(object, &variant_); + EXPECT_TRUE(NPVARIANT_IS_OBJECT(variant_)); + EXPECT_EQ(object.Get(), NPVARIANT_TO_OBJECT(variant_)); + + NPBrowser::get()->ReleaseVariantValue(&variant_); +} + +TEST_F(NPUtilsTest, TestNullValueToNPVariant) { + ValueToNPVariant(NPObjectPointer<NPObject>(), &variant_); + EXPECT_TRUE(NPVARIANT_IS_NULL(variant_)); +} + +TEST_F(NPUtilsTest, CanCopyObjectSmartVariant) { + NPObjectPointer<NPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL); + EXPECT_EQ(1, object->referenceCount); + { + SmartNPVariant v1(object); + EXPECT_EQ(2, object->referenceCount); + { + SmartNPVariant v2(v1); + EXPECT_EQ(3, object->referenceCount); + } + EXPECT_EQ(2, object->referenceCount); + } + EXPECT_EQ(1, object->referenceCount); +} + +TEST_F(NPUtilsTest, CanCopyStringSmartVariant) { + SmartNPVariant v1(std::string("hello")); + SmartNPVariant v2(v1); + std::string r; + EXPECT_TRUE(NPVariantToValue(&r, v2)); + EXPECT_EQ(std::string("hello"), r); + EXPECT_NE(v1.value.stringValue.UTF8Characters, + v2.value.stringValue.UTF8Characters); +} + +template <typename T> +class VariantMatcher : public testing::MatcherInterface<const NPVariant&> { + public: + explicit VariantMatcher(const T& value) : value_(value) { + } + + virtual bool Matches(const NPVariant& variant) const { + T other_value; + return NPVariantToValue(&other_value, variant) && other_value == value_; + } + + virtual void DescribeTo(::std::ostream* os) const { + *os << "equals " << value_; + } + + virtual void DescribeNegationTo(::std::ostream* os) const { + *os << "does not equal " << value_; + } + + private: + T value_; +}; + +template <typename T> +Matcher<const NPVariant&> VariantMatches(const T& value) { + return MakeMatcher(new VariantMatcher<T>(value)); +} + +TEST_F(NPUtilsTest, CanDetermineIfObjectHasMethod) { + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("foo"); + NPObjectPointer<MockBaseNPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL); + + EXPECT_CALL(*object, HasMethod(name)) + .WillOnce(Return(true)); + + EXPECT_TRUE(NPHasMethod(NULL, object, "foo")); +} + +TEST_F(NPUtilsTest, CanInvokeVoidMethodWithNativeTypes) { + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("foo"); + NPObjectPointer<MockBaseNPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL); + + VOID_TO_NPVARIANT(variant_); + + EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _)) + .WillOnce(DoAll(SetArgumentPointee<3>(variant_), + Return(true))); + + EXPECT_TRUE(NPInvokeVoid(NULL, object, "foo", 7)); +} + +TEST_F(NPUtilsTest, InvokeVoidMethodCanFail) { + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("foo"); + NPObjectPointer<MockBaseNPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL); + + VOID_TO_NPVARIANT(variant_); + + EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _)) + .WillOnce(DoAll(SetArgumentPointee<3>(variant_), + Return(false))); + + EXPECT_FALSE(NPInvokeVoid(NULL, object, "foo", 7)); +} + +TEST_F(NPUtilsTest, CanInvokeNonVoidMethodWithNativeTypes) { + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("foo"); + NPObjectPointer<MockBaseNPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL);; + + DOUBLE_TO_NPVARIANT(1.5, variant_); + + EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _)) + .WillOnce(DoAll(SetArgumentPointee<3>(variant_), + Return(true))); + + double r; + EXPECT_TRUE(NPInvoke(NULL, object, "foo", 7, &r)); + EXPECT_EQ(1.5, r); +} + +TEST_F(NPUtilsTest, InvokeNonVoidMethodCanFail) { + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("foo"); + NPObjectPointer<MockBaseNPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL);; + + DOUBLE_TO_NPVARIANT(1.5, variant_); + + EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _)) + .WillOnce(DoAll(SetArgumentPointee<3>(variant_), + Return(false))); + + double r; + EXPECT_FALSE(NPInvoke(NULL, object, "foo", 7, &r)); +} + +TEST_F(NPUtilsTest, InvokeNonVoidMethodFailsIfResultIsIncompatible) { + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("foo"); + NPObjectPointer<MockBaseNPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL);; + + DOUBLE_TO_NPVARIANT(1.5, variant_); + + EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _)) + .WillOnce(DoAll(SetArgumentPointee<3>(variant_), + Return(true))); + + int r; + EXPECT_FALSE(NPInvoke(NULL, object, "foo", 7, &r)); +} + +TEST_F(NPUtilsTest, CanDetermineIfObjectHasProperty) { + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("foo"); + NPObjectPointer<MockBaseNPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL); + + EXPECT_CALL(*object, HasProperty(name)) + .WillOnce(Return(true)); + + EXPECT_TRUE(NPHasProperty(NULL, object, "foo")); +} + +TEST_F(NPUtilsTest, CanGetPropertyValue) { + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("foo"); + NPObjectPointer<MockBaseNPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL);; + + DOUBLE_TO_NPVARIANT(1.5, variant_); + + EXPECT_CALL(*object, GetProperty(name, _)) + .WillOnce(DoAll(SetArgumentPointee<1>(variant_), + Return(true))); + + double r; + EXPECT_TRUE(NPGetProperty(NULL, object, "foo", &r)); +} + +TEST_F(NPUtilsTest, ReportsFailureIfResultTypeIsDifferent) { + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("foo"); + NPObjectPointer<MockBaseNPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL);; + + DOUBLE_TO_NPVARIANT(1.5, variant_); + + EXPECT_CALL(*object, GetProperty(name, _)) + .WillOnce(DoAll(SetArgumentPointee<1>(variant_), + Return(true))); + + bool r; + EXPECT_FALSE(NPGetProperty(NULL, object, "foo", &r)); +} + +TEST_F(NPUtilsTest, ReportsFailureFromGetProperty) { + NPIdentifier name = NPBrowser::get()->GetStringIdentifier("foo"); + NPObjectPointer<MockBaseNPObject> object = + NPCreateObject<StrictMock<MockBaseNPObject> >(NULL);; + + EXPECT_CALL(*object, GetProperty(name, _)) + .WillOnce(Return(false)); + + double r; + EXPECT_FALSE(NPGetProperty(NULL, object, "foo", &r)); +} + +} // namespace gpu_plugin +} // namespace o3d diff --git a/o3d/gpu_plugin/np_utils/np_variant_utils.h b/o3d/gpu_plugin/np_utils/np_variant_utils.h deleted file mode 100644 index f0f1c6a..0000000 --- a/o3d/gpu_plugin/np_utils/np_variant_utils.h +++ /dev/null @@ -1,143 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_VARIANT_UTILS_H_ -#define O3D_GPU_PLUGIN_NP_UTILS_NP_VARIANT_UTILS_H_ - -#include <string> - -#include "o3d/gpu_plugin/np_utils/npn_funcs.h" -#include "third_party/npapi/bindings/npapi.h" -#include "third_party/npapi/bindings/npruntime.h" - -namespace o3d { -namespace gpu_plugin { - -// Convert NPVariant to C++ type. Returns whether the conversion was successful. -bool NPVariantToValue(bool* value, const NPVariant& variant); -bool NPVariantToValue(int* value, const NPVariant& variant); -bool NPVariantToValue(float* value, const NPVariant& variant); -bool NPVariantToValue(double* value, const NPVariant& variant); -bool NPVariantToValue(std::string* value, const NPVariant& variant); -bool NPVariantToValue(NPObject** value, const NPVariant& variant); - -// Convert C++ type to NPVariant. -void ValueToNPVariant(bool value, NPVariant* variant); -void ValueToNPVariant(int value, NPVariant* variant); -void ValueToNPVariant(float value, NPVariant* variant); -void ValueToNPVariant(double value, NPVariant* variant); -void ValueToNPVariant(const std::string& value, NPVariant* variant); -void ValueToNPVariant(NPObject* value, NPVariant* variant); - -// NPVariant that autmatically releases in destructor. -class SmartNPVariant { - public: - SmartNPVariant() { - VOID_TO_NPVARIANT(variant_); - } - - ~SmartNPVariant() { - Release(); - } - - template <typename T> - bool GetValue(T* v) const { - return NPVariantToValue(v, variant_); - } - - template <typename T> - void SetValue(const T& v) { - ValueToNPVariant(v, &variant_); - } - - void Release() { - gpu_plugin::NPN_ReleaseVariantValue(&variant_); - VOID_TO_NPVARIANT(variant_); - } - - NPVariant& GetVariant() { - return variant_; - } - - const NPVariant& GetVariant() const { - return variant_; - } - - private: - NPVariant variant_; - DISALLOW_COPY_AND_ASSIGN(SmartNPVariant); -}; - -// These allow a method to be invoked with automatic conversion of C++ -// types to variants for arguments and return values. - -inline bool NPInvokeVoid(NPP npp, NPObject* object, NPIdentifier name) { - SmartNPVariant result; - return gpu_plugin::NPN_Invoke(npp, object, name, NULL, 0, - &result.GetVariant()); -} - -template<typename R> -bool NPInvoke(NPP npp, NPObject* object, NPIdentifier name, - R* r) { - SmartNPVariant result; - if (NPN_Invoke(npp, object, name, NULL, 0, - &result.GetVariant())) { - return result.GetValue(r); - } - return false; -} - -template<typename P0> -bool NPInvokeVoid(NPP npp, NPObject* object, NPIdentifier name, - P0 p0) { - SmartNPVariant args[1]; - args[0].SetValue(p0); - SmartNPVariant result; - return gpu_plugin::NPN_Invoke(npp, object, name, &args[0].GetVariant(), 1, - &result.GetVariant()); -} - -template<typename R, typename P0> -bool NPInvoke(NPP npp, NPObject* object, NPIdentifier name, - P0 p0, R* r) { - SmartNPVariant args[1]; - args[0].SetValue(p0); - SmartNPVariant result; - if (gpu_plugin::NPN_Invoke(npp, object, name, &args[0].GetVariant(), 1, - &result.GetVariant())) { - return result.GetValue(r); - } - return false; -} - -template<typename P0, typename P1> -bool NPInvokeVoid(NPP npp, NPObject* object, NPIdentifier name, - P0 p0, P1 p1) { - SmartNPVariant args[2]; - args[0].SetValue(p0); - args[1].SetValue(p1); - SmartNPVariant result; - return gpu_plugin::NPN_Invoke(npp, object, name, &args[0].GetVariant(), 2, - &result.GetVariant()); -} - -template<typename R, typename P0, typename P1> -bool NPInvoke(NPP npp, NPObject* object, NPIdentifier name, - P0 p0, P1 p1, R* r) { - SmartNPVariant args[2]; - args[0].SetValue(p0); - args[1].SetValue(p1); - SmartNPVariant result; - if (gpu_plugin::NPN_Invoke(npp, object, name, &args[0].GetVariant(), 2, - &result.GetVariant())) { - return result.GetValue(r); - } - return false; -} - -} // namespace gpu_plugin -} // namespace o3d - -#endif // O3D_GPU_PLUGIN_NP_UTILS_NP_VARIANT_UTILS_H_ diff --git a/o3d/gpu_plugin/np_utils/np_variant_utils_unittest.cc b/o3d/gpu_plugin/np_utils/np_variant_utils_unittest.cc deleted file mode 100644 index 4e47d22..0000000 --- a/o3d/gpu_plugin/np_utils/np_variant_utils_unittest.cc +++ /dev/null @@ -1,304 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "o3d/gpu_plugin/np_utils/base_np_object_mock.h" -#include "o3d/gpu_plugin/np_utils/np_variant_utils.h" -#include "o3d/gpu_plugin/np_utils/npn_test_stub.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -using testing::_; -using testing::DoAll; -using testing::MakeMatcher; -using testing::Matcher; -using testing::Pointee; -using testing::Return; -using testing::SetArgumentPointee; -using testing::StrictMock; - -namespace o3d { -namespace gpu_plugin { - -class NPVariantUtilsTest : public testing::Test { - protected: - virtual void SetUp() { - InitializeNPNTestStub(); - - np_class_ = const_cast<NPClass*>( - BaseNPObject::GetNPClass<StrictMock<MockBaseNPObject> >()); - - // Make sure no MockBaseNPObject objects exist before test. - ASSERT_EQ(0, MockBaseNPObject::count()); - } - - virtual void TearDown() { - // Make sure no MockBaseNPObject leaked an object. - ASSERT_EQ(0, MockBaseNPObject::count()); - - ShutdownNPNTestStub(); - } - - NPP_t npp_; - NPClass* np_class_; - NPVariant variant_; -}; - -TEST_F(NPVariantUtilsTest, TestBoolNPVariantToValue) { - bool v; - - BOOLEAN_TO_NPVARIANT(false, variant_); - EXPECT_TRUE(NPVariantToValue(&v, variant_)); - EXPECT_FALSE(v); - - BOOLEAN_TO_NPVARIANT(true, variant_); - EXPECT_TRUE(NPVariantToValue(&v, variant_)); - EXPECT_TRUE(v); - - INT32_TO_NPVARIANT(7, variant_); - EXPECT_FALSE(NPVariantToValue(&v, variant_)); -} - -TEST_F(NPVariantUtilsTest, TestIntNPVariantToValue) { - INT32_TO_NPVARIANT(7, variant_); - - int v1; - EXPECT_TRUE(NPVariantToValue(&v1, variant_)); - EXPECT_EQ(7, v1); - - float v2; - EXPECT_TRUE(NPVariantToValue(&v2, variant_)); - EXPECT_EQ(7.0f, v2); - - double v3; - EXPECT_TRUE(NPVariantToValue(&v3, variant_)); - EXPECT_EQ(7.0, v3); - - BOOLEAN_TO_NPVARIANT(false, variant_); - EXPECT_FALSE(NPVariantToValue(&v1, variant_)); -} - -TEST_F(NPVariantUtilsTest, TestFloatNPVariantToValue) { - float v; - - DOUBLE_TO_NPVARIANT(7.0, variant_); - EXPECT_TRUE(NPVariantToValue(&v, variant_)); - EXPECT_EQ(7.0f, v); - - BOOLEAN_TO_NPVARIANT(false, variant_); - EXPECT_FALSE(NPVariantToValue(&v, variant_)); -} - -TEST_F(NPVariantUtilsTest, TestDoubleNPVariantToValue) { - double v; - - DOUBLE_TO_NPVARIANT(7.0, variant_); - EXPECT_TRUE(NPVariantToValue(&v, variant_)); - EXPECT_EQ(7.0, v); - - BOOLEAN_TO_NPVARIANT(false, variant_); - EXPECT_FALSE(NPVariantToValue(&v, variant_)); -} - -TEST_F(NPVariantUtilsTest, TestStringNPVariantToValue) { - std::string v; - - STRINGZ_TO_NPVARIANT("hello", variant_); - EXPECT_TRUE(NPVariantToValue(&v, variant_)); - EXPECT_EQ(std::string("hello"), v); - - BOOLEAN_TO_NPVARIANT(false, variant_); - EXPECT_FALSE(NPVariantToValue(&v, variant_)); -} - -TEST_F(NPVariantUtilsTest, TestObjectNPVariantToValue) { - NPObject* object = gpu_plugin::NPN_CreateObject(NULL, np_class_); - NPObject* v; - - OBJECT_TO_NPVARIANT(object, variant_); - EXPECT_TRUE(NPVariantToValue(&v, variant_)); - EXPECT_EQ(object, v); - - BOOLEAN_TO_NPVARIANT(false, variant_); - EXPECT_FALSE(NPVariantToValue(&v, variant_)); - - gpu_plugin::NPN_ReleaseObject(object); -} - -TEST_F(NPVariantUtilsTest, TestNullNPVariantToValue) { - NPObject* v; - - NULL_TO_NPVARIANT(variant_); - EXPECT_TRUE(NPVariantToValue(&v, variant_)); - EXPECT_TRUE(NULL == v); - - BOOLEAN_TO_NPVARIANT(false, variant_); - EXPECT_FALSE(NPVariantToValue(&v, variant_)); -} - -TEST_F(NPVariantUtilsTest, TestBoolValueToNPVariant) { - ValueToNPVariant(true, &variant_); - EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(variant_)); - EXPECT_TRUE(NPVARIANT_TO_BOOLEAN(variant_)); - - ValueToNPVariant(false, &variant_); - EXPECT_TRUE(NPVARIANT_IS_BOOLEAN(variant_)); - EXPECT_FALSE(NPVARIANT_TO_BOOLEAN(variant_)); -} - -TEST_F(NPVariantUtilsTest, TestIntValueToNPVariant) { - ValueToNPVariant(7, &variant_); - EXPECT_TRUE(NPVARIANT_IS_INT32(variant_)); - EXPECT_EQ(7, NPVARIANT_TO_INT32(variant_)); -} - -TEST_F(NPVariantUtilsTest, TestFloatValueToNPVariant) { - ValueToNPVariant(7.0f, &variant_); - EXPECT_TRUE(NPVARIANT_IS_DOUBLE(variant_)); - EXPECT_EQ(7.0, NPVARIANT_TO_DOUBLE(variant_)); -} - -TEST_F(NPVariantUtilsTest, TestDoubleValueToNPVariant) { - ValueToNPVariant(7.0, &variant_); - EXPECT_TRUE(NPVARIANT_IS_DOUBLE(variant_)); - EXPECT_EQ(7.0, NPVARIANT_TO_DOUBLE(variant_)); -} - -TEST_F(NPVariantUtilsTest, TestStringValueToNPVariant) { - ValueToNPVariant(std::string("hello"), &variant_); - EXPECT_TRUE(NPVARIANT_IS_STRING(variant_)); - EXPECT_EQ(std::string("hello"), - std::string(variant_.value.stringValue.UTF8Characters, - variant_.value.stringValue.UTF8Length)); -} - -TEST_F(NPVariantUtilsTest, TestObjectValueToNPVariant) { - NPObject* object = gpu_plugin::NPN_CreateObject(NULL, np_class_); - - ValueToNPVariant(object, &variant_); - EXPECT_TRUE(NPVARIANT_IS_OBJECT(variant_)); - EXPECT_EQ(object, NPVARIANT_TO_OBJECT(variant_)); - - gpu_plugin::NPN_ReleaseVariantValue(&variant_); - gpu_plugin::NPN_ReleaseObject(object); -} - -TEST_F(NPVariantUtilsTest, TestNullValueToNPVariant) { - ValueToNPVariant(static_cast<NPObject*>(NULL), &variant_); - EXPECT_TRUE(NPVARIANT_IS_NULL(variant_)); -} - -template <typename T> -class VariantMatcher : public testing::MatcherInterface<const NPVariant&> { - public: - explicit VariantMatcher(const T& value) : value_(value) { - } - - virtual bool Matches(const NPVariant& variant) const { - T other_value; - return NPVariantToValue(&other_value, variant) && other_value == value_; - } - - virtual void DescribeTo(::std::ostream* os) const { - *os << "equals " << value_; - } - - virtual void DescribeNegationTo(::std::ostream* os) const { - *os << "does not equal " << value_; - } - - private: - T value_; -}; - -template <typename T> -Matcher<const NPVariant&> VariantMatches(const T& value) { - return MakeMatcher(new VariantMatcher<T>(value)); -} - -TEST_F(NPVariantUtilsTest, CanInvokeVoidMethodWithNativeTypes) { - NPIdentifier name = NPN_GetStringIdentifier("foo"); - MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - gpu_plugin::NPN_CreateObject(NULL, np_class_)); - - VOID_TO_NPVARIANT(variant_); - - EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _)) - .WillOnce(DoAll(SetArgumentPointee<3>(variant_), - Return(true))); - - EXPECT_TRUE(NPInvokeVoid(NULL, object, name, 7)); - - gpu_plugin::NPN_ReleaseObject(object); -} - -TEST_F(NPVariantUtilsTest, InvokeVoidMethodCanFail) { - NPIdentifier name = NPN_GetStringIdentifier("foo"); - MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - gpu_plugin::NPN_CreateObject(NULL, np_class_)); - - VOID_TO_NPVARIANT(variant_); - - EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _)) - .WillOnce(DoAll(SetArgumentPointee<3>(variant_), - Return(false))); - - EXPECT_FALSE(NPInvokeVoid(NULL, object, name, 7)); - - gpu_plugin::NPN_ReleaseObject(object); -} - -TEST_F(NPVariantUtilsTest, CanInvokeNonVoidMethodWithNativeTypes) { - NPIdentifier name = NPN_GetStringIdentifier("foo"); - MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - gpu_plugin::NPN_CreateObject(NULL, np_class_)); - - DOUBLE_TO_NPVARIANT(1.5, variant_); - - EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _)) - .WillOnce(DoAll(SetArgumentPointee<3>(variant_), - Return(true))); - - double r; - EXPECT_TRUE(NPInvoke(NULL, object, name, 7, &r)); - EXPECT_EQ(1.5, r); - - gpu_plugin::NPN_ReleaseObject(object); -} - -TEST_F(NPVariantUtilsTest, InvokeNonVoidMethodCanFail) { - NPIdentifier name = NPN_GetStringIdentifier("foo"); - MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - gpu_plugin::NPN_CreateObject(NULL, np_class_)); - - DOUBLE_TO_NPVARIANT(1.5, variant_); - - EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _)) - .WillOnce(DoAll(SetArgumentPointee<3>(variant_), - Return(false))); - - double r; - EXPECT_FALSE(NPInvoke(NULL, object, name, 7, &r)); - - gpu_plugin::NPN_ReleaseObject(object); -} - -TEST_F(NPVariantUtilsTest, InvokeNonVoidMethodFailsIfResultIsIncompatible) { - NPIdentifier name = NPN_GetStringIdentifier("foo"); - MockBaseNPObject* object = static_cast<MockBaseNPObject*>( - gpu_plugin::NPN_CreateObject(NULL, np_class_)); - - DOUBLE_TO_NPVARIANT(1.5, variant_); - - EXPECT_CALL(*object, Invoke(name, Pointee(VariantMatches<int>(7)), 1, _)) - .WillOnce(DoAll(SetArgumentPointee<3>(variant_), - Return(true))); - - int r; - EXPECT_FALSE(NPInvoke(NULL, object, name, 7, &r)); - - gpu_plugin::NPN_ReleaseObject(object); -} - -} // namespace gpu_plugin -} // namespace o3d diff --git a/o3d/gpu_plugin/np_utils/npn_test_stub.cc b/o3d/gpu_plugin/np_utils/npn_test_stub.cc deleted file mode 100644 index f217711..0000000 --- a/o3d/gpu_plugin/np_utils/npn_test_stub.cc +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include <stdlib.h> - -#include <set> -#include <string> - -#include "o3d/gpu_plugin/np_utils/npn_funcs.h" -#include "o3d/gpu_plugin/np_utils/npn_test_stub.h" -#include "webkit/glue/plugins/nphostapi.h" - -// Simple implementation of subset of the NPN functions for testing. - -namespace o3d { -namespace gpu_plugin { - -namespace { -std::set<std::string> names; - -NPIdentifier GetStringIdentifier(const NPUTF8* name) { - std::set<std::string>::iterator it = names.find(name); - if (it == names.end()) { - it = names.insert(name).first; - } - return const_cast<NPUTF8*>((*it).c_str()); -} - -void* MemAlloc(size_t size) { - return malloc(size); -} - -void MemFree(void* p) { - free(p); -} - -NPObject* CreateObject(NPP npp, NPClass* cl) { - NPObject* object = cl->allocate(npp, cl); - object->referenceCount = 1; - object->_class = cl; - return object; -} - -NPObject* RetainObject(NPObject* object) { - ++object->referenceCount; - return object; -} - -void ReleaseObject(NPObject* object) { - --object->referenceCount; - if (object->referenceCount == 0) { - object->_class->deallocate(object); - } -} - -void ReleaseVariantValue(NPVariant* variant) { - if (NPVARIANT_IS_STRING(*variant)) { - NPN_MemFree(const_cast<NPUTF8*>(variant->value.stringValue.UTF8Characters)); - } else if (NPVARIANT_IS_OBJECT(*variant)) { - gpu_plugin::NPN_ReleaseObject(NPVARIANT_TO_OBJECT(*variant)); - } -} - -bool Invoke(NPP npp, NPObject* object, NPIdentifier name, - const NPVariant* args, uint32_t num_args, NPVariant* result) { - return object->_class->invoke(object, name, args, num_args, result); -} -} // namespace anonymous - -void InitializeNPNTestStub() { - static NPNetscapeFuncs funcs = { - sizeof(NPNetscapeFuncs), - NP_VERSION_MINOR, - NULL, // geturl - NULL, // posturl - NULL, // requestread - NULL, // newstream - NULL, // write - NULL, // destroystream - NULL, // status - NULL, // uagent - MemAlloc, - MemFree, - NULL, // memflush - NULL, // reloadplugins - NULL, // getJavaEnv - NULL, // getJavaPeer - NULL, // geturlnotify - NULL, // posturlnotify - NULL, // getvalue - NULL, // setvalue - NULL, // invalidaterect - NULL, // invalidateregion - NULL, // forceredraw - GetStringIdentifier, - NULL, // getstringidentifiers - NULL, // getintidentifier - NULL, // identifierisstring - NULL, // utf8fromidentifier - NULL, // intfromidentifier - CreateObject, - RetainObject, - ReleaseObject, - Invoke, - NULL, // invokeDefault - NULL, // evaluate - NULL, // getproperty - NULL, // setproperty - NULL, // removeproperty - NULL, // hasproperty - NULL, // hasmethod - ReleaseVariantValue, - }; - SetBrowserFuncs(&funcs); -} - -void ShutdownNPNTestStub() { - SetBrowserFuncs(NULL); -} - -} // namespace gpu_plugin -} // namespace o3d diff --git a/o3d/gpu_plugin/np_utils/npn_test_stub.h b/o3d/gpu_plugin/np_utils/npn_test_stub.h deleted file mode 100644 index 6b07416..0000000 --- a/o3d/gpu_plugin/np_utils/npn_test_stub.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef O3D_GPU_PLUGIN_NP_UTILS_NPN_TEST_STUB_H_ -#define O3D_GPU_PLUGIN_NP_UTILS_NPN_TEST_STUB_H_ - -#include "third_party/npapi/bindings/npapi.h" -#include "third_party/npapi/bindings/npruntime.h" - -namespace o3d { -namespace gpu_plugin { - -// Simple implementation of subset of the NPN functions for testing. -void InitializeNPNTestStub(); -void ShutdownNPNTestStub(); - -} // namespace gpu_plugin -} // namespace o3d - -#endif // O3D_GPU_PLUGIN_NP_UTILS_NPN_TEST_STUB_H_ diff --git a/o3d/gpu_plugin/plugin_object_factory.cc b/o3d/gpu_plugin/plugin_object_factory.cc deleted file mode 100644 index 5165f0d..0000000 --- a/o3d/gpu_plugin/plugin_object_factory.cc +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "o3d/gpu_plugin/gpu_plugin_object.h" -#include "o3d/gpu_plugin/plugin_object_factory.h" - -namespace o3d { -namespace gpu_plugin { - -PluginObjectFactory::PluginObjectFactory() { -} - -PluginObjectFactory::~PluginObjectFactory() { -} - -PluginObject* PluginObjectFactory::CreatePluginObject(NPP npp, - NPMIMEType plugin_type) { - if (strcmp(plugin_type, GPUPluginObject::kPluginType) == 0) { - NPClass* np_class = const_cast<NPClass*>( - BaseNPObject::GetNPClass<GPUPluginObject>()); - return static_cast<GPUPluginObject*>( - gpu_plugin::NPN_CreateObject(npp, np_class)); - } - - return NULL; -} - -} // namespace gpu_plugin -} // namespace o3d diff --git a/o3d/gpu_plugin/plugin_object_factory.h b/o3d/gpu_plugin/plugin_object_factory.h deleted file mode 100644 index 3d10866..0000000 --- a/o3d/gpu_plugin/plugin_object_factory.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef O3D_GPU_PLUGIN_PLUGIN_OBJECT_FACTORY_H_ -#define O3D_GPU_PLUGIN_PLUGIN_OBJECT_FACTORY_H_ - -#include "third_party/npapi/bindings/npapi.h" -#include "third_party/npapi/bindings/npruntime.h" - -namespace o3d { -namespace gpu_plugin { - -class PluginObject; - -// Mockable factory used to create instances of PluginObject based on plugin -// mime type. -class PluginObjectFactory { - public: - PluginObjectFactory(); - virtual ~PluginObjectFactory(); - - virtual PluginObject* CreatePluginObject(NPP npp, NPMIMEType plugin_type); - - private: - DISALLOW_COPY_AND_ASSIGN(PluginObjectFactory); -}; - -} // namespace gpu_plugin -} // namespace o3d - -#endif // O3D_GPU_PLUGIN_PLUGIN_OBJECT_FACTORY_H_ |