Commit a82bb82a authored by haraken's avatar haraken Committed by Commit bot

Introduce DOMWindowClient

The lifetime of ExecutionContext and DOMWindow is a bit different.
In common case, they get detached at the same timing but in a case where
a frame navigates from an initial empty document to another same-origin document,
the ExecutionContext gets detached but the DOMWindow is reused.
Due to the (subtle) difference, we need DOMWindowClient in addition to ContextClient.

ContextClient is a client for objects that want to observe ExecutionContext.
DOMWindowClient is a client for objects that want to observe DOMWindow.

This CL uses DOMWindowClient for Navigator.

BUG=610176

Review-Url: https://codereview.chromium.org/2629493004
Cr-Commit-Position: refs/heads/master@{#443216}
parent 184a5d74
......@@ -5,10 +5,14 @@
#include "core/dom/ContextLifecycleObserver.h"
#include "core/dom/Document.h"
#include "core/frame/LocalDOMWindow.h"
#include "core/frame/LocalFrame.h"
namespace blink {
ContextClient::ContextClient(ExecutionContext* executionContext)
: m_executionContext(executionContext) {}
ContextClient::ContextClient(LocalFrame* frame)
: m_executionContext(frame ? frame->document() : nullptr) {}
......@@ -33,4 +37,22 @@ LocalFrame* ContextLifecycleObserver::frame() const {
? toDocument(getExecutionContext())->frame()
: nullptr;
}
DOMWindowClient::DOMWindowClient(LocalDOMWindow* window)
: m_domWindow(window) {}
DOMWindowClient::DOMWindowClient(LocalFrame* frame)
: m_domWindow(frame ? frame->domWindow() : nullptr) {}
LocalDOMWindow* DOMWindowClient::domWindow() const {
return m_domWindow && m_domWindow->frame() ? m_domWindow : nullptr;
}
LocalFrame* DOMWindowClient::frame() const {
return m_domWindow ? m_domWindow->frame() : nullptr;
}
DEFINE_TRACE(DOMWindowClient) {
visitor->trace(m_domWindow);
}
}
......@@ -33,17 +33,15 @@
namespace blink {
class LocalDOMWindow;
class LocalFrame;
// ContextClient and ContextLifecycleObserver are helpers to associate a
// class with an ExecutionContext. ContextLifecycleObserver provides an
// additional contextDestroyed() hook to execute cleanup code when a
// context is destroyed. Prefer the simpler ContextClient when possible.
// ContextClient and ContextLifecycleObserver are helpers to associate an
// object with an ExecutionContext.
//
// getExecutionContext() returns null after the observing context is detached.
// frame() returns null after the observing context is detached or if the
// context doesn't have a frame (i.e., if the context is not a Document).
// - getExecutionContext() returns null after the context is detached.
// - frame() is a syntax sugar for getExecutionContext()->frame(). It returns
// null after the context is detached or the context is not a Document.
class CORE_EXPORT ContextClient : public GarbageCollectedMixin {
public:
ExecutionContext* getExecutionContext() const;
......@@ -52,14 +50,16 @@ class CORE_EXPORT ContextClient : public GarbageCollectedMixin {
DECLARE_VIRTUAL_TRACE();
protected:
explicit ContextClient(ExecutionContext* executionContext)
: m_executionContext(executionContext) {}
explicit ContextClient(ExecutionContext*);
explicit ContextClient(LocalFrame*);
private:
WeakMember<ExecutionContext> m_executionContext;
};
// ContextLifecycleObserver provides an additional contextDestroyed() hook
// to execute cleanup code when a context is destroyed. Prefer the simpler
// ContextClient when possible.
class CORE_EXPORT ContextLifecycleObserver
: public LifecycleObserver<ExecutionContext, ContextLifecycleObserver> {
public:
......@@ -82,6 +82,40 @@ class CORE_EXPORT ContextLifecycleObserver
Type m_observerType;
};
// DOMWindowClient is a helper to associate an object with a LocalDOMWindow.
//
// - domWindow() returns null after the window is detached.
// - frame() is a syntax sugar for domWindow()->frame(). It returns
// null after the window is detached.
//
// If the object is a per-ExecutionContext thing, use ContextClient/
// ContextLifecycleObserver. If the object is a per-DOMWindow thing, use
// DOMWindowClient. Basically, DOMWindowClient is expected to be used (only)
// for objects directly held by LocalDOMWindow. Other objects should use
// ContextClient/ContextLifecycleObserver.
//
// There is a subtle difference between the timing when the context gets
// detached and the timing when the window gets detached. In common cases,
// these two happen at the same timing. The only exception is a case where
// a frame navigates from an initial empty document to another same-origin
// document. In this case, a Document is recreated but a DOMWindow is reused.
// Hence, in the navigated document ContextClient::getExecutionContext()
// returns null while DOMWindowClient::domWindow() keeps returning the window.
class CORE_EXPORT DOMWindowClient : public GarbageCollectedMixin {
public:
LocalDOMWindow* domWindow() const;
LocalFrame* frame() const;
DECLARE_VIRTUAL_TRACE();
protected:
explicit DOMWindowClient(LocalDOMWindow*);
explicit DOMWindowClient(LocalFrame*);
private:
WeakMember<LocalDOMWindow> m_domWindow;
};
} // namespace blink
#endif // ContextLifecycleObserver_h
......@@ -53,6 +53,7 @@
#include "core/fetch/ResourceFetcher.h"
#include "core/frame/BarProp.h"
#include "core/frame/DOMVisualViewport.h"
#include "core/frame/DOMWindowProperty.h"
#include "core/frame/EventHandlerRegistry.h"
#include "core/frame/FrameConsole.h"
#include "core/frame/FrameView.h"
......
......@@ -36,7 +36,7 @@
namespace blink {
Navigator::Navigator(LocalFrame* frame) : DOMWindowProperty(frame) {}
Navigator::Navigator(LocalFrame* frame) : DOMWindowClient(frame) {}
String Navigator::productSub() const {
return "20030107";
......@@ -98,7 +98,7 @@ Vector<String> Navigator::languages() {
}
DEFINE_TRACE(Navigator) {
DOMWindowProperty::trace(visitor);
DOMWindowClient::trace(visitor);
Supplementable<Navigator>::trace(visitor);
}
......
......@@ -22,7 +22,7 @@
#include "bindings/core/v8/ScriptWrappable.h"
#include "core/CoreExport.h"
#include "core/frame/DOMWindowProperty.h"
#include "core/dom/ContextLifecycleObserver.h"
#include "core/frame/NavigatorCPU.h"
#include "core/frame/NavigatorID.h"
#include "core/frame/NavigatorLanguage.h"
......@@ -41,7 +41,7 @@ class CORE_EXPORT Navigator final : public GarbageCollected<Navigator>,
public NavigatorLanguage,
public NavigatorOnLine,
public ScriptWrappable,
public DOMWindowProperty,
public DOMWindowClient,
public Supplementable<Navigator> {
DEFINE_WRAPPERTYPEINFO();
USING_GARBAGE_COLLECTED_MIXIN(Navigator);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment