Commit 9d32d07a authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

Make ScriptedAnimationController a ContextLifecycleStateObserver

This removes the need for special-case pausing/unpausing in Document, as well
as custom Document::Shutdown() logic.

Bug: 1029822
Change-Id: I2c9cda5bb3720626cdc1f3fcadb7876422ca375e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1949275
Commit-Queue: Nate Chapin <japhet@chromium.org>
Auto-Submit: Nate Chapin <japhet@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721599}
parent 4fa4076a
......@@ -3368,9 +3368,6 @@ void Document::Shutdown() {
probe::DocumentDetached(this);
// FIXME: consider using ContextLifecycleStateObserver.
if (scripted_animation_controller_)
scripted_animation_controller_->ClearDocumentPointer();
scripted_animation_controller_.Clear();
scripted_idle_task_controller_.Clear();
......@@ -7311,17 +7308,11 @@ void Document::AddConsoleMessageImpl(ConsoleMessage* console_message,
void Document::TasksWerePaused() {
GetScriptRunner()->Suspend();
if (scripted_animation_controller_)
scripted_animation_controller_->Pause();
}
void Document::TasksWereUnpaused() {
GetScriptRunner()->Resume();
if (scripted_animation_controller_)
scripted_animation_controller_->Unpause();
MutationObserver::ResumeSuspendedObservers();
if (dom_window_)
DOMWindowPerformance::performance(*dom_window_)->ResumeSuspendedObservers();
......@@ -7440,9 +7431,9 @@ ScriptedAnimationController& Document::EnsureScriptedAnimationController() {
scripted_animation_controller_ =
MakeGarbageCollected<ScriptedAnimationController>(this);
// We need to make sure that we don't start up the animation controller on a
// background tab, for example.
// detached document.
if (!GetPage())
scripted_animation_controller_->Pause();
scripted_animation_controller_->Disable();
}
return *scripted_animation_controller_;
}
......
......@@ -43,27 +43,22 @@ std::pair<EventTarget*, StringImpl*> EventTargetKey(const Event* event) {
}
ScriptedAnimationController::ScriptedAnimationController(Document* document)
: document_(document), callback_collection_(document), suspend_count_(0) {}
: ContextLifecycleStateObserver(document), callback_collection_(document) {
UpdateStateIfNeeded();
}
void ScriptedAnimationController::Trace(Visitor* visitor) {
visitor->Trace(document_);
ContextLifecycleStateObserver::Trace(visitor);
visitor->Trace(callback_collection_);
visitor->Trace(event_queue_);
visitor->Trace(media_query_list_listeners_);
visitor->Trace(per_frame_events_);
}
void ScriptedAnimationController::Pause() {
++suspend_count_;
}
void ScriptedAnimationController::Unpause() {
// It would be nice to put an DCHECK_GT(suspend_count_, 0) here, but in WK1
// resume() can be called even when suspend hasn't (if a tab was created in
// the background).
if (suspend_count_ > 0)
--suspend_count_;
ScheduleAnimationIfNeeded();
void ScriptedAnimationController::ContextLifecycleStateChanged(
mojom::FrameLifecycleState state) {
if (state == mojom::FrameLifecycleState::kRunning)
ScheduleAnimationIfNeeded();
}
void ScriptedAnimationController::DispatchEventsAndCallbacksForPrinting() {
......@@ -142,7 +137,7 @@ void ScriptedAnimationController::DispatchEvents(
void ScriptedAnimationController::ExecuteFrameCallbacks() {
// dispatchEvents() runs script which can cause the document to be destroyed.
if (!document_)
if (!GetDocument())
return;
callback_collection_.ExecuteFrameCallbacks(current_frame_time_ms_,
......@@ -159,24 +154,26 @@ void ScriptedAnimationController::CallMediaQueryListListeners() {
}
bool ScriptedAnimationController::HasScheduledFrameTasks() const {
if (suspend_count_)
if (disabled_ || !GetDocument() || GetDocument()->IsContextPaused())
return false;
return callback_collection_.HasFrameCallback() || !task_queue_.IsEmpty() ||
!event_queue_.IsEmpty() || !media_query_list_listeners_.IsEmpty() ||
(document_ && document_->HasAutofocusCandidates());
GetDocument()->HasAutofocusCandidates();
}
void ScriptedAnimationController::ServiceScriptedAnimations(
base::TimeTicks monotonic_time_now) {
if (document_ && document_->Loader()) {
if (GetDocument() && GetDocument()->Loader()) {
current_frame_time_ms_ =
document_->Loader()
GetDocument()
->Loader()
->GetTiming()
.MonotonicTimeToZeroBasedDocumentTime(monotonic_time_now)
.InMillisecondsF();
current_frame_legacy_time_ms_ =
document_->Loader()
GetDocument()
->Loader()
->GetTiming()
.MonotonicTimeToPseudoWallTime(monotonic_time_now)
.InMillisecondsF();
......@@ -191,8 +188,8 @@ void ScriptedAnimationController::ServiceScriptedAnimations(
// 10.5. For each fully active Document in docs, flush autofocus
// candidates for that Document if its browsing context is a top-level
// browsing context.
if (document_)
document_->FlushAutofocusCandidates();
if (GetDocument())
GetDocument()->FlushAutofocusCandidates();
// 10.8. For each fully active Document in docs, evaluate media
// queries and report changes for that Document, passing in now as the
......@@ -259,9 +256,9 @@ void ScriptedAnimationController::EnqueueMediaQueryChangeListeners(
}
void ScriptedAnimationController::ScheduleAnimationIfNeeded() {
if (suspend_count_ || !document_)
if (disabled_ || !GetDocument() || GetDocument()->IsContextPaused())
return;
LocalFrameView* frame_view = document_->View();
LocalFrameView* frame_view = GetDocument()->View();
if (!frame_view)
return;
......@@ -278,7 +275,7 @@ void ScriptedAnimationController::ScheduleAnimationIfNeeded() {
// no need to schedule another one.
if (!callback_collection_.HasPostFrameCallback())
return;
if (Page* page = document_->GetPage()) {
if (Page* page = GetDocument()->GetPage()) {
if (!page->Animator().IsServicingAnimations())
frame_view->ScheduleAnimation();
}
......
......@@ -28,8 +28,10 @@
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/frame_request_callback_collection.h"
#include "third_party/blink/renderer/core/execution_context/context_lifecycle_state_observer.h"
#include "third_party/blink/renderer/platform/bindings/name_client.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "third_party/blink/renderer/platform/wtf/text/string_impl.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
......@@ -43,16 +45,18 @@ class MediaQueryListListener;
class CORE_EXPORT ScriptedAnimationController
: public GarbageCollected<ScriptedAnimationController>,
public ContextLifecycleStateObserver,
public NameClient {
USING_GARBAGE_COLLECTED_MIXIN(ScriptedAnimationController);
public:
explicit ScriptedAnimationController(Document*);
virtual ~ScriptedAnimationController() = default;
~ScriptedAnimationController() override = default;
void Trace(Visitor*);
void Trace(Visitor*) override;
const char* NameInHeapSnapshot() const override {
return "ScriptedAnimationController";
}
void ClearDocumentPointer() { document_ = nullptr; }
// Animation frame callbacks are used for requestAnimationFrame().
typedef int CallbackId;
......@@ -82,8 +86,8 @@ class CORE_EXPORT ScriptedAnimationController
void ServiceScriptedAnimations(base::TimeTicks monotonic_time_now);
void RunPostFrameCallbacks();
void Pause();
void Unpause();
void ContextLifecycleStateChanged(mojom::FrameLifecycleState) final;
void Disable() { disabled_ = true; }
void DispatchEventsAndCallbacksForPrinting();
......@@ -101,9 +105,10 @@ class CORE_EXPORT ScriptedAnimationController
bool HasScheduledFrameTasks() const;
Member<Document> document_;
Document* GetDocument() const { return To<Document>(GetExecutionContext()); }
FrameRequestCallbackCollection callback_collection_;
int suspend_count_;
bool disabled_ = false;
Vector<base::OnceClosure> task_queue_;
HeapVector<Member<Event>> event_queue_;
HeapListHashSet<std::pair<Member<const EventTarget>, const StringImpl*>>
......
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