1
0

cacheManager.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "main.h"
  2. #include "./cacheManager.h"
  3. #include "./cacheGroup.h"
  4. #include "./ifc_wasabihelper.h"
  5. #include <shlwapi.h>
  6. #include <strsafe.h>
  7. CacheManager::CacheManager()
  8. : ref(1)
  9. {
  10. }
  11. CacheManager::~CacheManager()
  12. {
  13. Clear();
  14. }
  15. HRESULT CacheManager::CreateInstance(CacheManager **instance)
  16. {
  17. if (NULL == instance) return E_POINTER;
  18. *instance = NULL;
  19. *instance = new CacheManager();
  20. if (NULL == *instance) return E_OUTOFMEMORY;
  21. return S_OK;
  22. }
  23. size_t CacheManager::AddRef()
  24. {
  25. return InterlockedIncrement((LONG*)&ref);
  26. }
  27. size_t CacheManager::Release()
  28. {
  29. if (0 == ref)
  30. return ref;
  31. LONG r = InterlockedDecrement((LONG*)&ref);
  32. if (0 == r)
  33. delete(this);
  34. return r;
  35. }
  36. int CacheManager::QueryInterface(GUID interface_guid, void **object)
  37. {
  38. if (NULL == object) return E_POINTER;
  39. if (IsEqualIID(interface_guid, IFC_OmCacheManager))
  40. *object = static_cast<ifc_omcachemanager*>(this);
  41. else
  42. {
  43. *object = NULL;
  44. return E_NOINTERFACE;
  45. }
  46. if (NULL == *object)
  47. return E_UNEXPECTED;
  48. AddRef();
  49. return S_OK;
  50. }
  51. HRESULT CacheManager::Clear()
  52. {
  53. size_t index = groupList.size();
  54. while(index--)
  55. {
  56. CacheGroup *group = groupList[index];
  57. if (NULL != group) group->Release();
  58. }
  59. groupList.clear();
  60. return S_OK;
  61. }
  62. HRESULT CacheManager::Delete(LPCWSTR pszGroup)
  63. {
  64. HRESULT hr = S_FALSE;
  65. size_t index = groupList.size();
  66. while(index--)
  67. {
  68. CacheGroup *group = groupList[index];
  69. if (NULL != group && S_OK == group->IsEqualName(pszGroup))
  70. {
  71. groupList.erase(groupList.begin() + index);
  72. group->Release();
  73. hr = S_OK;
  74. break;
  75. }
  76. }
  77. return hr;
  78. }
  79. HRESULT CacheManager::Find(LPCWSTR pszGroup, BOOL fInsertMissing, CacheGroup **groupOut, BOOL *created)
  80. {
  81. if (NULL != created) *created = FALSE;
  82. if (NULL == groupOut) return E_POINTER;
  83. HRESULT hr = S_FALSE;
  84. size_t index = groupList.size();
  85. CacheGroup *group;
  86. while(index--)
  87. {
  88. group = groupList[index];
  89. if (NULL != group && S_OK == group->IsEqualName(pszGroup))
  90. {
  91. *groupOut = group;
  92. group->AddRef();
  93. hr = S_OK;
  94. break;
  95. }
  96. }
  97. if (S_FALSE == hr && FALSE != fInsertMissing)
  98. {
  99. hr = CacheGroup::CreateInstance(pszGroup, &group);
  100. if (SUCCEEDED(hr))
  101. {
  102. groupList.push_back(group);
  103. group->SetOwner(this);
  104. group->Load();
  105. group->AddRef();
  106. *groupOut = group;
  107. if (NULL != created) *created = TRUE;
  108. }
  109. }
  110. if (S_OK != hr)
  111. *groupOut = NULL;
  112. return hr;
  113. }
  114. HRESULT CacheManager::Load()
  115. {
  116. return S_OK;
  117. }
  118. HRESULT CacheManager::GetPath(LPWSTR pszBuffer, UINT cchBufferMax)
  119. {
  120. if (NULL == pszBuffer || 0 == cchBufferMax)
  121. return E_INVALIDARG;
  122. HRESULT hr;
  123. ifc_wasabihelper *wasabi;
  124. hr = Plugin_GetWasabiHelper(&wasabi);
  125. if (SUCCEEDED(hr))
  126. {
  127. api_application *application;
  128. hr = wasabi->GetApplicationApi(&application);
  129. if (SUCCEEDED(hr))
  130. {
  131. LPCWSTR pszUser = application->path_getUserSettingsPath();
  132. if (NULL != pszUser)
  133. {
  134. if (NULL == PathCombine(pszBuffer, pszUser, L"Plugins\\omBrowser\\cache"))
  135. hr = E_OUTOFMEMORY;
  136. }
  137. else
  138. {
  139. hr = E_UNEXPECTED;
  140. }
  141. application->Release();
  142. }
  143. wasabi->Release();
  144. }
  145. return hr;
  146. }
  147. #define CBCLASS CacheManager
  148. START_DISPATCH;
  149. CB(ADDREF, AddRef)
  150. CB(RELEASE, Release)
  151. CB(QUERYINTERFACE, QueryInterface)
  152. CB(API_FIND, Find)
  153. CB(API_DELETE, Delete)
  154. CB(API_CLEAR, Clear)
  155. END_DISPATCH;
  156. #undef CBCLASS