1
0

browserClass.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "main.h"
  2. #include "./browserClass.h"
  3. #include "./browserRegistry.h"
  4. #include "./configIni.h"
  5. #include <strsafe.h>
  6. OmBrowserClass::OmBrowserClass(LPCWSTR pszName)
  7. : ref(1), name(NULL), config(NULL), registry(NULL)
  8. {
  9. InitializeCriticalSection(&lock);
  10. name = Plugin_CopyString(pszName);
  11. }
  12. OmBrowserClass::~OmBrowserClass()
  13. {
  14. if (NULL != config)
  15. config->Release();
  16. if (NULL != registry)
  17. {
  18. registry->Delete();
  19. registry->Release();
  20. }
  21. Plugin_FreeString(name);
  22. DeleteCriticalSection(&lock);
  23. }
  24. HRESULT OmBrowserClass::CreateInstance(LPCWSTR pszName, OmBrowserClass **instance)
  25. {
  26. if (NULL == instance) return E_POINTER;
  27. *instance = NULL;
  28. *instance = new OmBrowserClass(pszName);
  29. if (NULL == *instance) return E_OUTOFMEMORY;
  30. return S_OK;
  31. }
  32. size_t OmBrowserClass::AddRef()
  33. {
  34. return InterlockedIncrement((LONG*)&ref);
  35. }
  36. size_t OmBrowserClass::Release()
  37. {
  38. if (0 == ref)
  39. return ref;
  40. LONG r = InterlockedDecrement((LONG*)&ref);
  41. if (0 == r)
  42. {
  43. if (NULL != name && L'\0' != *name)
  44. Plugin_UnregisterBrowserClass(name);
  45. delete(this);
  46. }
  47. return r;
  48. }
  49. int OmBrowserClass::QueryInterface(GUID interface_guid, void **object)
  50. {
  51. if (NULL == object) return E_POINTER;
  52. if (IsEqualIID(interface_guid, IFC_OmBrowserClass))
  53. *object = static_cast<ifc_ombrowserclass*>(this);
  54. else
  55. {
  56. *object = NULL;
  57. return E_NOINTERFACE;
  58. }
  59. if (NULL == *object)
  60. return E_UNEXPECTED;
  61. AddRef();
  62. return S_OK;
  63. }
  64. HRESULT OmBrowserClass::GetName(LPWSTR pszBuffer, INT cchBufferMax)
  65. {
  66. if (NULL == pszBuffer) return E_POINTER;
  67. return StringCchCopyEx(pszBuffer, cchBufferMax, name, NULL, NULL, STRSAFE_IGNORE_NULLS);
  68. }
  69. HRESULT OmBrowserClass::IsEqual(LPCWSTR pszName)
  70. {
  71. if (NULL == pszName || L'\0' == *pszName)
  72. return E_INVALIDARG;
  73. if (NULL == name)
  74. return E_UNEXPECTED;
  75. INT result = CompareString(CSTR_INVARIANT, NORM_IGNORECASE, pszName, -1, name, -1);
  76. if (0 == result)
  77. {
  78. DWORD errorCode = GetLastError();
  79. return HRESULT_FROM_WIN32(errorCode);
  80. }
  81. return (CSTR_EQUAL == result) ? S_OK : S_FALSE;
  82. }
  83. HRESULT OmBrowserClass::GetConfig(ifc_omconfig **instance)
  84. {
  85. if (NULL == instance) return E_POINTER;
  86. *instance = NULL;
  87. HRESULT hr = S_OK;
  88. EnterCriticalSection(&lock);
  89. if (NULL == config)
  90. {
  91. hr = OmConfigIni::CreateInstance(name, &config);
  92. if (FAILED(hr))
  93. {
  94. config = NULL;
  95. }
  96. }
  97. if (SUCCEEDED(hr))
  98. {
  99. *instance = config;
  100. config->AddRef();
  101. }
  102. LeaveCriticalSection(&lock);
  103. return S_OK;
  104. }
  105. HRESULT OmBrowserClass::GetRegistry(ifc_ombrowserregistry **instance)
  106. {
  107. if (NULL == instance) return E_POINTER;
  108. *instance = NULL;
  109. HRESULT hr = S_OK;
  110. EnterCriticalSection(&lock);
  111. if (NULL == registry)
  112. {
  113. hr = OmBrowserRegistry::CreateInstance(name, &registry);
  114. if (FAILED(hr))
  115. {
  116. registry = NULL;
  117. }
  118. else
  119. {
  120. registry->Write();
  121. }
  122. }
  123. if (SUCCEEDED(hr))
  124. {
  125. *instance = registry;
  126. registry->AddRef();
  127. }
  128. LeaveCriticalSection(&lock);
  129. return S_OK;
  130. }
  131. HRESULT OmBrowserClass::UpdateRegColors()
  132. {
  133. HRESULT hr = S_FALSE;
  134. EnterCriticalSection(&lock);
  135. if (NULL != registry)
  136. hr = registry->UpdateColors();
  137. LeaveCriticalSection(&lock);
  138. return hr;
  139. }
  140. #define CBCLASS OmBrowserClass
  141. START_DISPATCH;
  142. CB(ADDREF, AddRef)
  143. CB(RELEASE, Release)
  144. CB(QUERYINTERFACE, QueryInterface)
  145. CB(API_GETNAME, GetName)
  146. CB(API_ISEQUAL, IsEqual)
  147. CB(API_GETCONFIG, GetConfig)
  148. CB(API_GETREGISTRY, GetRegistry)
  149. CB(API_UPDATEREGCOLORS, UpdateRegColors)
  150. END_DISPATCH;
  151. #undef CBCLASS