1
0

browserHost.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. #include "main.h"
  2. #include "./browser.h"
  3. #include "./browserHost.h"
  4. //#include "./browserPopup.h"
  5. #include "./graphics.h"
  6. //#include "../winamp/wa_dlg.h"
  7. #include "../Plugins/General/gen_ml/colors.h"
  8. #include "./browserThread.h"
  9. #include "../winamp/IWasabiDispatchable.h"
  10. #include "../winamp/JSAPI_Info.h"
  11. #include "./service.h"
  12. //#include "./menu.h"
  13. #include "./obj_ombrowser.h"
  14. #include "./ifc_skinhelper.h"
  15. #include "./ifc_skinnedbrowser.h"
  16. #include "./ifc_omservice.h"
  17. #include "./ifc_travelloghelper.h"
  18. #include "./ifc_wasabihelper.h"
  19. #include <exdispid.h>
  20. #include <strsafe.h>
  21. #define NWC_OMBROWSERHOST L"Nullsoft_omBrowserHost"
  22. typedef struct __BROWSERHOSTCREATEPARAM
  23. {
  24. HWND hParent;
  25. INT controlId;
  26. UINT fStyle;
  27. INT x;
  28. INT y;
  29. INT cx;
  30. INT cy;
  31. HINSTANCE hInstance;
  32. HACCEL hAccel;
  33. obj_ombrowser *browserManager;
  34. } BROWSERHOSTCREATEPARAM;
  35. typedef struct __BROWSERHOST
  36. {
  37. Browser *container;
  38. HACCEL hAccel;
  39. obj_ombrowser *browserManager;
  40. } BROWSERHOST;
  41. #define BHT_CONTAINERDOWNLOAD 27
  42. #define BHT_ACTIVATECONTAINER_DELAY 15
  43. #define BHT_DEACTIVATECONTAINER_DELAY 75
  44. #define GetHost(__hwnd) ((BROWSERHOST*)(LONG_PTR)(LONGX86)GetWindowLongPtr((__hwnd), 0))
  45. static LRESULT CALLBACK BrowserHost_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  46. static void CALLBACK BrowserHost_OnDocumentReady(Browser *browser, IDispatch *pDispatch, VARIANT *URL);
  47. static void CALLBACK BrowserHost_OnNavigateComplete(Browser *browser, IDispatch *pDispatch, VARIANT *URL);
  48. static void CALLBACK BrowserHost_OnContainerDestroy(Browser *browser);
  49. static void CALLBACK BrowserHost_OnDownloadBegin(Browser *browser);
  50. static void CALLBACK BrowserHost_OnDownloadComplete(Browser *browser);
  51. static void CALLBACK BrowserHost_OnCommandStateChange(Browser *browser, INT commandId, BOOL fEnabled);
  52. static void CALLBACK BrowserHost_OnStatusChange(Browser *browser, LPCWSTR pszText);
  53. static void CALLBACK BrowserHost_OnTitleChange(Browser *browser, LPCWSTR pszText);
  54. static void CALLBACK BrowserHost_OnSecureLockIconChange(Browser *browser);
  55. static void CALLBACK BrowserHost_OnCreatePopup(Browser *browser, IDispatch **ppDisp, VARIANT_BOOL *Cancel);
  56. static void CALLBACK BrowserHost_OnVisibleChange(Browser *browser, VARIANT_BOOL fVisible);
  57. static void CALLBACK BrowserHost_OnSetResizable(Browser *browser, VARIANT_BOOL fAllow);
  58. static void CALLBACK BrowserHost_OnSetFullScreen(Browser *browser, VARIANT_BOOL fAllow);
  59. static void CALLBACK BrowserHost_OnWindowClosing(Browser *browser, VARIANT_BOOL fIsChild, VARIANT_BOOL *fCancel);
  60. static void CALLBACK BrowserHost_OnShowUiElenent(Browser *browser, UINT elementId, VARIANT_BOOL fShow);
  61. static void CALLBACK BrowserHost_OnClientToHost(Browser *browser, LONG *cx, LONG *cy);
  62. static void CALLBACK BrowserHost_OnSetWindowPos(Browser *browser, UINT Flags, LONG x, LONG y, LONG cx, LONG cy);
  63. static void CALLBACK BrowserHost_OnFocusChange(Browser *browser, VARIANT_BOOL *fAllow);
  64. static void CALLBACK BrowserHost_OnClosePopup(Browser *browser);
  65. static HRESULT CALLBACK BrowserHost_GetOmService(Browser *browser, ifc_omservice **ppService);
  66. static LRESULT CALLBACK BrowserHost_RedirectKey(Browser *browser, MSG *pMsg);
  67. static BOOL BrowserHost_RegisterClass(HINSTANCE hInstance)
  68. {
  69. WNDCLASS wc = {0};
  70. if (GetClassInfo(hInstance, NWC_OMBROWSERHOST, &wc)) return TRUE;
  71. ZeroMemory(&wc, sizeof(WNDCLASS));
  72. wc.hInstance = hInstance;
  73. wc.lpszClassName = NWC_OMBROWSERHOST;
  74. wc.lpfnWndProc = BrowserHost_WindowProc;
  75. wc.style = CS_DBLCLKS;
  76. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  77. wc.hbrBackground = NULL;
  78. wc.cbWndExtra = sizeof(BROWSERHOST*);
  79. return ( 0 != RegisterClassW(&wc));
  80. }
  81. static HWND CALLBACK BrowserHost_CreateHostWindow(ULONG_PTR user)
  82. {
  83. BROWSERHOSTCREATEPARAM *param = (BROWSERHOSTCREATEPARAM*)user;
  84. if (NULL == param) return NULL;
  85. return CreateWindowEx(WS_EX_NOPARENTNOTIFY | WS_EX_CONTROLPARENT,
  86. NWC_OMBROWSERHOST, NULL,
  87. WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | (0x00000FFF & param->fStyle),
  88. param->x, param->y, param->cx, param->cy,
  89. param->hParent, (HMENU)(INT_PTR)param->controlId,
  90. param->hInstance, param);
  91. }
  92. static BOOL CALLBACK BrowserHost_KeyFilter(HWND hwnd, MSG *pMsg)
  93. {
  94. BROWSERHOST *host = GetHost(hwnd);
  95. if (NULL == host) return FALSE;
  96. UINT vKey = (UINT)pMsg->wParam;
  97. switch(vKey)
  98. {
  99. case VK_CONTROL:
  100. case VK_MENU:
  101. case VK_SHIFT:
  102. return TRUE;
  103. }
  104. if (NULL != host->hAccel && (hwnd == pMsg->hwnd || IsChild(hwnd, pMsg->hwnd)))
  105. {
  106. HWND hParent = GetParent(hwnd);
  107. if (NULL != hParent && 0 != TranslateAccelerator(hParent, host->hAccel, pMsg))
  108. return TRUE;
  109. }
  110. return (NULL != host->container &&
  111. FALSE != host->container->TranslateKey(pMsg));
  112. }
  113. HWND BrowserHost_CreateWindow(obj_ombrowser *browserManager, HWND hParent, UINT fStyle, INT x, INT y, INT cx, INT cy, INT controlId, HACCEL hAccel)
  114. {
  115. if (FALSE == BrowserHost_RegisterClass(Plugin_GetInstance()))
  116. return NULL;
  117. BROWSERHOSTCREATEPARAM param = {0};
  118. param.hParent = hParent;
  119. param.controlId = controlId;
  120. param.hInstance = Plugin_GetInstance();
  121. param.x = x;
  122. param.y = y;
  123. param.cx = cx;
  124. param.cy = cy;
  125. param.fStyle = fStyle;
  126. param.hAccel = hAccel;
  127. param.browserManager = browserManager;
  128. HWND hWinamp = NULL;
  129. if (FAILED(Plugin_GetWinampWnd(&hWinamp)))
  130. hWinamp = NULL;
  131. HWND hHost = NULL;
  132. HANDLE hThread = BrowserThread_Create(hWinamp, BrowserHost_CreateHostWindow, (ULONG_PTR)&param, BrowserHost_KeyFilter, &hHost, NULL);
  133. if (NULL != hThread)
  134. CloseHandle(hThread);
  135. return hHost;
  136. }
  137. static LRESULT BrowserHost_SendBrowserNotification(Browser *browser, INT code, NMHDR *pnmh)
  138. {
  139. HWND hHost = (NULL != browser) ? browser->GetParentHWND() : NULL;
  140. if (NULL == hHost || NULL == pnmh)
  141. return 0;
  142. HWND hParent = GetParent(hHost);
  143. if (NULL == hParent)
  144. return 0;
  145. pnmh->code = code;
  146. pnmh->hwndFrom = hHost;
  147. pnmh->idFrom = GetDlgCtrlID(hHost);
  148. return SendMessage(hParent, WM_NOTIFY, (WPARAM)pnmh->idFrom, (LPARAM)pnmh);
  149. }
  150. static LRESULT BrowserHost_SendNotification(HWND hwnd, NMHDR *phdr)
  151. {
  152. HWND hParent = GetParent(hwnd);
  153. if (NULL == hParent || NULL == phdr || !IsWindow(hParent))
  154. return 0;
  155. return SendMessage(hParent, WM_NOTIFY, (WPARAM)phdr->idFrom, (LPARAM)phdr);
  156. }
  157. static void BrowserHost_UpdateLayout(HWND hwnd, BOOL fRedraw)
  158. {
  159. BROWSERHOST *host = GetHost(hwnd);
  160. if (NULL == host) return;
  161. RECT clientRect;
  162. if (!GetClientRect(hwnd, &clientRect))
  163. return;
  164. if (NULL != host->container)
  165. {
  166. host->container->SetLocation(clientRect.left, clientRect.top,
  167. clientRect.right - clientRect.left,
  168. clientRect.bottom - clientRect.top);
  169. }
  170. }
  171. static COLORREF BrwoserHost_GetBackColor(HWND hwnd)
  172. {
  173. COLORREF rgb;
  174. ifc_skinnedbrowser *skinnedBrowser = NULL;
  175. if (SUCCEEDED(Plugin_GetBrowserSkin(&skinnedBrowser)) && skinnedBrowser != NULL)
  176. {
  177. rgb = skinnedBrowser->GetBackColor();
  178. skinnedBrowser->Release();
  179. }
  180. else
  181. rgb = GetSysColor(COLOR_WINDOW);
  182. return rgb;
  183. }
  184. static void BrowserHost_Paint(HWND hwnd, HDC hdc, const RECT *prcPaint, BOOL fErase)
  185. {
  186. SetBkColor(hdc, BrwoserHost_GetBackColor(hwnd));
  187. ExtTextOut(hdc, 0, 0, ETO_OPAQUE, prcPaint, NULL, 0, NULL);
  188. }
  189. static LRESULT BrowserHost_OnCreate(HWND hwnd, CREATESTRUCT *pcs)
  190. {
  191. BROWSERHOST *host= (BROWSERHOST*)calloc(1, sizeof(BROWSERHOST));
  192. if (NULL != host)
  193. {
  194. SetLastError(ERROR_SUCCESS);
  195. if (!SetWindowLongPtr(hwnd, 0, (LONGX86)(LONG_PTR)host) && ERROR_SUCCESS != GetLastError())
  196. {
  197. free(host);
  198. host = NULL;
  199. }
  200. }
  201. if (NULL == host)
  202. {
  203. DestroyWindow(hwnd);
  204. return -1;
  205. }
  206. BROWSERHOSTCREATEPARAM *param = (BROWSERHOSTCREATEPARAM*)pcs->lpCreateParams;
  207. host->hAccel = param->hAccel;
  208. host->browserManager = param->browserManager;
  209. if (NULL != host->browserManager)
  210. host->browserManager->AddRef();
  211. return 0;
  212. }
  213. static void BrowserHost_OnDestroy(HWND hwnd)
  214. {
  215. BROWSERHOST *host = GetHost(hwnd);
  216. /*if (NULL != host->container)
  217. {
  218. // SendMessage(hwnd, BTM_RELEASECONTAINER, 0, 0L);
  219. // BrowserThread_SetFlags(BHTF_BEGINDESTROY, BHTF_BEGINDESTROY, FALSE);
  220. }*/
  221. SetWindowLongPtr(hwnd, 0, 0L);
  222. if (NULL != host)
  223. {
  224. if (NULL != host->browserManager)
  225. {
  226. host->browserManager->Release();
  227. }
  228. free(host);
  229. }
  230. #ifdef _DEBUG
  231. aTRACE_FMT("[%d] %S: host window destroyed\r\n", GetCurrentThreadId(), OMBROWSER_NAME);
  232. #endif // _DEBUG
  233. }
  234. static void BrowserHost_OnPaint(HWND hwnd)
  235. {
  236. PAINTSTRUCT ps = {0};
  237. if (BeginPaint(hwnd, &ps))
  238. {
  239. if (ps.rcPaint.left != ps.rcPaint.right)
  240. BrowserHost_Paint(hwnd, ps.hdc, &ps.rcPaint, ps.fErase);
  241. EndPaint(hwnd, &ps);
  242. }
  243. }
  244. static void BrowserHost_OnPrintClient(HWND hwnd, HDC hdc, UINT options)
  245. {
  246. RECT clientRect;
  247. if (GetClientRect(hwnd, &clientRect))
  248. BrowserHost_Paint(hwnd, hdc, &clientRect, 0 != (PRF_ERASEBKGND & options));
  249. }
  250. static void BrowserHost_OnWindowPosChanged(HWND hwnd, WINDOWPOS *pwp)
  251. {
  252. if (SWP_NOSIZE == ((SWP_NOSIZE | SWP_FRAMECHANGED) & pwp->flags))
  253. return;
  254. BrowserHost_UpdateLayout(hwnd, 0 == (SWP_NOREDRAW & pwp->flags));
  255. }
  256. static void BrowserHost_OnCommand(HWND hwnd, INT controlId, INT eventId, HWND hControl)
  257. {
  258. }
  259. static HRESULT BrowserHost_AssociateDispatch(HWND hwnd, IDispatch *pDispatch)
  260. {
  261. if (NULL == pDispatch)
  262. return E_INVALIDARG;
  263. IWasabiDispatchable *pWasabi = NULL;
  264. HRESULT hr = pDispatch->QueryInterface(IID_IWasabiDispatchable, (void**)&pWasabi);
  265. if (SUCCEEDED(hr) && pWasabi != NULL)
  266. {
  267. JSAPI::ifc_info *pInfo = NULL;
  268. hr = pWasabi->QueryDispatchable(JSAPI::IID_JSAPI_ifc_info, (Dispatchable**)&pInfo);
  269. if (SUCCEEDED(hr) && pInfo != NULL)
  270. {
  271. HWND hParent = GetParent(hwnd);
  272. if (NULL == hParent) hParent = hwnd;
  273. pInfo->SetHWND(hParent);
  274. WCHAR szBuffer[512] = {0};
  275. BROWSERHOST *host = GetHost(hwnd);
  276. if (NULL != host && NULL != host->container)
  277. {
  278. ifc_omservice *service = NULL;
  279. if (SUCCEEDED(BrowserHost_GetOmService(host->container, &service)) && service != NULL)
  280. {
  281. if (FAILED(service->GetName(szBuffer, ARRAYSIZE(szBuffer))))
  282. szBuffer[0]= L'\0';
  283. service->Release();
  284. }
  285. }
  286. pInfo->SetName(szBuffer);
  287. pInfo->Release();
  288. }
  289. pWasabi->Release();
  290. }
  291. return hr;
  292. }
  293. static void BrowserHost_OnContainerInit(HWND winampWindow, HWND hwnd)
  294. {
  295. BROWSERHOST *host = GetHost(hwnd);
  296. if (NULL == host) return;
  297. if (NULL != host->container) return; // we're alredy running
  298. DWORD windowStyle = GetWindowStyle(hwnd);
  299. HTMLContainer2_Initialize();
  300. Browser *container = Browser::CreateInstance(host->browserManager, winampWindow, hwnd);
  301. host->container = container;
  302. if (NULL != container)
  303. {
  304. ifc_omservice *service = NULL;
  305. if (SUCCEEDED(BrowserHost_GetOmService(container, &service)) && service != NULL)
  306. {
  307. IDispatch *pDispatch = NULL;
  308. if (SUCCEEDED(service->GetExternal(&pDispatch)) && NULL != pDispatch)
  309. {
  310. BrowserHost_AssociateDispatch(hwnd, pDispatch);
  311. container->SetExternal(pDispatch);
  312. pDispatch->Release();
  313. }
  314. service->Release();
  315. }
  316. container->EventDocumentReady = BrowserHost_OnDocumentReady;
  317. container->EventNavigateComplete = BrowserHost_OnNavigateComplete;
  318. container->EventContainerDestroyed = BrowserHost_OnContainerDestroy;
  319. container->EventDownloadBegin = BrowserHost_OnDownloadBegin;
  320. container->EventDownloadComplete = BrowserHost_OnDownloadComplete;
  321. container->EventCommandStateChange = BrowserHost_OnCommandStateChange;
  322. container->EventStatusChange = BrowserHost_OnStatusChange;
  323. container->EventTitleChange = BrowserHost_OnTitleChange;
  324. container->EventSecureLockIconChange = BrowserHost_OnSecureLockIconChange;
  325. container->EventCreatePopup = BrowserHost_OnCreatePopup;
  326. container->EventFocusChange = BrowserHost_OnFocusChange;
  327. container->EventWindowClosing = BrowserHost_OnWindowClosing;
  328. if (0 != (NBHS_POPUP & windowStyle))
  329. {
  330. container->EventVisible = BrowserHost_OnVisibleChange;
  331. container->EventSetResizable = BrowserHost_OnSetResizable;
  332. container->EventSetFullscreen = BrowserHost_OnSetFullScreen;
  333. container->EventShowUiElement = BrowserHost_OnShowUiElenent;
  334. container->EventClientToHost = BrowserHost_OnClientToHost;
  335. container->EventSetWindowPos = BrowserHost_OnSetWindowPos;
  336. container->EventClosePopup = BrowserHost_OnClosePopup;
  337. }
  338. container->CallbackGetOmService = BrowserHost_GetOmService;
  339. container->CallbackRedirectKey = BrowserHost_RedirectKey;
  340. UINT uiFlags = 0;
  341. if (0 != (NBHS_DISABLECONTEXTMENU & windowStyle)) uiFlags |= Browser::flagUiDisableContextMenu;
  342. if (0 != (NBHS_DIALOGMODE & windowStyle)) uiFlags |= Browser::flagUiDialogMode;
  343. if (0 != (NBHS_DISABLEHOSTCSS & windowStyle)) uiFlags |= Browser::flagUiDisableHostCss;
  344. if (0 != uiFlags)
  345. container->SetUiFlags(uiFlags, uiFlags);
  346. HRESULT hr = container->Initialize(0 != (NBHS_POPUP & windowStyle));
  347. if (SUCCEEDED(hr) && 0 == (NBHS_SCRIPTMODE & windowStyle))
  348. {
  349. // we need to do this to allow script error disabling
  350. hr = container->NavigateToName(NULL, 0);
  351. }
  352. if (FAILED(hr) || 0 != (NBHS_SCRIPTMODE & windowStyle))
  353. {
  354. if (0 == (NBHS_BROWSERREADY & windowStyle))
  355. {
  356. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle | NBHS_BROWSERREADY);
  357. NMHDR hdr;
  358. BrowserHost_SendBrowserNotification(container, NBHN_READY, &hdr);
  359. }
  360. }
  361. }
  362. HWND hParent = GetParent(hwnd);
  363. if (NULL != hParent)
  364. {
  365. DWORD threadMine = 0, threadParent = 0;
  366. threadMine = GetCurrentThreadId();
  367. threadParent = GetWindowThreadProcessId(hParent, NULL);
  368. if (0 != threadMine && threadMine != threadParent)
  369. {
  370. AttachThreadInput(threadMine, threadParent, TRUE);
  371. HKL hkl = GetKeyboardLayout(threadParent);
  372. if (NULL != hkl)
  373. ActivateKeyboardLayout(hkl, 0);
  374. }
  375. }
  376. }
  377. static void BrowserHost_OnContainerRelease(HWND hwnd)
  378. {
  379. BROWSERHOST *host = GetHost(hwnd);
  380. if (NULL == host) return;
  381. Browser *container = host->container;
  382. host->container = NULL;
  383. if (NULL == container) return;
  384. container->EventDocumentReady = NULL;
  385. container->EventNavigateComplete = NULL;
  386. container->EventDownloadBegin = NULL;
  387. container->EventDownloadComplete = NULL;
  388. container->EventCommandStateChange = NULL;
  389. container->EventStatusChange = NULL;
  390. container->EventTitleChange = NULL;
  391. container->EventSecureLockIconChange = NULL;
  392. container->EventCreatePopup = NULL;
  393. container->EventVisible = NULL;
  394. container->EventSetResizable = NULL;
  395. container->EventSetFullscreen = NULL;
  396. container->EventWindowClosing = NULL;
  397. container->EventShowUiElement = NULL;
  398. container->EventClientToHost = NULL;
  399. container->EventSetWindowPos = NULL;
  400. container->EventFocusChange = NULL;
  401. container->CallbackGetOmService = NULL;
  402. container->CallbackRedirectKey = NULL;
  403. if (SUCCEEDED(container->SendCommand(Browser::commandStop)) &&
  404. SUCCEEDED(container->NavigateToName(NULL, navNoHistory | navNoWriteToCache)))
  405. {
  406. IWebBrowser2 *pWeb2 = NULL;
  407. if (SUCCEEDED(container->GetIWebBrowser2(&pWeb2)) && pWeb2 != NULL)
  408. {
  409. ReplyMessage(0);
  410. BrowserThread_WaitNavigateComplete(pWeb2, 30000);
  411. pWeb2->Release();
  412. }
  413. }
  414. else
  415. {
  416. }
  417. container->Finish();
  418. container->Release();
  419. HWND hParent = GetParent(hwnd);
  420. if (NULL != hParent)
  421. {
  422. DWORD threadMine = 0, threadParent = 0;
  423. threadMine = GetCurrentThreadId();
  424. threadParent = GetWindowThreadProcessId(hParent, NULL);
  425. if (0 != threadMine && threadMine != threadParent)
  426. {
  427. AttachThreadInput(threadMine, threadParent, FALSE);
  428. }
  429. }
  430. }
  431. static LRESULT BrowserHost_OnNiceDestroy(HWND hwnd, BOOL fImmediate)
  432. {
  433. DWORD windowStyle = GetWindowStyle(hwnd);
  434. if (0 != (WS_VISIBLE & windowStyle))
  435. SetWindowLongPtr(hwnd, GWL_STYLE, windowStyle & ~WS_VISIBLE);
  436. BROWSERHOST *host = GetHost(hwnd);
  437. if (NULL != host && NULL != host->container && FALSE == fImmediate)
  438. {
  439. HWND hWinamp = NULL;
  440. if (FAILED(Plugin_GetWinampWnd(&hWinamp)))
  441. hWinamp = NULL;
  442. if (hWinamp != GetParent(hwnd))
  443. {
  444. SetParent(hwnd, hWinamp);
  445. SetWindowLongPtr(hwnd, GWLP_HWNDPARENT, (LONGX86)(LONG_PTR)hWinamp);
  446. }
  447. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
  448. if (FALSE != BrowserThread_PostDestroy(hwnd))
  449. return TRUE;
  450. }
  451. BrowserThread_SetFlags(BHTF_BEGINDESTROY, BHTF_BEGINDESTROY, FALSE);
  452. BrowserHost_OnContainerRelease(hwnd);
  453. return (0 != DestroyWindow(hwnd));
  454. }
  455. static void BrowserHost_OnContainerCommand(HWND hwnd, INT commandId)
  456. {
  457. BROWSERHOST *host = GetHost(hwnd);
  458. if (NULL == host) return;
  459. if (NULL == host->container) return;
  460. host->container->SendCommand(commandId);
  461. }
  462. static void BrowserHost_OnUpdateSkin(HWND hwnd)
  463. {
  464. BROWSERHOST *host = GetHost(hwnd);
  465. if (NULL == host) return;
  466. if (NULL != host->container)
  467. {
  468. //HWND hWinamp;
  469. //if (NULL == host->browserManager || FAILED(host->browserManager->GetWinampWindow(&hWinamp)))
  470. // hWinamp = NULL;
  471. //HCURSOR cursor = (HCURSOR)SENDWAIPC(hWinamp, IPC_GETSKINCURSORS, WACURSOR_NORMAL);
  472. //if (NULL != cursor)
  473. //{
  474. // host->container->RegisterBrowserCursor(/*OCR_NORMAL*/32512, CopyCursor(cursor));
  475. //}
  476. }
  477. UINT windowStyle = GetWindowStyle(hwnd);
  478. if (NBHS_BROWSERREADY == ((NBHS_BROWSERREADY | NBHS_BROWSERACTIVE) & windowStyle))
  479. {
  480. PostMessage(hwnd, NBHM_CONTAINERCOMMAND, (WPARAM)Browser::commandRefresh, 0L);
  481. }
  482. }
  483. static void BrowserHost_OnNavigate(HWND hwnd, BSTR url, UINT flags)
  484. {
  485. BROWSERHOST *host = GetHost(hwnd);
  486. if (NULL != host &&
  487. NULL != host->container &&
  488. FALSE == BrowserThread_IsQuiting())
  489. {
  490. host->container->NavigateToName(url, flags);
  491. }
  492. if (NULL != url)
  493. {
  494. SysFreeString(url);
  495. }
  496. }
  497. static void BrowserHost_OnEnableContainerUpdate(HWND hwnd, BOOL fEnable, BOOL fRedraw)
  498. {
  499. BROWSERHOST *host = GetHost(hwnd);
  500. if (NULL == host || NULL == host->container) return;
  501. HWND hContainer = host->container->GetHostHWND();
  502. if (NULL == hContainer) return;
  503. SendMessage(hContainer, WM_SETREDRAW, fEnable, 0L);
  504. if (fEnable && fRedraw)
  505. RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_UPDATENOW | RDW_NOERASE | RDW_NOFRAME | RDW_NOINTERNALPAINT | RDW_VALIDATE | RDW_ERASENOW );
  506. }
  507. static void BrowserHost_OnQueueAPC(HWND hwnd, PAPCFUNC pfnApc, ULONG_PTR param)
  508. {
  509. if (NULL == pfnApc) return;
  510. pfnApc(param);
  511. }
  512. static void BrowserHost_OnShowHistoryPopup(HWND hwnd, UINT fuFlags, POINTS pts)
  513. {
  514. BROWSERHOST *host = GetHost(hwnd);
  515. if (NULL != host && NULL != host->container)
  516. {
  517. ifc_travelloghelper *travelLog;
  518. if (SUCCEEDED(host->container->GetTravelLog(&travelLog)))
  519. {
  520. travelLog->ShowPopup(fuFlags, pts.x, pts.y, hwnd, NULL);
  521. travelLog->Release();
  522. }
  523. }
  524. }
  525. static void BrowserHost_OnGetDispatchApc(HWND hwnd, DISPATCHAPC callback, ULONG_PTR param)
  526. {
  527. if (NULL == callback) return;
  528. IDispatch *pDisp = NULL;
  529. BROWSERHOST *host = GetHost(hwnd);
  530. if (NULL != host && NULL != host->container)
  531. {
  532. IWebBrowser2 *pWeb2 = NULL;
  533. if (SUCCEEDED(host->container->GetIWebBrowser2(&pWeb2)) && pWeb2 != NULL)
  534. {
  535. pWeb2->get_Application(&pDisp);
  536. pWeb2->Release();
  537. }
  538. }
  539. callback(pDisp, param);
  540. if (NULL != pDisp)
  541. pDisp->Release();
  542. }
  543. static void BrowserHost_OnActivate(HWND hwnd)
  544. {
  545. BROWSERHOST *host = GetHost(hwnd);
  546. if (NULL != host && NULL != host->container)
  547. {
  548. host->container->SetFocus(TRUE);
  549. }
  550. }
  551. static void BrowserHost_OnQueryTitle(HWND hwnd)
  552. {
  553. BSTR bstrName = NULL;
  554. BROWSERHOST *host = GetHost(hwnd);
  555. if (NULL != host && NULL != host->container)
  556. {
  557. IWebBrowser2 *pWeb2 = NULL;
  558. if (SUCCEEDED(host->container->GetIWebBrowser2(&pWeb2)) && pWeb2 != NULL)
  559. {
  560. if (FAILED(pWeb2->get_LocationName(&bstrName)))
  561. bstrName = NULL;
  562. pWeb2->Release();
  563. }
  564. }
  565. BHNTEXTCHANGE textChange = {0};
  566. textChange.hdr.code = NBHN_TITLECHANGE;
  567. textChange.hdr.hwndFrom = hwnd;
  568. textChange.hdr.idFrom = GetDlgCtrlID(hwnd);
  569. textChange.pszText = bstrName;
  570. BrowserHost_SendNotification(hwnd, (NMHDR*)&textChange);
  571. SysFreeString(bstrName);
  572. }
  573. static void BrowserHost_OnToggleFullscreen(HWND hwnd)
  574. {
  575. BROWSERHOST *host = GetHost(hwnd);
  576. if (NULL == host || NULL == host->container)
  577. return;
  578. host->container->ToggleFullscreen();
  579. }
  580. static void BrowserHost_OnWriteDocument(HWND hwnd, BSTR documentData)
  581. {
  582. BROWSERHOST *host = GetHost(hwnd);
  583. if (NULL == host ||
  584. NULL == host->container ||
  585. FAILED(host->container->WriteDocument(documentData)))
  586. {
  587. SysFreeString(documentData);
  588. }
  589. }
  590. static void BrowserHost_OnEnableWindow(HWND hwnd, BOOL fEnable)
  591. {
  592. EnableWindow(hwnd, fEnable);
  593. }
  594. static void BrowserHost_OnUpdateExternal(HWND hwnd)
  595. {
  596. BROWSERHOST *host = GetHost(hwnd);
  597. if (NULL == host) return;
  598. if (NULL != host->container)
  599. {
  600. ifc_omservice *service = NULL;
  601. if (SUCCEEDED(BrowserHost_GetOmService(host->container, &service)) && service != NULL)
  602. {
  603. IDispatch *pDispatch = NULL;
  604. if (SUCCEEDED(service->GetExternal(&pDispatch)) && NULL != pDispatch)
  605. {
  606. BrowserHost_AssociateDispatch(hwnd, pDispatch);
  607. host->container->SetExternal(pDispatch);
  608. pDispatch->Release();
  609. }
  610. service->Release();
  611. }
  612. }
  613. }
  614. static LRESULT CALLBACK BrowserHost_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  615. {
  616. switch(uMsg)
  617. {
  618. case WM_CREATE:
  619. return BrowserHost_OnCreate(hwnd, (CREATESTRUCT*)lParam);
  620. case WM_DESTROY:
  621. BrowserHost_OnDestroy(hwnd);
  622. break;
  623. case WM_ERASEBKGND:
  624. return 0;
  625. case WM_PAINT:
  626. BrowserHost_OnPaint(hwnd);
  627. return 0;
  628. case WM_PRINTCLIENT:
  629. BrowserHost_OnPrintClient(hwnd, (HDC)wParam, (UINT)lParam);
  630. return 0;
  631. case WM_WINDOWPOSCHANGED:
  632. BrowserHost_OnWindowPosChanged(hwnd, (WINDOWPOS*)lParam);
  633. return 0;
  634. case WM_COMMAND:
  635. BrowserHost_OnCommand(hwnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam);
  636. break;
  637. case BTM_INITCONTAINER:
  638. BrowserHost_OnContainerInit((HWND)wParam, hwnd);
  639. return 0;
  640. case BTM_RELEASECONTAINER:
  641. BrowserHost_OnContainerRelease(hwnd);
  642. return 0;
  643. case NBHM_DESTROY:
  644. return BrowserHost_OnNiceDestroy(hwnd, (BOOL)wParam);
  645. case NBHM_CONTAINERCOMMAND:
  646. BrowserHost_OnContainerCommand(hwnd, (INT)wParam);
  647. return 0;
  648. case NBHM_UPDATESKIN:
  649. BrowserHost_OnUpdateSkin(hwnd);
  650. return 0;
  651. case NBHM_NAVIGATE:
  652. BrowserHost_OnNavigate(hwnd, (BSTR)lParam, (UINT)wParam);
  653. return 0;
  654. case NBHM_ENABLECONTAINERUPDATE:
  655. BrowserHost_OnEnableContainerUpdate(hwnd, (BOOL)wParam, (BOOL)lParam);
  656. return 0;
  657. case NBHM_QUEUEAPC:
  658. BrowserHost_OnQueueAPC(hwnd, (PAPCFUNC)lParam, (ULONG_PTR)wParam);
  659. return 0;
  660. case NBHM_SHOWHISTORYPOPUP:
  661. BrowserHost_OnShowHistoryPopup(hwnd, (UINT)wParam, MAKEPOINTS(lParam));
  662. return 0;
  663. case NBHM_GETDISPATCHAPC:
  664. BrowserHost_OnGetDispatchApc(hwnd, (DISPATCHAPC)lParam, (ULONG_PTR)wParam);
  665. return 0;
  666. case NBHM_ACTIVATE:
  667. BrowserHost_OnActivate(hwnd);
  668. return 0;
  669. case NBHM_QUERYTITLE:
  670. BrowserHost_OnQueryTitle(hwnd);
  671. return 0;
  672. case NBHM_TOGGLEFULLSCREEN:
  673. BrowserHost_OnToggleFullscreen(hwnd);
  674. return 0;
  675. case NBHM_WRITEDOCUMENT:
  676. BrowserHost_OnWriteDocument(hwnd, (BSTR)lParam);
  677. return 0;
  678. case NBHM_ENABLEWINDOW:
  679. BrowserHost_OnEnableWindow(hwnd, (BOOL)lParam);
  680. return 0;
  681. case NBHM_UPDATEEXTERNAL:
  682. BrowserHost_OnUpdateExternal(hwnd);
  683. return 0;
  684. }
  685. return DefWindowProcW(hwnd, uMsg, wParam, lParam);
  686. }
  687. static DWORD BrowserHost_GetBrowserFlags(Browser *browser)
  688. {
  689. if (NULL == browser) return 0;
  690. HWND hHost = browser->GetParentHWND();
  691. if (NULL == hHost) return 0;
  692. DWORD windowStyle = GetWindowStyle(hHost);
  693. return (NBHS_BROWSERMASK & windowStyle);
  694. }
  695. static void CALLBACK BrowserHost_OnDocumentReady(Browser *browser, IDispatch *pDispatch, VARIANT *URL)
  696. {
  697. HWND hHost = browser->GetParentHWND();
  698. if (NULL == hHost) return;
  699. DWORD windowStyle = GetWindowStyle(hHost);
  700. if (0 == (NBHS_BROWSERREADY & windowStyle))
  701. {
  702. SetWindowLongPtr(hHost, GWL_STYLE, windowStyle | NBHS_BROWSERREADY);
  703. NMHDR hdr = {0};
  704. BrowserHost_SendBrowserNotification(browser, NBHN_READY, &hdr);
  705. }
  706. else
  707. {
  708. BHNNAVCOMPLETE document = {0};
  709. document.pDispatch = pDispatch;
  710. document.URL = URL;
  711. document.fTopFrame = TRUE;
  712. BrowserHost_SendBrowserNotification(browser, NBHN_DOCUMENTREADY, (NMHDR*)&document);
  713. }
  714. }
  715. static void CALLBACK BrowserHost_OnNavigateComplete(Browser *browser, IDispatch *pDispatch, VARIANT *URL)
  716. {
  717. HWND hHost = browser->GetParentHWND();
  718. if (NULL == hHost) return;
  719. DWORD windowStyle = GetWindowStyle(hHost);
  720. if (0 == (NBHS_BROWSERREADY & windowStyle))
  721. return;
  722. IWebBrowser2 *pWeb1 = NULL, *pWeb2 = NULL;
  723. if (FAILED(browser->GetIWebBrowser2(&pWeb1)))
  724. pWeb1 = NULL;
  725. if (NULL == pDispatch || FAILED(pDispatch->QueryInterface(IID_IWebBrowser2, (void**)&pWeb2)))
  726. pWeb2 = NULL;
  727. if (NULL != pWeb1) pWeb1->Release();
  728. if (NULL != pWeb2) pWeb2->Release();
  729. BHNNAVCOMPLETE document = {0};
  730. document.pDispatch = pDispatch;
  731. document.URL = URL;
  732. document.fTopFrame = (NULL != pWeb1 && pWeb1 == pWeb2);
  733. BrowserHost_SendBrowserNotification(browser, NBHN_NAVIGATECOMPLETE, (NMHDR*)&document);
  734. }
  735. static void CALLBACK BrowserHost_OnContainerDestroy(Browser *browser)
  736. {
  737. BrowserThread_SetFlags(BHTF_BEGINDESTROY, BHTF_BEGINDESTROY, FALSE);
  738. PostMessage(NULL, WM_QUIT, 0, 0L);
  739. }
  740. static void BrowserHost_NotifyContainerActive(HWND hwnd, BOOL fActive, UINT_PTR timerId)
  741. {
  742. if (0 != timerId)
  743. KillTimer(hwnd, timerId);
  744. DWORD windowStyle = GetWindowStyle(hwnd);
  745. if ((FALSE == fActive) != (0 == (NBHS_BROWSERACTIVE & windowStyle)))
  746. return;
  747. if (0 == (NBHS_BROWSERREADY & windowStyle))
  748. fActive = FALSE;
  749. BHNACTIVE active = {0};
  750. active.hdr.code = NBHN_BROWSERACTIVE;
  751. active.hdr.hwndFrom = hwnd;
  752. active.hdr.idFrom = GetDlgCtrlID(hwnd);
  753. active.fActive = fActive;
  754. BrowserHost_SendNotification(hwnd, (NMHDR*)&active);
  755. BROWSERHOST *host = GetHost(hwnd);
  756. INT commandState = 0;
  757. if (NULL != host &&
  758. NULL != host->container &&
  759. SUCCEEDED(host->container->QueryCommandState(Browser::commandStop, &commandState)))
  760. {
  761. BOOL fEnable = (0 != (Browser::commandStateEnabled & commandState));
  762. BrowserHost_OnCommandStateChange(host->container, Browser::commandStop, fEnable);
  763. }
  764. }
  765. static void CALLBACK BrowserHost_ActivateContainerTimer(HWND hwnd, UINT uMsg, UINT_PTR eventId, DWORD time)
  766. {
  767. BrowserHost_NotifyContainerActive(hwnd, TRUE, eventId);
  768. }
  769. static void CALLBACK BrowserHost_DeactivateContainerTimer(HWND hwnd, UINT uMsg, UINT_PTR eventId, DWORD time)
  770. {
  771. BrowserHost_NotifyContainerActive(hwnd, FALSE, eventId);
  772. }
  773. static void CALLBACK BrowserHost_OnDownloadBegin(Browser *browser)
  774. {
  775. HWND hHost = browser->GetParentHWND();
  776. if (NULL == hHost) return;
  777. DWORD windowStyle = GetWindowStyle(hHost);
  778. if (NBHS_BROWSERREADY == ((NBHS_BROWSERACTIVE | NBHS_BROWSERREADY) & windowStyle))
  779. {
  780. SetWindowLongPtr(hHost, GWL_STYLE, windowStyle | NBHS_BROWSERACTIVE);
  781. SetTimer(hHost, BHT_CONTAINERDOWNLOAD, BHT_ACTIVATECONTAINER_DELAY, BrowserHost_ActivateContainerTimer);
  782. }
  783. }
  784. static void CALLBACK BrowserHost_OnDownloadComplete(Browser *browser)
  785. {
  786. HWND hHost = browser->GetParentHWND();
  787. if (NULL == hHost) return;
  788. DWORD windowStyle = GetWindowStyle(hHost);
  789. if (0 != (NBHS_BROWSERACTIVE & windowStyle))
  790. {
  791. SetWindowLongPtr(hHost, GWL_STYLE, windowStyle & ~NBHS_BROWSERACTIVE);
  792. SetTimer(hHost, BHT_CONTAINERDOWNLOAD, BHT_DEACTIVATECONTAINER_DELAY, BrowserHost_DeactivateContainerTimer);
  793. }
  794. }
  795. static void CALLBACK BrowserHost_OnCommandStateChange(Browser *browser, INT commandId, BOOL fEnabled)
  796. {
  797. if (Browser::commandStop == commandId && FALSE != fEnabled &&
  798. NBHS_BROWSERREADY == ((NBHS_BROWSERACTIVE | NBHS_BROWSERREADY) & BrowserHost_GetBrowserFlags(browser)))
  799. {
  800. fEnabled = FALSE;
  801. }
  802. BHNCMDSTATE commandState = {0};
  803. commandState.commandId = commandId;
  804. commandState.fEnabled = fEnabled;
  805. BrowserHost_SendBrowserNotification(browser, NBHN_COMMANDSTATECHANGE, (NMHDR*)&commandState);
  806. }
  807. static void CALLBACK BrowserHost_OnStatusChange(Browser *browser, LPCWSTR pszText)
  808. {
  809. if (0 == (NBHS_BROWSERREADY & BrowserHost_GetBrowserFlags(browser)))
  810. return;
  811. BHNTEXTCHANGE textChange = {0};
  812. textChange.pszText = pszText;
  813. BrowserHost_SendBrowserNotification(browser, NBHN_STATUSCHANGE, (NMHDR*)&textChange);
  814. }
  815. static void CALLBACK BrowserHost_OnTitleChange(Browser *browser, LPCWSTR pszText)
  816. {
  817. if (0 == (NBHS_BROWSERREADY & BrowserHost_GetBrowserFlags(browser)))
  818. return;
  819. BHNTEXTCHANGE textChange = {0};
  820. textChange.pszText = pszText;
  821. BrowserHost_SendBrowserNotification(browser, NBHN_TITLECHANGE, (NMHDR*)&textChange);
  822. }
  823. static void CALLBACK BrowserHost_OnSecureLockIconChange(Browser *browser)
  824. {
  825. BHNSECUREICON icon = {0};
  826. icon.iconId = browser->GetSecueLockIcon();
  827. BrowserHost_SendBrowserNotification(browser, NBHN_SECUREICONCHANGE, (NMHDR*)&icon);
  828. }
  829. typedef struct __POPUPCALLBACKPARAM
  830. {
  831. IDispatch **ppDisp;
  832. HANDLE readyEvent;
  833. HWND hHost;
  834. } POPUPCALLBACKPARAM;
  835. typedef struct __DISPATCHMARSHALPARAM
  836. {
  837. LPSTREAM pStream;
  838. IDispatch *pDisp;
  839. HRESULT hr;
  840. } DISPATCHMARSHALPARAM;
  841. static void CALLBACK BrowserHost_DispatchMarshalApc(ULONG_PTR user)
  842. {
  843. DISPATCHMARSHALPARAM *param = (DISPATCHMARSHALPARAM*)user;
  844. if (NULL == param) return;
  845. param->hr = CoGetInterfaceAndReleaseStream(param->pStream, IID_IDispatch, (void**)&param->pDisp);
  846. //if (SUCCEEDED(param->hr) && NULL != param->pDisp)
  847. // param->pDisp->AddRef();
  848. }
  849. static void CALLBACK BrowserHost_DispatchCallbackApc(IDispatch *pDisp, ULONG_PTR user)
  850. {
  851. POPUPCALLBACKPARAM *param = (POPUPCALLBACKPARAM*)user;
  852. if (NULL == param) return;
  853. if (NULL != pDisp)
  854. {
  855. LPSTREAM pStream = NULL;
  856. if (SUCCEEDED(CoMarshalInterThreadInterfaceInStream(IID_IDispatch, pDisp, &pStream)))
  857. { // switch
  858. DISPATCHMARSHALPARAM marshal = {0};
  859. marshal.pStream = pStream;
  860. SendMessage(param->hHost, NBHM_QUEUEAPC, (WPARAM)&marshal, (LPARAM)BrowserHost_DispatchMarshalApc);
  861. if (SUCCEEDED(marshal.hr) && NULL != marshal.pDisp)
  862. {
  863. *param->ppDisp = marshal.pDisp;
  864. }
  865. }
  866. }
  867. if (NULL != param->readyEvent)
  868. SetEvent(param->readyEvent);
  869. }
  870. static HRESULT BrowserHost_GetSecurityApi(JSAPI2::api_security **securityApi)
  871. {
  872. if (NULL == securityApi)
  873. return E_POINTER;
  874. *securityApi = NULL;
  875. ifc_wasabihelper *wasabiHelper = NULL;
  876. HRESULT hr = Plugin_GetWasabiHelper(&wasabiHelper);
  877. if (SUCCEEDED(hr) && wasabiHelper != NULL)
  878. {
  879. hr = wasabiHelper->GetSecurityApi(securityApi);
  880. wasabiHelper->Release();
  881. }
  882. return hr;
  883. }
  884. static BOOL BrowserHost_IsPopupAllowed(HWND hwnd, Browser *browser)
  885. {
  886. ifc_omservice *service = NULL;
  887. if (FAILED(BrowserHost_GetOmService(browser, &service)) || NULL == service)
  888. return TRUE;
  889. BOOL fAllowed = TRUE;
  890. JSAPI::ifc_info *pInfo = NULL;
  891. WCHAR szKey[64] = {0};
  892. if (FAILED(StringCchPrintfW(szKey, ARRAYSIZE(szKey), L"%u", service->GetId())))
  893. szKey[0] = L'\0';
  894. IDispatch *pDispatch = NULL;
  895. if (SUCCEEDED(browser->GetExternal(&pDispatch)) && NULL != pDispatch)
  896. {
  897. IWasabiDispatchable *pWasabi = NULL;
  898. if (SUCCEEDED(pDispatch->QueryInterface(IID_IWasabiDispatchable, (void**)&pWasabi)) && pWasabi != NULL)
  899. {
  900. if (FAILED(pWasabi->QueryDispatchable(JSAPI::IID_JSAPI_ifc_info, (Dispatchable**)&pInfo)))
  901. pInfo = NULL;
  902. pWasabi->Release();
  903. }
  904. pDispatch->Release();
  905. }
  906. if (NULL != pInfo && L'\0' != szKey)
  907. {
  908. JSAPI2::api_security *security = NULL;
  909. if (SUCCEEDED(BrowserHost_GetSecurityApi(&security)) && security != NULL)
  910. {
  911. if (security->GetActionAuthorization(L"application", L"launchurl", szKey, pInfo,
  912. JSAPI2::api_security::ACTION_PROMPT) != JSAPI2::api_security::ACTION_ALLOWED)
  913. {
  914. fAllowed = FALSE;
  915. }
  916. security->Release();
  917. }
  918. }
  919. if (NULL != pInfo)
  920. pInfo->Release();
  921. service->Release();
  922. return fAllowed;
  923. }
  924. static void CALLBACK BrowserHost_OnCreatePopup(Browser *browser, IDispatch **ppDisp, VARIANT_BOOL *Cancel)
  925. {
  926. HWND hHost = browser->GetParentHWND();
  927. if (FALSE == BrowserHost_IsPopupAllowed(hHost, browser))
  928. {
  929. if (NULL != Cancel) *Cancel = VARIANT_TRUE;
  930. return;
  931. }
  932. POPUPCALLBACKPARAM param = {0};
  933. param.hHost = browser->GetParentHWND();
  934. if (NULL == param.hHost) return;
  935. param.readyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  936. param.ppDisp = ppDisp;
  937. BHNCREATEPOPUP popup = {0};
  938. popup.callback = BrowserHost_DispatchCallbackApc;
  939. popup.param = (ULONG_PTR)&param;
  940. if (FALSE != BrowserHost_SendBrowserNotification(browser, NBHN_CREATEPOPUP, (NMHDR*)&popup))
  941. {
  942. BrowserThread_ModalLoop(param.hHost, param.readyEvent, 30000);
  943. }
  944. if (NULL != param.readyEvent)
  945. CloseHandle(param.readyEvent);
  946. }
  947. static void CALLBACK BrowserHost_OnVisibleChange(Browser *browser, VARIANT_BOOL fVisible)
  948. {
  949. BHNVISIBLE visible = {0};
  950. visible.fVisible = (VARIANT_FALSE != fVisible);
  951. BrowserHost_SendBrowserNotification(browser, NBHN_VISIBLECHANGE, (NMHDR*)&visible);
  952. }
  953. static void CALLBACK BrowserHost_OnSetResizable(Browser *browser, VARIANT_BOOL fAllow)
  954. {
  955. BHNRESIZABLE resizable = {0};
  956. resizable.fEnabled = (VARIANT_FALSE != fAllow);
  957. BrowserHost_SendBrowserNotification(browser, NBHN_RESIZABLE, (NMHDR*)&resizable);
  958. }
  959. static void CALLBACK BrowserHost_OnSetFullScreen(Browser *browser, VARIANT_BOOL fEnable)
  960. {
  961. BHNFULLSCREEN fullscreen = {0};
  962. fullscreen.fEnable = (VARIANT_FALSE != fEnable);
  963. BrowserHost_SendBrowserNotification(browser, NBHN_FULLSCREEN, (NMHDR*)&fullscreen);
  964. }
  965. static void CALLBACK BrowserHost_OnWindowClosing(Browser *browser, VARIANT_BOOL fIsChild, VARIANT_BOOL *fCancel)
  966. {
  967. BHNCLOSING closing = {0};
  968. closing.isChild = (VARIANT_FALSE != fIsChild);
  969. closing.cancel = FALSE;
  970. BrowserHost_SendBrowserNotification(browser, NBHN_CLOSING, (NMHDR*)&closing);
  971. if (FALSE != closing.cancel && NULL != fCancel)
  972. *fCancel = VARIANT_TRUE;
  973. }
  974. static void CALLBACK BrowserHost_OnShowUiElenent(Browser *browser, UINT elementId, VARIANT_BOOL fShow)
  975. {
  976. BHNSHOWUI ui = {0};
  977. ui.elementId = elementId;
  978. ui.fShow = fShow;
  979. BrowserHost_SendBrowserNotification(browser, NBHN_SHOWUI, (NMHDR*)&ui);
  980. }
  981. static void CALLBACK BrowserHost_OnClientToHost(Browser *browser, LONG *cx, LONG *cy)
  982. {
  983. BHNCLIENTTOHOST convert = {0};
  984. convert.cx = *cx;
  985. convert.cy = *cy;
  986. BrowserHost_SendBrowserNotification(browser, NBHN_CLIENTTOHOST, (NMHDR*)&convert);
  987. *cx = convert.cx;
  988. *cy = convert.cy;
  989. }
  990. static void CALLBACK BrowserHost_OnSetWindowPos(Browser *browser, UINT flags, LONG x, LONG y, LONG cx, LONG cy)
  991. {
  992. BHNSETWINDOWPOS windowPos = {0};
  993. windowPos.flags = flags;
  994. windowPos.x = x;
  995. windowPos.y = y;
  996. windowPos.cx = cx;
  997. windowPos.cy = cy;
  998. BrowserHost_SendBrowserNotification(browser, NBHN_SETWINDOWPOS, (NMHDR*)&windowPos);
  999. }
  1000. static void CALLBACK BrowserHost_OnFocusChange(Browser *browser, VARIANT_BOOL *fAllow)
  1001. {
  1002. BHNFOCUSCHANGE focus = {0};
  1003. focus.fAllow = (VARIANT_FALSE != *fAllow);
  1004. BrowserHost_SendBrowserNotification(browser, NBHN_FOCUSCHANGE, (NMHDR*)&focus);
  1005. *fAllow = (FALSE != focus.fAllow) ? VARIANT_TRUE : VARIANT_FALSE;
  1006. }
  1007. static void CALLBACK BrowserHost_OnClosePopup(Browser *browser)
  1008. {
  1009. NMHDR hdr = {0};
  1010. BrowserHost_SendBrowserNotification(browser, NBHN_CLOSEPOPUP, &hdr);
  1011. }
  1012. static HRESULT CALLBACK BrowserHost_GetOmService(Browser *browser, ifc_omservice **ppService)
  1013. {
  1014. if (NULL == ppService) return E_POINTER;
  1015. BHNSERVICE service = {0};
  1016. if (FALSE != BrowserHost_SendBrowserNotification(browser, NBHN_GETOMSERVICE, (NMHDR*)&service) &&
  1017. NULL != service.instance)
  1018. {
  1019. *ppService = (ifc_omservice*)service.instance;
  1020. return S_OK;
  1021. }
  1022. *ppService = NULL;
  1023. return E_NOTIMPL;
  1024. }
  1025. static LRESULT CALLBACK BrowserHost_RedirectKey(Browser *browser, MSG *pMsg)
  1026. {
  1027. HWND hHost = browser->GetParentHWND();
  1028. HWND hParent = GetParent(hHost);
  1029. if (NULL == hParent)
  1030. {
  1031. BROWSERHOST *host = GetHost(hHost);
  1032. if (NULL != host && FAILED(Plugin_GetWinampWnd(&hParent)))
  1033. hParent = NULL;
  1034. }
  1035. return PostMessage(hParent, pMsg->message, pMsg->wParam, pMsg->lParam);
  1036. }