Skip to content

Commit

Permalink
Introduce HeapMojoReceiverSet and use it for ManifestManager
Browse files Browse the repository at this point in the history
Introduces HeapMojoReceiverSet which is a wrapper around mojo::ReceiverSet.
And uses it for ManifestManager.

Bug: 1052319
Change-Id: I683567f0352c9b60811263a662bda85e31299426
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2063615
Commit-Queue: Keishi Hattori <keishi@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743049}
  • Loading branch information
Keishi Hattori authored and Commit Bot committed Feb 20, 2020
1 parent e0723ae commit 9b056af
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
15 changes: 7 additions & 8 deletions third_party/blink/renderer/modules/manifest/manifest_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ ManifestManager::ManifestManager(LocalFrame& frame)
: Supplement<LocalFrame>(frame),
ExecutionContextLifecycleObserver(frame.GetDocument()),
may_have_manifest_(false),
manifest_dirty_(true) {
manifest_dirty_(true),
receivers_(GetExecutionContext()) {
if (frame.IsMainFrame()) {
manifest_change_notifier_ =
MakeGarbageCollected<ManifestChangeNotifier>(frame);
Expand Down Expand Up @@ -256,7 +257,10 @@ bool ManifestManager::ManifestUseCredentials() const {

void ManifestManager::BindReceiver(
mojo::PendingReceiver<mojom::blink::ManifestManager> receiver) {
receivers_.Add(this, std::move(receiver));
receivers_.Add(
this, std::move(receiver),
GetSupplementable()->GetDocument()->ToExecutionContext()->GetTaskRunner(
TaskType::kNetworking));
}

void ManifestManager::ContextDestroyed() {
Expand All @@ -267,17 +271,12 @@ void ManifestManager::ContextDestroyed() {
// will be aware of the RenderFrame dying and should act on that. Consumers
// in the renderer process should be correctly notified.
ResolveCallbacks(ResolveStateFailure);

receivers_.Clear();
}

void ManifestManager::Prefinalize() {
receivers_.Clear();
}

void ManifestManager::Trace(Visitor* visitor) {
visitor->Trace(fetcher_);
visitor->Trace(manifest_change_notifier_);
visitor->Trace(receivers_);
Supplement<LocalFrame>::Trace(visitor);
ExecutionContextLifecycleObserver::Trace(visitor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver_set.h"
#include "third_party/blink/renderer/platform/supplementable.h"

namespace blink {
Expand All @@ -36,7 +37,6 @@ class MODULES_EXPORT ManifestManager
public mojom::blink::ManifestManager,
public ExecutionContextLifecycleObserver {
USING_GARBAGE_COLLECTED_MIXIN(ManifestManager);
USING_PRE_FINALIZER(ManifestManager, Prefinalize);

public:
static const char kSupplementName[];
Expand Down Expand Up @@ -86,8 +86,6 @@ class MODULES_EXPORT ManifestManager
void BindReceiver(
mojo::PendingReceiver<mojom::blink::ManifestManager> receiver);

void Prefinalize();

friend class ManifestManagerTest;

Member<ManifestFetcher> fetcher_;
Expand All @@ -113,7 +111,7 @@ class MODULES_EXPORT ManifestManager

Vector<InternalRequestManifestCallback> pending_callbacks_;

mojo::ReceiverSet<mojom::blink::ManifestManager> receivers_;
HeapMojoReceiverSet<mojom::blink::ManifestManager> receivers_;

DISALLOW_COPY_AND_ASSIGN(ManifestManager);
};
Expand Down
2 changes: 2 additions & 0 deletions third_party/blink/renderer/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,8 @@ jumbo_component("platform") {
"mojo/fetch_api_request_headers_mojom_traits.h",
"mojo/heap_mojo_receiver.h",
"mojo/heap_mojo_receiver.h",
"mojo/heap_mojo_receiver_set.h",
"mojo/heap_mojo_receiver_set.h",
"mojo/heap_mojo_remote.h",
"mojo/heap_mojo_remote.h",
"mojo/kurl_mojom_traits.h",
Expand Down
72 changes: 72 additions & 0 deletions third_party/blink/renderer/platform/mojo/heap_mojo_receiver_set.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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.

#ifndef 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 "third_party/blink/renderer/platform/context_lifecycle_observer.h"
#include "third_party/blink/renderer/platform/heap/heap.h"

namespace blink {

// HeapMojoReceiverSet is a wrapper for mojo::ReceiverSet to be owned by a
// garbage-collected object. Blink is expected to use HeapMojoReceiverSet by
// default. HeapMojoReceiverSet must be associated with context.
// HeapMojoReceiverSet's constructor takes context as a mandatory parameter.
// HeapMojoReceiverSet resets the mojo connection when 1) the owner object is
// garbage-collected or 2) the associated ExecutionContext is detached.
template <typename Interface>
class HeapMojoReceiverSet {
DISALLOW_NEW();

public:
using ImplPointerType = typename mojo::Receiver<Interface>::ImplPointerType;

explicit HeapMojoReceiverSet(ContextLifecycleNotifier* context)
: wrapper_(MakeGarbageCollected<Wrapper>(context)) {}

// Methods to redirect to mojo::ReceiverSet:
mojo::ReceiverId Add(ImplPointerType impl,
mojo::PendingReceiver<Interface> receiver,
scoped_refptr<base::SequencedTaskRunner> task_runner) {
return wrapper_->receiver_set().Add(std::move(impl), std::move(receiver));
}
void Clear() { wrapper_->receiver_set().Clear(); }

void Trace(Visitor* visitor) { visitor->Trace(wrapper_); }

private:
// Garbage collected wrapper class to add a prefinalizer.
class Wrapper final : public GarbageCollected<Wrapper>,
public ContextLifecycleObserver {
USING_PRE_FINALIZER(Wrapper, Dispose);
USING_GARBAGE_COLLECTED_MIXIN(Wrapper);

public:
explicit Wrapper(ContextLifecycleNotifier* notifier) {
SetContextLifecycleNotifier(notifier);
}

void Trace(Visitor* visitor) override {
ContextLifecycleObserver::Trace(visitor);
}

void Dispose() { receiver_set_.Clear(); }

mojo::ReceiverSet<Interface>& receiver_set() { return receiver_set_; }

// ContextLifecycleObserver methods
void ContextDestroyed() override { receiver_set_.Clear(); }

private:
mojo::ReceiverSet<Interface> receiver_set_;
};

Member<Wrapper> wrapper_;
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_MOJO_HEAP_MOJO_RECEIVER_SET_H_

0 comments on commit 9b056af

Please sign in to comment.