browserObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #include "main.h"
  2. #include "./browserObject.h"
  3. #include "./ifc_menucustomizer.h"
  4. #include "./ifc_wasabihelper.h"
  5. #include "./ifc_omconfig.h"
  6. #include "./browserView.h"
  7. #include "./browserPopup.h"
  8. #include "./options.h"
  9. #include "./ifc_ombrowserconfig.h"
  10. #include "./ifc_ombrowserregistry.h"
  11. #include "./ifc_ombrowserevent.h"
  12. #include "./ifc_omtoolbarconfig.h"
  13. #include "./ifc_omstatusbarconfig.h"
  14. #include "./ifc_ombrowserclass.h"
  15. #include "./ifc_omservice.h"
  16. #include "./ieversion.h"
  17. #include "./browserWndEnum.h"
  18. #include "./browserWndRecord.h"
  19. #include <strsafe.h>
  20. OmBrowserObject::OmBrowserObject() : ref(1), flags(0), browserClass(NULL)
  21. {
  22. InitializeCriticalSection(&lock);
  23. }
  24. OmBrowserObject::~OmBrowserObject()
  25. {
  26. if (NULL != browserClass)
  27. browserClass->Release();
  28. DeleteCriticalSection(&lock);
  29. }
  30. HRESULT OmBrowserObject::CreateInstance(OmBrowserObject **instance)
  31. {
  32. if (NULL == instance) return E_POINTER;
  33. *instance = new OmBrowserObject();
  34. if (NULL == *instance) return E_OUTOFMEMORY;
  35. return S_OK;
  36. }
  37. size_t OmBrowserObject::AddRef()
  38. {
  39. return InterlockedIncrement((LONG*)&ref);
  40. }
  41. size_t OmBrowserObject::Release()
  42. {
  43. if (0 == ref)
  44. return ref;
  45. LONG r = InterlockedDecrement((LONG*)&ref);
  46. if (0 == r)
  47. delete(this);
  48. return r;
  49. }
  50. int OmBrowserObject::QueryInterface(GUID interface_guid, void **object)
  51. {
  52. if (NULL == object) return E_POINTER;
  53. if (IsEqualIID(interface_guid, OBJ_OmBrowser))
  54. *object = static_cast<obj_ombrowser*>(this);
  55. else if (IsEqualIID(interface_guid, IFC_OmBrowserWindowManager))
  56. *object = static_cast<ifc_ombrowserwndmngr*>(this);
  57. else if (IsEqualIID(interface_guid, IFC_OmBrowserEventManager))
  58. *object = static_cast<ifc_ombrowsereventmngr*>(this);
  59. else
  60. {
  61. *object = NULL;
  62. return E_NOINTERFACE;
  63. }
  64. if (NULL == *object)
  65. return E_UNEXPECTED;
  66. AddRef();
  67. return S_OK;
  68. }
  69. HRESULT OmBrowserObject::Initialize(LPCWSTR pszName, HWND hwndWinamp)
  70. {
  71. if ( browserClass != NULL )
  72. return S_FALSE;
  73. HRESULT hr = Plugin_Initialize(hwndWinamp);
  74. if (SUCCEEDED(hr))
  75. {
  76. if (NULL == pszName || L'\0' == *pszName)
  77. pszName = OMBROWSER_NAME;
  78. hr = Plugin_GetBrowserClass(pszName, &browserClass);
  79. }
  80. return hr;
  81. }
  82. HRESULT OmBrowserObject::Finish(void)
  83. {
  84. HRESULT hr = S_OK;
  85. EnterCriticalSection(&lock);
  86. flags |= flagFinishing;
  87. size_t index = windowList.size();
  88. while(index--)
  89. {
  90. OmBrowserWndRecord *r = windowList[index];
  91. if (FALSE == DestroyWindow(r->GetHwnd()))
  92. {
  93. hr = E_FAIL;
  94. }
  95. else
  96. {
  97. windowList.erase(windowList.begin() + index);
  98. r->Release();
  99. }
  100. }
  101. while(!eventList.empty())
  102. {
  103. ifc_ombrowserevent *e = eventList.back();
  104. if (NULL == e) break;
  105. eventList.pop_back();
  106. e->Release();
  107. }
  108. if (NULL != browserClass)
  109. {
  110. browserClass->Release();
  111. browserClass = NULL;
  112. }
  113. LeaveCriticalSection(&lock);
  114. return hr;
  115. }
  116. HRESULT OmBrowserObject::RegisterWinampHook(ifc_winamphook *hook, UINT *cookieOut)
  117. {
  118. return Plugin_RegisterWinampHook(hook, cookieOut);
  119. }
  120. HRESULT OmBrowserObject::UnregisterWinampHook(UINT cookie)
  121. {
  122. return Plugin_UnregisterWinampHook(cookie);
  123. }
  124. HRESULT OmBrowserObject::GetConfig(const GUID *configIfc, void **configOut)
  125. {
  126. if (NULL == configOut) return E_POINTER;
  127. if (NULL == browserClass) return E_UNEXPECTED;
  128. ifc_omconfig *config = NULL;
  129. HRESULT hr = browserClass->GetConfig(&config);
  130. if (SUCCEEDED(hr) && config != NULL)
  131. {
  132. if (NULL == configIfc || IsEqualIID(*configIfc, GUID_NULL))
  133. *configOut = config;
  134. else
  135. {
  136. hr = config->QueryInterface(*configIfc, configOut);
  137. config->Release();
  138. }
  139. }
  140. return hr;
  141. }
  142. HRESULT OmBrowserObject::GetSessionId(LPWSTR pszBuffer, INT cchBufferMax)
  143. {
  144. if (NULL == pszBuffer) return E_POINTER;
  145. *pszBuffer = L'\0';
  146. ifc_wasabihelper *wasabi = NULL;
  147. HRESULT hr = Plugin_GetWasabiHelper(&wasabi);
  148. if (SUCCEEDED(hr) && wasabi != NULL)
  149. {
  150. api_application *app = NULL;
  151. hr = wasabi->GetApplicationApi(&app);
  152. if (SUCCEEDED(hr))
  153. {
  154. UUID uid;
  155. if (API_APPLICATION_SUCCESS == app->GetSessionID(&uid))
  156. {
  157. hr = Plugin_FormatUuidString(uid, pszBuffer, cchBufferMax);
  158. }
  159. else
  160. {
  161. hr = E_FAIL;
  162. }
  163. app->Release();
  164. }
  165. wasabi->Release();
  166. }
  167. if (FAILED(hr))
  168. hr = StringCchCopy(pszBuffer, cchBufferMax, L"0123456789ABCDEF");
  169. return hr;
  170. }
  171. HRESULT OmBrowserObject::GetClientId(LPWSTR pszBuffer, INT cchBufferMax)
  172. {
  173. if (NULL == pszBuffer) return E_POINTER;
  174. *pszBuffer = L'\0';
  175. ifc_ombrowserconfig *browserConfig;
  176. if (FAILED(GetConfig(&IFC_OmBrowserConfig, (void**)&browserConfig)))
  177. return E_NOINTERFACE;
  178. HRESULT hr = browserConfig->GetClientId(pszBuffer, cchBufferMax);
  179. if (S_OK != hr)
  180. {
  181. UUID uid;
  182. LPWSTR cursor = NULL;
  183. size_t remaining = 0;
  184. StringCchCopyEx(pszBuffer, cchBufferMax, L"WA-", &cursor, &remaining, 0);
  185. if (RPC_S_OK != UuidCreate(&uid) ||
  186. FAILED(Plugin_FormatUuidString(uid, cursor, remaining)) ||
  187. FAILED(browserConfig->SetClientId(pszBuffer)))
  188. {
  189. *pszBuffer = L'\0';
  190. hr = E_FAIL;
  191. }
  192. }
  193. browserConfig->Release();
  194. return hr;
  195. }
  196. HRESULT OmBrowserObject::GetRegistry(ifc_ombrowserregistry **registryOut)
  197. {
  198. if (NULL == registryOut) return E_POINTER;
  199. if (NULL == browserClass) return E_UNEXPECTED;
  200. return browserClass->GetRegistry(registryOut);
  201. }
  202. HRESULT OmBrowserObject::CreateView(ifc_omservice *service, HWND hParent, LPCWSTR forceUrl, UINT viewStyle, HWND *hView)
  203. {
  204. if (NULL == hView) return E_POINTER;
  205. *hView = NULL;
  206. if (NULL == hParent) return E_INVALIDARG;
  207. *hView = BrowserView_Create(this, service, hParent, forceUrl, viewStyle);
  208. if (NULL == *hView) return E_FAIL;
  209. BrowserView_UpdateSkin(*hView, FALSE);
  210. return S_OK;
  211. }
  212. HRESULT OmBrowserObject::CreatePopup(ifc_omservice *service, INT x, INT y, INT cx, INT cy, HWND hOwner, LPCWSTR forceUrl, UINT viewStyle, HWND *hWindow)
  213. {
  214. if (NULL == hWindow) return E_POINTER;
  215. *hWindow = NULL;
  216. if (NULL == hOwner && FAILED(Plugin_GetWinampWnd(&hOwner)))
  217. return E_INVALIDARG;
  218. HWND hPopup = BrowserPopup_Create(this, service, viewStyle, x, y, cx, cy, hOwner, NULL, 0L);
  219. if (NULL == hPopup) return E_FAIL;
  220. *hWindow = hPopup;
  221. BrowserPopup_UpdateSkin(hPopup, FALSE);
  222. if (NULL != forceUrl)
  223. {
  224. BrowserPopup_Navigate(hPopup, forceUrl, 0L);
  225. }
  226. else
  227. {
  228. BrowserPopup_Navigate(hPopup, NAVIGATE_HOME, 0L);
  229. }
  230. return S_OK;
  231. }
  232. HRESULT OmBrowserObject::IsFinishing(void)
  233. {
  234. return (0 != (flagFinishing & flags)) ? S_OK : S_FALSE;
  235. }
  236. HRESULT OmBrowserObject::GetClass(ifc_ombrowserclass **instance)
  237. {
  238. if (NULL == instance)
  239. return E_POINTER;
  240. *instance = browserClass;
  241. if (NULL == *instance) return E_UNEXPECTED;
  242. browserClass->AddRef();
  243. return S_OK;
  244. }
  245. HRESULT OmBrowserObject::GetVersion(int *major, int *minor)
  246. {
  247. if (NULL != major) *major = OMBROWSER_VERSION_MAJOR;
  248. if (NULL != minor) *minor = OMBROWSER_VERSION_MINOR;
  249. return S_OK;
  250. }
  251. HRESULT OmBrowserObject::GetIEVersion(int *major, int *minor, int *build, int *subbuild)
  252. {
  253. return MSIE_GetVersion(major, minor, build, subbuild);
  254. }
  255. HRESULT OmBrowserObject::ShowOptions(HWND hOwner, UINT style, BROWSEROPTIONSCALLBACK callback, ULONG_PTR user)
  256. {
  257. return BrowserOptions_ShowDialog(this, hOwner, style, callback, user);
  258. }
  259. HRESULT OmBrowserObject::RegisterWindow(HWND hwnd, const GUID *windowType)
  260. {
  261. if (NULL == hwnd) return E_INVALIDARG;
  262. if (S_FALSE != IsFinishing())
  263. return E_UNEXPECTED;
  264. EnterCriticalSection(&lock);
  265. size_t index = windowList.size();
  266. while(index--)
  267. {
  268. if (windowList[index]->GetHwnd() == hwnd)
  269. {
  270. LeaveCriticalSection(&lock);
  271. return S_FALSE;
  272. }
  273. }
  274. OmBrowserWndRecord *r = NULL;
  275. HRESULT hr = OmBrowserWndRecord::CreateInstance(hwnd, windowType, &r);
  276. if (SUCCEEDED(hr) && r != NULL)
  277. {
  278. windowList.push_back(r);
  279. }
  280. LeaveCriticalSection(&lock);
  281. return hr;
  282. }
  283. HRESULT OmBrowserObject::UnregisterWindow(HWND hwnd)
  284. {
  285. if (NULL == hwnd) return E_INVALIDARG;
  286. if (S_FALSE != IsFinishing())
  287. return E_PENDING;
  288. EnterCriticalSection(&lock);
  289. size_t index = windowList.size();
  290. while(index--)
  291. {
  292. OmBrowserWndRecord *r = windowList[index];
  293. if (r->GetHwnd() == hwnd)
  294. {
  295. windowList.erase(windowList.begin() + index);
  296. r->Release();
  297. LeaveCriticalSection(&lock);
  298. return S_OK;
  299. }
  300. }
  301. LeaveCriticalSection(&lock);
  302. return S_FALSE;
  303. }
  304. HRESULT OmBrowserObject::Enumerate(const GUID *windowType, UINT *serviceIdFilter, ifc_ombrowserwndenum **enumerator)
  305. {
  306. EnterCriticalSection(&lock);
  307. HRESULT hr = OmBrowserWndEnumerator::CreateInstance(windowType, serviceIdFilter, windowList.size() ? &windowList.at(0) : nullptr,
  308. windowList.size(), (OmBrowserWndEnumerator**)enumerator);
  309. LeaveCriticalSection(&lock);
  310. return hr;
  311. }
  312. HRESULT OmBrowserObject::RegisterEventHandler(ifc_ombrowserevent *eventHandler)
  313. {
  314. if (NULL == eventHandler)
  315. return E_INVALIDARG;
  316. if (S_FALSE != IsFinishing())
  317. return E_PENDING;
  318. EnterCriticalSection(&lock);
  319. size_t index = eventList.size();
  320. while (index--)
  321. {
  322. if (eventList[index] == eventHandler)
  323. {
  324. LeaveCriticalSection(&lock);
  325. return E_FAIL;
  326. }
  327. }
  328. eventList.push_back(eventHandler);
  329. eventHandler->AddRef();
  330. LeaveCriticalSection(&lock);
  331. return S_OK;
  332. }
  333. HRESULT OmBrowserObject::UnregisterEventHandler(ifc_ombrowserevent *eventHandler)
  334. {
  335. if (NULL == eventHandler)
  336. return E_INVALIDARG;
  337. if (S_FALSE != IsFinishing())
  338. return E_PENDING;
  339. EnterCriticalSection(&lock);
  340. size_t index = eventList.size();
  341. while (index--)
  342. {
  343. if (eventList[index] == eventHandler)
  344. {
  345. eventList.erase(eventList.begin() + index);
  346. eventHandler->Release();
  347. LeaveCriticalSection(&lock);
  348. return S_OK;
  349. }
  350. }
  351. LeaveCriticalSection(&lock);
  352. return S_FALSE;
  353. }
  354. HRESULT OmBrowserObject::Signal_WindowCreate(HWND hwnd, const GUID *windowType)
  355. {
  356. EnterCriticalSection(&lock);
  357. size_t index = eventList.size();
  358. while (index--)
  359. {
  360. eventList[index]->WindowCreate(hwnd, windowType);
  361. }
  362. LeaveCriticalSection(&lock);
  363. return S_OK;
  364. }
  365. HRESULT OmBrowserObject::Signal_WindowClose(HWND hwnd, const GUID *windowType)
  366. {
  367. EnterCriticalSection(&lock);
  368. size_t index = eventList.size();
  369. while (index--)
  370. {
  371. eventList[index]->WindowClose(hwnd, windowType);
  372. }
  373. LeaveCriticalSection(&lock);
  374. return S_OK;
  375. }
  376. #define CBCLASS OmBrowserObject
  377. START_MULTIPATCH;
  378. START_PATCH(MPIID_OMBROWSER)
  379. M_CB(MPIID_OMBROWSER, obj_ombrowser, ADDREF, AddRef);
  380. M_CB(MPIID_OMBROWSER, obj_ombrowser, RELEASE, Release);
  381. M_CB(MPIID_OMBROWSER, obj_ombrowser, QUERYINTERFACE, QueryInterface);
  382. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_INITIALIZE, Initialize);
  383. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_FINISH, Finish);
  384. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_REGISTERWINAMPHOOK, RegisterWinampHook);
  385. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_UNREGISTERWINAMPHOOK, UnregisterWinampHook);
  386. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_GETCONFIG, GetConfig);
  387. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_GETSESSIONID, GetSessionId);
  388. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_GETCLIENTID, GetClientId);
  389. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_GETREGISTRY, GetRegistry);
  390. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_CREATEVIEW, CreateView);
  391. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_CREATEPOPUP, CreatePopup);
  392. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_ISFINISHING, IsFinishing);
  393. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_GETCLASS, GetClass);
  394. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_GETVERSION, GetVersion);
  395. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_GETIEVERSION, GetIEVersion);
  396. M_CB(MPIID_OMBROWSER, obj_ombrowser, API_SHOWOPTIONS, ShowOptions);
  397. NEXT_PATCH(MPIID_OMBROWSERWNDMNGR)
  398. M_CB(MPIID_OMBROWSERWNDMNGR, ifc_ombrowserwndmngr, ADDREF, AddRef);
  399. M_CB(MPIID_OMBROWSERWNDMNGR, ifc_ombrowserwndmngr, RELEASE, Release);
  400. M_CB(MPIID_OMBROWSERWNDMNGR, ifc_ombrowserwndmngr, QUERYINTERFACE, QueryInterface);
  401. M_CB(MPIID_OMBROWSERWNDMNGR, ifc_ombrowserwndmngr, API_REGISTERWINDOW, RegisterWindow);
  402. M_CB(MPIID_OMBROWSERWNDMNGR, ifc_ombrowserwndmngr, API_UNREGISTERWINDOW, UnregisterWindow);
  403. M_CB(MPIID_OMBROWSERWNDMNGR, ifc_ombrowserwndmngr, API_ENUMERATE, Enumerate);
  404. NEXT_PATCH(MPIID_OMBROWSEREVENTMNGR)
  405. M_CB(MPIID_OMBROWSEREVENTMNGR, ifc_ombrowsereventmngr, ADDREF, AddRef);
  406. M_CB(MPIID_OMBROWSEREVENTMNGR, ifc_ombrowsereventmngr, RELEASE, Release);
  407. M_CB(MPIID_OMBROWSEREVENTMNGR, ifc_ombrowsereventmngr, QUERYINTERFACE, QueryInterface);
  408. M_CB(MPIID_OMBROWSEREVENTMNGR, ifc_ombrowsereventmngr, API_REGISTERHANDLER, RegisterEventHandler);
  409. M_CB(MPIID_OMBROWSEREVENTMNGR, ifc_ombrowsereventmngr, API_UNREGISTERHANDLER, UnregisterEventHandler);
  410. M_CB(MPIID_OMBROWSEREVENTMNGR, ifc_ombrowsereventmngr, API_SIGNAL_WINDOWCREATE, Signal_WindowCreate);
  411. M_CB(MPIID_OMBROWSEREVENTMNGR, ifc_ombrowsereventmngr, API_SIGNAL_WINDOWCLOSE, Signal_WindowClose);
  412. END_PATCH
  413. END_MULTIPATCH;
  414. #undef CBCLASS