Commit f84b1f1d authored by Keishi Hattori's avatar Keishi Hattori Committed by Commit Bot

Remove LifecycleObserver and LifecycleNotifier

There is no more users for LifecycleObserver and LifecycleNotifier so this CL removes them.

Bug: 1052319
Change-Id: I1cd9a45867d66247add3ba53b69cd964cd3a630c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2060428
Commit-Queue: Keishi Hattori <keishi@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#742070}
parent bf410a40
...@@ -1165,8 +1165,6 @@ jumbo_component("platform") { ...@@ -1165,8 +1165,6 @@ jumbo_component("platform") {
"keyboard_codes.h", "keyboard_codes.h",
"language.cc", "language.cc",
"language.h", "language.h",
"lifecycle_notifier.h",
"lifecycle_observer.h",
"link_hash.cc", "link_hash.cc",
"link_hash.h", "link_hash.h",
"mac/block_exceptions.h", "mac/block_exceptions.h",
...@@ -1852,7 +1850,6 @@ jumbo_source_set("blink_platform_unittests_sources") { ...@@ -1852,7 +1850,6 @@ jumbo_source_set("blink_platform_unittests_sources") {
"image-decoders/webp/webp_image_decoder_test.cc", "image-decoders/webp/webp_image_decoder_test.cc",
"json/json_parser_test.cc", "json/json_parser_test.cc",
"json/json_values_test.cc", "json/json_values_test.cc",
"lifecycle_context_test.cc",
"loader/allowed_by_nosniff_test.cc", "loader/allowed_by_nosniff_test.cc",
"loader/cors/cors_test.cc", "loader/cors/cors_test.cc",
"mac/graphics_context_canvas_test.mm", "mac/graphics_context_canvas_test.mm",
......
/*
* Copyright (C) 2013 Google Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "base/test/scoped_feature_list.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/heap/heap_test_utilities.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/heap/thread_state.h"
#include "third_party/blink/renderer/platform/lifecycle_notifier.h"
#include "third_party/blink/renderer/platform/lifecycle_observer.h"
#include "third_party/blink/renderer/platform/testing/runtime_enabled_features_test_helpers.h"
namespace blink {
class TestingObserver;
class DummyContext final
: public GarbageCollected<DummyContext>,
public LifecycleNotifier<DummyContext, TestingObserver> {
USING_GARBAGE_COLLECTED_MIXIN(DummyContext);
public:
void Trace(blink::Visitor* visitor) override {
LifecycleNotifier<DummyContext, TestingObserver>::Trace(visitor);
}
// Make the protected method public for testing.
using LifecycleNotifier<DummyContext, TestingObserver>::ForEachObserver;
};
class TestingObserver final
: public GarbageCollected<TestingObserver>,
public LifecycleObserver<DummyContext, TestingObserver> {
USING_GARBAGE_COLLECTED_MIXIN(TestingObserver);
public:
explicit TestingObserver(DummyContext* context)
: LifecycleObserver(context), context_destroyed_called_(false) {}
void ContextDestroyed(DummyContext* destroyed_context) {
if (observer_to_remove_on_destruct_) {
destroyed_context->RemoveObserver(observer_to_remove_on_destruct_);
observer_to_remove_on_destruct_.Clear();
}
context_destroyed_called_ = true;
}
void Trace(blink::Visitor* visitor) override {
visitor->Trace(observer_to_remove_on_destruct_);
LifecycleObserver::Trace(visitor);
}
void Unobserve() { SetContext(nullptr); }
void SetObserverToRemoveAndDestroy(
TestingObserver* observer_to_remove_on_destruct) {
DCHECK(!observer_to_remove_on_destruct_);
observer_to_remove_on_destruct_ = observer_to_remove_on_destruct;
}
TestingObserver* InnerObserver() const {
return observer_to_remove_on_destruct_;
}
bool ContextDestroyedCalled() const { return context_destroyed_called_; }
private:
Member<TestingObserver> observer_to_remove_on_destruct_;
bool context_destroyed_called_;
};
TEST(LifecycleContextTest, ShouldObserveContextDestroyed) {
auto* context = MakeGarbageCollected<DummyContext>();
Persistent<TestingObserver> observer =
MakeGarbageCollected<TestingObserver>(context);
EXPECT_EQ(observer->LifecycleContext(), context);
EXPECT_FALSE(observer->ContextDestroyedCalled());
context->NotifyContextDestroyed();
context = nullptr;
ThreadState::Current()->CollectAllGarbageForTesting();
EXPECT_EQ(observer->LifecycleContext(), static_cast<DummyContext*>(nullptr));
EXPECT_TRUE(observer->ContextDestroyedCalled());
}
TEST(LifecycleContextTest, ShouldNotObserveContextDestroyedIfUnobserve) {
auto* context = MakeGarbageCollected<DummyContext>();
Persistent<TestingObserver> observer =
MakeGarbageCollected<TestingObserver>(context);
observer->Unobserve();
context->NotifyContextDestroyed();
context = nullptr;
ThreadState::Current()->CollectAllGarbageForTesting();
EXPECT_EQ(observer->LifecycleContext(), static_cast<DummyContext*>(nullptr));
EXPECT_FALSE(observer->ContextDestroyedCalled());
}
TEST(LifecycleContextTest, ObserverRemovedDuringNotifyDestroyed) {
auto* context = MakeGarbageCollected<DummyContext>();
Persistent<TestingObserver> observer =
MakeGarbageCollected<TestingObserver>(context);
auto* inner_observer = MakeGarbageCollected<TestingObserver>(context);
// Attach the observer to the other. When 'observer' is notified
// of destruction, it will remove & destroy 'innerObserver'.
observer->SetObserverToRemoveAndDestroy(inner_observer);
EXPECT_EQ(observer->LifecycleContext(), context);
EXPECT_EQ(observer->InnerObserver()->LifecycleContext(), context);
EXPECT_FALSE(observer->ContextDestroyedCalled());
EXPECT_FALSE(observer->InnerObserver()->ContextDestroyedCalled());
context->NotifyContextDestroyed();
EXPECT_EQ(observer->InnerObserver(), nullptr);
context = nullptr;
ThreadState::Current()->CollectAllGarbageForTesting();
EXPECT_EQ(observer->LifecycleContext(), static_cast<DummyContext*>(nullptr));
EXPECT_TRUE(observer->ContextDestroyedCalled());
}
// This is a regression test for http://crbug.com/854639.
TEST(LifecycleContextTest, ShouldNotHitCFICheckOnIncrementalMarking) {
base::test::ScopedFeatureList scoped_feature_list;
// Disable concurrent marking and concurrent sweeping as worker_pool task
// environment is not set.
scoped_feature_list.InitWithFeatures(
{blink::features::kBlinkHeapIncrementalMarking},
{blink::features::kBlinkHeapConcurrentMarking,
blink::features::kBlinkHeapConcurrentSweeping});
IncrementalMarkingTestDriver driver(ThreadState::Current());
driver.Start();
auto* context = MakeGarbageCollected<DummyContext>();
// This should not cause a CFI check failure.
Persistent<TestingObserver> observer =
MakeGarbageCollected<TestingObserver>(context);
EXPECT_FALSE(observer->ContextDestroyedCalled());
context->NotifyContextDestroyed();
EXPECT_TRUE(observer->ContextDestroyedCalled());
context = nullptr;
driver.FinishGC();
}
TEST(LifecycleContextTest, ForEachObserver) {
Persistent<DummyContext> context = MakeGarbageCollected<DummyContext>();
Persistent<TestingObserver> observer =
MakeGarbageCollected<TestingObserver>(context);
HeapVector<Member<TestingObserver>> seen_observers;
context->ForEachObserver(
[&](TestingObserver* observer) { seen_observers.push_back(observer); });
ASSERT_EQ(1u, seen_observers.size());
EXPECT_EQ(observer.Get(), seen_observers[0].Get());
seen_observers.clear();
observer.Clear();
ThreadState::Current()->CollectAllGarbageForTesting();
context->ForEachObserver(
[&](TestingObserver* observer) { seen_observers.push_back(observer); });
ASSERT_EQ(0u, seen_observers.size());
}
} // namespace blink
/*
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
* Copyright (C) 2013 Google Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_LIFECYCLE_NOTIFIER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LIFECYCLE_NOTIFIER_H_
#include "base/auto_reset.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/heap/heap_allocator.h"
namespace blink {
class LifecycleObserverBase;
template <typename T, typename Observer>
class LifecycleNotifier : public GarbageCollectedMixin {
public:
virtual ~LifecycleNotifier();
void AddObserver(LifecycleObserverBase*);
void RemoveObserver(LifecycleObserverBase*);
// NotifyContextDestroyed() should be explicitly dispatched from an
// observed context to detach its observers and, if the observer kind
// requires it, notify each observer by invoking ContextDestroyed().
//
// When ContextDestroyed() is called, it is supplied the context as
// an argument, but the observer's LifecycleContext() is still valid
// and safe to use while handling the notification.
virtual void NotifyContextDestroyed();
void Trace(blink::Visitor* visitor) override { visitor->Trace(observers_); }
bool IsIteratingOverObservers() const {
return iteration_state_ != kNotIterating;
}
protected:
LifecycleNotifier() : iteration_state_(kNotIterating) {}
T* Context() { return static_cast<T*>(this); }
// Safely iterate over the registered lifecycle observers.
//
// Adding or removing observers is not allowed during iteration. The callable
// will only be called synchronously inside ForEachObserver().
//
// Sample usage:
// ForEachObserver([](ObserverType* observer) {
// observer->SomeMethod();
// });
template <typename ForEachCallable>
void ForEachObserver(const ForEachCallable& callable) const {
base::AutoReset<IterationState> scope(&iteration_state_, kAllowingNone);
for (LifecycleObserverBase* observer_base : observers_) {
Observer* observer = static_cast<Observer*>(observer_base);
callable(observer);
}
}
private:
using ObserverSet = HeapLinkedHashSet<WeakMember<LifecycleObserverBase>>;
enum IterationState {
kAllowingNone = 0,
kAllowingAddition = 1,
kAllowingRemoval = 2,
kNotIterating = kAllowingAddition | kAllowingRemoval,
};
// Iteration state is recorded while iterating the observer set,
// optionally barring add or remove mutations.
mutable IterationState iteration_state_;
ObserverSet observers_;
};
template <typename T, typename Observer>
inline LifecycleNotifier<T, Observer>::~LifecycleNotifier() {
// FIXME: Enable the following ASSERT. Also see a FIXME in
// Document::detachLayoutTree().
// DCHECK(!m_observers.size());
}
// Determine if |contextDestroyed(Observer*) is a public method on
// class type |Observer|, or any of the class types it derives from.
template <typename Observer, typename T>
class HasContextDestroyed {
using YesType = char;
using NoType = int;
template <typename V>
static YesType CheckHasContextDestroyedMethod(
V* observer,
T* context = nullptr,
typename std::enable_if<
std::is_same<decltype(observer->ContextDestroyed(context)),
void>::value>::type* g = nullptr);
template <typename V>
static NoType CheckHasContextDestroyedMethod(...);
public:
static_assert(sizeof(Observer), "Observer's class declaration not in scope");
static const bool value =
sizeof(YesType) ==
sizeof(CheckHasContextDestroyedMethod<Observer>(nullptr));
};
// If |Observer::contextDestroyed()| is present, invoke it.
template <typename Observer,
typename T,
bool = HasContextDestroyed<Observer, T>::value>
class ContextDestroyedNotifier {
STATIC_ONLY(ContextDestroyedNotifier);
public:
static void Call(Observer* observer, T* context) {
observer->ContextDestroyed(context);
}
};
template <typename Observer, typename T>
class ContextDestroyedNotifier<Observer, T, false> {
STATIC_ONLY(ContextDestroyedNotifier);
public:
static void Call(Observer*, T*) {}
};
template <typename T, typename Observer>
inline void LifecycleNotifier<T, Observer>::NotifyContextDestroyed() {
// Observer unregistration is allowed, but effectively a no-op.
base::AutoReset<IterationState> scope(&iteration_state_, kAllowingRemoval);
ObserverSet observers;
observers_.Swap(observers);
for (LifecycleObserverBase* observer_base : observers) {
Observer* observer = static_cast<Observer*>(observer_base);
DCHECK(observer->LifecycleContext() == Context());
ContextDestroyedNotifier<Observer, T>::Call(observer, Context());
observer->ClearContext();
}
// Explicitly free the backing store to avoid memory regressions.
// TODO(bikineev): Revisit after young generation is done.
observers.clear();
}
template <typename T, typename Observer>
inline void LifecycleNotifier<T, Observer>::AddObserver(
LifecycleObserverBase* observer) {
CHECK(iteration_state_ & kAllowingAddition);
observers_.insert(observer);
}
template <typename T, typename Observer>
inline void LifecycleNotifier<T, Observer>::RemoveObserver(
LifecycleObserverBase* observer) {
CHECK(iteration_state_ & kAllowingRemoval);
observers_.erase(observer);
}
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LIFECYCLE_NOTIFIER_H_
/*
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_LIFECYCLE_OBSERVER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LIFECYCLE_OBSERVER_H_
#include "third_party/blink/renderer/platform/heap/handle.h"
namespace blink {
template <typename Context, typename Observer>
class LifecycleNotifier;
class LifecycleObserverBase : public GarbageCollectedMixin {};
template <typename Context, typename Observer>
class LifecycleObserver : public LifecycleObserverBase {
public:
void Trace(blink::Visitor* visitor) override {
visitor->Trace(lifecycle_context_);
}
Context* LifecycleContext() const { return lifecycle_context_; }
void ClearContext() { SetContext(nullptr); }
protected:
explicit LifecycleObserver(Context* context) : lifecycle_context_(nullptr) {
SetContext(context);
}
void SetContext(Context*);
private:
WeakMember<Context> lifecycle_context_;
};
template <typename Context, typename Observer>
inline void LifecycleObserver<Context, Observer>::SetContext(Context* context) {
using Notifier = LifecycleNotifier<Context, Observer>;
if (lifecycle_context_ == context)
return;
if (lifecycle_context_) {
static_cast<Notifier*>(lifecycle_context_)->RemoveObserver(this);
}
lifecycle_context_ = context;
if (lifecycle_context_) {
static_cast<Notifier*>(lifecycle_context_)->AddObserver(this);
}
}
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LIFECYCLE_OBSERVER_H_
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