#pragma once
#include <JavaScriptCore/Strong.h>
#include <wtf/HashMap.h>
#include <wtf/RefCounted.h>
#include <wtf/UniqueRef.h>
namespace JSC {
class Debugger;
}
namespace WebCore {
class AbstractDOMWindow;
class AbstractFrame;
class DOMWrapperWorld;
class JSDOMGlobalObject;
class JSWindowProxy;
class WindowProxy : public RefCounted<WindowProxy> {
WTF_MAKE_FAST_ALLOCATED;
public:
using ProxyMap = HashMap<RefPtr<DOMWrapperWorld>, JSC::Strong<JSWindowProxy>>;
static Ref<WindowProxy> create(AbstractFrame& frame)
{
return adoptRef(*new WindowProxy(frame));
}
WEBCORE_EXPORT ~WindowProxy();
AbstractFrame* frame() const { return m_frame; }
void detachFromFrame();
void destroyJSWindowProxy(DOMWrapperWorld&);
ProxyMap::ValuesConstIteratorRange jsWindowProxies() const;
Vector<JSC::Strong<JSWindowProxy>> jsWindowProxiesAsVector() const;
WEBCORE_EXPORT ProxyMap releaseJSWindowProxies();
WEBCORE_EXPORT void setJSWindowProxies(ProxyMap&&);
JSWindowProxy* jsWindowProxy(DOMWrapperWorld& world)
{
if (!m_frame)
return nullptr;
if (auto* existingProxy = existingJSWindowProxy(world))
return existingProxy;
return &createJSWindowProxyWithInitializedScript(world);
}
JSWindowProxy* existingJSWindowProxy(DOMWrapperWorld& world) const
{
auto it = m_jsWindowProxies->find(&world);
return (it != m_jsWindowProxies->end()) ? it->value.get() : nullptr;
}
WEBCORE_EXPORT JSDOMGlobalObject* globalObject(DOMWrapperWorld&);
void clearJSWindowProxiesNotMatchingDOMWindow(AbstractDOMWindow*, bool goingIntoBackForwardCache);
WEBCORE_EXPORT void setDOMWindow(AbstractDOMWindow*);
void attachDebugger(JSC::Debugger*);
WEBCORE_EXPORT AbstractDOMWindow* window() const;
private:
explicit WindowProxy(AbstractFrame&);
JSWindowProxy& createJSWindowProxy(DOMWrapperWorld&);
WEBCORE_EXPORT JSWindowProxy& createJSWindowProxyWithInitializedScript(DOMWrapperWorld&);
AbstractFrame* m_frame;
UniqueRef<ProxyMap> m_jsWindowProxies;
};
}