diff --git a/modules/juce_gui_extra/misc/juce_WebBrowserComponent.cpp b/modules/juce_gui_extra/misc/juce_WebBrowserComponent.cpp index 77a2c53381..905a69ec9f 100644 --- a/modules/juce_gui_extra/misc/juce_WebBrowserComponent.cpp +++ b/modules/juce_gui_extra/misc/juce_WebBrowserComponent.cpp @@ -593,9 +593,17 @@ WebBrowserComponent::WebBrowserComponent (const Options& options) ignoreUnused (blankPageShown); ignoreUnused (unloadPageWhenHidden); #endif + + for (const auto& l : options.getLifetimeListeners()) + lifetimeListeners.add (l); + + lifetimeListeners.call (&WebViewLifetimeListener::webViewConstructed, this); } -WebBrowserComponent::~WebBrowserComponent() = default; +WebBrowserComponent::~WebBrowserComponent() +{ + lifetimeListeners.call (&WebViewLifetimeListener::webViewDestructed, this); +} void WebBrowserComponent::emitEventIfBrowserIsVisible (const Identifier& eventId, const var& object) { diff --git a/modules/juce_gui_extra/misc/juce_WebBrowserComponent.h b/modules/juce_gui_extra/misc/juce_WebBrowserComponent.h index 688630bc63..8366bc4ea3 100644 --- a/modules/juce_gui_extra/misc/juce_WebBrowserComponent.h +++ b/modules/juce_gui_extra/misc/juce_WebBrowserComponent.h @@ -43,6 +43,25 @@ namespace juce #define JUCE_WEB_BROWSER_RESOURCE_PROVIDER_AVAILABLE 0 #endif +class WebBrowserComponent; + +/** Type for a listener registered with + WebBrowserComponent::Options::withWebViewLifetimeListener. This can be useful for + types using the WebBrowserComponent::Options::withOptionsFrom function as they have to be + constructed before the WebBrowserComponent. +*/ +class WebViewLifetimeListener +{ +public: + virtual ~WebViewLifetimeListener() = default; + + /** Called from the WebBrowserComponent constructor. */ + virtual void webViewConstructed (WebBrowserComponent*) = 0; + + /** Called from the WebBrowserComponent destructor. */ + virtual void webViewDestructed (WebBrowserComponent*) = 0; +}; + //============================================================================== /** A component that displays an embedded web browser. @@ -372,6 +391,16 @@ public: } #endif + /** Adds an object that will be notified when the WebBrowserComponent is constructed and + destructed. + */ + [[nodiscard]] Options withWebViewLifetimeListener (WebViewLifetimeListener* listener) + { + auto copy = *this; + copy.lifetimeListeners.push_back (listener); + return copy; + } + /** Adds all options provided by the builder to the returned Options object. */ template @@ -393,6 +422,7 @@ public: const auto& getInitialisationData() const { return initialisationData; } auto getResourceProvider() const { return resourceProvider; } const auto& getAllowedOrigin() const { return allowedOrigin; } + const auto& getLifetimeListeners() const { return lifetimeListeners; } private: //============================================================================== @@ -408,6 +438,7 @@ public: std::vector> initialisationData; ResourceProvider resourceProvider; std::optional allowedOrigin; + std::vector lifetimeListeners; }; //============================================================================== @@ -615,6 +646,7 @@ private: String lastURL; StringArray lastHeaders; MemoryBlock lastPostData; + ListenerList lifetimeListeners; void reloadLastURL();