Back to index

lightning-sunbird  0.9+nobinonly
npp_gate.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 
00039 //
00040 // Implementation of plugin entry points (NPP_*)
00041 //
00042 #include "plugbase.h"
00043 #include "logger.h"
00044 
00045 extern CLogger * pLogger;
00046 
00047 static char szINIFile[] = NPAPI_INI_FILE_NAME;
00048 static char szTarget[] = LOGGER_DEFAULT_TARGET;
00049 
00050 // here the plugin creates a plugin instance object which 
00051 // will be associated with this newly created NPP instance and 
00052 // will do all the necessary job
00053 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved)
00054 {   
00055   DWORD dwTickEnter = XP_GetTickCount();
00056   NPError ret = NPERR_NO_ERROR;
00057   CPluginBase * pPlugin = NULL;
00058 
00059   if(!instance) {
00060     ret = NPERR_INVALID_INSTANCE_ERROR;
00061     goto Return;
00062   }
00063 
00064   pPlugin = (CPluginBase *)CreatePlugin(instance, mode);
00065 
00066   if(!pPlugin) {
00067     ret = NPERR_OUT_OF_MEMORY_ERROR;
00068     goto Return;
00069   }
00070 
00071   instance->pdata = (void *)pPlugin;
00072 
00073   // recreate logger if needed
00074   if (!pLogger)
00075     pLogger = new CLogger(szTarget);
00076   else if (pLogger->isStale()) {
00077     delete pLogger;
00078     pLogger = new CLogger(szTarget);
00079   }
00080 
00081   char szFileName[_MAX_PATH];
00082   pPlugin->getModulePath(szFileName, sizeof(szFileName));
00083   strcat(szFileName, szINIFile);
00084   pLogger->restorePreferences(szFileName);
00085   
00086   pLogger->associate(pPlugin);
00087 
00088   if (pPlugin->isStandAlone())
00089     pPlugin->initStandAlone();
00090 
00091 Return:
00092   DWORD dwTickReturn = XP_GetTickCount();
00093   pLogger->appendToLog(action_npp_new, dwTickEnter, dwTickReturn, (DWORD)ret, 
00094                        (DWORD)pluginType, (DWORD)instance, 
00095                        (DWORD)mode, (DWORD)argc, (DWORD)argn, (DWORD)argv, (DWORD)saved);
00096 
00097   pPlugin->autoStartScriptIfNeeded();
00098 
00099   return ret;
00100 }
00101 
00102 // here is the place to clean up and destroy the nsPluginInstance object
00103 NPError NPP_Destroy (NPP instance, NPSavedData** save)
00104 {
00105   DWORD dwTickEnter = XP_GetTickCount();
00106   NPError ret = NPERR_NO_ERROR;
00107   CPluginBase * pPlugin = NULL;
00108 
00109   if(!instance) {
00110     ret = NPERR_INVALID_INSTANCE_ERROR;
00111     goto Return;
00112   }
00113 
00114   pPlugin = (CPluginBase *)instance->pdata;
00115   if(pPlugin) {
00116     if (pPlugin->isStandAlone())
00117       pPlugin->shutStandAlone();
00118 
00119     pPlugin->shut();
00120     DestroyPlugin(pPlugin);
00121     goto Return;
00122   }
00123 
00124 Return:
00125   pLogger->blockDumpToFrame(TRUE);
00126   DWORD dwTickReturn = XP_GetTickCount();
00127   pLogger->appendToLog(action_npp_destroy, dwTickEnter, dwTickReturn, (DWORD)ret, (DWORD)instance, (DWORD)save);
00128   pLogger->blockDumpToFrame(FALSE);
00129 
00130   // mark logger stale as the dll can remain in memory with no NP_Shutdown called
00131   // and then come back with NPP_New where we should recreate the logger
00132   pLogger->markStale();
00133   return ret;
00134 }
00135 
00136 // during this call we know when the plugin window is ready or
00137 // is about to be destroyed so we can do some gui specific
00138 // initialization and shutdown
00139 NPError NPP_SetWindow (NPP instance, NPWindow* pNPWindow)
00140 {    
00141   DWORD dwTickEnter = XP_GetTickCount();
00142   CPluginBase * pPlugin = NULL;
00143   NPError ret = NPERR_NO_ERROR;
00144 
00145   if(!instance ) {
00146     ret = NPERR_INVALID_INSTANCE_ERROR;
00147     goto Return;
00148   }
00149 
00150   if(!pNPWindow) {
00151     ret = NPERR_INVALID_INSTANCE_ERROR;
00152     goto Return;
00153   }
00154 
00155   pPlugin = (CPluginBase *)instance->pdata;
00156 
00157   if(!pPlugin) {
00158     ret = NPERR_GENERIC_ERROR;
00159     goto Return;
00160   }
00161 
00162   if (!pPlugin->isStandAlone())
00163   {
00164     // window just created
00165     if(!pPlugin->isInitialized() && pNPWindow->window) { 
00166       if(!pPlugin->init((DWORD)pNPWindow->window)) {
00167         delete pPlugin;
00168         pPlugin = NULL;
00169         ret = NPERR_MODULE_LOAD_FAILED_ERROR;
00170         goto Return;
00171       }
00172 
00173       if(pLogger->getShowImmediatelyFlag()) {
00174         pLogger->dumpLogToTarget();
00175         pLogger->clearLog();
00176       }
00177       goto Return;
00178     }
00179 
00180     // window goes away
00181     if(!pNPWindow->window && pPlugin->isInitialized()) {
00182       pPlugin->shut();
00183       ret = NPERR_NO_ERROR;
00184       goto Return;
00185     }
00186 
00187     // window resized?
00188     if(pPlugin->isInitialized() && pNPWindow->window) {
00189       ret = NPERR_NO_ERROR;
00190       goto Return;
00191     }
00192 
00193     // this should not happen, nothing to do
00194     if(!pNPWindow->window && !pPlugin->isInitialized()) {
00195       ret = NPERR_NO_ERROR;
00196       goto Return;
00197     }
00198   }
00199 
00200 Return:
00201   DWORD dwTickReturn = XP_GetTickCount();
00202   pLogger->appendToLog(action_npp_set_window, dwTickEnter, dwTickReturn, (DWORD)ret, (DWORD)instance, (DWORD)pNPWindow);
00203   return ret;
00204 }
00205 
00206 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
00207 {
00208   DWORD dwTickEnter = XP_GetTickCount();
00209   CPluginBase * pPlugin = NULL;
00210   NPError ret = NPERR_NO_ERROR;
00211 
00212   if(!instance) {
00213     ret = NPERR_INVALID_INSTANCE_ERROR;
00214     goto Return;
00215   }
00216 
00217   pPlugin = (CPluginBase *)instance->pdata;
00218   pPlugin->onNPP_NewStream(instance, (LPSTR)type, stream, seekable, stype);
00219 
00220 Return:
00221   DWORD dwTickReturn = XP_GetTickCount();
00222   pLogger->appendToLog(action_npp_new_stream, dwTickEnter, dwTickReturn, (DWORD)ret, (DWORD)instance, 
00223                        (DWORD)type, (DWORD)stream, (DWORD)seekable, (DWORD)stype);
00224   if (pPlugin->m_firstAction == action_npn_request_read && seekable) {
00225     *stype = NP_SEEK;
00226   }
00227   return ret;
00228 }
00229 
00230 int32 NPP_WriteReady (NPP instance, NPStream *stream)
00231 {
00232   DWORD dwTickEnter = XP_GetTickCount();
00233   CPluginBase * pPlugin = NULL;
00234   int32 ret = 0x0FFFFFFF;
00235 
00236   if(!instance) {
00237     ret = 0L;
00238     goto Return;
00239   }
00240 
00241   pPlugin = (CPluginBase *)instance->pdata;
00242 
00243 Return:
00244   DWORD dwTickReturn = XP_GetTickCount();
00245   pLogger->appendToLog(action_npp_write_ready, dwTickEnter, dwTickReturn, (DWORD)ret, 
00246                        (DWORD)instance, (DWORD)stream);
00247   return ret;
00248 }
00249 
00250 int32 NPP_Write (NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
00251 {   
00252   DWORD dwTickEnter = XP_GetTickCount();
00253   CPluginBase * pPlugin = NULL;
00254   int32 ret = len;
00255 
00256   if(!instance)
00257     goto Return;
00258   
00259   pPlugin = (CPluginBase *)instance->pdata;
00260 
00261 Return:
00262   DWORD dwTickReturn = XP_GetTickCount();
00263   pLogger->appendToLog(action_npp_write, dwTickEnter, dwTickReturn, (DWORD)ret, 
00264                        (DWORD)instance, (DWORD)stream, 
00265                        (DWORD)offset, (DWORD)len, (DWORD)buffer);
00266   if (pPlugin->m_firstAction == action_npn_request_read) {
00267     if (stream->notifyData) {
00268       NPByteRange* rangeList =     (NPByteRange*) stream->notifyData;
00269       NPN_RequestRead(stream, rangeList);
00270       stream->notifyData = 0;
00271     }
00272   }
00273   return ret;
00274 }
00275 
00276 NPError NPP_DestroyStream (NPP instance, NPStream *stream, NPError reason)
00277 {
00278   DWORD dwTickEnter = XP_GetTickCount();
00279   CPluginBase * pPlugin = NULL;
00280   NPError ret = NPERR_NO_ERROR;
00281 
00282   if(!instance) {
00283     ret = NPERR_INVALID_INSTANCE_ERROR;
00284     goto Return;
00285   }
00286 
00287   pPlugin = (CPluginBase *)instance->pdata;
00288 
00289   pPlugin->onNPP_DestroyStream(stream);
00290   if(pLogger->onNPP_DestroyStream(stream))
00291     return ret;
00292 
00293 Return:
00294   DWORD dwTickReturn = XP_GetTickCount();
00295   pLogger->appendToLog(action_npp_destroy_stream, dwTickEnter, dwTickReturn, (DWORD)ret, 
00296                        (DWORD)instance, (DWORD)stream, (DWORD)reason);
00297   return ret;
00298 }
00299 
00300 void NPP_StreamAsFile (NPP instance, NPStream* stream, const char* fname)
00301 {
00302   DWORD dwTickEnter = XP_GetTickCount();
00303   CPluginBase * pPlugin = NULL;
00304 
00305   if(!instance)
00306     goto Return;
00307 
00308   pPlugin = (CPluginBase *)instance->pdata;
00309 
00310   pPlugin->onNPP_StreamAsFile(instance, stream, fname);
00311 
00312 Return:
00313   DWORD dwTickReturn = XP_GetTickCount();
00314   pLogger->appendToLog(action_npp_stream_as_file, dwTickEnter, dwTickReturn, 0L, 
00315                        (DWORD)instance, (DWORD)stream, (DWORD)fname);
00316 }
00317 
00318 void NPP_Print (NPP instance, NPPrint* printInfo)
00319 {
00320   DWORD dwTickEnter = XP_GetTickCount();
00321   CPluginBase * pPlugin = NULL;
00322 
00323   if(!instance)
00324     goto Return;
00325 
00326   pPlugin = (CPluginBase *)instance->pdata;
00327 
00328 Return:
00329   DWORD dwTickReturn = XP_GetTickCount();
00330   pLogger->appendToLog(action_npp_print, dwTickEnter, dwTickReturn, 0L, 
00331                        (DWORD)instance, (DWORD)printInfo);
00332 }
00333 
00334 void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
00335 {
00336   DWORD dwTickEnter = XP_GetTickCount();
00337   CPluginBase * pPlugin = NULL;
00338 
00339   if(!instance)
00340     goto Return;
00341 
00342   pPlugin = (CPluginBase *)instance->pdata;
00343 
00344 Return:
00345   DWORD dwTickReturn = XP_GetTickCount();
00346   pLogger->appendToLog(action_npp_url_notify, dwTickEnter, dwTickReturn, 0L, 
00347                        (DWORD)instance, (DWORD)url, (DWORD)reason, (DWORD)notifyData);
00348 }
00349 
00350 NPError       NPP_GetValue(NPP instance, NPPVariable variable, void *value)
00351 {
00352   DWORD dwTickEnter = XP_GetTickCount();
00353   CPluginBase * pPlugin = NULL;
00354   NPError ret = NPERR_NO_ERROR;
00355 
00356   if(!instance) {
00357     ret = NPERR_INVALID_INSTANCE_ERROR;
00358     goto Return;
00359   }
00360 
00361   pPlugin = (CPluginBase *)instance->pdata;
00362 
00363 #ifdef XP_UNIX
00364   switch (variable) {
00365     case NPPVpluginNameString:   
00366       *((char **)value) = "API Tester plugin";
00367       break;
00368     case NPPVpluginDescriptionString:
00369       *((char **)value) = "This plugins reads and executes test scripts.";
00370       break;
00371     default:
00372       ret = NPERR_GENERIC_ERROR;
00373   }
00374 #endif // XP_UNIX (displays name and description)
00375 
00376 Return:
00377   DWORD dwTickReturn = XP_GetTickCount();
00378   pLogger->appendToLog(action_npp_get_value, dwTickEnter, dwTickReturn, (DWORD)ret, 
00379                        (DWORD)instance, (DWORD)variable, (DWORD)value);
00380   return ret;
00381 }
00382 
00383 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
00384 {
00385   DWORD dwTickEnter = XP_GetTickCount();
00386   CPluginBase * pPlugin = NULL;
00387   NPError ret = NPERR_NO_ERROR;
00388 
00389   if(!instance) {
00390     ret = NPERR_INVALID_INSTANCE_ERROR;
00391     goto Return;
00392   }
00393 
00394   pPlugin = (CPluginBase *)instance->pdata;
00395 
00396 Return:
00397   DWORD dwTickReturn = XP_GetTickCount();
00398   pLogger->appendToLog(action_npp_set_value, dwTickEnter, dwTickReturn, (DWORD)ret, 
00399                        (DWORD)instance, (DWORD)variable, (DWORD)value);
00400   return ret;
00401 }
00402 
00403 int16  NPP_HandleEvent(NPP instance, void* event)
00404 {
00405   DWORD dwTickEnter = XP_GetTickCount();
00406   CPluginBase * pPlugin = NULL;
00407   int16 ret = (int16)TRUE;
00408 
00409   if(!instance)
00410     goto Return;
00411 
00412   pPlugin = (CPluginBase *)instance->pdata;
00413 
00414 Return:
00415   DWORD dwTickReturn = XP_GetTickCount();
00416   pLogger->appendToLog(action_npp_handle_event, dwTickEnter, dwTickReturn, (DWORD)ret, 
00417                        (DWORD)instance, (DWORD)event);
00418   return ret;
00419 }
00420 
00421 jref NPP_GetJavaClass (void)
00422 {
00423   DWORD dwTickEnter = XP_GetTickCount();
00424   DWORD dwTickReturn = XP_GetTickCount();
00425   if(pLogger)
00426     pLogger->appendToLog(action_npp_get_java_class, dwTickEnter, dwTickReturn, 0L);
00427   return NULL;
00428 }
00429 
00430 /**************************************************/
00431 /*                                                */
00432 /*                     Mac                        */
00433 /*                                                */
00434 /**************************************************/
00435 
00436 // Mac needs these wrappers, see npplat.h for more info
00437 
00438 #ifdef XP_MAC
00439 
00440 NPError       Private_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved)
00441 {
00442   EnterCodeResource();
00443   NPError rv = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
00444   ExitCodeResource();
00445   return rv;  
00446 }
00447 
00448 NPError Private_Destroy(NPP instance, NPSavedData** save)
00449 {
00450   EnterCodeResource();
00451   NPError rv = NPP_Destroy(instance, save);
00452   ExitCodeResource();
00453   return rv;
00454 }
00455 
00456 NPError Private_SetWindow(NPP instance, NPWindow* window)
00457 {
00458   EnterCodeResource();
00459   NPError rv = NPP_SetWindow(instance, window);
00460   ExitCodeResource();
00461   return rv;
00462 }
00463 
00464 NPError Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
00465 {
00466   EnterCodeResource();
00467   NPError rv = NPP_NewStream(instance, type, stream, seekable, stype);
00468   ExitCodeResource();
00469   return rv;
00470 }
00471 
00472 int32 Private_WriteReady(NPP instance, NPStream* stream)
00473 {
00474   EnterCodeResource();
00475   int32 rv = NPP_WriteReady(instance, stream);
00476   ExitCodeResource();
00477   return rv;
00478 }
00479 
00480 int32 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
00481 {
00482   EnterCodeResource();
00483   int32 rv = NPP_Write(instance, stream, offset, len, buffer);
00484   ExitCodeResource();
00485   return rv;
00486 }
00487 
00488 void Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
00489 {
00490   EnterCodeResource();
00491   NPP_StreamAsFile(instance, stream, fname);
00492   ExitCodeResource();
00493 }
00494 
00495 
00496 NPError Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
00497 {
00498   EnterCodeResource();
00499   NPError rv = NPP_DestroyStream(instance, stream, reason);
00500   ExitCodeResource();
00501   return rv;
00502 }
00503 
00504 int16 Private_HandleEvent(NPP instance, void* event)
00505 {
00506   EnterCodeResource();
00507   int16 rv = NPP_HandleEvent(instance, event);
00508   ExitCodeResource();
00509   return rv;
00510 }
00511 
00512 void Private_Print(NPP instance, NPPrint* platformPrint)
00513 {
00514   EnterCodeResource();
00515   NPP_Print(instance, platformPrint);
00516   ExitCodeResource();
00517 }
00518 
00519 void Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
00520 {
00521   EnterCodeResource();
00522   NPP_URLNotify(instance, url, reason, notifyData);
00523   ExitCodeResource();
00524 }
00525 
00526 jref Private_GetJavaClass(void)
00527 {
00528   return NULL;
00529 }
00530 
00531 NPError Private_GetValue(NPP instance, NPPVariable variable, void *result)
00532 {
00533   EnterCodeResource();
00534   NPError rv = NPP_GetValue(instance, variable, result);
00535   ExitCodeResource();
00536   return rv;
00537 }
00538 
00539 NPError Private_SetValue(NPP instance, NPNVariable variable, void *value)
00540 {
00541   EnterCodeResource();
00542   NPError rv = NPP_SetValue(instance, variable, value);
00543   ExitCodeResource();
00544   return rv;
00545 }
00546 
00547 #endif //XP_MAC