1
0

plugin.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Netscape Public License
  6. * Version 1.1 (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/NPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the NPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the NPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. //////////////////////////////////////////////////
  38. //
  39. // CPlugin class implementation
  40. //
  41. #include <windows.h>
  42. #include <windowsx.h>
  43. #include "plugin.h"
  44. #include "npupp.h"
  45. #include "nsError.h"
  46. #include <malloc.h>
  47. static NPIdentifier sGetVersion_id;
  48. //static NPObject *sWindowObj;
  49. #define BUFFER_LEN 1024
  50. // Helper class that can be used to map calls to the NPObject hooks
  51. // into virtual methods on instances of classes that derive from this
  52. // class.
  53. class ScriptablePluginObjectBase : public NPObject
  54. {
  55. public:
  56. ScriptablePluginObjectBase(NPP npp)
  57. : mNpp(npp)
  58. {
  59. }
  60. virtual ~ScriptablePluginObjectBase()
  61. {
  62. }
  63. // Virtual NPObject hooks called through this base class. Override
  64. // as you see fit.
  65. virtual void Invalidate();
  66. virtual bool HasMethod(NPIdentifier name);
  67. virtual bool Invoke(NPIdentifier name, const NPVariant *args,
  68. uint32_t argCount, NPVariant *result);
  69. virtual bool InvokeDefault(const NPVariant *args, uint32_t argCount,
  70. NPVariant *result);
  71. virtual bool HasProperty(NPIdentifier name);
  72. virtual bool GetProperty(NPIdentifier name, NPVariant *result);
  73. virtual bool SetProperty(NPIdentifier name, const NPVariant *value);
  74. virtual bool RemoveProperty(NPIdentifier name);
  75. public:
  76. static void _Deallocate(NPObject *npobj);
  77. static void _Invalidate(NPObject *npobj);
  78. static bool _HasMethod(NPObject *npobj, NPIdentifier name);
  79. static bool _Invoke(NPObject *npobj, NPIdentifier name,
  80. const NPVariant *args, uint32_t argCount,
  81. NPVariant *result);
  82. static bool _InvokeDefault(NPObject *npobj, const NPVariant *args,
  83. uint32_t argCount, NPVariant *result);
  84. static bool _HasProperty(NPObject * npobj, NPIdentifier name);
  85. static bool _GetProperty(NPObject *npobj, NPIdentifier name,
  86. NPVariant *result);
  87. static bool _SetProperty(NPObject *npobj, NPIdentifier name,
  88. const NPVariant *value);
  89. static bool _RemoveProperty(NPObject *npobj, NPIdentifier name);
  90. protected:
  91. NPP mNpp;
  92. };
  93. #define DECLARE_NPOBJECT_CLASS_WITH_BASE(_class, ctor) \
  94. static NPClass s##_class##_NPClass = { \
  95. NP_CLASS_STRUCT_VERSION, \
  96. ctor, \
  97. ScriptablePluginObjectBase::_Deallocate, \
  98. ScriptablePluginObjectBase::_Invalidate, \
  99. ScriptablePluginObjectBase::_HasMethod, \
  100. ScriptablePluginObjectBase::_Invoke, \
  101. ScriptablePluginObjectBase::_InvokeDefault, \
  102. ScriptablePluginObjectBase::_HasProperty, \
  103. ScriptablePluginObjectBase::_GetProperty, \
  104. ScriptablePluginObjectBase::_SetProperty, \
  105. ScriptablePluginObjectBase::_RemoveProperty, \
  106. }
  107. #define GET_NPOBJECT_CLASS(_class) &s##_class##_NPClass
  108. void
  109. ScriptablePluginObjectBase::Invalidate()
  110. {
  111. }
  112. bool
  113. ScriptablePluginObjectBase::HasMethod(NPIdentifier name)
  114. {
  115. return false;
  116. }
  117. bool
  118. ScriptablePluginObjectBase::Invoke(NPIdentifier name, const NPVariant *args,
  119. uint32_t argCount, NPVariant *result)
  120. {
  121. return false;
  122. }
  123. bool
  124. ScriptablePluginObjectBase::InvokeDefault(const NPVariant *args,
  125. uint32_t argCount, NPVariant *result)
  126. {
  127. return false;
  128. }
  129. bool
  130. ScriptablePluginObjectBase::HasProperty(NPIdentifier name)
  131. {
  132. return false;
  133. }
  134. bool
  135. ScriptablePluginObjectBase::GetProperty(NPIdentifier name, NPVariant *result)
  136. {
  137. return false;
  138. }
  139. bool
  140. ScriptablePluginObjectBase::SetProperty(NPIdentifier name,
  141. const NPVariant *value)
  142. {
  143. return false;
  144. }
  145. bool
  146. ScriptablePluginObjectBase::RemoveProperty(NPIdentifier name)
  147. {
  148. return false;
  149. }
  150. // static
  151. void
  152. ScriptablePluginObjectBase::_Deallocate(NPObject *npobj)
  153. {
  154. // Call the virtual destructor.
  155. delete (ScriptablePluginObjectBase *)npobj;
  156. }
  157. // static
  158. void
  159. ScriptablePluginObjectBase::_Invalidate(NPObject *npobj)
  160. {
  161. ((ScriptablePluginObjectBase *)npobj)->Invalidate();
  162. }
  163. // static
  164. bool
  165. ScriptablePluginObjectBase::_HasMethod(NPObject *npobj, NPIdentifier name)
  166. {
  167. return ((ScriptablePluginObjectBase *)npobj)->HasMethod(name);
  168. }
  169. // static
  170. bool
  171. ScriptablePluginObjectBase::_Invoke(NPObject *npobj, NPIdentifier name,
  172. const NPVariant *args, uint32_t argCount,
  173. NPVariant *result)
  174. {
  175. return ((ScriptablePluginObjectBase *)npobj)->Invoke(name, args, argCount,
  176. result);
  177. }
  178. // static
  179. bool
  180. ScriptablePluginObjectBase::_InvokeDefault(NPObject *npobj,
  181. const NPVariant *args,
  182. uint32_t argCount,
  183. NPVariant *result)
  184. {
  185. return ((ScriptablePluginObjectBase *)npobj)->InvokeDefault(args, argCount,
  186. result);
  187. }
  188. // static
  189. bool
  190. ScriptablePluginObjectBase::_HasProperty(NPObject * npobj, NPIdentifier name)
  191. {
  192. return ((ScriptablePluginObjectBase *)npobj)->HasProperty(name);
  193. }
  194. // static
  195. bool
  196. ScriptablePluginObjectBase::_GetProperty(NPObject *npobj, NPIdentifier name,
  197. NPVariant *result)
  198. {
  199. return ((ScriptablePluginObjectBase *)npobj)->GetProperty(name, result);
  200. }
  201. // static
  202. bool
  203. ScriptablePluginObjectBase::_SetProperty(NPObject *npobj, NPIdentifier name,
  204. const NPVariant *value)
  205. {
  206. return ((ScriptablePluginObjectBase *)npobj)->SetProperty(name, value);
  207. }
  208. // static
  209. bool
  210. ScriptablePluginObjectBase::_RemoveProperty(NPObject *npobj, NPIdentifier name)
  211. {
  212. return ((ScriptablePluginObjectBase *)npobj)->RemoveProperty(name);
  213. }
  214. class ScriptablePluginObject : public ScriptablePluginObjectBase
  215. {
  216. public:
  217. ScriptablePluginObject(NPP npp)
  218. : ScriptablePluginObjectBase(npp)
  219. {
  220. }
  221. virtual bool HasMethod(NPIdentifier name);
  222. virtual bool HasProperty(NPIdentifier name);
  223. virtual bool GetProperty(NPIdentifier name, NPVariant *result);
  224. virtual bool Invoke(NPIdentifier name, const NPVariant *args,
  225. uint32_t argCount, NPVariant *result);
  226. virtual bool InvokeDefault(const NPVariant *args, uint32_t argCount,
  227. NPVariant *result);
  228. };
  229. static NPObject *
  230. AllocateScriptablePluginObject(NPP npp, NPClass *aClass)
  231. {
  232. return new ScriptablePluginObject(npp);
  233. }
  234. DECLARE_NPOBJECT_CLASS_WITH_BASE(ScriptablePluginObject,
  235. AllocateScriptablePluginObject);
  236. bool
  237. ScriptablePluginObject::HasMethod(NPIdentifier name)
  238. {
  239. return name == sGetVersion_id;
  240. }
  241. bool
  242. ScriptablePluginObject::HasProperty(NPIdentifier name)
  243. {
  244. return PR_FALSE;
  245. }
  246. bool
  247. ScriptablePluginObject::GetProperty(NPIdentifier name, NPVariant *result)
  248. {
  249. VOID_TO_NPVARIANT(*result);
  250. return PR_FALSE;
  251. }
  252. bool
  253. ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args,
  254. uint32_t argCount, NPVariant *result)
  255. {
  256. VOID_TO_NPVARIANT(*result);
  257. if (name == sGetVersion_id) {
  258. char csVersion[BUFFER_LEN];
  259. memset(&csVersion[0], '\0', BUFFER_LEN);
  260. DWORD BufferSize = BUFFER_LEN;
  261. DWORD cbData;
  262. bool keyFound = false;
  263. wchar_t exeName[] = L"\\winamp.exe";
  264. wchar_t fileName[BUFFER_LEN];
  265. memset(&fileName[0],'\0',BUFFER_LEN);
  266. wchar_t fileNameTemp[BUFFER_LEN];
  267. HKEY hKey;
  268. cbData = BUFFER_LEN;
  269. // first check the protocol handler registry key, we're looking for
  270. // the winamp:// protocol handler. If we find this, then this is the
  271. // "right" exe for winamp we need to get the version number on
  272. if (RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("winamp\\shell\\open\\command"), 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
  273. if ( RegQueryValueEx( hKey,
  274. TEXT(""),
  275. NULL,
  276. NULL,
  277. (LPBYTE) fileNameTemp,
  278. &cbData ) != ERROR_SUCCESS) {
  279. return PR_FALSE;
  280. }
  281. RegCloseKey (hKey);
  282. if (wcsstr(fileNameTemp,L"winamp.exe")) {
  283. int indexOfFirstQuote = wcscspn(fileNameTemp, L"\"");
  284. int indexOfSecondQuote = wcscspn(&fileNameTemp[indexOfFirstQuote+1], L"\"");
  285. if (indexOfFirstQuote >= 0) {
  286. keyFound = true;
  287. wcsncpy(fileName,&fileNameTemp[indexOfFirstQuote+1], indexOfSecondQuote);
  288. }
  289. } else {
  290. // some other app (itunes ??) controlling the winamp:// protocol
  291. // return error
  292. return PR_FALSE;
  293. }
  294. }
  295. if (!keyFound) {
  296. // See if the reg key exists
  297. if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Winamp"), 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
  298. return PR_FALSE;
  299. }
  300. cbData = BUFFER_LEN;
  301. if ( RegQueryValueEx( hKey,
  302. TEXT(""),
  303. NULL,
  304. NULL,
  305. (LPBYTE) fileName,
  306. &cbData ) != ERROR_SUCCESS) {
  307. return PR_FALSE;
  308. }
  309. RegCloseKey (hKey);
  310. keyFound = true;
  311. wcscat(fileName,exeName);
  312. }
  313. if (!keyFound) {
  314. return PR_FALSE;
  315. }
  316. static TCHAR sBackSlash[] = {'\\','\0'};
  317. DWORD dwVersionDataLen = GetFileVersionInfoSize(fileName, NULL);
  318. if (dwVersionDataLen) {
  319. char* fvBuf = (char *)alloca(dwVersionDataLen);
  320. if (GetFileVersionInfo(fileName, 0, dwVersionDataLen, fvBuf)) {
  321. LPVOID pVal;
  322. UINT nValLen;
  323. if (VerQueryValue(fvBuf, sBackSlash, &pVal, &nValLen)) {
  324. if (nValLen == sizeof(VS_FIXEDFILEINFO)) {
  325. VS_FIXEDFILEINFO* pFixedFileInfo = (VS_FIXEDFILEINFO*)pVal;
  326. //sprintf(csVersion, "%d.%d.%d.%d",
  327. //HIWORD(pFixedFileInfo->dwFileVersionMS), LOWORD(pFixedFileInfo->dwFileVersionMS),
  328. //HIWORD(pFixedFileInfo->dwFileVersionLS), LOWORD(pFixedFileInfo->dwFileVersionLS));
  329. sprintf(csVersion, "%d.%d%d",
  330. HIWORD(pFixedFileInfo->dwFileVersionMS), LOWORD(pFixedFileInfo->dwFileVersionMS),
  331. HIWORD(pFixedFileInfo->dwFileVersionLS));
  332. }
  333. }
  334. }
  335. }
  336. size_t versionLen = (uint32)strlen(&csVersion[0]) + 1;
  337. char *targetResult = (char *) NPN_MemAlloc(versionLen);
  338. if (targetResult != NULL )
  339. memcpy(targetResult, csVersion, versionLen);
  340. else
  341. return PR_FALSE;
  342. STRINGZ_TO_NPVARIANT(targetResult, *result);
  343. return PR_TRUE;
  344. }
  345. return PR_FALSE;
  346. }
  347. bool
  348. ScriptablePluginObject::InvokeDefault(const NPVariant *args, uint32_t argCount,
  349. NPVariant *result)
  350. {
  351. VOID_TO_NPVARIANT(*result);
  352. return PR_FALSE;
  353. }
  354. CPlugin::CPlugin(NPP pNPInstance) :
  355. m_pNPInstance(pNPInstance),
  356. m_bInitialized(FALSE),
  357. m_pScriptableObject(NULL)
  358. {
  359. //NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &sWindowObj);
  360. sGetVersion_id = NPN_GetStringIdentifier("getVersion");
  361. }
  362. CPlugin::~CPlugin()
  363. {
  364. //if (sWindowObj)
  365. // NPN_ReleaseObject(sWindowObj);
  366. if (m_pScriptableObject)
  367. NPN_ReleaseObject(m_pScriptableObject);
  368. //sWindowObj = 0;
  369. }
  370. NPBool CPlugin::init(NPWindow* pNPWindow)
  371. {
  372. if(pNPWindow == NULL)
  373. return FALSE;
  374. m_Window = pNPWindow;
  375. m_bInitialized = TRUE;
  376. return TRUE;
  377. }
  378. void CPlugin::shut()
  379. {
  380. m_bInitialized = FALSE;
  381. }
  382. NPBool CPlugin::isInitialized()
  383. {
  384. return m_bInitialized;
  385. }
  386. int16 CPlugin::handleEvent(void* event)
  387. {
  388. return 0;
  389. }
  390. NPObject *
  391. CPlugin::GetScriptableObject()
  392. {
  393. if (!m_pScriptableObject) {
  394. m_pScriptableObject =
  395. NPN_CreateObject(m_pNPInstance,
  396. GET_NPOBJECT_CLASS(ScriptablePluginObject));
  397. }
  398. if (m_pScriptableObject) {
  399. NPN_RetainObject(m_pScriptableObject);
  400. }
  401. return m_pScriptableObject;
  402. }