plugin.cpp 7.43 KB
Newer Older
李晓兵's avatar
李晓兵 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
#include "plugin.h"
#include "tokenizer.h"

#ifdef _WINDOWS
#include <windows.h>
BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD ul_reason_for_call,
                       LPVOID lpReserved )
{
    return TRUE;
}
#else
#include <errno.h>
#include <string.h>

extern int errno;
#endif

SendPluginEv SendPluginEvent;

string g_GetSysErrMsg( void )
{
    string strError = "Unknown";
    // Problem loading
#ifdef _WINDOWS
    int nErrorCode = GetLastError();
    LPTSTR s;
    if ( ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
    NULL, nErrorCode, 0, ( LPTSTR ) &s, 0, NULL ) )
    {
        strError = s;
    }
    else
    {
        char szBuf[ 20 ];
        _snprintf_s( szBuf, _countof(szBuf), 19, "%d", nErrorCode );
        strError = szBuf;
    }
#else
    char szError[80];
    if ( strerror_r( errno, szError, sizeof(szError)  ) )
    {
        strError = "no description found";
    }
    else
    {
        strError = szError;
    }
#endif
    return strError;
}

void g_sleep( unsigned int mseconds )
{
#ifdef _WINDOWS
    Sleep( mseconds );
#else
    usleep( mseconds * 1000 );
#endif
}

string& g_trim( string& str )
{
    // Whitespace characters
    char whspc[] = " \t\r\n\v\f";

    // Whack off first part
    size_t pos = str.find_first_not_of( whspc );

    if ( pos != string::npos )
        str.replace( 0, pos, "" );

    // Whack off trailing stuff
    pos = str.find_last_not_of( whspc );

    if ( pos != string::npos )
        str.replace( pos + 1, str.length() - pos, "" );

    return str;
}

void g_tokenize( const string& str, const string& delimiters, vector<string>& tokens )
{
    tokenize( str, tokens, delimiters );
}

char* SetEventFunc( SendPluginEv funcPtr )
{
    static char * szObjList = onGetObjList();
    SendPluginEvent = funcPtr;
    return szObjList;
}


const int nMAXSIZE = 512;
char* g_pszRetVal = NULL;

//-----------------------------------------------------------
// Map from an object Id to an object instance
//-----------------------------------------------------------
typedef std::map<string, JSExt*> StringToJExt_T;

//-----------------------------------------------------------
// Map from a browser context to an id mapping
//-----------------------------------------------------------
typedef std::map<void*, StringToJExt_T*> VoidToMap_T;

VoidToMap_T g_context2Map;

class GlobalSharedModule
{

public:
    GlobalSharedModule( void )
    {
        g_pszRetVal = new char[ nMAXSIZE ];
    }

    ~GlobalSharedModule()
    {
        delete [] g_pszRetVal;

        VoidToMap_T::iterator posMaps;

        for ( posMaps = g_context2Map.begin(); posMaps != g_context2Map.end(); ++posMaps )
        {
            StringToJExt_T& id2Obj = *posMaps->second;
            StringToJExt_T::iterator posMap;

            for ( posMap = id2Obj.begin(); posMap != id2Obj.end(); ++posMap )
            {
                JSExt* pJSExt = posMap->second;

                if ( pJSExt->CanDelete() )
                {
                    delete pJSExt;
                }
            }

            id2Obj.erase( id2Obj.begin(), id2Obj.end() );
        }

        g_context2Map.erase( g_context2Map.begin(), g_context2Map.end() );
    }
};

GlobalSharedModule g_sharedModule;

char* g_str2global( const string& strRetVal )
{
    int nLen = strRetVal.size();

    if ( nLen >= nMAXSIZE )
    {
        delete [] g_pszRetVal;
        g_pszRetVal = new char[ nLen + 1 ];
    }

    else
    {
        // To minimaize the number of memory reallocations, the assumption
        // is that in most times this will be the case
        delete [] g_pszRetVal;
        g_pszRetVal = new char[ nMAXSIZE ];
    }

    strcpy( g_pszRetVal, strRetVal.c_str() );
    return g_pszRetVal;
}

bool g_unregisterObject( const string& strObjId, void* pContext )
{
    // Called by the plugin extension implementation
    // if the extension handles the deletion of its object

    StringToJExt_T * pID2Obj = NULL;

    VoidToMap_T::iterator iter = g_context2Map.find( pContext );

    if ( iter != g_context2Map.end() )
    {
        pID2Obj = iter->second;
    }
    else
    {
        return false;
    }

    StringToJExt_T& mapID2Obj = *pID2Obj;

    StringToJExt_T::iterator r = mapID2Obj.find( strObjId );

    if ( r == mapID2Obj.end() )
    {
        return false;
    }

    mapID2Obj.erase( strObjId );
    return true;
}

char* InvokeFunction( const char* szCommand, void* pContext )
{
    StringToJExt_T * pID2Obj = NULL;

    VoidToMap_T::iterator iter = g_context2Map.find( pContext );

    if ( iter != g_context2Map.end() )
    {
        pID2Obj = iter->second;
    }
    else
    {
        pID2Obj = new StringToJExt_T;
        g_context2Map[ pContext ] = pID2Obj;
    }

    StringToJExt_T& mapID2Obj = *pID2Obj;

    string strFullCommand = szCommand;
    vector<string> arParams;
    g_tokenize( strFullCommand, " ", arParams );
    string strCommand = arParams[ 0 ];
    string strRetVal = szERROR;

    if ( strCommand == szCREATE )
    {
        string strClassName = arParams[ 1 ];
        string strObjId = arParams[ 2 ];

        StringToJExt_T::iterator r = mapID2Obj.find( strObjId );

        if ( r != mapID2Obj.end() )
        {
            strRetVal += strObjId;
            strRetVal += " :Object already exists.";
            return g_str2global( strRetVal );
        }

        JSExt* pJSExt = onCreateObject( strClassName, strObjId );

        if ( pJSExt == NULL )
        {
            strRetVal += strObjId;
            strRetVal += " :Unknown object type ";
            strRetVal += strClassName;
            return g_str2global( strRetVal );
        }

        pJSExt->m_pContext = pContext;
        mapID2Obj[ strObjId ] = pJSExt;

        strRetVal = szOK;
        strRetVal += strObjId;
        return g_str2global( strRetVal );
    }
    else
    if ( strCommand == szINVOKE )
    {
        string strObjId = arParams[ 1 ];
        string strMethod = arParams[ 2 ];

        StringToJExt_T::iterator r = mapID2Obj.find( strObjId );

        if ( r == mapID2Obj.end() )
        {
            strRetVal += strObjId;
            strRetVal += " :No object found for id.";
            return g_str2global( strRetVal );
        }

        JSExt* pJSExt = r->second;

        size_t nLoc = strFullCommand.find( strObjId );

        if ( nLoc == string::npos )
        {
            strRetVal += strObjId;
            strRetVal += " :Internal InvokeMethod error.";
            return g_str2global( strRetVal );
        }

        if ( strMethod == szDISPOSE )
        {
            StringToJExt_T::iterator r = mapID2Obj.find( strObjId );

            if ( r == mapID2Obj.end() )
            {
                strRetVal = szERROR;
                strRetVal += strObjId;
                return g_str2global( strRetVal );
            }

            JSExt * pJSExt = mapID2Obj[ strObjId ];

            if ( pJSExt->CanDelete() )
            {
                delete pJSExt;
            }

            mapID2Obj.erase( strObjId );
            strRetVal = szOK;
            strRetVal += strObjId;
            return g_str2global( strRetVal );
        }

        size_t nSuffixLoc = nLoc + strObjId.size();
        string strInvoke = strFullCommand.substr( nSuffixLoc );
        strInvoke = g_trim( strInvoke );
        strRetVal = pJSExt->InvokeMethod( strInvoke );
        return g_str2global( strRetVal );
    }

    strRetVal += " :Unknown command ";
    strRetVal += strCommand;
    return g_str2global( strRetVal );
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%