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.

214 lines
8.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. private:
  111. //==============================================================================
  112. friend class WindowsWebView2WebBrowserComponent;
  113. explicit WebBrowserComponent (bool unloadPageWhenBrowserIsHidden,
  114. const File& dllLocation,
  115. const File& userDataFolder);
  116. //==============================================================================
  117. std::unique_ptr<Pimpl> browser;
  118. bool blankPageShown = false, unloadPageWhenBrowserIsHidden;
  119. String lastURL;
  120. StringArray lastHeaders;
  121. MemoryBlock lastPostData;
  122. void reloadLastURL();
  123. void checkWindowAssociation();
  124. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WebBrowserComponent)
  125. };
  126. //==============================================================================
  127. /**
  128. If you have enabled the JUCE_USE_WIN_WEBVIEW2 flag then this wrapper will attempt to
  129. use the Microsoft Edge (Chromium) WebView2 control instead of IE on Windows. It will
  130. behave the same as WebBrowserComponent on all other platforms and will fall back to
  131. IE on Windows if the WebView2 requirements are not met.
  132. This requires Microsoft Edge (minimum version 82.0.488.0) to be installed at runtime.
  133. Currently this also requires that WebView2Loader.dll, which can be found in the
  134. Microsoft.Web.WebView package, is installed at runtime. As this is not a standard
  135. system DLL, we can't rely on it being found via the normal system DLL search paths.
  136. Therefore in order to use WebView2 you need to ensure that WebView2Loader.dll is
  137. installed either to a location covered by the Windows DLL system search paths or
  138. to the folder specified in the constructor of this class.
  139. @tags{GUI}
  140. */
  141. class WindowsWebView2WebBrowserComponent : public WebBrowserComponent
  142. {
  143. public:
  144. /** Creates a WebBrowserComponent that is compatible with the WebView2 control
  145. on Windows.
  146. This allows you to specify a custom location for the WebView2Loader.dll as
  147. well as a non-default location for storing user data for the browser instance.
  148. @param unloadPageWhenBrowserIsHidden if this is true, then when the browser
  149. component is taken offscreen, it'll clear the current page
  150. and replace it with a blank page - this can be handy to stop
  151. the browser using resources in the background when it's not
  152. actually being used.
  153. @param dllLocation the path to WebView2Loader.dll, if this is empty then the default
  154. system DLL search paths will be used
  155. @param userDataFolder a directory in which the WebView2 user data will be stored, if
  156. this is empty then a directory will be created next to the
  157. executable
  158. */
  159. explicit WindowsWebView2WebBrowserComponent (bool unloadPageWhenBrowserIsHidden = true,
  160. const File& dllLocation = {},
  161. const File& userDataFolder = {})
  162. : WebBrowserComponent (unloadPageWhenBrowserIsHidden, dllLocation, userDataFolder)
  163. {
  164. }
  165. };
  166. #endif
  167. } // namespace juce