Back to index

lightning-sunbird  0.9+nobinonly
plugbase.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 "xp.h"
00039 
00040 #include "plugbase.h"
00041 #include "logger.h"
00042 #include "scripter.h"
00043 
00044 extern CLogger * pLogger;
00045 static char szINIFile[] = NPAPI_INI_FILE_NAME;
00046 #ifdef XP_UNIX
00047     static char szTarget[] = LOGGER_DEFAULT_TARGET;
00048 #endif
00049 
00050 CPluginBase::CPluginBase(NPP pNPInstance, WORD wMode) :
00051   m_pNPInstance(pNPInstance),
00052   m_wMode(wMode),
00053   m_pStream(NULL),
00054   m_pScriptStream(NULL),
00055   m_bWaitingForScriptStream(FALSE),
00056   m_pNPNAlloced(NULL),
00057   m_pValue(NULL)
00058 {
00059   assert(m_pNPInstance != NULL);
00060   m_szScriptCacheFile[0] = '\0';
00061 
00062   if(m_wMode == NP_FULL)
00063   {
00064     // next should be NPP_NewStream with the script to perform
00065     // so we set a flag for a special stream treatment
00066     m_bWaitingForScriptStream = TRUE;
00067   }
00068 }
00069 
00070 CPluginBase::~CPluginBase()
00071 {
00072   if(m_pStream != NULL)
00073   {
00074     NPN_DestroyStream(m_pNPInstance, m_pStream, NPRES_DONE);
00075     m_pStream = NULL;
00076   }
00077 
00078   if(m_pScriptStream != NULL)
00079   {
00080     NPN_DestroyStream(m_pNPInstance, m_pScriptStream, NPRES_DONE);
00081     m_pScriptStream = NULL;
00082   }
00083 
00084   if(m_pNPNAlloced != NULL)
00085   {
00086     NPN_MemFree(m_pNPNAlloced);
00087     m_pNPNAlloced = NULL;
00088   }
00089 }
00090 
00091 BOOL CPluginBase::initFull(DWORD dwInitData)
00092 {
00093   pLogger->setLogToFrameFlag(FALSE);
00094   pLogger->setLogToFileFlag(FALSE);
00095   pLogger->clearLog();
00096   
00097   return TRUE;
00098 }
00099 
00100 void CPluginBase::shutFull()
00101 {
00102 }
00103 
00104 BOOL CPluginBase::init(DWORD dwInitData)
00105 {
00106   if(m_wMode == NP_EMBED)
00107     return initEmbed(dwInitData);
00108   else
00109     return initFull(dwInitData);
00110 }
00111 
00112 void CPluginBase::shut()
00113 {
00114   if(m_wMode == NP_EMBED)
00115     shutEmbed();
00116   else
00117     shutFull();
00118 }
00119 
00120 void CPluginBase::getLogFileName(LPSTR szLogFileName, int iSize)
00121 {
00122   char szFileName[_MAX_PATH];
00123   getModulePath(szFileName, sizeof(szFileName));
00124   strcat(szFileName, szINIFile);
00125   XP_GetPrivateProfileString(SECTION_LOG, KEY_FILE_NAME, "", szLogFileName, (DWORD)iSize, szFileName);
00126   if(!*szLogFileName)
00127   {
00128     strcpy(szLogFileName, m_szScriptCacheFile);
00129 
00130     // last three chars MUST be pts
00131     int iLength = strlen(szLogFileName);
00132     szLogFileName[iLength - 3] = 'l';
00133     szLogFileName[iLength - 2] = 'o';
00134     szLogFileName[iLength - 1] = 'g';
00135   }
00136 }
00137 
00138 const NPP CPluginBase::getNPInstance()
00139 {
00140   return m_pNPInstance;
00141 }
00142 
00143 const WORD CPluginBase::getMode()
00144 {
00145   return m_wMode;
00146 }
00147 
00148 const NPStream * CPluginBase::getNPStream()
00149 {
00150   return m_pStream;
00151 }
00152 
00153 void CPluginBase::onNPP_NewStream(NPP pInstance, LPSTR szMIMEType, NPStream * pStream, NPBool bSeekable, uint16 * puType)
00154 {
00155   if(!m_bWaitingForScriptStream)
00156     return;
00157 
00158   m_pScriptStream = pStream;
00159   *puType = NP_ASFILEONLY;
00160 
00161   m_bWaitingForScriptStream = FALSE;
00162 }
00163 
00164 void CPluginBase::onNPP_StreamAsFile(NPP pInstance, NPStream * pStream, const char * szFileName)
00165 {
00166   if((m_pScriptStream == NULL) || (pStream != m_pScriptStream) || (szFileName == NULL))
00167     return;
00168 
00169   strcpy(m_szScriptCacheFile, szFileName);
00170 #ifdef DEBUG
00171   fprintf(stderr,"CPluginBase::onNPP_StreamAsFile(), Script Cache File \"%s\"", m_szScriptCacheFile);
00172 #endif
00173 }
00174 
00175 void CPluginBase::onNPP_DestroyStream(NPStream * pStream)
00176 {
00177   // if this is a stream created by tester, reflect the fact it is destroyed
00178   if(pStream == m_pStream)
00179     m_pStream = NULL;
00180 
00181   // if this is a script stream, reflect the fact it is destroyed too, and execute the script
00182   if(pStream == m_pScriptStream)
00183   {
00184     m_pScriptStream = NULL;
00185 
00186     if(!XP_IsFile(m_szScriptCacheFile))
00187       return;
00188 
00189     CScripter * pScripter = new CScripter();
00190     pScripter->associate(this);
00191 
00192     if(pScripter->createScriptFromFile(m_szScriptCacheFile))
00193     {
00194       char szOutput[128];
00195       char szExecutingScript[] = "Executing script...";
00196 
00197       strcpy(szOutput, szExecutingScript);
00198       NPN_Status(m_pNPInstance, szOutput);
00199 
00200       int iRepetitions = pScripter->getCycleRepetitions();
00201       int iDelay = pScripter->getCycleDelay();
00202       if(iDelay < 0)
00203         iDelay = 0;
00204 
00205       assert(pLogger != NULL);
00206       pLogger->resetStartTime();
00207 
00208       pLogger->setLogToFileFlag(TRUE);
00209       pLogger->blockDumpToFile(FALSE);
00210 
00211       for(int i = 0; i < iRepetitions; i++)
00212       {
00213         wsprintf(szOutput, "%s %i", szExecutingScript, i);
00214         NPN_Status(m_pNPInstance, szOutput);
00215 
00216         pScripter->executeScript();
00217         if(iDelay != 0)
00218           XP_Sleep(iDelay);
00219       }
00220     }
00221     else
00222       NPN_Status(m_pNPInstance, "Cannot create script...");
00223 
00224 
00225     pLogger->setLogToFileFlag(FALSE);
00226 
00227     delete pScripter;
00228 
00229     m_szScriptCacheFile[0] = '\0';
00230 
00231     NPN_Status(m_pNPInstance, "Script execution complete");
00232   }
00233 }
00234 
00235 DWORD CPluginBase::makeNPNCall(NPAPI_Action action, DWORD dw1, DWORD dw2, DWORD dw3, 
00236                            DWORD dw4, DWORD dw5, DWORD dw6, DWORD dw7)
00237 {
00238   DWORD dwRet = 0L;
00239   DWORD dwTickEnter = XP_GetTickCount();
00240 
00241   switch (action)
00242   {
00243     case action_invalid:
00244       assert(0);
00245       break;
00246     case action_npn_version:
00247     {
00248       static int iP_maj = 0;
00249       static int iP_min = 0;
00250       static int iN_maj = 0;
00251       static int iN_min = 0;
00252       if(dw1 == DEFAULT_DWARG_VALUE)
00253         dw1 = (DWORD)&iP_maj;
00254       if(dw2 == DEFAULT_DWARG_VALUE)
00255         dw2 = (DWORD)&iP_min;
00256       if(dw3 == DEFAULT_DWARG_VALUE)
00257         dw3 = (DWORD)&iN_maj;
00258       if(dw4 == DEFAULT_DWARG_VALUE)
00259         dw4 = (DWORD)&iN_min;
00260       NPN_Version((int *)dw1, (int *)dw2, (int *)dw3, (int *)dw4);
00261       break;
00262     }
00263     case action_npn_get_url_notify:
00264       if(dw1 == DEFAULT_DWARG_VALUE)
00265         dw1 = (DWORD)m_pNPInstance;
00266       dwRet = NPN_GetURLNotify((NPP)dw1, (char *)dw2, (char *)dw3, (void *)dw4);
00267       break;
00268     case action_npn_get_url:
00269       if(dw1 == DEFAULT_DWARG_VALUE)
00270         dw1 = (DWORD)m_pNPInstance;
00271       dwRet = NPN_GetURL((NPP)dw1, (char *)dw2, (char *)dw3);
00272       break;
00273     case action_npn_post_url_notify:
00274       if(dw1 == DEFAULT_DWARG_VALUE)
00275         dw1 = (DWORD)m_pNPInstance;
00276       dwRet = NPN_PostURLNotify((NPP)dw1, (char *)dw2, (char *)dw3, (int32)dw4, (char *)dw5, 
00277                                 (BOOL)dw6, (void *)dw7);
00278       break;
00279     case action_npn_post_url:
00280       if(dw1 == DEFAULT_DWARG_VALUE)
00281         dw1 = (DWORD)m_pNPInstance;
00282       dwRet = NPN_PostURL((NPP)dw1, (char *)dw2, (char *)dw3, (int32)dw4, (char *)dw5, (BOOL)dw6);
00283       break;
00284     case action_npn_new_stream:
00285       assert(m_pStream == NULL);
00286       if(dw1 == DEFAULT_DWARG_VALUE)
00287         dw1 = (DWORD)m_pNPInstance;
00288       if(dw4 == DEFAULT_DWARG_VALUE)
00289         dw4 = (DWORD)&m_pStream;
00290       dwRet = NPN_NewStream((NPP)dw1, (char *)dw2, (char *)dw3, (NPStream **)dw4);
00291       break;
00292     case action_npn_destroy_stream:
00293       assert(m_pStream != NULL);
00294       if(dw1 == DEFAULT_DWARG_VALUE)
00295         dw1 = (DWORD)m_pNPInstance;
00296       if(dw2 == DEFAULT_DWARG_VALUE)
00297         dw2 = (DWORD)m_pStream;
00298       dwRet = NPN_DestroyStream((NPP)dw1, (NPStream *)dw2, (NPError)dw3);
00299       m_pStream = NULL;
00300       break;
00301     case action_npn_request_read:
00302       break;
00303     case action_npn_write:
00304       if(dw1 == DEFAULT_DWARG_VALUE)
00305         dw1 = (DWORD)m_pNPInstance;
00306       if(dw2 == DEFAULT_DWARG_VALUE)
00307         dw2 = (DWORD)m_pStream;
00308       dwRet = NPN_Write((NPP)dw1, (NPStream *)dw2, (int32)dw3, (void *)dw4);
00309       break;
00310     case action_npn_status:
00311       if(dw1 == DEFAULT_DWARG_VALUE)
00312         dw1 = (DWORD)m_pNPInstance;
00313       NPN_Status((NPP)dw1, (char *)dw2);
00314       break;
00315     case action_npn_user_agent:
00316       if(dw1 == DEFAULT_DWARG_VALUE)
00317         dw1 = (DWORD)m_pNPInstance;
00318       dwRet = (DWORD)NPN_UserAgent((NPP)dw1);
00319       break;
00320     case action_npn_mem_alloc:
00321       assert(m_pNPNAlloced == NULL);
00322       m_pNPNAlloced = NPN_MemAlloc((int32)dw1);
00323       dwRet = (DWORD)m_pNPNAlloced;
00324       if(m_pNPNAlloced != NULL)
00325       {
00326         for(int i = 0; i < (int)dw1; i++)
00327           *(((BYTE *)m_pNPNAlloced) + i) = 255;
00328       }
00329       break;
00330     case action_npn_mem_free:
00331       assert(m_pNPNAlloced != NULL);
00332       dw1 = (DWORD)m_pNPNAlloced;
00333       NPN_MemFree((void *)dw1);
00334       m_pNPNAlloced = NULL;
00335       break;
00336     case action_npn_mem_flush:
00337       dwRet = (DWORD)NPN_MemFlush((int32)dw1);
00338       break;
00339     case action_npn_reload_plugins:
00340       NPN_ReloadPlugins((NPBool)dw1);
00341       break;
00342     case action_npn_get_java_env:
00343       dwRet = (DWORD)NPN_GetJavaEnv();
00344       break;
00345     case action_npn_get_java_peer:
00346       if(dw1 == DEFAULT_DWARG_VALUE)
00347         dw1 = (DWORD)m_pNPInstance;
00348       dwRet = (DWORD)NPN_GetJavaPeer((NPP)dw1);
00349       break;
00350     case action_npn_get_value:
00351       if(dw1 == DEFAULT_DWARG_VALUE)
00352         dw1 = (DWORD)m_pNPInstance;
00353       if(dw3 == DEFAULT_DWARG_VALUE)
00354         dw3 = (DWORD)m_pValue;
00355       dwRet = (DWORD)NPN_GetValue((NPP)dw1, (NPNVariable)dw2, (void *)dw3);
00356       break;
00357     case action_npn_set_value:
00358       if(dw1 == DEFAULT_DWARG_VALUE)
00359         dw1 = (DWORD)m_pNPInstance;
00360       if(dw3 == DEFAULT_DWARG_VALUE)
00361         dw3 = (DWORD)m_pValue;
00362 
00363       // note that Mozilla expects boolean values not as a pointer to BOOL 
00364       // but rather as simply null and not null, let's convert
00365       if((dw2 == NPPVpluginWindowBool) ||
00366          (dw2 == NPPVpluginTransparentBool) ||
00367          (dw2 == NPPVpluginKeepLibraryInMemory)) {
00368         dwRet = (DWORD)NPN_SetValue((NPP)dw1, (NPPVariable)dw2, (void *)(*(BOOL *)dw3));
00369       } else
00370         dwRet = (DWORD)NPN_SetValue((NPP)dw1, (NPPVariable)dw2, (void *)dw3);
00371       break;
00372     case action_npn_invalidate_rect:
00373       if(dw1 == DEFAULT_DWARG_VALUE)
00374         dw1 = (DWORD)m_pNPInstance;
00375       NPN_InvalidateRect((NPP)dw1, (NPRect *)dw2);
00376       break;
00377     case action_npn_invalidate_region:
00378       if(dw1 == DEFAULT_DWARG_VALUE)
00379         dw1 = (DWORD)m_pNPInstance;
00380       NPN_InvalidateRegion((NPP)dw1, (NPRegion)dw2);
00381       break;
00382     case action_npn_force_redraw:
00383       if(dw1 == DEFAULT_DWARG_VALUE)
00384         dw1 = (DWORD)m_pNPInstance;
00385       NPN_ForceRedraw((NPP)dw1);
00386       break;
00387     default:
00388       assert(0);
00389       break;
00390   }
00391 
00392   DWORD dwTickReturn = XP_GetTickCount();
00393   pLogger->appendToLog(action, dwTickEnter, dwTickReturn, dwRet, dw1, dw2, dw3, dw4, dw5, dw6, dw7);
00394 
00395   return dwRet;
00396 }
00397 
00398 void CPluginBase::autoStartScriptIfNeeded()
00399 {
00400 }