Back to index

lightning-sunbird  0.9+nobinonly
plugin.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <windows.h>
00039 #include <windowsx.h>
00040 
00041 #include "plugin.h"
00042 #include "nsIServiceManager.h"
00043 #include "nsISupportsUtils.h" // some usefule macros are defined here
00044 
00046 //
00047 // general initialization and shutdown
00048 //
00049 NPError NS_PluginInitialize()
00050 {
00051   return NPERR_NO_ERROR;
00052 }
00053 
00054 void NS_PluginShutdown()
00055 {
00056 }
00057 
00059 //
00060 // construction and destruction of our plugin instance object
00061 //
00062 nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
00063 {
00064   if(!aCreateDataStruct)
00065     return NULL;
00066 
00067   nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
00068   return plugin;
00069 }
00070 
00071 void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
00072 {
00073   if(aPlugin)
00074     delete (nsPluginInstance *)aPlugin;
00075 }
00076 
00078 //
00079 // nsPluginInstance class implementation
00080 //
00081 nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
00082   mInstance(aInstance),
00083   mInitialized(FALSE),
00084   mScriptablePeer(NULL)
00085 {
00086   mhWnd = NULL;
00087   mString[0] = '\0';
00088 }
00089 
00090 nsPluginInstance::~nsPluginInstance()
00091 {
00092   // mScriptablePeer may be also held by the browser 
00093   // so releasing it here does not guarantee that it is over
00094   // we should take precaution in case it will be called later
00095   // and zero its mPlugin member
00096   mScriptablePeer->SetInstance(NULL);
00097   NS_IF_RELEASE(mScriptablePeer);
00098 }
00099 
00100 static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM);
00101 static WNDPROC lpOldProc = NULL;
00102 
00103 NPBool nsPluginInstance::init(NPWindow* aWindow)
00104 {
00105   if(aWindow == NULL)
00106     return FALSE;
00107 
00108   mhWnd = (HWND)aWindow->window;
00109   if(mhWnd == NULL)
00110     return FALSE;
00111 
00112   // subclass window so we can intercept window messages and
00113   // do our drawing to it
00114   lpOldProc = SubclassWindow(mhWnd, (WNDPROC)PluginWinProc);
00115 
00116   // associate window with our nsPluginInstance object so we can access 
00117   // it in the window procedure
00118   SetWindowLong(mhWnd, GWL_USERDATA, (LONG)this);
00119 
00120   mInitialized = TRUE;
00121   return TRUE;
00122 }
00123 
00124 void nsPluginInstance::shut()
00125 {
00126   // subclass it back
00127   SubclassWindow(mhWnd, lpOldProc);
00128   mhWnd = NULL;
00129   mInitialized = FALSE;
00130 }
00131 
00132 NPBool nsPluginInstance::isInitialized()
00133 {
00134   return mInitialized;
00135 }
00136 
00137 // this will force to draw a version string in the plugin window
00138 void nsPluginInstance::showVersion()
00139 {
00140   const char *ua = NPN_UserAgent(mInstance);
00141   strcpy(mString, ua);
00142   InvalidateRect(mhWnd, NULL, TRUE);
00143   UpdateWindow(mhWnd);
00144 }
00145 
00146 // this will clean the plugin window
00147 void nsPluginInstance::clear()
00148 {
00149   strcpy(mString, "");
00150   InvalidateRect(mhWnd, NULL, TRUE);
00151   UpdateWindow(mhWnd);
00152 }
00153 
00154 // ==============================
00155 // ! Scriptability related code !
00156 // ==============================
00157 //
00158 // here the plugin is asked by Mozilla to tell if it is scriptable
00159 // we should return a valid interface id and a pointer to 
00160 // nsScriptablePeer interface which we should have implemented
00161 // and which should be defined in the corressponding *.xpt file
00162 // in the bin/components folder
00163 NPError       nsPluginInstance::GetValue(NPPVariable aVariable, void *aValue)
00164 {
00165   NPError rv = NPERR_NO_ERROR;
00166 
00167   if (aVariable == NPPVpluginScriptableInstance) {
00168     // addref happens in getter, so we don't addref here
00169     nsIScriptablePluginSample * scriptablePeer = getScriptablePeer();
00170     if (scriptablePeer) {
00171       *(nsISupports **)aValue = scriptablePeer;
00172     } else
00173       rv = NPERR_OUT_OF_MEMORY_ERROR;
00174   }
00175   else if (aVariable == NPPVpluginScriptableIID) {
00176     static nsIID scriptableIID = NS_ISCRIPTABLEPLUGINSAMPLE_IID;
00177     nsIID* ptr = (nsIID *)NPN_MemAlloc(sizeof(nsIID));
00178     if (ptr) {
00179         *ptr = scriptableIID;
00180         *(nsIID **)aValue = ptr;
00181     } else
00182       rv = NPERR_OUT_OF_MEMORY_ERROR;
00183   }
00184 
00185   return rv;
00186 }
00187 
00188 // ==============================
00189 // ! Scriptability related code !
00190 // ==============================
00191 //
00192 // this method will return the scriptable object (and create it if necessary)
00193 nsScriptablePeer* nsPluginInstance::getScriptablePeer()
00194 {
00195   if (!mScriptablePeer) {
00196     mScriptablePeer = new nsScriptablePeer(this);
00197     if(!mScriptablePeer)
00198       return NULL;
00199 
00200     NS_ADDREF(mScriptablePeer);
00201   }
00202 
00203   // add reference for the caller requesting the object
00204   NS_ADDREF(mScriptablePeer);
00205   return mScriptablePeer;
00206 }
00207 
00208 static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
00209 {
00210   switch (msg) {
00211     case WM_PAINT:
00212       {
00213         // draw a frame and display the string
00214         PAINTSTRUCT ps;
00215         HDC hdc = BeginPaint(hWnd, &ps);
00216         RECT rc;
00217         GetClientRect(hWnd, &rc);
00218         FrameRect(hdc, &rc, GetStockBrush(BLACK_BRUSH));
00219 
00220         // get our plugin instance object and ask it for the version string
00221         nsPluginInstance *plugin = (nsPluginInstance *)GetWindowLong(hWnd, GWL_USERDATA);
00222         if (plugin)
00223           DrawText(hdc, plugin->mString, strlen(plugin->mString), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
00224         else {
00225           char string[] = "Error occured";
00226           DrawText(hdc, string, strlen(string), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
00227         }
00228 
00229         EndPaint(hWnd, &ps);
00230       }
00231       break;
00232     default:
00233       break;
00234   }
00235 
00236   return DefWindowProc(hWnd, msg, wParam, lParam);
00237 }