Commit 067e8cae authored by Tal Pressman's avatar Tal Pressman Committed by Commit Bot

Add missing methods on HeapMojoReceiverSet.

Also in this CL:
1. Pass the task runner through to the wrapped ReceiverSet.
2. Move helper classes in HeapMojoUniqueReceiverSetTest to anonymous namespace.

Bug: 1049056
Change-Id: Ib0b90a673a77d04c340c49145e6451ab799bc0e1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2090565
Commit-Queue: Tal Pressman <talp@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#749476}
parent 50af1016
...@@ -1871,6 +1871,7 @@ jumbo_source_set("blink_platform_unittests_sources") { ...@@ -1871,6 +1871,7 @@ jumbo_source_set("blink_platform_unittests_sources") {
"mediastream/webrtc_uma_histograms_test.cc", "mediastream/webrtc_uma_histograms_test.cc",
"mhtml/mhtml_parser_test.cc", "mhtml/mhtml_parser_test.cc",
"mojo/big_string_mojom_traits_test.cc", "mojo/big_string_mojom_traits_test.cc",
"mojo/heap_mojo_receiver_set_test.cc",
"mojo/heap_mojo_receiver_test.cc", "mojo/heap_mojo_receiver_test.cc",
"mojo/heap_mojo_remote_test.cc", "mojo/heap_mojo_remote_test.cc",
"mojo/heap_mojo_unique_receiver_set_test.cc", "mojo/heap_mojo_unique_receiver_set_test.cc",
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_MOJO_HEAP_MOJO_RECEIVER_SET_H_ #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_MOJO_HEAP_MOJO_RECEIVER_SET_H_
#include "mojo/public/cpp/bindings/receiver.h" #include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "third_party/blink/renderer/platform/context_lifecycle_observer.h" #include "third_party/blink/renderer/platform/context_lifecycle_observer.h"
#include "third_party/blink/renderer/platform/heap/heap.h" #include "third_party/blink/renderer/platform/heap/heap.h"
...@@ -25,17 +26,29 @@ class HeapMojoReceiverSet { ...@@ -25,17 +26,29 @@ class HeapMojoReceiverSet {
using ImplPointerType = typename mojo::Receiver<Interface>::ImplPointerType; using ImplPointerType = typename mojo::Receiver<Interface>::ImplPointerType;
explicit HeapMojoReceiverSet(ContextLifecycleNotifier* context) explicit HeapMojoReceiverSet(ContextLifecycleNotifier* context)
: wrapper_(MakeGarbageCollected<Wrapper>(context)) {} : wrapper_(MakeGarbageCollected<Wrapper>(context)) {
DCHECK(context);
}
// Methods to redirect to mojo::ReceiverSet: // Methods to redirect to mojo::ReceiverSet:
mojo::ReceiverId Add(ImplPointerType impl, mojo::ReceiverId Add(ImplPointerType impl,
mojo::PendingReceiver<Interface> receiver, mojo::PendingReceiver<Interface> receiver,
scoped_refptr<base::SequencedTaskRunner> task_runner) { scoped_refptr<base::SequencedTaskRunner> task_runner) {
DCHECK(task_runner); DCHECK(task_runner);
return wrapper_->receiver_set().Add(std::move(impl), std::move(receiver)); return wrapper_->receiver_set().Add(std::move(impl), std::move(receiver),
task_runner);
}
bool Remove(mojo::ReceiverId id) {
return wrapper_->receiver_set().Remove(id);
} }
void Clear() { wrapper_->receiver_set().Clear(); } void Clear() { wrapper_->receiver_set().Clear(); }
bool HasReceiver(mojo::ReceiverId id) {
return wrapper_->receiver_set().HasReceiver(id);
}
void Trace(Visitor* visitor) { visitor->Trace(wrapper_); } void Trace(Visitor* visitor) { visitor->Trace(wrapper_); }
private: private:
...@@ -62,7 +75,7 @@ class HeapMojoReceiverSet { ...@@ -62,7 +75,7 @@ class HeapMojoReceiverSet {
void ContextDestroyed() override { receiver_set_.Clear(); } void ContextDestroyed() override { receiver_set_.Clear(); }
private: private:
mojo::ReceiverSet<Interface> receiver_set_; ::mojo::ReceiverSet<Interface> receiver_set_;
}; };
Member<Wrapper> wrapper_; Member<Wrapper> wrapper_;
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver_set.h"
#include "base/test/null_task_runner.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/interfaces/bindings/tests/sample_service.mojom-blink.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/context_lifecycle_notifier.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_observer_list.h"
namespace blink {
namespace {
class FakeContextNotifier final : public GarbageCollected<FakeContextNotifier>,
public ContextLifecycleNotifier {
USING_GARBAGE_COLLECTED_MIXIN(FakeContextNotifier);
public:
FakeContextNotifier() = default;
void AddContextLifecycleObserver(
ContextLifecycleObserver* observer) override {
observers_.AddObserver(observer);
}
void RemoveContextLifecycleObserver(
ContextLifecycleObserver* observer) override {
observers_.RemoveObserver(observer);
}
void NotifyContextDestroyed() {
observers_.ForEachObserver([](ContextLifecycleObserver* observer) {
observer->ContextDestroyed();
});
}
void Trace(Visitor* visitor) override {
visitor->Trace(observers_);
ContextLifecycleNotifier::Trace(visitor);
}
private:
HeapObserverList<ContextLifecycleObserver> observers_;
};
class MockService : public sample::blink::Service {
public:
MockService() = default;
void Frobinate(sample::blink::FooPtr foo,
Service::BazOptions baz,
mojo::PendingRemote<sample::blink::Port> port,
FrobinateCallback callback) override {}
void GetPort(mojo::PendingReceiver<sample::blink::Port> receiver) override {}
};
class GCOwner : public GarbageCollected<GCOwner> {
public:
explicit GCOwner(FakeContextNotifier* context) : receiver_set_(context) {}
void Trace(Visitor* visitor) { visitor->Trace(receiver_set_); }
HeapMojoReceiverSet<sample::blink::Service>& receiver_set() {
return receiver_set_;
}
private:
HeapMojoReceiverSet<sample::blink::Service> receiver_set_;
};
} // namespace
class HeapMojoReceiverSetTest : public TestSupportingGC {
public:
FakeContextNotifier* context() { return context_; }
scoped_refptr<base::NullTaskRunner> task_runner() {
return null_task_runner_;
}
GCOwner* owner() { return owner_; }
void ClearOwner() { owner_ = nullptr; }
protected:
void SetUp() override {
context_ = MakeGarbageCollected<FakeContextNotifier>();
owner_ = MakeGarbageCollected<GCOwner>(context());
}
void TearDown() override {}
Persistent<FakeContextNotifier> context_;
Persistent<GCOwner> owner_;
scoped_refptr<base::NullTaskRunner> null_task_runner_ =
base::MakeRefCounted<base::NullTaskRunner>();
};
// GC the HeapMojoReceiverSet and verify that the receiver is no longer
// part of the set, and that the service was deleted.
TEST_F(HeapMojoReceiverSetTest, RemovesReceiver) {
auto receiver_set = owner()->receiver_set();
MockService service;
auto receiver = mojo::PendingReceiver<sample::blink::Service>(
mojo::MessagePipe().handle0);
mojo::ReceiverId rid =
receiver_set.Add(&service, std::move(receiver), task_runner());
EXPECT_TRUE(receiver_set.HasReceiver(rid));
receiver_set.Remove(rid);
EXPECT_FALSE(receiver_set.HasReceiver(rid));
}
TEST_F(HeapMojoReceiverSetTest, ClearLeavesSetEmpty) {
auto receiver_set = owner()->receiver_set();
MockService service;
auto receiver = mojo::PendingReceiver<sample::blink::Service>(
mojo::MessagePipe().handle0);
mojo::ReceiverId rid =
receiver_set.Add(&service, std::move(receiver), task_runner());
EXPECT_TRUE(receiver_set.HasReceiver(rid));
receiver_set.Clear();
EXPECT_FALSE(receiver_set.HasReceiver(rid));
}
} // namespace blink
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
namespace blink { namespace blink {
namespace {
class FakeContextNotifier final : public GarbageCollected<FakeContextNotifier>, class FakeContextNotifier final : public GarbageCollected<FakeContextNotifier>,
public ContextLifecycleNotifier { public ContextLifecycleNotifier {
USING_GARBAGE_COLLECTED_MIXIN(FakeContextNotifier); USING_GARBAGE_COLLECTED_MIXIN(FakeContextNotifier);
...@@ -58,6 +59,7 @@ class GCOwner : public GarbageCollected<GCOwner> { ...@@ -58,6 +59,7 @@ class GCOwner : public GarbageCollected<GCOwner> {
HeapMojoUniqueReceiverSet<sample::blink::Service> receiver_set_; HeapMojoUniqueReceiverSet<sample::blink::Service> receiver_set_;
}; };
} // namespace
class HeapMojoUniqueReceiverSetTest : public TestSupportingGC { class HeapMojoUniqueReceiverSetTest : public TestSupportingGC {
public: public:
FakeContextNotifier* context() { return context_; } FakeContextNotifier* context() { return context_; }
...@@ -84,6 +86,8 @@ class HeapMojoUniqueReceiverSetTest : public TestSupportingGC { ...@@ -84,6 +86,8 @@ class HeapMojoUniqueReceiverSetTest : public TestSupportingGC {
bool service_deleted_ = false; bool service_deleted_ = false;
}; };
namespace {
class MockService : public sample::blink::Service { class MockService : public sample::blink::Service {
public: public:
explicit MockService(HeapMojoUniqueReceiverSetTest* test) : test_(test) {} explicit MockService(HeapMojoUniqueReceiverSetTest* test) : test_(test) {}
...@@ -100,6 +104,8 @@ class MockService : public sample::blink::Service { ...@@ -100,6 +104,8 @@ class MockService : public sample::blink::Service {
HeapMojoUniqueReceiverSetTest* test_; HeapMojoUniqueReceiverSetTest* test_;
}; };
} // namespace
// GC the HeapMojoUniqueReceiverSet and verify that the receiver is no longer // GC the HeapMojoUniqueReceiverSet and verify that the receiver is no longer
// part of the set, and that the service was deleted. // part of the set, and that the service was deleted.
TEST_F(HeapMojoUniqueReceiverSetTest, ResetsOnGC) { TEST_F(HeapMojoUniqueReceiverSetTest, ResetsOnGC) {
......
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