Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_win32_WebBrowserComponent.cpp 9.9KB

10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class WebBrowserComponent::Pimpl : public ActiveXControlComponent
  18. {
  19. public:
  20. Pimpl()
  21. : browser (nullptr),
  22. connectionPoint (nullptr),
  23. adviseCookie (0)
  24. {
  25. }
  26. ~Pimpl()
  27. {
  28. if (connectionPoint != nullptr)
  29. connectionPoint->Unadvise (adviseCookie);
  30. if (browser != nullptr)
  31. browser->Release();
  32. }
  33. void createBrowser()
  34. {
  35. createControl (&CLSID_WebBrowser);
  36. browser = (IWebBrowser2*) queryInterface (&IID_IWebBrowser2);
  37. if (IConnectionPointContainer* connectionPointContainer
  38. = (IConnectionPointContainer*) queryInterface (&IID_IConnectionPointContainer))
  39. {
  40. connectionPointContainer->FindConnectionPoint (DIID_DWebBrowserEvents2, &connectionPoint);
  41. if (connectionPoint != nullptr)
  42. {
  43. WebBrowserComponent* const owner = dynamic_cast <WebBrowserComponent*> (getParentComponent());
  44. jassert (owner != nullptr);
  45. EventHandler* handler = new EventHandler (*owner);
  46. connectionPoint->Advise (handler, &adviseCookie);
  47. handler->Release();
  48. }
  49. }
  50. }
  51. void goToURL (const String& url,
  52. const StringArray* headers,
  53. const MemoryBlock* postData)
  54. {
  55. if (browser != nullptr)
  56. {
  57. LPSAFEARRAY sa = nullptr;
  58. VARIANT flags, frame, postDataVar, headersVar; // (_variant_t isn't available in all compilers)
  59. VariantInit (&flags);
  60. VariantInit (&frame);
  61. VariantInit (&postDataVar);
  62. VariantInit (&headersVar);
  63. if (headers != nullptr)
  64. {
  65. V_VT (&headersVar) = VT_BSTR;
  66. V_BSTR (&headersVar) = SysAllocString ((const OLECHAR*) headers->joinIntoString ("\r\n").toWideCharPointer());
  67. }
  68. if (postData != nullptr && postData->getSize() > 0)
  69. {
  70. sa = SafeArrayCreateVector (VT_UI1, 0, (ULONG) postData->getSize());
  71. if (sa != nullptr)
  72. {
  73. void* data = nullptr;
  74. SafeArrayAccessData (sa, &data);
  75. jassert (data != nullptr);
  76. if (data != nullptr)
  77. {
  78. postData->copyTo (data, 0, postData->getSize());
  79. SafeArrayUnaccessData (sa);
  80. VARIANT postDataVar2;
  81. VariantInit (&postDataVar2);
  82. V_VT (&postDataVar2) = VT_ARRAY | VT_UI1;
  83. V_ARRAY (&postDataVar2) = sa;
  84. postDataVar = postDataVar2;
  85. }
  86. }
  87. }
  88. browser->Navigate ((BSTR) (const OLECHAR*) url.toWideCharPointer(),
  89. &flags, &frame, &postDataVar, &headersVar);
  90. if (sa != nullptr)
  91. SafeArrayDestroy (sa);
  92. VariantClear (&flags);
  93. VariantClear (&frame);
  94. VariantClear (&postDataVar);
  95. VariantClear (&headersVar);
  96. }
  97. }
  98. //==============================================================================
  99. IWebBrowser2* browser;
  100. private:
  101. IConnectionPoint* connectionPoint;
  102. DWORD adviseCookie;
  103. //==============================================================================
  104. class EventHandler : public ComBaseClassHelper <IDispatch>,
  105. public ComponentMovementWatcher
  106. {
  107. public:
  108. EventHandler (WebBrowserComponent& owner_)
  109. : ComponentMovementWatcher (&owner_),
  110. owner (owner_)
  111. {
  112. }
  113. JUCE_COMRESULT GetTypeInfoCount (UINT*) { return E_NOTIMPL; }
  114. JUCE_COMRESULT GetTypeInfo (UINT, LCID, ITypeInfo**) { return E_NOTIMPL; }
  115. JUCE_COMRESULT GetIDsOfNames (REFIID, LPOLESTR*, UINT, LCID, DISPID*) { return E_NOTIMPL; }
  116. JUCE_COMRESULT Invoke (DISPID dispIdMember, REFIID /*riid*/, LCID /*lcid*/, WORD /*wFlags*/, DISPPARAMS* pDispParams,
  117. VARIANT* /*pVarResult*/, EXCEPINFO* /*pExcepInfo*/, UINT* /*puArgErr*/)
  118. {
  119. if (dispIdMember == DISPID_BEFORENAVIGATE2)
  120. {
  121. *pDispParams->rgvarg->pboolVal
  122. = owner.pageAboutToLoad (getStringFromVariant (pDispParams->rgvarg[5].pvarVal)) ? VARIANT_FALSE
  123. : VARIANT_TRUE;
  124. return S_OK;
  125. }
  126. else if (dispIdMember == DISPID_DOCUMENTCOMPLETE)
  127. {
  128. owner.pageFinishedLoading (getStringFromVariant (pDispParams->rgvarg[0].pvarVal));
  129. return S_OK;
  130. }
  131. else if (dispIdMember == 263 /*DISPID_WINDOWCLOSING*/)
  132. {
  133. owner.windowCloseRequest();
  134. // setting this bool tells the browser to ignore the event - we'll handle it.
  135. if (pDispParams->cArgs > 0 && pDispParams->rgvarg[0].vt == (VT_BYREF | VT_BOOL))
  136. *pDispParams->rgvarg[0].pboolVal = VARIANT_TRUE;
  137. return S_OK;
  138. }
  139. return E_NOTIMPL;
  140. }
  141. void componentMovedOrResized (bool, bool) override {}
  142. void componentPeerChanged() override {}
  143. void componentVisibilityChanged() override { owner.visibilityChanged(); }
  144. private:
  145. WebBrowserComponent& owner;
  146. static String getStringFromVariant (VARIANT* v)
  147. {
  148. return (v->vt & VT_BYREF) != 0 ? *v->pbstrVal
  149. : v->bstrVal;
  150. }
  151. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EventHandler)
  152. };
  153. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  154. };
  155. //==============================================================================
  156. WebBrowserComponent::WebBrowserComponent (const bool unloadPageWhenBrowserIsHidden_)
  157. : browser (nullptr),
  158. blankPageShown (false),
  159. unloadPageWhenBrowserIsHidden (unloadPageWhenBrowserIsHidden_)
  160. {
  161. setOpaque (true);
  162. addAndMakeVisible (browser = new Pimpl());
  163. }
  164. WebBrowserComponent::~WebBrowserComponent()
  165. {
  166. delete browser;
  167. }
  168. //==============================================================================
  169. void WebBrowserComponent::goToURL (const String& url,
  170. const StringArray* headers,
  171. const MemoryBlock* postData)
  172. {
  173. lastURL = url;
  174. if (headers != nullptr)
  175. lastHeaders = *headers;
  176. else
  177. lastHeaders.clear();
  178. if (postData != nullptr)
  179. lastPostData = *postData;
  180. else
  181. lastPostData.reset();
  182. blankPageShown = false;
  183. browser->goToURL (url, headers, postData);
  184. }
  185. void WebBrowserComponent::stop()
  186. {
  187. if (browser->browser != nullptr)
  188. browser->browser->Stop();
  189. }
  190. void WebBrowserComponent::goBack()
  191. {
  192. lastURL.clear();
  193. blankPageShown = false;
  194. if (browser->browser != nullptr)
  195. browser->browser->GoBack();
  196. }
  197. void WebBrowserComponent::goForward()
  198. {
  199. lastURL.clear();
  200. if (browser->browser != nullptr)
  201. browser->browser->GoForward();
  202. }
  203. void WebBrowserComponent::refresh()
  204. {
  205. if (browser->browser != nullptr)
  206. browser->browser->Refresh();
  207. }
  208. //==============================================================================
  209. void WebBrowserComponent::paint (Graphics& g)
  210. {
  211. if (browser->browser == nullptr)
  212. g.fillAll (Colours::white);
  213. }
  214. void WebBrowserComponent::checkWindowAssociation()
  215. {
  216. if (isShowing())
  217. {
  218. if (browser->browser == nullptr && getPeer() != nullptr)
  219. {
  220. browser->createBrowser();
  221. reloadLastURL();
  222. }
  223. else
  224. {
  225. if (blankPageShown)
  226. goBack();
  227. }
  228. }
  229. else
  230. {
  231. if (browser != nullptr && unloadPageWhenBrowserIsHidden && ! blankPageShown)
  232. {
  233. // when the component becomes invisible, some stuff like flash
  234. // carries on playing audio, so we need to force it onto a blank
  235. // page to avoid this..
  236. blankPageShown = true;
  237. browser->goToURL ("about:blank", 0, 0);
  238. }
  239. }
  240. }
  241. void WebBrowserComponent::reloadLastURL()
  242. {
  243. if (lastURL.isNotEmpty())
  244. {
  245. goToURL (lastURL, &lastHeaders, &lastPostData);
  246. lastURL.clear();
  247. }
  248. }
  249. void WebBrowserComponent::parentHierarchyChanged()
  250. {
  251. checkWindowAssociation();
  252. }
  253. void WebBrowserComponent::resized()
  254. {
  255. browser->setSize (getWidth(), getHeight());
  256. }
  257. void WebBrowserComponent::visibilityChanged()
  258. {
  259. checkWindowAssociation();
  260. }