123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #include "main.h"
- #include "./cacheManager.h"
- #include "./cacheGroup.h"
- #include "./ifc_wasabihelper.h"
- #include <shlwapi.h>
- #include <strsafe.h>
- CacheManager::CacheManager()
- : ref(1)
- {
-
- }
- CacheManager::~CacheManager()
- {
- Clear();
- }
- HRESULT CacheManager::CreateInstance(CacheManager **instance)
- {
- if (NULL == instance) return E_POINTER;
- *instance = NULL;
- *instance = new CacheManager();
- if (NULL == *instance) return E_OUTOFMEMORY;
- return S_OK;
- }
- size_t CacheManager::AddRef()
- {
- return InterlockedIncrement((LONG*)&ref);
- }
- size_t CacheManager::Release()
- {
- if (0 == ref)
- return ref;
-
- LONG r = InterlockedDecrement((LONG*)&ref);
- if (0 == r)
- delete(this);
-
- return r;
- }
- int CacheManager::QueryInterface(GUID interface_guid, void **object)
- {
- if (NULL == object) return E_POINTER;
- if (IsEqualIID(interface_guid, IFC_OmCacheManager))
- *object = static_cast<ifc_omcachemanager*>(this);
- else
- {
- *object = NULL;
- return E_NOINTERFACE;
- }
- if (NULL == *object)
- return E_UNEXPECTED;
- AddRef();
- return S_OK;
- }
- HRESULT CacheManager::Clear()
- {
- size_t index = groupList.size();
- while(index--)
- {
- CacheGroup *group = groupList[index];
- if (NULL != group) group->Release();
- }
- groupList.clear();
- return S_OK;
- }
- HRESULT CacheManager::Delete(LPCWSTR pszGroup)
- {
- HRESULT hr = S_FALSE;
- size_t index = groupList.size();
- while(index--)
- {
- CacheGroup *group = groupList[index];
- if (NULL != group && S_OK == group->IsEqualName(pszGroup))
- {
- groupList.erase(groupList.begin() + index);
- group->Release();
- hr = S_OK;
- break;
- }
- }
- return hr;
- }
- HRESULT CacheManager::Find(LPCWSTR pszGroup, BOOL fInsertMissing, CacheGroup **groupOut, BOOL *created)
- {
- if (NULL != created) *created = FALSE;
- if (NULL == groupOut) return E_POINTER;
-
- HRESULT hr = S_FALSE;
- size_t index = groupList.size();
- CacheGroup *group;
- while(index--)
- {
- group = groupList[index];
- if (NULL != group && S_OK == group->IsEqualName(pszGroup))
- {
- *groupOut = group;
- group->AddRef();
- hr = S_OK;
- break;
- }
- }
-
- if (S_FALSE == hr && FALSE != fInsertMissing)
- {
- hr = CacheGroup::CreateInstance(pszGroup, &group);
- if (SUCCEEDED(hr))
- {
- groupList.push_back(group);
- group->SetOwner(this);
- group->Load();
- group->AddRef();
- *groupOut = group;
- if (NULL != created) *created = TRUE;
- }
- }
- if (S_OK != hr)
- *groupOut = NULL;
-
- return hr;
- }
- HRESULT CacheManager::Load()
- {
- return S_OK;
- }
- HRESULT CacheManager::GetPath(LPWSTR pszBuffer, UINT cchBufferMax)
- {
- if (NULL == pszBuffer || 0 == cchBufferMax)
- return E_INVALIDARG;
-
- HRESULT hr;
- ifc_wasabihelper *wasabi;
- hr = Plugin_GetWasabiHelper(&wasabi);
- if (SUCCEEDED(hr))
- {
- api_application *application;
- hr = wasabi->GetApplicationApi(&application);
- if (SUCCEEDED(hr))
- {
- LPCWSTR pszUser = application->path_getUserSettingsPath();
- if (NULL != pszUser)
- {
- if (NULL == PathCombine(pszBuffer, pszUser, L"Plugins\\omBrowser\\cache"))
- hr = E_OUTOFMEMORY;
- }
- else
- {
- hr = E_UNEXPECTED;
- }
- application->Release();
- }
- wasabi->Release();
- }
- return hr;
- }
- #define CBCLASS CacheManager
- START_DISPATCH;
- CB(ADDREF, AddRef)
- CB(RELEASE, Release)
- CB(QUERYINTERFACE, QueryInterface)
- CB(API_FIND, Find)
- CB(API_DELETE, Delete)
- CB(API_CLEAR, Clear)
- END_DISPATCH;
- #undef CBCLASS
|