/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */ /* this file is part of evince, a gnome document viewer * * Copyright (C) 2009 litl, LLC * * Evince is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Evince is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ #include #include "plugin.h" #include // FIXME: attempts to call methods are resulting in // Error: NPMethod called on non-NPObject wrapped JSObject! #define CAN_CALL_METHODS 0 void ScriptablePluginObjectBase::Invalidate () { } bool ScriptablePluginObjectBase::HasMethod (NPIdentifier name) { return false; } bool ScriptablePluginObjectBase::Invoke (NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) { return false; } bool ScriptablePluginObjectBase::InvokeDefault (const NPVariant *args, uint32_t argCount, NPVariant *result) { return false; } bool ScriptablePluginObjectBase::HasProperty (NPIdentifier name) { return false; } bool ScriptablePluginObjectBase::GetProperty (NPIdentifier name, NPVariant *result) { return false; } bool ScriptablePluginObjectBase::SetProperty (NPIdentifier name, const NPVariant *value) { return false; } bool ScriptablePluginObjectBase::RemoveProperty (NPIdentifier name) { return false; } bool ScriptablePluginObjectBase::Enumerate (NPIdentifier **identifier, uint32_t *count) { return false; } bool ScriptablePluginObjectBase::Construct (const NPVariant *args, uint32_t argCount, NPVariant *result) { return false; } void ScriptablePluginObjectBase::_Deallocate (NPObject *npobj) { delete (ScriptablePluginObjectBase *)npobj; } void ScriptablePluginObjectBase::_Invalidate (NPObject *npobj) { ((ScriptablePluginObjectBase *)npobj)->Invalidate (); } bool ScriptablePluginObjectBase::_HasMethod (NPObject *npobj, NPIdentifier name) { return ((ScriptablePluginObjectBase *)npobj)->HasMethod (name); } bool ScriptablePluginObjectBase::_Invoke (NPObject *npobj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) { return ((ScriptablePluginObjectBase *)npobj)->Invoke ( name, args, argCount, result); } bool ScriptablePluginObjectBase::_InvokeDefault (NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result) { return ((ScriptablePluginObjectBase *)npobj)->InvokeDefault ( args, argCount, result); } bool ScriptablePluginObjectBase::_HasProperty (NPObject * npobj, NPIdentifier name) { return ((ScriptablePluginObjectBase *)npobj)->HasProperty (name); } bool ScriptablePluginObjectBase::_GetProperty (NPObject *npobj, NPIdentifier name, NPVariant *result) { return ((ScriptablePluginObjectBase *)npobj)->GetProperty (name, result); } bool ScriptablePluginObjectBase::_SetProperty (NPObject *npobj, NPIdentifier name, const NPVariant *value) { return ((ScriptablePluginObjectBase *)npobj)->SetProperty (name, value); } bool ScriptablePluginObjectBase::_RemoveProperty (NPObject *npobj, NPIdentifier name) { return ((ScriptablePluginObjectBase *)npobj)->RemoveProperty (name); } bool ScriptablePluginObjectBase::_Enumerate (NPObject *npobj, NPIdentifier **identifier, uint32_t *count) { return ((ScriptablePluginObjectBase *)npobj)->Enumerate(identifier, count); } bool ScriptablePluginObjectBase::_Construct (NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result) { return ((ScriptablePluginObjectBase *)npobj)->Construct (args, argCount, result); } NPObject * AllocateConstructablePluginObject (NPP npp, NPClass *aClass) { return new ConstructablePluginObject (npp); } bool ConstructablePluginObject::Construct (const NPVariant *args, uint32_t argCount, NPVariant *result) { NPObject *myobj = NPN_CreateObject ( mNpp, GET_NPOBJECT_CLASS (ConstructablePluginObject)); if (!myobj) { return false; } OBJECT_TO_NPVARIANT (myobj, *result); return true; } NPObject * AllocateScriptablePluginObject (NPP npp, NPClass *aClass) { return new ScriptablePluginObject (npp); } ScriptablePluginObject::ScriptablePluginObject (NPP npp) : ScriptablePluginObjectBase (npp), mMagicWrapper(0) { mTitleID = NPN_GetStringIdentifier ("title"); mZoomID = NPN_GetStringIdentifier ("zoom"); mCanCopyID = NPN_GetStringIdentifier ("canCopy"); mCanFindOnPageID = NPN_GetStringIdentifier ("canFindOnPage"); mMagicWrapperID = NPN_GetStringIdentifier ("magicWrapper"); mZoomInID = NPN_GetStringIdentifier ("zoomIn"); mZoomOutID = NPN_GetStringIdentifier ("zoomOut"); mFindTermsID = NPN_GetStringIdentifier ("findTerms"); mFindPreviousID = NPN_GetStringIdentifier ("findPrevious"); mFindNextID = NPN_GetStringIdentifier ("findNext"); } bool ScriptablePluginObject::HasMethod (NPIdentifier name) { #if CAN_CALL_METHODS NPIdentifier methods[] = { mZoomInID, mZoomOutID, mFindPreviousID, mFindNextID }; for (guint i = 0; i < G_N_ELEMENTS(methods); i++) { if (name == methods[i]) { return true; } } #endif return false; } bool ScriptablePluginObject::HasProperty (NPIdentifier name) { NPIdentifier properties[] = { #if !CAN_CALL_METHODS mFindTermsID, mFindNextID, mFindPreviousID, #endif mTitleID, mZoomID, mCanCopyID, mCanFindOnPageID, mMagicWrapperID }; for (guint i = 0; i < G_N_ELEMENTS(properties); i++) { if (name == properties[i]) { return true; } } return false; } bool ScriptablePluginObject::GetProperty (NPIdentifier name, NPVariant *result) { Plugin *plugin = reinterpret_cast (mNpp->pdata); if (name == mTitleID) { if (plugin->mTitle) { // STRINGZ_TO_NPVARIANT evaluates its argument twice.. char *np_string = NPN_StrDup (plugin->mTitle); STRINGZ_TO_NPVARIANT (np_string, *result); } else { NULL_TO_NPVARIANT (*result); } return true; } else if (name == mZoomID) { DOUBLE_TO_NPVARIANT (plugin->GetZoom(), *result); return true; } else if (name == mCanCopyID) { BOOLEAN_TO_NPVARIANT (plugin->mHasSelection, *result); return true; } else if (name == mCanFindOnPageID) { BOOLEAN_TO_NPVARIANT (plugin->mCanFindOnPage, *result); return true; } return false; } bool ScriptablePluginObject::SetProperty (NPIdentifier name, const NPVariant *value) { Plugin *plugin = reinterpret_cast (mNpp->pdata); if (name == mMagicWrapperID) { NPObject *newWrapper = NULL; if (NPVARIANT_IS_OBJECT (*value)) { newWrapper = NPVARIANT_TO_OBJECT (*value); } else if (NPVARIANT_IS_VOID (*value) || NPVARIANT_IS_NULL (*value)) { // ok, newWrapper=NULL } else { NPN_SetException (this, "Invalid type for magicWrapper, expected object or null"); return false; } if (newWrapper) { NPN_RetainObject (newWrapper); } if (mMagicWrapper) { NPN_ReleaseObject (mMagicWrapper); } mMagicWrapper = newWrapper; if (plugin->mTitle) { CallBrowser ("onTitleChanged"); } return true; } else if (name == mZoomID) { double zoom; if (NPVARIANT_IS_DOUBLE (*value)) { zoom = NPVARIANT_TO_DOUBLE (*value); } else if (NPVARIANT_IS_INT32 (*value)) { zoom = NPVARIANT_TO_INT32 (*value); } else { NPN_SetException (this, "Invalid type for zoom, expected a number"); return false; } plugin->SetZoom (zoom); return true; } else if (name == mCanCopyID) { // FIXME fix method calls (using write access to a property as // an awful workaround) if (!plugin->mHasSelection) { NPN_SetException (this, "Nothing to copy"); return false; } plugin->CopyClipboard (); return true; #if !CAN_CALL_METHODS } else if (name == mFindTermsID) { if (!NPVARIANT_IS_STRING (*value)) { NPN_SetException (this, "Invalid type for findTerms, expected a string"); return false; } plugin->Find (NPVARIANT_TO_STRING (*value).utf8characters); return true; } else if (name == mFindNextID) { plugin->FindNext (); return true; } else if (name == mFindPreviousID) { plugin->FindPrevious (); return true; #endif } return false; } void ScriptablePluginObject::CallBrowser (const char *function_name) { if (!mMagicWrapper) { return; } NPVariant rval; if (NPERR_NO_ERROR == NPN_Invoke (mNpp, mMagicWrapper, NPN_GetStringIdentifier (function_name), NULL, 0, &rval)) { NPN_ReleaseVariantValue (&rval); } } bool ScriptablePluginObject::Invoke (NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) { Plugin *plugin = reinterpret_cast (mNpp->pdata); if (name == mZoomInID) { plugin->ZoomIn (); return true; } else if (name == mZoomOutID) { plugin->ZoomOut (); return true; #if CAN_CALL_METHODS } else if (name == mFindNextID) { if (argCount < 1 || !NPVARIANT_IS_STRING (args[0])) { return false; } plugin->FindNext (NPVARIANT_TO_STRING (args[0]).utf8characters); return true; } else if (name == mFindPreviousID) { if (argCount < 1 || !NPVARIANT_IS_STRING (args[0])) { return false; } plugin->FindPrevious(NPVARIANT_TO_STRING (args[0]).utf8characters); return true; #endif } return false; } bool ScriptablePluginObject::InvokeDefault (const NPVariant *args, uint32_t argCount, NPVariant *result) { return false; }