[MBI] Bind AgentSchedulingGroup(Host) mojo interfaces.

The interfaces are still empty and don't do anything.

Bug: 1111231
Change-Id: I778d365eaaaf53cf8e9855bc131f1cebc69d4f11
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2374186
Reviewed-by: Kinuko Yasuda <[email protected]>
Reviewed-by: Kouhei Ueno <[email protected]>
Reviewed-by: Dominic Farolino <[email protected]>
Commit-Queue: Tal Pressman <[email protected]>
Cr-Commit-Position: refs/heads/master@{#803324}
diff --git a/content/renderer/agent_scheduling_group.cc b/content/renderer/agent_scheduling_group.cc
index 24a95459..47dd82d 100644
--- a/content/renderer/agent_scheduling_group.cc
+++ b/content/renderer/agent_scheduling_group.cc
@@ -6,7 +6,19 @@
 
 namespace content {
 
-AgentSchedulingGroup::AgentSchedulingGroup() = default;
+AgentSchedulingGroup::AgentSchedulingGroup(
+    mojo::PendingRemote<mojom::AgentSchedulingGroupHost> host_remote,
+    mojo::PendingReceiver<mojom::AgentSchedulingGroup> receiver,
+    base::OnceCallback<void(const AgentSchedulingGroup*)>
+        mojo_disconnect_handler)
+    // TODO(crbug.com/1111231): Mojo interfaces should be associated with
+    // per-ASG task runners instead of default.
+    : receiver_(this, std::move(receiver)),
+      host_remote_(std::move(host_remote)) {
+  receiver_.set_disconnect_handler(
+      base::BindOnce(std::move(mojo_disconnect_handler), this));
+}
+
 AgentSchedulingGroup::~AgentSchedulingGroup() = default;
 
 }  // namespace content
diff --git a/content/renderer/agent_scheduling_group.h b/content/renderer/agent_scheduling_group.h
index de64c9f..a0d9d73 100644
--- a/content/renderer/agent_scheduling_group.h
+++ b/content/renderer/agent_scheduling_group.h
@@ -5,10 +5,10 @@
 #ifndef CONTENT_RENDERER_AGENT_SCHEDULING_GROUP_H_
 #define CONTENT_RENDERER_AGENT_SCHEDULING_GROUP_H_
 
-#include <memory>
-
+#include "base/callback.h"
 #include "content/common/agent_scheduling_group.mojom.h"
 #include "content/common/content_export.h"
+#include "mojo/public/cpp/bindings/receiver.h"
 #include "mojo/public/cpp/bindings/remote.h"
 
 namespace content {
@@ -18,10 +18,16 @@
 // Blink's unit of scheduling and performance isolation, which is the only way
 // to obtain ordering guarantees between different Mojo (associated) interfaces
 // and legacy IPC messages.
-class CONTENT_EXPORT AgentSchedulingGroup {
+class CONTENT_EXPORT AgentSchedulingGroup : public mojom::AgentSchedulingGroup {
  public:
-  explicit AgentSchedulingGroup();
-  ~AgentSchedulingGroup();
+  // |mojo_disconnect_handler| will be called with |this| when |receiver| is
+  // disconnected.
+  AgentSchedulingGroup(
+      mojo::PendingRemote<mojom::AgentSchedulingGroupHost> host_remote,
+      mojo::PendingReceiver<mojom::AgentSchedulingGroup> receiver,
+      base::OnceCallback<void(const AgentSchedulingGroup*)>
+          mojo_disconnect_handler);
+  ~AgentSchedulingGroup() override;
 
   AgentSchedulingGroup(const AgentSchedulingGroup&) = delete;
   AgentSchedulingGroup(const AgentSchedulingGroup&&) = delete;
@@ -29,14 +35,13 @@
   AgentSchedulingGroup& operator=(const AgentSchedulingGroup&&) = delete;
 
  private:
-  // Internal implementation of content::mojom::AgentSchedulingGroup, used for
-  // responding to calls from the (browser-side) AgentSchedulingGroupHost.
-  std::unique_ptr<content::mojom::AgentSchedulingGroup> mojo_impl_;
+  // Implementation of `mojom::AgentSchedulingGroup`, used for responding to
+  // calls from the (browser-side) `AgentSchedulingGroupHost`.
+  mojo::Receiver<mojom::AgentSchedulingGroup> receiver_;
 
-  // Remote stub of content::mojom::AgentSchedulingGroupHost, used for sending
-  // calls to the (browser-side) AgentSchedulingGroupHost.
-  std::unique_ptr<mojo::Remote<content::mojom::AgentSchedulingGroupHost>>
-      mojo_remote_;
+  // Remote stub of mojom::AgentSchedulingGroupHost, used for sending calls to
+  // the (browser-side) AgentSchedulingGroupHost.
+  mojo::Remote<mojom::AgentSchedulingGroupHost> host_remote_;
 };
 
 }  // namespace content
diff --git a/content/renderer/render_thread_impl.cc b/content/renderer/render_thread_impl.cc
index afebe5d..164c1cf 100644
--- a/content/renderer/render_thread_impl.cc
+++ b/content/renderer/render_thread_impl.cc
@@ -579,6 +579,10 @@
 void RenderThreadImpl::Init() {
   TRACE_EVENT0("startup", "RenderThreadImpl::Init");
 
+  remove_agent_scheduling_group_callback_ =
+      base::BindRepeating(&RenderThreadImpl::RemoveAgentSchedulingGroup,
+                          weak_factory_.GetWeakPtr());
+
   GetContentClient()->renderer()->PostIOThreadCreated(GetIOTaskRunner().get());
 
   base::trace_event::TraceLog::GetInstance()->SetThreadSortIndex(
@@ -1859,8 +1863,21 @@
       params->has_committed_real_load);
 }
 
-void RenderThreadImpl::CreateAgentSchedulingGroup() {
-  agent_scheduling_groups_.emplace(std::make_unique<AgentSchedulingGroup>());
+void RenderThreadImpl::RemoveAgentSchedulingGroup(
+    const AgentSchedulingGroup* agent_scheduling_group) {
+  DCHECK(agent_scheduling_group);
+  DCHECK(base::Contains(agent_scheduling_groups_, agent_scheduling_group));
+  auto it = agent_scheduling_groups_.find(agent_scheduling_group);
+  agent_scheduling_groups_.erase(it);
+}
+
+void RenderThreadImpl::CreateAgentSchedulingGroup(
+    mojo::PendingRemote<mojom::AgentSchedulingGroupHost>
+        agent_scheduling_group_host,
+    mojo::PendingReceiver<mojom::AgentSchedulingGroup> agent_scheduling_group) {
+  agent_scheduling_groups_.emplace(std::make_unique<AgentSchedulingGroup>(
+      std::move(agent_scheduling_group_host), std::move(agent_scheduling_group),
+      remove_agent_scheduling_group_callback_));
 }
 
 void RenderThreadImpl::CreateFrameProxy(
diff --git a/content/renderer/render_thread_impl.h b/content/renderer/render_thread_impl.h
index bb128ce..f7671088 100644
--- a/content/renderer/render_thread_impl.h
+++ b/content/renderer/render_thread_impl.h
@@ -17,6 +17,7 @@
 
 #include "base/cancelable_callback.h"
 #include "base/clang_profiling_buildflags.h"
+#include "base/containers/unique_ptr_adapters.h"
 #include "base/macros.h"
 #include "base/memory/discardable_memory_allocator.h"
 #include "base/memory/memory_pressure_listener.h"
@@ -28,6 +29,7 @@
 #include "base/time/time.h"
 #include "build/build_config.h"
 #include "content/child/child_thread_impl.h"
+#include "content/common/agent_scheduling_group.mojom.h"
 #include "content/common/content_export.h"
 #include "content/common/frame.mojom.h"
 #include "content/common/frame_replication_state.h"
@@ -443,7 +445,11 @@
   void CreateView(mojom::CreateViewParamsPtr params) override;
   void DestroyView(int32_t view_id) override;
   void CreateFrame(mojom::CreateFrameParamsPtr params) override;
-  void CreateAgentSchedulingGroup() override;
+  void CreateAgentSchedulingGroup(
+      mojo::PendingRemote<mojom::AgentSchedulingGroupHost>
+          agent_scheduling_group_host,
+      mojo::PendingReceiver<mojom::AgentSchedulingGroup> agent_scheduling_group)
+      override;
   void CreateFrameProxy(
       int32_t routing_id,
       int32_t render_view_routing_id,
@@ -508,6 +514,11 @@
   void OnRendererInterfaceReceiver(
       mojo::PendingAssociatedReceiver<mojom::Renderer> receiver);
 
+  void RemoveAgentSchedulingGroup(
+      const AgentSchedulingGroup* agent_scheduling_group);
+  base::RepeatingCallback<void(const AgentSchedulingGroup*)>
+      remove_agent_scheduling_group_callback_;
+
   std::unique_ptr<base::DiscardableMemoryAllocator>
       discardable_memory_allocator_;
 
@@ -616,7 +627,8 @@
 
   mojo::AssociatedRemote<mojom::RenderMessageFilter> render_message_filter_;
 
-  std::set<std::unique_ptr<AgentSchedulingGroup>> agent_scheduling_groups_;
+  std::set<std::unique_ptr<AgentSchedulingGroup>, base::UniquePtrComparator>
+      agent_scheduling_groups_;
 
   RendererMemoryMetrics purge_and_suspend_memory_metrics_;
   bool needs_to_record_first_active_paint_;