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.

288 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. #if JUCE_WEB_BROWSER || DOXYGEN
  21. //==============================================================================
  22. /**
  23. A component that displays an embedded web browser.
  24. The browser itself will be platform-dependent. On Mac and iOS it will be
  25. WebKit, on Android it will be Chrome, and on Linux it will be WebKit.
  26. On Windows it will be IE, but if JUCE_USE_WIN_WEBVIEW2 is enabled then using
  27. the WindowsWebView2WebBrowserComponent wrapper instead of this class directly
  28. will attempt to use the Microsoft Edge (Chromium) WebView2. See the documentation
  29. of that class for more information about its requirements.
  30. @tags{GUI}
  31. */
  32. class JUCE_API WebBrowserComponent : public Component
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a WebBrowserComponent.
  37. Once it's created and visible, send the browser to a URL using goToURL().
  38. @param unloadPageWhenBrowserIsHidden if this is true, then when the browser
  39. component is taken offscreen, it'll clear the current page
  40. and replace it with a blank page - this can be handy to stop
  41. the browser using resources in the background when it's not
  42. actually being used.
  43. */
  44. explicit WebBrowserComponent (bool unloadPageWhenBrowserIsHidden = true);
  45. /** Destructor. */
  46. ~WebBrowserComponent() override;
  47. //==============================================================================
  48. /** Sends the browser to a particular URL.
  49. @param url the URL to go to.
  50. @param headers an optional set of parameters to put in the HTTP header. If
  51. you supply this, it should be a set of string in the form
  52. "HeaderKey: HeaderValue"
  53. @param postData an optional block of data that will be attached to the HTTP
  54. POST request
  55. */
  56. void goToURL (const String& url,
  57. const StringArray* headers = nullptr,
  58. const MemoryBlock* postData = nullptr);
  59. /** Stops the current page loading. */
  60. void stop();
  61. /** Sends the browser back one page. */
  62. void goBack();
  63. /** Sends the browser forward one page. */
  64. void goForward();
  65. /** Refreshes the browser. */
  66. void refresh();
  67. /** Clear cookies that the OS has stored for the WebComponents of this application */
  68. static void clearCookies();
  69. //==============================================================================
  70. /** This callback is called when the browser is about to navigate
  71. to a new location.
  72. You can override this method to perform some action when the user
  73. tries to go to a particular URL. To allow the operation to carry on,
  74. return true, or return false to stop the navigation happening.
  75. */
  76. virtual bool pageAboutToLoad (const String& newURL) { ignoreUnused (newURL); return true; }
  77. /** This callback happens when the browser has finished loading a page. */
  78. virtual void pageFinishedLoading (const String& url) { ignoreUnused (url); }
  79. /** This callback happens when a network error was encountered while
  80. trying to load a page.
  81. You can override this method to show some other error page by calling
  82. goToURL. Return true to allow the browser to carry on to the internal
  83. browser error page.
  84. The errorInfo contains some platform dependent string describing the
  85. error.
  86. */
  87. virtual bool pageLoadHadNetworkError (const String& errorInfo) { ignoreUnused (errorInfo); return true; }
  88. /** This callback occurs when a script or other activity in the browser asks for
  89. the window to be closed.
  90. */
  91. virtual void windowCloseRequest() {}
  92. /** This callback occurs when the browser attempts to load a URL in a new window.
  93. This won't actually load the window but gives you a chance to either launch a
  94. new window yourself or just load the URL into the current window with goToURL().
  95. */
  96. virtual void newWindowAttemptingToLoad (const String& newURL) { ignoreUnused (newURL); }
  97. //==============================================================================
  98. /** @internal */
  99. void paint (Graphics&) override;
  100. /** @internal */
  101. void resized() override;
  102. /** @internal */
  103. void parentHierarchyChanged() override;
  104. /** @internal */
  105. void visibilityChanged() override;
  106. /** @internal */
  107. void focusGained (FocusChangeType) override;
  108. /** @internal */
  109. class Pimpl;
  110. protected:
  111. friend class WindowsWebView2WebBrowserComponent;
  112. /** @internal */
  113. struct ConstructWithoutPimpl
  114. {
  115. explicit ConstructWithoutPimpl (bool unloadOnHide) : unloadWhenHidden (unloadOnHide) {}
  116. const bool unloadWhenHidden;
  117. };
  118. explicit WebBrowserComponent (ConstructWithoutPimpl);
  119. private:
  120. //==============================================================================
  121. std::unique_ptr<Pimpl> browser;
  122. bool blankPageShown = false, unloadPageWhenHidden;
  123. String lastURL;
  124. StringArray lastHeaders;
  125. MemoryBlock lastPostData;
  126. void reloadLastURL();
  127. void checkWindowAssociation();
  128. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WebBrowserComponent)
  129. };
  130. //==============================================================================
  131. /** Class used to create a set of preferences to pass to the WindowsWebView2WebBrowserComponent
  132. wrapper constructor to modify aspects of its behaviour and settings.
  133. You can chain together a series of calls to this class's methods to create a set of whatever
  134. preferences you want to specify.
  135. @tags{GUI}
  136. */
  137. class JUCE_API WebView2Preferences
  138. {
  139. public:
  140. //==============================================================================
  141. /** Sets a custom location for the WebView2Loader.dll that is not a part of the
  142. standard system DLL search paths.
  143. */
  144. JUCE_NODISCARD WebView2Preferences withDLLLocation (const File& location) const { return with (&WebView2Preferences::dllLocation, location); }
  145. /** Sets a non-default location for storing user data for the browser instance. */
  146. WebView2Preferences withUserDataFolder (const File& folder) const { return with (&WebView2Preferences::userDataFolder, folder); }
  147. /** If this is set, the status bar usually displayed in the lower-left of the webview
  148. will be disabled.
  149. */
  150. JUCE_NODISCARD WebView2Preferences withStatusBarDisabled() const { return with (&WebView2Preferences::disableStatusBar, true); }
  151. /** If this is set, a blank page will be displayed on error instead of the default
  152. built-in error page.
  153. */
  154. JUCE_NODISCARD WebView2Preferences withBuiltInErrorPageDisabled() const { return with (&WebView2Preferences::disableBuiltInErrorPage, true); }
  155. /** Sets the background colour that WebView2 renders underneath all web content.
  156. This colour must either be fully opaque or transparent. On Windows 7 this
  157. colour must be opaque.
  158. */
  159. JUCE_NODISCARD WebView2Preferences withBackgroundColour (const Colour& colour) const
  160. {
  161. // the background colour must be either fully opaque or transparent!
  162. jassert (colour.isOpaque() || colour.isTransparent());
  163. return with (&WebView2Preferences::backgroundColour, colour);
  164. }
  165. //==============================================================================
  166. File getDLLLocation() const { return dllLocation; }
  167. File getUserDataFolder() const { return userDataFolder; }
  168. bool getIsStatusBarDisabled() const noexcept { return disableStatusBar; }
  169. bool getIsBuiltInErrorPageDisabled() const noexcept { return disableBuiltInErrorPage; }
  170. Colour getBackgroundColour() const { return backgroundColour; }
  171. private:
  172. //==============================================================================
  173. template <typename Member, typename Item>
  174. WebView2Preferences with (Member&& member, Item&& item) const
  175. {
  176. auto options = *this;
  177. options.*member = std::forward<Item> (item);
  178. return options;
  179. }
  180. File dllLocation, userDataFolder;
  181. bool disableStatusBar = false, disableBuiltInErrorPage = false;
  182. Colour backgroundColour = Colours::white;
  183. };
  184. /**
  185. If you have enabled the JUCE_USE_WIN_WEBVIEW2 flag then this wrapper will attempt to
  186. use the Microsoft Edge (Chromium) WebView2 control instead of IE on Windows. It will
  187. behave the same as WebBrowserComponent on all other platforms and will fall back to
  188. IE on Windows if the WebView2 requirements are not met.
  189. This requires Microsoft Edge (minimum version 82.0.488.0) to be installed at runtime.
  190. Currently this also requires that WebView2Loader.dll, which can be found in the
  191. Microsoft.Web.WebView package, is installed at runtime. As this is not a standard
  192. system DLL, we can't rely on it being found via the normal system DLL search paths.
  193. Therefore in order to use WebView2 you need to ensure that WebView2Loader.dll is
  194. installed either to a location covered by the Windows DLL system search paths or
  195. to the folder specified in the WebView2Preferences.
  196. @tags{GUI}
  197. */
  198. class WindowsWebView2WebBrowserComponent : public WebBrowserComponent
  199. {
  200. public:
  201. //==============================================================================
  202. /** Creates a WebBrowserComponent that is compatible with the WebView2 control
  203. on Windows.
  204. @param unloadPageWhenBrowserIsHidden if this is true, then when the browser
  205. component is taken offscreen, it'll clear the current page
  206. and replace it with a blank page - this can be handy to stop
  207. the browser using resources in the background when it's not
  208. actually being used.
  209. @param preferences a set of preferences used to control aspects of the webview's
  210. behaviour.
  211. @see WebView2Preferences
  212. */
  213. WindowsWebView2WebBrowserComponent (bool unloadPageWhenBrowserIsHidden = true,
  214. const WebView2Preferences& preferences = {});
  215. // This constructor has been deprecated. Use the new constructor that takes a
  216. // WebView2Preferences instead.
  217. explicit WindowsWebView2WebBrowserComponent (bool unloadPageWhenBrowserIsHidden = true,
  218. const File& dllLocation = {},
  219. const File& userDataFolder = {})
  220. : WindowsWebView2WebBrowserComponent (unloadPageWhenBrowserIsHidden,
  221. WebView2Preferences().withDLLLocation (dllLocation)
  222. .withUserDataFolder (userDataFolder))
  223. {
  224. }
  225. };
  226. #endif
  227. } // namespace juce