service.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "main.h"
  2. #include "service.h"
  3. #include "api__gen_ml.h"
  4. #include "../winamp/wa_ipc.h"
  5. #include <strsafe.h>
  6. #define IS_INVALIDISPATCH(__disp) (((IDispatch *)1) == (__disp) || NULL == (__disp))
  7. OmService::OmService(UINT nId)
  8. : ref(1), id(nId), name(NULL), url(NULL)
  9. {
  10. }
  11. OmService::~OmService()
  12. {
  13. free(name);
  14. free(url);
  15. }
  16. HRESULT OmService::CreateInstance(UINT nId, LPCWSTR pszName, OmService **instance)
  17. {
  18. if (NULL == instance) return E_POINTER;
  19. *instance = NULL;
  20. OmService *service = new OmService(nId);
  21. if (NULL == service) return E_OUTOFMEMORY;
  22. service->SetName(pszName);
  23. wchar_t url[256] = {0};
  24. if (nId == SERVICE_LABS)
  25. {
  26. lstrcpynW(url, L"http://www.winamp.com/labs/pc", 256);
  27. }
  28. service->SetUrl(url);
  29. *instance = service;
  30. return S_OK;
  31. }
  32. size_t OmService::AddRef()
  33. {
  34. return InterlockedIncrement((LONG*)&ref);
  35. }
  36. size_t OmService::Release()
  37. {
  38. if (0 == ref)
  39. return ref;
  40. LONG r = InterlockedDecrement((LONG*)&ref);
  41. if (0 == r)
  42. delete(this);
  43. return r;
  44. }
  45. int OmService::QueryInterface(GUID interface_guid, void **object)
  46. {
  47. if (NULL == object) return E_POINTER;
  48. if (IsEqualIID(interface_guid, IFC_OmService))
  49. *object = static_cast<ifc_omservice*>(this);
  50. else
  51. {
  52. *object = NULL;
  53. return E_NOINTERFACE;
  54. }
  55. if (NULL == *object)
  56. return E_UNEXPECTED;
  57. AddRef();
  58. return S_OK;
  59. }
  60. unsigned int OmService::GetId()
  61. {
  62. return id;
  63. }
  64. HRESULT OmService::GetName(wchar_t *pszBuffer, int cchBufferMax)
  65. {
  66. return StringCchCopyW(pszBuffer, cchBufferMax, name);
  67. }
  68. HRESULT OmService::GetUrl(wchar_t *pszBuffer, int cchBufferMax)
  69. {
  70. return StringCchCopyExW(pszBuffer, cchBufferMax, url, NULL, NULL, STRSAFE_IGNORE_NULLS);
  71. }
  72. HRESULT OmService::GetIcon(wchar_t *pszBuffer, int cchBufferMax)
  73. {
  74. return E_FAIL;
  75. }
  76. HRESULT OmService::GetExternal(IDispatch **ppDispatch)
  77. {
  78. if (NULL == ppDispatch)
  79. return E_POINTER;
  80. *ppDispatch = NULL;
  81. // try JSAPI2 first
  82. WCHAR szBuffer[64] = {0};
  83. if (SUCCEEDED(StringCchPrintfW(szBuffer, ARRAYSIZE(szBuffer), L"%u", id)))
  84. {
  85. *ppDispatch = (IDispatch*)SendMessage(plugin.hwndParent, WM_WA_IPC, (WPARAM)szBuffer, IPC_JSAPI2_GET_DISPATCH_OBJECT);
  86. AGAVE_API_JSAPI2_SECURITY->SetBypass(szBuffer, true);
  87. }
  88. else
  89. return E_FAIL;
  90. return S_OK;
  91. }
  92. HRESULT OmService::SetName(LPCWSTR pszName)
  93. {
  94. free(name);
  95. name = wcsdup(pszName);
  96. return S_OK;
  97. }
  98. HRESULT OmService::SetUrl(LPCWSTR pszUrl)
  99. {
  100. free(url);
  101. url = wcsdup(pszUrl);
  102. return S_OK;
  103. }
  104. HRESULT OmService::SetIcon(LPCWSTR pszIcon)
  105. {
  106. return S_OK;
  107. }
  108. #define CBCLASS OmService
  109. START_DISPATCH;
  110. CB(ADDREF, AddRef)
  111. CB(RELEASE, Release)
  112. CB(QUERYINTERFACE, QueryInterface)
  113. CB(API_GETID, GetId)
  114. CB(API_GETNAME, GetName)
  115. CB(API_GETURL, GetUrl)
  116. CB(API_GETICON, GetIcon)
  117. CB(API_GETEXTERNAL, GetExternal)
  118. END_DISPATCH;
  119. #undef CBCLASS