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.

326 lines
10KB

  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. if (browser->browser == nullptr)
  184. checkWindowAssociation();
  185. browser->goToURL (url, headers, postData);
  186. }
  187. void WebBrowserComponent::stop()
  188. {
  189. if (browser->browser != nullptr)
  190. browser->browser->Stop();
  191. }
  192. void WebBrowserComponent::goBack()
  193. {
  194. lastURL.clear();
  195. blankPageShown = false;
  196. if (browser->browser != nullptr)
  197. browser->browser->GoBack();
  198. }
  199. void WebBrowserComponent::goForward()
  200. {
  201. lastURL.clear();
  202. if (browser->browser != nullptr)
  203. browser->browser->GoForward();
  204. }
  205. void WebBrowserComponent::refresh()
  206. {
  207. if (browser->browser != nullptr)
  208. browser->browser->Refresh();
  209. }
  210. //==============================================================================
  211. void WebBrowserComponent::paint (Graphics& g)
  212. {
  213. if (browser->browser == nullptr)
  214. {
  215. g.fillAll (Colours::white);
  216. checkWindowAssociation();
  217. }
  218. }
  219. void WebBrowserComponent::checkWindowAssociation()
  220. {
  221. if (isShowing())
  222. {
  223. if (browser->browser == nullptr && getPeer() != nullptr)
  224. {
  225. browser->createBrowser();
  226. reloadLastURL();
  227. }
  228. else
  229. {
  230. if (blankPageShown)
  231. goBack();
  232. }
  233. }
  234. else
  235. {
  236. if (browser != nullptr && unloadPageWhenBrowserIsHidden && ! blankPageShown)
  237. {
  238. // when the component becomes invisible, some stuff like flash
  239. // carries on playing audio, so we need to force it onto a blank
  240. // page to avoid this..
  241. blankPageShown = true;
  242. browser->goToURL ("about:blank", 0, 0);
  243. }
  244. }
  245. }
  246. void WebBrowserComponent::reloadLastURL()
  247. {
  248. if (lastURL.isNotEmpty())
  249. {
  250. goToURL (lastURL, &lastHeaders, &lastPostData);
  251. lastURL.clear();
  252. }
  253. }
  254. void WebBrowserComponent::parentHierarchyChanged()
  255. {
  256. checkWindowAssociation();
  257. }
  258. void WebBrowserComponent::resized()
  259. {
  260. browser->setSize (getWidth(), getHeight());
  261. }
  262. void WebBrowserComponent::visibilityChanged()
  263. {
  264. checkWindowAssociation();
  265. }