123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- #include <windows.h>
- #include <windowsx.h>
- #include "plugin.h"
- #include "npupp.h"
- #include "nsError.h"
- #include <malloc.h>
- static NPIdentifier sGetVersion_id;
- #define BUFFER_LEN 1024
- 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);
- public:
- 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);
- protected:
- NPP mNpp;
- };
- #define DECLARE_NPOBJECT_CLASS_WITH_BASE(_class, ctor) \
- static NPClass s##_class##_NPClass = { \
- NP_CLASS_STRUCT_VERSION, \
- ctor, \
- ScriptablePluginObjectBase::_Deallocate, \
- ScriptablePluginObjectBase::_Invalidate, \
- ScriptablePluginObjectBase::_HasMethod, \
- ScriptablePluginObjectBase::_Invoke, \
- ScriptablePluginObjectBase::_InvokeDefault, \
- ScriptablePluginObjectBase::_HasProperty, \
- ScriptablePluginObjectBase::_GetProperty, \
- ScriptablePluginObjectBase::_SetProperty, \
- ScriptablePluginObjectBase::_RemoveProperty, \
- }
- #define GET_NPOBJECT_CLASS(_class) &s##_class##_NPClass
- 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;
- }
- 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);
- }
- class ScriptablePluginObject : public ScriptablePluginObjectBase
- {
- public:
- ScriptablePluginObject(NPP npp)
- : ScriptablePluginObjectBase(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);
- };
- static NPObject *
- AllocateScriptablePluginObject(NPP npp, NPClass *aClass)
- {
- return new ScriptablePluginObject(npp);
- }
- DECLARE_NPOBJECT_CLASS_WITH_BASE(ScriptablePluginObject,
- AllocateScriptablePluginObject);
- bool
- ScriptablePluginObject::HasMethod(NPIdentifier name)
- {
- return name == sGetVersion_id;
- }
- bool
- ScriptablePluginObject::HasProperty(NPIdentifier name)
- {
- return PR_FALSE;
- }
- bool
- ScriptablePluginObject::GetProperty(NPIdentifier name, NPVariant *result)
- {
- VOID_TO_NPVARIANT(*result);
-
- return PR_FALSE;
- }
- bool
- ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args,
- uint32_t argCount, NPVariant *result)
- {
- VOID_TO_NPVARIANT(*result);
- if (name == sGetVersion_id) {
- char csVersion[BUFFER_LEN];
- memset(&csVersion[0], '\0', BUFFER_LEN);
- DWORD BufferSize = BUFFER_LEN;
- DWORD cbData;
- bool keyFound = false;
- wchar_t exeName[] = L"\\winamp.exe";
- wchar_t fileName[BUFFER_LEN];
- memset(&fileName[0],'\0',BUFFER_LEN);
- wchar_t fileNameTemp[BUFFER_LEN];
- HKEY hKey;
- cbData = BUFFER_LEN;
-
-
-
- if (RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("winamp\\shell\\open\\command"), 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
- if ( RegQueryValueEx( hKey,
- TEXT(""),
- NULL,
- NULL,
- (LPBYTE) fileNameTemp,
- &cbData ) != ERROR_SUCCESS) {
- return PR_FALSE;
- }
- RegCloseKey (hKey);
- if (wcsstr(fileNameTemp,L"winamp.exe")) {
- int indexOfFirstQuote = wcscspn(fileNameTemp, L"\"");
- int indexOfSecondQuote = wcscspn(&fileNameTemp[indexOfFirstQuote+1], L"\"");
- if (indexOfFirstQuote >= 0) {
- keyFound = true;
- wcsncpy(fileName,&fileNameTemp[indexOfFirstQuote+1], indexOfSecondQuote);
- }
- } else {
-
-
- return PR_FALSE;
- }
- }
- if (!keyFound) {
-
- if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Winamp"), 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
- return PR_FALSE;
- }
- cbData = BUFFER_LEN;
- if ( RegQueryValueEx( hKey,
- TEXT(""),
- NULL,
- NULL,
- (LPBYTE) fileName,
- &cbData ) != ERROR_SUCCESS) {
- return PR_FALSE;
- }
- RegCloseKey (hKey);
- keyFound = true;
- wcscat(fileName,exeName);
- }
- if (!keyFound) {
- return PR_FALSE;
- }
- static TCHAR sBackSlash[] = {'\\','\0'};
- DWORD dwVersionDataLen = GetFileVersionInfoSize(fileName, NULL);
- if (dwVersionDataLen) {
- char* fvBuf = (char *)alloca(dwVersionDataLen);
- if (GetFileVersionInfo(fileName, 0, dwVersionDataLen, fvBuf)) {
-
- LPVOID pVal;
- UINT nValLen;
- if (VerQueryValue(fvBuf, sBackSlash, &pVal, &nValLen)) {
- if (nValLen == sizeof(VS_FIXEDFILEINFO)) {
- VS_FIXEDFILEINFO* pFixedFileInfo = (VS_FIXEDFILEINFO*)pVal;
-
-
-
- sprintf(csVersion, "%d.%d%d",
- HIWORD(pFixedFileInfo->dwFileVersionMS), LOWORD(pFixedFileInfo->dwFileVersionMS),
- HIWORD(pFixedFileInfo->dwFileVersionLS));
- }
- }
- }
- }
- size_t versionLen = (uint32)strlen(&csVersion[0]) + 1;
- char *targetResult = (char *) NPN_MemAlloc(versionLen);
- if (targetResult != NULL )
- memcpy(targetResult, csVersion, versionLen);
- else
- return PR_FALSE;
- STRINGZ_TO_NPVARIANT(targetResult, *result);
- return PR_TRUE;
- }
- return PR_FALSE;
- }
- bool
- ScriptablePluginObject::InvokeDefault(const NPVariant *args, uint32_t argCount,
- NPVariant *result)
- {
- VOID_TO_NPVARIANT(*result);
- return PR_FALSE;
- }
- CPlugin::CPlugin(NPP pNPInstance) :
- m_pNPInstance(pNPInstance),
- m_bInitialized(FALSE),
- m_pScriptableObject(NULL)
- {
-
- sGetVersion_id = NPN_GetStringIdentifier("getVersion");
- }
- CPlugin::~CPlugin()
- {
-
-
- if (m_pScriptableObject)
- NPN_ReleaseObject(m_pScriptableObject);
-
- }
- NPBool CPlugin::init(NPWindow* pNPWindow)
- {
- if(pNPWindow == NULL)
- return FALSE;
- m_Window = pNPWindow;
- m_bInitialized = TRUE;
- return TRUE;
- }
- void CPlugin::shut()
- {
- m_bInitialized = FALSE;
- }
- NPBool CPlugin::isInitialized()
- {
- return m_bInitialized;
- }
- int16 CPlugin::handleEvent(void* event)
- {
- return 0;
- }
- NPObject *
- CPlugin::GetScriptableObject()
- {
- if (!m_pScriptableObject) {
- m_pScriptableObject =
- NPN_CreateObject(m_pNPInstance,
- GET_NPOBJECT_CLASS(ScriptablePluginObject));
- }
- if (m_pScriptableObject) {
- NPN_RetainObject(m_pScriptableObject);
- }
- return m_pScriptableObject;
- }
|