From 4ef6544ecca1803b48985882b1e0e769257b1c93 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 13 Mar 2009 20:59:34 +0000 Subject: Add a browser plugin implementation. --- (limited to 'browser-plugin') diff --git a/browser-plugin/Makefile.am b/browser-plugin/Makefile.am new file mode 100644 index 0000000..cbf7674 --- /dev/null +++ b/browser-plugin/Makefile.am @@ -0,0 +1,22 @@ +plugindir = ${libdir}/mozilla/plugins +plugin_LTLIBRARIES = libevince-plugin.la + +libevince_plugin_la_SOURCES = \ + glue.cpp \ + plugin.cpp \ + plugin.h \ + scriptable.cpp \ + scriptable.h + +libevince_plugin_la_CPPFLAGS = \ + -I$(top_srcdir)/libview \ + -DXP_UNIX \ + -DMOZ_X11 \ + $(BROWSER_PLUGIN_CFLAGS) \ + $(AM_CPPFLAGS) + +libevince_plugin_la_LIBADD = \ + $(top_builddir)/libview/libevview.la \ + $(BROWSER_PLUGIN_LIBS) + +libevince_plugin_la_LDFLAGS = -avoid-version -module diff --git a/browser-plugin/glue.cpp b/browser-plugin/glue.cpp new file mode 100644 index 0000000..f7df522 --- /dev/null +++ b/browser-plugin/glue.cpp @@ -0,0 +1,477 @@ +/* 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 + +#include "evince-view.h" +#include "plugin.h" + +#ifndef HIBYTE +#define HIBYTE(x) ((((uint32_t)(x)) & 0xff00) >> 8) +#endif + +#ifndef LOBYTE +#define LOBYTE(W) ((W) & 0xFF) +#endif + +NPNetscapeFuncs NPNFuncs; + +char * +NP_GetMIMEDescription (void) +{ + ev_init (); + + GString *desc = g_string_new (NULL); + GList *all_types = ev_backends_manager_get_all_types_info (); + + for (GList *l = all_types; l; l = g_list_next (l)){ + EvTypeInfo *info = (EvTypeInfo *)l->data; + const char *mime; + int i = 0; + + while ((mime = info->mime_types[i++])) { + g_string_append_printf (desc, "%s:%s:;", mime, info->desc); + } + } + + return g_string_free (desc, FALSE); +} + +NPError +NP_Initialize (NPNetscapeFuncs *aNPNFuncs, NPPluginFuncs *aNPPFuncs) +{ + /* We need to force libevview to stay in memory because unloading breaks + gobject type registration and possibly other things. */ + void *handle; + handle = dlopen ("libevview.so.1.0.0", RTLD_NOW | RTLD_NODELETE); + if (!handle) { + return NPERR_MODULE_LOAD_FAILED_ERROR; + } + dlclose (handle); + + ev_init (); + + if (aNPNFuncs == NULL || aNPPFuncs == NULL) { + return NPERR_INVALID_FUNCTABLE_ERROR; + } + + if ((aNPNFuncs->version >> 8) > NP_VERSION_MAJOR) { + return NPERR_INCOMPATIBLE_VERSION_ERROR; + } + + if (aNPNFuncs->size < sizeof (NPNetscapeFuncs)) { + return NPERR_INVALID_FUNCTABLE_ERROR; + } + + if (aNPPFuncs->size < sizeof (NPPluginFuncs)) { + return NPERR_INVALID_FUNCTABLE_ERROR; + } + + memcpy (&NPNFuncs, aNPNFuncs, sizeof (NPNetscapeFuncs)); + NPNFuncs.size = sizeof (NPNetscapeFuncs); + + aNPPFuncs->size = sizeof (NPPluginFuncs); + aNPPFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR; + aNPPFuncs->newp = NewNPP_NewProc (NPP_New); + aNPPFuncs->getvalue = NewNPP_GetValueProc (NPP_GetValue); + aNPPFuncs->setvalue = NewNPP_SetValueProc (NPP_SetValue); + aNPPFuncs->setwindow = NewNPP_SetWindowProc (NPP_SetWindow); + aNPPFuncs->destroy = NewNPP_DestroyProc (NPP_Destroy); + aNPPFuncs->newstream = NewNPP_NewStreamProc (NPP_NewStream); + aNPPFuncs->destroystream = NewNPP_DestroyStreamProc (NPP_DestroyStream); + aNPPFuncs->asfile = NewNPP_StreamAsFileProc (NPP_StreamAsFile); + aNPPFuncs->writeready = NewNPP_WriteReadyProc (NPP_WriteReady); + aNPPFuncs->write = NewNPP_WriteProc (NPP_Write); + aNPPFuncs->print = NewNPP_PrintProc (NPP_Print); + aNPPFuncs->event = NewNPP_HandleEventProc (NPP_HandleEvent); + aNPPFuncs->urlnotify = NewNPP_URLNotifyProc (NPP_URLNotify); + + return NPERR_NO_ERROR; +} + +NPError +NP_Shutdown (void) +{ + return NPERR_NO_ERROR; +} + +NPError +NPP_NewStream (NPP instance, NPMIMEType type, + NPStream* stream, NPBool seekable, + uint16* stype) +{ + *stype = NP_ASFILEONLY; + + return NPERR_NO_ERROR; +} + +NPError +NPP_DestroyStream (NPP instance, NPStream* stream, NPReason reason) +{ + return NPERR_NO_ERROR; +} + +int32 +NPP_WriteReady (NPP instance, NPStream* stream) +{ + return -1; +} + +int32 NPP_Write (NPP instance, NPStream* stream, + int32 offset, int32 len, void* buffer) +{ + return -1; +} + +void NPP_StreamAsFile (NPP instance, NPStream* stream, const char* fname) +{ + Plugin *plugin = reinterpret_cast (instance->pdata); + plugin->Load (fname); +} + +void NPP_Print (NPP instance, NPPrint* platformPrint) +{ +} + +int16 NPP_HandleEvent (NPP instance, void* event) +{ + return FALSE; +} + +void +NPP_URLNotify (NPP instance, const char* url, NPReason reason, void* notifyData) +{ +} + +NPError NPP_SetValue (NPP instance, NPNVariable variable, void *value) +{ + return NPERR_NO_ERROR; +} + +NPError +NPP_New (NPMIMEType mimetype, + NPP instance, + uint16_t mode, + int16_t argc, + char *argn[], + char *argv[], + NPSavedData *savedData) +{ + Plugin *plugin = new Plugin (instance); + instance->pdata = reinterpret_cast (plugin); + + return NPERR_NO_ERROR; +} + +NPError +NPP_Destroy (NPP instance, NPSavedData** save) +{ + Plugin *plugin = reinterpret_cast (instance->pdata); + + delete plugin; + + return NPERR_NO_ERROR; +} + +NPError +NPP_GetValue(NPP instance, NPPVariable variable, void *value) +{ + NPError err = NPERR_NO_ERROR; + Plugin *plugin = reinterpret_cast (instance->pdata); + + switch (variable) { + case NPPVpluginNeedsXEmbed: + *(PRBool *)value = PR_TRUE; + break; + case NPPVpluginScriptableNPObject: + *(NPObject **)value = plugin->GetScriptableNPObject (); + break; + + default: + err = NPERR_GENERIC_ERROR; + } + + return err; +} + +NPError +NPP_SetWindow (NPP instance, NPWindow *window) +{ + Plugin *plugin = reinterpret_cast (instance->pdata); + plugin->SetWindow ((GdkNativeWindow)window->window); + + return NPERR_NO_ERROR; +} + +void NPN_Version (int *plugin_major, int *plugin_minor, int *netscape_major, + int *netscape_minor) +{ + *plugin_major = NP_VERSION_MAJOR; + *plugin_minor = NP_VERSION_MINOR; + *netscape_major = HIBYTE (NPNFuncs.version); + *netscape_minor = LOBYTE (NPNFuncs.version); +} + +NPError NPN_GetURLNotify (NPP instance, const char *url, + const char *target, void *notifyData) +{ + return NPNFuncs.geturlnotify (instance, url, target, notifyData); +} + +NPError NPN_GetURL (NPP instance, const char *url, const char *target) +{ + return NPNFuncs.geturl (instance, url, target); +} + +NPError NPN_PostURLNotify (NPP instance, const char *url, const char *window, + uint32_t len, const char *buf, NPBool file, + void *notifyData) +{ + return NPNFuncs.posturlnotify (instance, url, window, + len, buf, file, notifyData); +} + +NPError NPN_PostURL (NPP instance, const char *url, const char *window, + uint32_t len, const char *buf, NPBool file) +{ + return NPNFuncs.posturl (instance, url, window, len, buf, file); +} + +NPError NPN_RequestRead (NPStream* stream, NPByteRange* rangeList) +{ + return NPNFuncs.requestread (stream, rangeList); +} + +NPError NPN_NewStream (NPP instance, NPMIMEType type, + const char *target, NPStream **stream) +{ + return NPNFuncs.newstream (instance, type, target, stream); +} + +int32_t NPN_Write (NPP instance, NPStream *stream, int32_t len, void *buffer) +{ + return NPNFuncs.write (instance, stream, len, buffer); +} + +NPError NPN_DestroyStream (NPP instance, NPStream* stream, NPError reason) +{ + return NPNFuncs.destroystream (instance, stream, reason); +} + +void NPN_Status (NPP instance, const char *message) +{ + NPNFuncs.status (instance, message); +} + +const char* NPN_UserAgent(NPP instance) +{ + return NPNFuncs.uagent (instance); +} + +void* NPN_MemAlloc (uint32 size) +{ + return NPNFuncs.memalloc (size); +} + +void NPN_MemFree (void* ptr) +{ + /* Make it null-safe */ + if (!ptr) + return; + + NPNFuncs.memfree (ptr); +} + +uint32_t NPN_MemFlush (uint32_t size) +{ + return NPNFuncs.memflush (size); +} + +void NPN_ReloadPlugins (NPBool reloadPages) +{ + NPNFuncs.reloadplugins (reloadPages); +} + +JRIEnv* NPN_GetJavaEnv (void) +{ + return NPNFuncs.getJavaEnv (); +} + +jref NPN_GetJavaPeer (NPP instance) +{ + return NPNFuncs.getJavaPeer (instance); +} + +NPError NPN_GetValue (NPP instance, NPNVariable variable, void *value) +{ + return NPNFuncs.getvalue (instance, variable, value); +} + +NPError NPN_SetValue (NPP instance, NPPVariable variable, void *value) +{ + return NPNFuncs.setvalue (instance, variable, value); +} + +void NPN_InvalidateRect (NPP instance, NPRect *invalidRect) +{ + NPNFuncs.invalidaterect (instance, invalidRect); +} + +void NPN_InvalidateRegion (NPP instance, NPRegion invalidRegion) +{ + NPNFuncs.invalidateregion (instance, invalidRegion); +} + +void NPN_ForceRedraw (NPP instance) +{ + NPNFuncs.forceredraw (instance); +} + +NPIdentifier NPN_GetStringIdentifier (const NPUTF8 *name) +{ + return NPNFuncs.getstringidentifier (name); +} + +void NPN_GetStringIdentifiers (const NPUTF8 **names, int32_t nameCount, + NPIdentifier *identifiers) +{ + return NPNFuncs.getstringidentifiers (names, nameCount, identifiers); +} + +NPIdentifier NPN_GetStringIdentifier (int32_t intid) +{ + return NPNFuncs.getintidentifier (intid); +} + +bool NPN_IdentifierIsString (NPIdentifier identifier) +{ + return NPNFuncs.identifierisstring (identifier); +} + +NPUTF8 *NPN_UTF8FromIdentifier (NPIdentifier identifier) +{ + return NPNFuncs.utf8fromidentifier (identifier); +} + +int32_t NPN_IntFromIdentifier (NPIdentifier identifier) +{ + return NPNFuncs.intfromidentifier (identifier); +} + +NPObject *NPN_CreateObject (NPP npp, NPClass *aClass) +{ + return NPNFuncs.createobject (npp, aClass); +} + +NPObject *NPN_RetainObject (NPObject *obj) +{ + return NPNFuncs.retainobject (obj); +} + +void NPN_ReleaseObject(NPObject *obj) +{ + return NPNFuncs.releaseobject (obj); +} + +bool NPN_Invoke (NPP npp, NPObject *obj, NPIdentifier methodName, + const NPVariant *args, uint32_t argCount, NPVariant *result) +{ + return NPNFuncs.invoke (npp, obj, methodName, args, argCount, result); +} + +bool NPN_InvokeDefault (NPP npp, NPObject *obj, const NPVariant *args, + uint32_t argCount, NPVariant *result) +{ + return NPNFuncs.invokeDefault (npp, obj, args, argCount, result); +} + +bool NPN_Evaluate (NPP npp, NPObject *obj, NPString *script, + NPVariant *result) +{ + return NPNFuncs.evaluate (npp, obj, script, result); +} + +bool NPN_GetProperty (NPP npp, NPObject *obj, NPIdentifier propertyName, + NPVariant *result) +{ + return NPNFuncs.getproperty (npp, obj, propertyName, result); +} + +bool NPN_SetProperty (NPP npp, NPObject *obj, NPIdentifier propertyName, + const NPVariant *value) +{ + return NPNFuncs.setproperty (npp, obj, propertyName, value); +} + +bool NPN_RemoveProperty (NPP npp, NPObject *obj, NPIdentifier propertyName) +{ + return NPNFuncs.removeproperty (npp, obj, propertyName); +} + +bool NPN_Enumerate (NPP npp, NPObject *obj, NPIdentifier **identifier, + uint32_t *count) +{ + return NPNFuncs.enumerate (npp, obj, identifier, count); +} + +bool NPN_Construct (NPP npp, NPObject *obj, const NPVariant *args, + uint32_t argCount, NPVariant *result) +{ + return NPNFuncs.construct (npp, obj, args, argCount, result); +} + +bool NPN_HasProperty (NPP npp, NPObject *obj, NPIdentifier propertyName) +{ + return NPNFuncs.hasproperty (npp, obj, propertyName); +} + +bool NPN_HasMethod (NPP npp, NPObject *obj, NPIdentifier methodName) +{ + return NPNFuncs.hasmethod (npp, obj, methodName); +} + +void NPN_ReleaseVariantValue (NPVariant *variant) +{ + NPNFuncs.releasevariantvalue (variant); +} + +void NPN_SetException (NPObject *obj, const NPUTF8 *message) +{ + NPNFuncs.setexception (obj, message); +} + +void* NPN_MemDup (const void *aMem, uint32 aLen) +{ + if (!aMem || !aLen) { + return NULL; + } + + void *dup = NPN_MemAlloc (aLen); + if (!dup) { + return NULL; + } + + return memcpy (dup, aMem, aLen); +} + +char* NPN_StrDup (const char *aString) +{ + return (char *) NPN_MemDup(aString, strlen (aString) + 1); +} diff --git a/browser-plugin/plugin.cpp b/browser-plugin/plugin.cpp new file mode 100644 index 0000000..40db488 --- /dev/null +++ b/browser-plugin/plugin.cpp @@ -0,0 +1,179 @@ +/* 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 "evince-view.h" +#include "plugin.h" +#include "scriptable.h" + +Plugin::Plugin (NPP instance) +{ + mInstance = instance; + mWindow = 0; + mScriptableObject = NULL; + + mScrolledWindow = gtk_scrolled_window_new (NULL, NULL); + + mView = ev_view_new (); + g_signal_connect (G_OBJECT (mView), + "notify::sizing-mode", + G_CALLBACK (Plugin::SizingModeCallback), + reinterpret_cast(this)); + + /* FIXME should be set automatically in EvView. It seem to be currently + used only to calculate the minimum and maximux allowed zoom level. */ + ev_view_set_screen_dpi (EV_VIEW(mView), 96); + + gtk_container_add (GTK_CONTAINER(mScrolledWindow), mView); + gtk_widget_show (mView); + + UpdateSizingMode (); +} + +Plugin::~Plugin () +{ +} + +void +Plugin::ZoomIn () +{ + ev_view_set_sizing_mode (EV_VIEW (mView), EV_SIZING_FREE); + ev_view_zoom_in (EV_VIEW (mView)); +} + +void +Plugin::ZoomOut () +{ + ev_view_set_sizing_mode (EV_VIEW (mView), EV_SIZING_FREE); + ev_view_zoom_out (EV_VIEW (mView)); +} + +void +Plugin::FindNext (const char *text) +{ +} + +void +Plugin::FindPrevious (const char *text) +{ +} + +void +Plugin::SetWindow (GdkNativeWindow window) +{ + if (mWindow != 0) { + return; + } + + mWindow = window; + + GtkWidget *plug = gtk_plug_new (window); + gtk_container_add (GTK_CONTAINER (plug), mScrolledWindow); + gtk_widget_show (mScrolledWindow); + + gtk_widget_show (plug); +} + +void +Plugin::Load (const char *fname) +{ + GError *error = NULL; + + GFile *file = g_file_new_for_path (fname); + char *uri = g_file_get_uri (file); + g_object_unref (file); + + /* FIXME display loading error in the UI */ + + EvDocument *document = ev_document_factory_get_document (uri, &error); + if (error) { + g_warning ("Cannot create document for %s", uri); + g_error_free (error); + return; + } + + ev_view_set_document (EV_VIEW (mView), document); + + ev_document_load (document, uri, &error); + if (error) { + g_warning ("Cannot load %s", uri); + g_error_free (error); + return; + } + + g_free (uri); +} + +NPObject * +Plugin::GetScriptableNPObject () +{ + if (!mScriptableObject) { + mScriptableObject = NPN_CreateObject ( + mInstance, GET_NPOBJECT_CLASS (ScriptablePluginObject)); + } + + if (mScriptableObject) { + NPN_RetainObject (mScriptableObject); + } + + return mScriptableObject; +} + +void +Plugin::UpdateSizingMode () +{ + EvSizingMode sizingMode; + + g_signal_handlers_disconnect_by_func ( + mView, (gpointer)ev_view_update_view_size, mScrolledWindow); + + g_object_get (G_OBJECT (mView), + "sizing-mode", &sizingMode, + NULL); + + if (sizingMode != EV_SIZING_FREE) + ev_view_update_view_size (EV_VIEW (mView), + GTK_SCROLLED_WINDOW (mScrolledWindow)); + + switch (sizingMode) { + case EV_SIZING_BEST_FIT: + case EV_SIZING_FIT_WIDTH: + g_object_set (G_OBJECT (mScrolledWindow), + "hscrollbar-policy", GTK_POLICY_NEVER, + "vscrollbar-policy", GTK_POLICY_ALWAYS, + NULL); + g_signal_connect (mView, "zoom_invalid", + G_CALLBACK (ev_view_update_view_size), + mScrolledWindow); + break; + + case EV_SIZING_FREE: + g_object_set (G_OBJECT (mScrolledWindow), + "hscrollbar-policy", GTK_POLICY_AUTOMATIC, + "vscrollbar-policy", GTK_POLICY_AUTOMATIC, + NULL); + break; + } +} + +void +Plugin::SizingModeCallback (EvView *view, GParamSpec *pspec, gpointer data) +{ + Plugin *plugin = reinterpret_cast (data); + plugin->UpdateSizingMode (); +} diff --git a/browser-plugin/plugin.h b/browser-plugin/plugin.h new file mode 100644 index 0000000..79cfaa3 --- /dev/null +++ b/browser-plugin/plugin.h @@ -0,0 +1,56 @@ +/* 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. + */ + +#ifndef __PLUGIN_H__ +#define __PLUGIN_H__ + +#include +#include + +#include "evince-view.h" + +class Plugin { + public: + Plugin (NPP instance); + ~Plugin (); + + void SetWindow (GdkNativeWindow window); + void Load (const char *fname); + void ZoomIn (); + void ZoomOut (); + void FindNext (const char *text); + void FindPrevious (const char *text); + NPObject *GetScriptableNPObject (); + + private: + + void UpdateSizingMode (); + + static void SizingModeCallback (EvView *view, + GParamSpec *pspec, + gpointer data); + + NPP mInstance; + NPObject *mScriptableObject; + GdkNativeWindow mWindow; + GtkWidget *mScrolledWindow; + GtkWidget *mView; +}; + +#endif // __PLUGIN_H__ diff --git a/browser-plugin/scriptable.cpp b/browser-plugin/scriptable.cpp new file mode 100644 index 0000000..d44a053 --- /dev/null +++ b/browser-plugin/scriptable.cpp @@ -0,0 +1,262 @@ +/* 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" + +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) +{ + mZoomInID = NPN_GetStringIdentifier ("zoomIn"); + mZoomOutID = NPN_GetStringIdentifier ("zoomOut"); + mFindPreviousID = NPN_GetStringIdentifier ("findPrevious"); + mFindNextID = NPN_GetStringIdentifier ("findNext"); +} + +bool +ScriptablePluginObject::HasMethod (NPIdentifier name) +{ + NPIdentifier methods[] = { + mZoomInID, + mZoomOutID, + mFindPreviousID, + mFindNextID + }; + + for (int i = 0; i < G_N_ELEMENTS(methods); i++) { + if (name == methods[i]) { + return true; + } + } + + return false; +} + +bool +ScriptablePluginObject::HasProperty (NPIdentifier name) +{ + return false; +} + +bool +ScriptablePluginObject::GetProperty (NPIdentifier name, NPVariant *result) +{ + return false; +} + +bool +ScriptablePluginObject::Invoke (NPIdentifier name, const NPVariant *args, + uint32_t argCount, NPVariant *result) +{ + Plugin *plugin = reinterpret_cast (mNpp->pdata); + + if (name == mZoomInID) { + plugin->ZoomIn (); + } else if (name == mZoomOutID) { + plugin->ZoomOut (); + } else if (name == mFindNextID) { + if (argCount < 1 || !NPVARIANT_IS_STRING (args[0])) { + return false; + } + plugin->FindNext (NPVARIANT_TO_STRING (args[0]).utf8characters); + } else if (name == mFindPreviousID) { + if (argCount < 1 || !NPVARIANT_IS_STRING (args[0])) { + return false; + } + plugin->FindPrevious(NPVARIANT_TO_STRING (args[0]).utf8characters); + } +} + +bool +ScriptablePluginObject::InvokeDefault (const NPVariant *args, uint32_t argCount, + NPVariant *result) +{ + return false; +} diff --git a/browser-plugin/scriptable.h b/browser-plugin/scriptable.h new file mode 100644 index 0000000..325cf50 --- /dev/null +++ b/browser-plugin/scriptable.h @@ -0,0 +1,133 @@ +/* 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. + */ + +#ifndef __SCRIPTABLE_PLUGIN_H__ +#define __SCRIPTABLE_PLUGIN_H__ + +#include +#include + +NPObject *AllocateScriptablePluginObject (NPP npp, NPClass *aClass); +NPObject *AllocateConstructablePluginObject (NPP npp, NPClass *aClass); + +class ScriptablePluginObjectBase : public NPObject +{ + public: + ScriptablePluginObjectBase (NPP npp) : mNpp(npp) + { + } + + virtual ~ScriptablePluginObjectBase () + { + } + + virtual void Invalidate (); + virtual bool HasMethod (NPIdentifier name); + virtual bool Invoke (NPIdentifier name, const NPVariant *args, + uint32_t argCount, NPVariant *result); + virtual bool InvokeDefault (const NPVariant *args, uint32_t argCount, + NPVariant *result); + virtual bool HasProperty (NPIdentifier name); + virtual bool GetProperty (NPIdentifier name, NPVariant *result); + virtual bool SetProperty (NPIdentifier name, const NPVariant *value); + virtual bool RemoveProperty (NPIdentifier name); + virtual bool Enumerate (NPIdentifier **identifier, uint32_t *count); + virtual bool Construct (const NPVariant *args, uint32_t argCount, + NPVariant *result); + + static void _Deallocate (NPObject *npobj); + static void _Invalidate (NPObject *npobj); + static bool _HasMethod (NPObject *npobj, NPIdentifier name); + static bool _Invoke (NPObject *npobj, NPIdentifier name, + const NPVariant *args, uint32_t argCount, + NPVariant *result); + static bool _InvokeDefault (NPObject *npobj, const NPVariant *args, + uint32_t argCount, NPVariant *result); + static bool _HasProperty (NPObject * npobj, NPIdentifier name); + static bool _GetProperty (NPObject *npobj, NPIdentifier name, + NPVariant *result); + static bool _SetProperty (NPObject *npobj, NPIdentifier name, + const NPVariant *value); + static bool _RemoveProperty (NPObject *npobj, NPIdentifier name); + static bool _Enumerate (NPObject *npobj, NPIdentifier **identifier, + uint32_t *count); + static bool _Construct (NPObject *npobj, const NPVariant *args, + uint32_t argCount, NPVariant *result); + + protected: + NPP mNpp; +}; + +class ScriptablePluginObject : public ScriptablePluginObjectBase +{ + public: + ScriptablePluginObject (NPP npp); + + virtual bool HasMethod (NPIdentifier name); + virtual bool HasProperty (NPIdentifier name); + virtual bool GetProperty (NPIdentifier name, NPVariant *result); + virtual bool Invoke (NPIdentifier name, const NPVariant *args, + uint32_t argCount, NPVariant *result); + virtual bool InvokeDefault (const NPVariant *args, uint32_t argCount, + NPVariant *result); + + private: + NPIdentifier mZoomInID; + NPIdentifier mZoomOutID; + NPIdentifier mFindPreviousID; + NPIdentifier mFindNextID; +}; + +class ConstructablePluginObject : public ScriptablePluginObjectBase +{ + public: + ConstructablePluginObject (NPP npp) : ScriptablePluginObjectBase (npp) + { + } + + virtual bool Construct (const NPVariant *args, uint32_t argCount, + NPVariant *result); +}; + +#define DECLARE_NPOBJECT_CLASS_WITH_BASE(_class, ctor) \ +static NPClass s##_class##_NPClass = { \ + NP_CLASS_STRUCT_VERSION_CTOR, \ + ctor, \ + ScriptablePluginObjectBase::_Deallocate, \ + ScriptablePluginObjectBase::_Invalidate, \ + ScriptablePluginObjectBase::_HasMethod, \ + ScriptablePluginObjectBase::_Invoke, \ + ScriptablePluginObjectBase::_InvokeDefault, \ + ScriptablePluginObjectBase::_HasProperty, \ + ScriptablePluginObjectBase::_GetProperty, \ + ScriptablePluginObjectBase::_SetProperty, \ + ScriptablePluginObjectBase::_RemoveProperty, \ + ScriptablePluginObjectBase::_Enumerate, \ + ScriptablePluginObjectBase::_Construct \ +} + +#define GET_NPOBJECT_CLASS(_class) &s##_class##_NPClass + +DECLARE_NPOBJECT_CLASS_WITH_BASE (ScriptablePluginObject, + AllocateScriptablePluginObject); + +DECLARE_NPOBJECT_CLASS_WITH_BASE (ConstructablePluginObject, + AllocateConstructablePluginObject); + +#endif // __SCRIPTABLE_PLUGIN_H__ -- cgit v0.9.1