JSAPI2_CallbackManager.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "../nu/AutoLock.h"
  3. #include <vector>
  4. #include "../Components/wac_downloadManager/wac_downloadManager_api.h"
  5. extern "C" HANDLE DuplicateCurrentThread();
  6. namespace JSAPI2
  7. {
  8. template <class API> struct CallbackInfo
  9. {
  10. CallbackInfo()
  11. {
  12. api = 0;
  13. threadId = 0;
  14. threadHandle = 0;
  15. }
  16. CallbackInfo(API *me)
  17. {
  18. api = me;
  19. threadId = GetCurrentThreadId();
  20. threadHandle = DuplicateCurrentThread();
  21. }
  22. ~CallbackInfo()
  23. {
  24. CloseHandle(threadHandle);
  25. threadHandle = 0;
  26. }
  27. API *api;
  28. DWORD threadId;
  29. HANDLE threadHandle;
  30. };
  31. class TransportAPI;
  32. class MediaCoreAPI;
  33. class AsyncDownloaderAPI;
  34. class CallbackManager
  35. {
  36. public:
  37. CallbackManager();
  38. public:
  39. /** stuff for Winamp to call to trigger callbacks
  40. ** these are primarily responsible for getting over to the correct thread
  41. ** to keep that particular logic out of the various functions
  42. */
  43. void OnStop(int position, int is_full_stop);
  44. void OnPlay(const wchar_t *filename);
  45. void OnPause(bool pause_state);
  46. /** Stuff that's OK to call on any thread
  47. */
  48. bool OverrideMetadata(const wchar_t *filename, const wchar_t *tag, wchar_t *out, size_t outCch);
  49. /** stuff for Winamp to call to trigger callbacks
  50. ** these are primarily responsible for getting over to the correct thread
  51. ** to keep that particular logic out of the various functions
  52. */
  53. void OnInit(const wchar_t *url, const wchar_t *onlinesvcId);
  54. void OnConnect(const wchar_t *url, const wchar_t *onlinesvcId);
  55. void OnData(const wchar_t *url, size_t downloadedlen, size_t totallen, const wchar_t *onlinesvcId);
  56. void OnCancel(const wchar_t *url, const wchar_t *onlinesvcId);
  57. void OnError(const wchar_t *url, int error, const wchar_t *onlinesvcId);
  58. void OnFinish(const wchar_t *url, const wchar_t *destfilename, const wchar_t *onlinesvcId);
  59. public:
  60. /* stuff for other JSAPI2 classes to call */
  61. void Register(JSAPI2::TransportAPI *me);
  62. void Deregister(JSAPI2::TransportAPI *me);
  63. void Register(JSAPI2::MediaCoreAPI *me);
  64. void Deregister(JSAPI2::MediaCoreAPI *me);
  65. void Register(JSAPI2::AsyncDownloaderAPI *me);
  66. void Deregister(JSAPI2::AsyncDownloaderAPI *me);
  67. private:
  68. /* Transport API callbacks */
  69. typedef CallbackInfo<JSAPI2::TransportAPI> TransportCallback;
  70. typedef std::vector<TransportCallback*> TransportsList;
  71. TransportsList transports;
  72. typedef std::vector<MediaCoreAPI*> MediaCoreList;
  73. MediaCoreList mediaCores;
  74. typedef CallbackInfo<JSAPI2::AsyncDownloaderAPI> AsyncDownloaderCallback;
  75. typedef std::vector<AsyncDownloaderCallback*> AsyncDownloadersList;
  76. AsyncDownloadersList asyncDownloaders;
  77. Nullsoft::Utility::LockGuard callbackGuard;
  78. };
  79. extern CallbackManager callbackManager;
  80. }