1
0

service.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "main.h"
  2. #include "./service.h"
  3. #include "api__ml_wire.h"
  4. #include "./util.h"
  5. #include "./resource.h"
  6. #include "./externalCOM.h"
  7. #include "../winamp/wa_ipc.h"
  8. #include <strsafe.h>
  9. #define IS_INVALIDISPATCH(__disp) (((IDispatch *)1) == (__disp) || NULL == (__disp))
  10. OmService::OmService( UINT nId ) : id( nId )
  11. {}
  12. OmService::~OmService()
  13. {
  14. Plugin_FreeResString( name );
  15. Plugin_FreeResString( url );
  16. Plugin_FreeResString( icon );
  17. }
  18. HRESULT OmService::CreateRemote( UINT nId, LPCWSTR pszName, LPCWSTR pszIcon, LPCWSTR pszUrl, OmService **instance )
  19. {
  20. if ( instance == NULL )
  21. return E_POINTER;
  22. *instance = NULL;
  23. if ( nId == 0 || pszName == NULL )
  24. return E_INVALIDARG;
  25. OmService *service = new OmService( nId );
  26. if ( service == NULL )
  27. return E_OUTOFMEMORY;
  28. service->SetName( pszName );
  29. service->SetIcon( pszIcon );
  30. service->SetUrl( pszUrl );
  31. *instance = service;
  32. return S_OK;
  33. }
  34. HRESULT OmService::CreateLocal( UINT nId, LPCWSTR pszName, LPCWSTR pszIcon, SVCWNDCREATEPROC windowCreator, OmService **instance )
  35. {
  36. if ( instance == NULL )
  37. return E_POINTER;
  38. *instance = NULL;
  39. if ( nId == 0 || pszName == NULL )
  40. return E_INVALIDARG;
  41. OmService *service = new OmService( nId );
  42. if ( service == NULL )
  43. return E_OUTOFMEMORY;
  44. service->SetFlags( flagLocal, flagLocal );
  45. service->SetName( pszName );
  46. service->SetIcon( pszIcon );
  47. service->SetWindowCreator( windowCreator );
  48. *instance = service;
  49. return S_OK;
  50. }
  51. size_t OmService::AddRef()
  52. {
  53. return _ref.fetch_add( 1 );
  54. }
  55. size_t OmService::Release()
  56. {
  57. if ( _ref.load() == 0 )
  58. return _ref.load();
  59. LONG r = _ref.fetch_sub( 1 );
  60. if ( r == 0 )
  61. delete( this );
  62. return r;
  63. }
  64. int OmService::QueryInterface( GUID interface_guid, void **object )
  65. {
  66. if ( object == NULL )
  67. return E_POINTER;
  68. if ( IsEqualIID( interface_guid, IFC_OmService ) )
  69. *object = static_cast<ifc_omservice *>( this );
  70. else
  71. {
  72. *object = NULL;
  73. return E_NOINTERFACE;
  74. }
  75. if ( *object == NULL )
  76. return E_UNEXPECTED;
  77. AddRef();
  78. return S_OK;
  79. }
  80. unsigned int OmService::GetId()
  81. {
  82. return id;
  83. }
  84. HRESULT OmService::GetName( wchar_t *pszBuffer, int cchBufferMax )
  85. {
  86. return Plugin_CopyResString( pszBuffer, cchBufferMax, name );
  87. }
  88. HRESULT OmService::GetUrl( wchar_t *pszBuffer, int cchBufferMax )
  89. {
  90. return Plugin_CopyResString( pszBuffer, cchBufferMax, url );
  91. }
  92. HRESULT OmService::GetIcon( wchar_t *pszBuffer, int cchBufferMax )
  93. {
  94. if ( icon != NULL && IS_INTRESOURCE( icon ) )
  95. {
  96. WCHAR szPath[ 2 * MAX_PATH ] = { 0 };
  97. if ( GetModuleFileName( plugin.hDllInstance, szPath, ARRAYSIZE( szPath ) ) == 0 )
  98. return E_FAIL;
  99. return StringCchPrintf( pszBuffer, cchBufferMax, L"res://%s/#%d/#%d", szPath, RT_RCDATA, icon );
  100. }
  101. return StringCchCopyEx( pszBuffer, cchBufferMax, icon, NULL, NULL, STRSAFE_IGNORE_NULLS );
  102. }
  103. HRESULT OmService::GetExternal( IDispatch **ppDispatch )
  104. {
  105. if ( ppDispatch == NULL )
  106. return E_POINTER;
  107. *ppDispatch = NULL;
  108. HWND hWinamp = plugin.hwndWinampParent;
  109. if ( hWinamp == NULL )
  110. return E_UNEXPECTED;
  111. //////*ppDispatch = (IDispatch*)SENDWAIPC(hWinamp, IPC_GET_DISPATCH_OBJECT, 0);
  112. WCHAR szBuffer[ 64 ] = { 0 };
  113. if ( SUCCEEDED( StringCchPrintfW( szBuffer, ARRAYSIZE( szBuffer ), L"%u", id ) ) )
  114. *ppDispatch = (IDispatch *) SENDWAIPC( hWinamp, IPC_JSAPI2_GET_DISPATCH_OBJECT, (WPARAM) szBuffer );
  115. if (IS_INVALIDISPATCH(*ppDispatch) && FAILED(ExternalCOM::CreateInstance((ExternalCOM**)ppDispatch)))
  116. {
  117. *ppDispatch = NULL;
  118. return E_FAIL;
  119. }
  120. return S_OK;
  121. }
  122. HRESULT OmService::SetName( LPCWSTR pszName )
  123. {
  124. Plugin_FreeResString( name );
  125. name = Plugin_DuplicateResString( pszName );
  126. return S_OK;
  127. }
  128. HRESULT OmService::SetUrl( LPCWSTR pszUrl )
  129. {
  130. Plugin_FreeResString( url );
  131. url = Plugin_DuplicateResString( pszUrl );
  132. return S_OK;
  133. }
  134. HRESULT OmService::SetIcon( LPCWSTR pszIcon )
  135. {
  136. Plugin_FreeResString( icon );
  137. icon = Plugin_DuplicateResString( pszIcon );
  138. return S_OK;
  139. }
  140. void OmService::SetFlags( UINT mask, UINT newFlags )
  141. {
  142. flags = ( flags & ~mask ) | ( mask & newFlags );
  143. }
  144. UINT OmService::GetFlags( void )
  145. {
  146. return flags;
  147. }
  148. HRESULT OmService::SetWindowCreator( SVCWNDCREATEPROC proc )
  149. {
  150. windowCreator = proc;
  151. return S_OK;
  152. }
  153. HRESULT OmService::GetWindowCreator( SVCWNDCREATEPROC *proc )
  154. {
  155. if ( proc == NULL )
  156. return E_INVALIDARG;
  157. *proc = windowCreator;
  158. return S_OK;
  159. }
  160. HRESULT OmService::CreateView( HWND hParent, HWND *hView )
  161. {
  162. if ( hView == NULL )
  163. return E_POINTER;
  164. *hView = NULL;
  165. HRESULT hr = S_OK;
  166. if ( ( flagLocal & flags ) != 0 )
  167. {
  168. if ( windowCreator != NULL )
  169. {
  170. *hView = windowCreator( hParent, this );
  171. if ( *hView == NULL )
  172. hr = E_FAIL;
  173. }
  174. else
  175. hr = E_INVALIDARG;
  176. }
  177. else
  178. {
  179. if ( OMBROWSERMNGR != NULL )
  180. {
  181. hr = OMBROWSERMNGR->Initialize( NULL, plugin.hwndWinampParent );
  182. if ( SUCCEEDED( hr ) )
  183. hr = OMBROWSERMNGR->CreateView( this, hParent, NULL, 0, hView );
  184. }
  185. else
  186. hr = E_UNEXPECTED;
  187. }
  188. return hr;
  189. }
  190. #define CBCLASS OmService
  191. START_DISPATCH;
  192. CB( ADDREF, AddRef )
  193. CB( RELEASE, Release )
  194. CB( QUERYINTERFACE, QueryInterface )
  195. CB( API_GETID, GetId )
  196. CB( API_GETNAME, GetName )
  197. CB( API_GETURL, GetUrl )
  198. CB( API_GETICON, GetIcon )
  199. CB( API_GETEXTERNAL, GetExternal )
  200. END_DISPATCH;
  201. #undef CBCLASS