Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.0 KiB

  1. #ifndef _throttle_h
  2. #define _throttle_h
  3. #include "factory.h"
  4. #define MAX_RUNNING_ITEMS 3
  5. #define STATE_USER_IDLE_BEGIN 1
  6. #define STATE_USER_IDLE_END 2
  7. #define WC_INTERNAL_S_PAUSED (MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_ITF, 0xF000))
  8. #define WC_INTERNAL_S_RESUMING (MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_ITF, 0xF001))
  9. #define WC_INTERNAL_S_PENDING (MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_ITF, 0xF002))
  10. void IdleBegin(HWND hwnd);
  11. void IdleEnd(void);
  12. class COfflineSync;
  13. struct CSyncMgrNode;
  14. struct CUpdateItem;
  15. #define THROTTLER_WNDCLASS TEXT("WCThrottlerClass")
  16. class CThrottler : public ISubscriptionAgentEvents,
  17. public ISubscriptionThrottler
  18. {
  19. public:
  20. // IUnknown members
  21. STDMETHODIMP QueryInterface(REFIID riid, void **punk);
  22. STDMETHODIMP_(ULONG) AddRef();
  23. STDMETHODIMP_(ULONG) Release();
  24. // ISubscriptionAgentEvents members
  25. STDMETHODIMP UpdateBegin(
  26. const SUBSCRIPTIONCOOKIE *pSubscriptionCookie);
  27. STDMETHODIMP UpdateProgress(
  28. const SUBSCRIPTIONCOOKIE *pSubscriptionCookie,
  29. long lSizeDownloaded,
  30. long lProgressCurrent,
  31. long lProgressMax,
  32. HRESULT hrStatus,
  33. LPCWSTR wszStatus);
  34. STDMETHODIMP UpdateEnd(
  35. const SUBSCRIPTIONCOOKIE *pSubscriptionCookie,
  36. long lSizeDownloaded,
  37. HRESULT hrResult,
  38. LPCWSTR wszResult);
  39. STDMETHODIMP ReportError(
  40. const SUBSCRIPTIONCOOKIE *pSubscriptionCookie,
  41. HRESULT hrError,
  42. LPCWSTR wszError);
  43. STDMETHODIMP GetSubscriptionRunState(
  44. /* [in] */ DWORD dwNumCookies,
  45. /* [size_is][in] */ const SUBSCRIPTIONCOOKIE *pCookies,
  46. /* [size_is][out] */ DWORD *pdwRunState);
  47. STDMETHODIMP AbortItems(
  48. /* [in] */ DWORD dwNumCookies,
  49. /* [size_is][in] */ const SUBSCRIPTIONCOOKIE *pCookies);
  50. STDMETHODIMP AbortAll();
  51. static HRESULT GetThrottler(CThrottler **ppThrottler);
  52. HRESULT RunCookies(
  53. DWORD dwNumCookies,
  54. const SUBSCRIPTIONCOOKIE *pSubscriptionCookies,
  55. DWORD dwSyncFlags);
  56. HRESULT Advise(COfflineSync *pOfflineSync);
  57. HRESULT Unadvise(COfflineSync *pOfflineSync);
  58. ULONG ExternalAddRef();
  59. ULONG ExternalRelease();
  60. static LRESULT ThrottlerWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
  61. static void OnIdleStateChange(DWORD dwState);
  62. private:
  63. enum { WM_THROTTLER_ABORTALL =WM_USER+99,
  64. WM_THROTTLER_ABORTITEM=WM_USER+100,
  65. WM_THROTTLER_AUTOCACHESIZE_ASK=WM_USER+101};
  66. DWORD m_dwRegister;
  67. #ifdef DEBUG
  68. DWORD m_dwThreadId;
  69. #endif
  70. static CThrottler *s_pThrottler;
  71. const static CFactoryData s_ThrottlerFactoryData;
  72. static HRESULT CreateInstance(IUnknown *punkOuter, IUnknown **ppunk);
  73. HRESULT RevokeClassObject();
  74. inline void ReportThrottlerError(const SUBSCRIPTIONCOOKIE *pCookie, HRESULT hrError,
  75. LPCWSTR pwszErrMsg)
  76. {
  77. ReportError(pCookie, hrError, pwszErrMsg);
  78. }
  79. HRESULT AutoCacheSizeRequest(const SUBSCRIPTIONCOOKIE *pCookie);
  80. HRESULT AutoCacheSizeAskUser(DWORD dwCacheSizeKB);
  81. HRESULT IncreaseCacheSize(DWORD *pdwNewCacheSizeKB);
  82. CThrottler();
  83. ~CThrottler();
  84. ULONG m_cRef;
  85. ULONG m_cExternalRef;
  86. CSyncMgrNode *m_pSyncMgrs;
  87. CUpdateItem *m_pItemsHead;
  88. CUpdateItem *m_pItemsTail;
  89. CUpdateItem *m_updateQueue[MAX_RUNNING_ITEMS];
  90. int m_nUpdating;
  91. HWND m_hwndThrottler;
  92. HWND m_hwndParent;
  93. BOOL m_fAbortingAll:1;
  94. BOOL m_fUserIsIdle:1;
  95. BOOL m_fFillingTheQueue:1;
  96. BOOL m_fForcedGlobalOnline:1;
  97. BOOL m_fAutoDialed:1;
  98. BOOL m_fAutoCacheSizePending:1;
  99. DWORD m_dwMaxAutoCacheSize;
  100. DWORD m_dwAutoCacheSizeIncrease;
  101. int m_nAutoCacheSizeTimesAsked;
  102. typedef enum {NH_UPDATEBEGIN, NH_UPDATEPROGRESS, NH_UPDATEEND, NH_REPORTERROR};
  103. HRESULT AddItemToListTail(CUpdateItem *pAddItem);
  104. HRESULT RemoveItemFromList(CUpdateItem *pRemoveItem, BOOL fDelete);
  105. void OnIdleBegin();
  106. void OnIdleEnd();
  107. BOOL IsQueueSlotFree() { return m_nUpdating < ARRAYSIZE(m_updateQueue); }
  108. int GetFreeQueueSlot();
  109. int GetCookieIndexInQueue(const SUBSCRIPTIONCOOKIE *pCookie);
  110. void FillTheQueue();
  111. void FailedUpdate(HRESULT hr, const SUBSCRIPTIONCOOKIE *pCookie);
  112. void RunItem(int queueSlot, CUpdateItem *pUpdateItem);
  113. HRESULT CanScheduledItemRun(ISubscriptionItem *pSubsItem);
  114. STDMETHODIMP ActuallyAbortItems(DWORD dwNumCookies, const SUBSCRIPTIONCOOKIE *pCookies);
  115. STDMETHODIMP ActuallyAbortAll();
  116. HRESULT DoAbortItem(CUpdateItem *pUpdateItem);
  117. HRESULT CreateThrottlerWnd();
  118. HRESULT NotifyHandlers(int idCmd, const SUBSCRIPTIONCOOKIE *pSubscriptionCookie, ...);
  119. HRESULT FindCookie(
  120. const SUBSCRIPTIONCOOKIE *pSubscriptionCookie,
  121. CUpdateItem **ppUpdateItem);
  122. };
  123. #include "offsync.h"
  124. struct CSyncMgrNode
  125. {
  126. CSyncMgrNode(COfflineSync *pOfflineSync, CSyncMgrNode *pNext) :
  127. m_pOfflineSync(pOfflineSync),
  128. m_pNext(pNext)
  129. {
  130. ASSERT(NULL != m_pOfflineSync);
  131. }
  132. ~CSyncMgrNode()
  133. {
  134. SAFERELEASE(m_pOfflineSync);
  135. }
  136. COfflineSync *m_pOfflineSync;
  137. CSyncMgrNode *m_pNext;
  138. };
  139. struct CUpdateItem
  140. {
  141. CUpdateItem(const SUBSCRIPTIONCOOKIE& cookie,
  142. DWORD dwRunState) :
  143. m_cookie(cookie),
  144. m_dwRunState(dwRunState)
  145. {
  146. ASSERT(NULL == m_pNext);
  147. ASSERT(NULL == m_pSubsAgentCtl);
  148. ASSERT(CLSID_NULL != m_cookie);
  149. m_nMax = 128;
  150. }
  151. ~CUpdateItem()
  152. {
  153. SAFERELEASE(m_pSubsAgentCtl);
  154. }
  155. ISubscriptionAgentControl *m_pSubsAgentCtl;
  156. SUBSCRIPTIONCOOKIE m_cookie;
  157. DWORD m_dwRunState;
  158. CUpdateItem *m_pNext;
  159. LONG m_nMax;
  160. };
  161. #endif _throttle_h