Migrate scenic_gpu_*.mojom to new Mojo types

Highlights:
* There are some other changes to the code logic, derived from the
usage of PendingRemote in function arguments instead of InterfacePtr.
* Shorthands provided by the new Mojo types allowed to simplify some
implementations.
* Moved initialization of affected members to their declarations,
where possible, to comply with coding guidelines.

Bug: 955171
Change-Id: Iebdc315dfcfebc671e0c91c9cf4f9fdd9fdf7b24
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1847309
Reviewed-by: Mario Sanchez Prada <mario@igalia.com>
Reviewed-by: Michael Spang <spang@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Oksana Zhuravlova <oksamyt@chromium.org>
Commit-Queue: Jacobo Aragunde Pérez <jaragunde@igalia.com>
Cr-Commit-Position: refs/heads/master@{#710313}
diff --git a/ui/ozone/platform/scenic/ozone_platform_scenic.cc b/ui/ozone/platform/scenic/ozone_platform_scenic.cc
index 6240b25..a74c904 100644
--- a/ui/ozone/platform/scenic/ozone_platform_scenic.cc
+++ b/ui/ozone/platform/scenic/ozone_platform_scenic.cc
@@ -135,10 +135,11 @@
     base::MessageLoopCurrent::Get()->AddDestructionObserver(this);
 
     scenic_gpu_host_ = std::make_unique<ScenicGpuHost>(window_manager_.get());
-    scenic_gpu_host_ptr_ = scenic_gpu_host_->CreateHostProcessSelfBinding();
+    scenic_gpu_host_remote_.Bind(
+        scenic_gpu_host_->CreateHostProcessSelfRemote());
 
     surface_factory_ =
-        std::make_unique<ScenicSurfaceFactory>(scenic_gpu_host_ptr_.get());
+        std::make_unique<ScenicSurfaceFactory>(scenic_gpu_host_remote_.get());
   }
 
   void InitializeGPU(const InitParams& params) override {
@@ -153,9 +154,9 @@
     } else {
       DCHECK(!surface_factory_);
       scenic_gpu_service_ = std::make_unique<ScenicGpuService>(
-          mojo::MakeRequest(&scenic_gpu_host_ptr_));
+          scenic_gpu_host_remote_.BindNewPipeAndPassReceiver());
       surface_factory_ =
-          std::make_unique<ScenicSurfaceFactory>(scenic_gpu_host_ptr_.get());
+          std::make_unique<ScenicSurfaceFactory>(scenic_gpu_host_remote_.get());
     }
   }
 
@@ -175,7 +176,7 @@
   void WillDestroyCurrentMessageLoop() override {
     // We must ensure to destroy any resources which rely on the MessageLoop's
     // async_dispatcher.
-    scenic_gpu_host_ptr_ = nullptr;
+    scenic_gpu_host_remote_.reset();
     surface_factory_ = nullptr;
     scenic_gpu_host_ = nullptr;
     overlay_manager_ = nullptr;
@@ -195,7 +196,7 @@
   std::unique_ptr<ScenicGpuService> scenic_gpu_service_;
   std::unique_ptr<ScenicSurfaceFactory> surface_factory_;
 
-  mojom::ScenicGpuHostPtr scenic_gpu_host_ptr_;
+  mojo::Remote<mojom::ScenicGpuHost> scenic_gpu_host_remote_;
 
   DISALLOW_COPY_AND_ASSIGN(OzonePlatformScenic);
 };
diff --git a/ui/ozone/platform/scenic/scenic_gpu_host.cc b/ui/ozone/platform/scenic/scenic_gpu_host.cc
index 9e0da5c..9b6200a 100644
--- a/ui/ozone/platform/scenic/scenic_gpu_host.cc
+++ b/ui/ozone/platform/scenic/scenic_gpu_host.cc
@@ -26,9 +26,9 @@
                                  mojo::ScopedMessagePipeHandle)>;
 
 template <typename Interface>
-void BindInterface(mojo::InterfaceRequest<Interface> request,
+void BindInterface(mojo::PendingReceiver<Interface> receiver,
                    const BinderCallback& binder_callback) {
-  binder_callback.Run(Interface::Name_, request.PassMessagePipe());
+  binder_callback.Run(Interface::Name_, receiver.PassPipe());
 }
 
 }  // namespace
@@ -37,10 +37,7 @@
 
 ScenicGpuHost::ScenicGpuHost(ScenicWindowManager* scenic_window_manager)
     : scenic_window_manager_(scenic_window_manager),
-      host_binding_(this),
-      gpu_binding_(this),
-      ui_thread_runner_(base::ThreadTaskRunnerHandle::Get()),
-      weak_ptr_factory_(this) {
+      ui_thread_runner_(base::ThreadTaskRunnerHandle::Get()) {
   DETACH_FROM_THREAD(io_thread_checker_);
 }
 
@@ -48,11 +45,10 @@
   DCHECK_CALLED_ON_VALID_THREAD(ui_thread_checker_);
 }
 
-mojom::ScenicGpuHostPtr ScenicGpuHost::CreateHostProcessSelfBinding() {
-  DCHECK(!host_binding_.is_bound());
-  mojom::ScenicGpuHostPtr gpu_host;
-  host_binding_.Bind(mojo::MakeRequest(&gpu_host));
-  return gpu_host;
+mojo::PendingRemote<mojom::ScenicGpuHost>
+ScenicGpuHost::CreateHostProcessSelfRemote() {
+  DCHECK(!host_receiver_.is_bound());
+  return host_receiver_.BindNewPipeAndPassRemote();
 }
 
 void ScenicGpuHost::AttachSurfaceToWindow(
@@ -86,24 +82,23 @@
     GpuHostTerminateCallback terminate_callback) {
   DCHECK_CALLED_ON_VALID_THREAD(io_thread_checker_);
 
-  mojom::ScenicGpuServicePtr scenic_gpu_service;
-  BindInterface(mojo::MakeRequest(&scenic_gpu_service), binder);
+  mojo::PendingRemote<mojom::ScenicGpuService> scenic_gpu_service;
+  BindInterface(scenic_gpu_service.InitWithNewPipeAndPassReceiver(), binder);
   ui_thread_runner_->PostTask(
       FROM_HERE, base::BindOnce(&ScenicGpuHost::OnGpuServiceLaunchedOnUI,
                                 weak_ptr_factory_.GetWeakPtr(),
-                                scenic_gpu_service.PassInterface()));
+                                std::move(scenic_gpu_service)));
 }
 
 void ScenicGpuHost::OnGpuServiceLaunchedOnUI(
-    mojo::InterfacePtrInfo<mojom::ScenicGpuService> gpu_service_ptr_info) {
+    mojo::PendingRemote<mojom::ScenicGpuService> gpu_service) {
   DCHECK_CALLED_ON_VALID_THREAD(ui_thread_checker_);
 
-  mojom::ScenicGpuHostPtr gpu_host;
-  gpu_binding_.Close();
-  gpu_binding_.Bind(mojo::MakeRequest(&gpu_host));
+  gpu_receiver_.reset();
+  gpu_service_.reset();
 
-  gpu_service_.Bind(std::move(gpu_service_ptr_info));
-  gpu_service_->Initialize(std::move(gpu_host));
+  gpu_service_.Bind(std::move(gpu_service));
+  gpu_service_->Initialize(gpu_receiver_.BindNewPipeAndPassRemote());
 }
 
 void ScenicGpuHost::OnMessageReceived(const IPC::Message& message) {
diff --git a/ui/ozone/platform/scenic/scenic_gpu_host.h b/ui/ozone/platform/scenic/scenic_gpu_host.h
index c0e6567..1e472e8 100644
--- a/ui/ozone/platform/scenic/scenic_gpu_host.h
+++ b/ui/ozone/platform/scenic/scenic_gpu_host.h
@@ -12,7 +12,10 @@
 #include "base/memory/weak_ptr.h"
 #include "base/single_thread_task_runner.h"
 #include "base/threading/thread_checker.h"
-#include "mojo/public/cpp/bindings/binding.h"
+#include "mojo/public/cpp/bindings/pending_receiver.h"
+#include "mojo/public/cpp/bindings/pending_remote.h"
+#include "mojo/public/cpp/bindings/receiver.h"
+#include "mojo/public/cpp/bindings/remote.h"
 #include "ui/ozone/public/gpu_platform_support_host.h"
 #include "ui/ozone/public/mojom/scenic_gpu_host.mojom.h"
 #include "ui/ozone/public/mojom/scenic_gpu_service.mojom.h"
@@ -32,9 +35,9 @@
   ScenicGpuHost(ScenicWindowManager* scenic_window_manager);
   ~ScenicGpuHost() override;
 
-  // Creates browser process binding. This is used to create a software output
+  // Creates browser process remote. This is used to create a software output
   // on the UI thread.
-  mojom::ScenicGpuHostPtr CreateHostProcessSelfBinding();
+  mojo::PendingRemote<mojom::ScenicGpuHost> CreateHostProcessSelfRemote();
 
   // mojom::ScenicGpuHost:
   void AttachSurfaceToWindow(int32_t window_id,
@@ -57,21 +60,21 @@
 
  private:
   void OnGpuServiceLaunchedOnUI(
-      mojo::InterfacePtrInfo<mojom::ScenicGpuService> gpu_service_ptr_info);
-  void UpdateBinding(uint32_t service_launch_count,
-                     mojom::ScenicGpuHostRequest scenic_gpu_host_request);
+      mojo::PendingRemote<mojom::ScenicGpuService> gpu_service);
+  void UpdateReceiver(uint32_t service_launch_count,
+                      mojo::PendingReceiver<mojom::ScenicGpuHost> receiver);
 
   ScenicWindowManager* const scenic_window_manager_;
-  mojo::Binding<mojom::ScenicGpuHost> host_binding_;
-  mojo::Binding<mojom::ScenicGpuHost> gpu_binding_;
+  mojo::Receiver<mojom::ScenicGpuHost> host_receiver_{this};
+  mojo::Receiver<mojom::ScenicGpuHost> gpu_receiver_{this};
 
-  mojom::ScenicGpuServicePtr gpu_service_;
+  mojo::Remote<mojom::ScenicGpuService> gpu_service_;
   scoped_refptr<base::SingleThreadTaskRunner> ui_thread_runner_;
 
   THREAD_CHECKER(ui_thread_checker_);
   THREAD_CHECKER(io_thread_checker_);
 
-  base::WeakPtrFactory<ScenicGpuHost> weak_ptr_factory_;
+  base::WeakPtrFactory<ScenicGpuHost> weak_ptr_factory_{this};
 
   DISALLOW_COPY_AND_ASSIGN(ScenicGpuHost);
 };
diff --git a/ui/ozone/platform/scenic/scenic_gpu_service.cc b/ui/ozone/platform/scenic/scenic_gpu_service.cc
index 58018ea..b0c548c 100644
--- a/ui/ozone/platform/scenic/scenic_gpu_service.cc
+++ b/ui/ozone/platform/scenic/scenic_gpu_service.cc
@@ -11,42 +11,43 @@
 
 namespace {
 
-// Fulfills an InterfaceRequest<T> using an InterfacePtr<T>.
+// Fulfills a PendingReceiver<T> using a PendingRemote<T>.
 //
-// Messages queued on the InterfaceRequest's message pipe are preserved and will
-// be eventually delivered to the remote end of InterfacePtr<T>'s.
+// Messages queued on the PendingReceiver's message pipe are preserved and will
+// be eventually delivered to the remote end of PendingRemote<T>'s.
 //
-// InterfacePtr<T> must be a brand new interface; i.e., it not have been
+// PendingRemote<T> must be a brand new interface; i.e., it not have been
 // previously used to send a message.
 template <typename Interface>
-void FulfillInterfaceRequest(
-    mojo::InterfaceRequest<Interface> interface_request,
-    mojo::InterfacePtr<Interface> interface_ptr) {
+void FulfillPendingReceiver(mojo::PendingReceiver<Interface> receiver,
+                            mojo::PendingRemote<Interface> remote) {
   MojoResult result =
-      mojo::FuseMessagePipes(interface_ptr.PassInterface().PassHandle(),
-                             interface_request.PassMessagePipe());
+      mojo::FuseMessagePipes(remote.PassPipe(), receiver.PassPipe());
   DCHECK_EQ(result, MOJO_RESULT_OK);
 }
 
 }  // namespace
 
-ScenicGpuService::ScenicGpuService(mojom::ScenicGpuHostRequest gpu_host_request)
-    : gpu_host_request_(std::move(gpu_host_request)), weak_ptr_factory_(this) {}
+ScenicGpuService::ScenicGpuService(
+    mojo::PendingReceiver<mojom::ScenicGpuHost> gpu_host_receiver)
+    : gpu_host_receiver_(std::move(gpu_host_receiver)) {}
 
 ScenicGpuService::~ScenicGpuService() {}
 
-base::RepeatingCallback<void(mojom::ScenicGpuServiceRequest)>
+base::RepeatingCallback<void(mojo::PendingReceiver<mojom::ScenicGpuService>)>
 ScenicGpuService::GetBinderCallback() {
-  return base::BindRepeating(&ScenicGpuService::AddBinding,
+  return base::BindRepeating(&ScenicGpuService::AddReceiver,
                              weak_ptr_factory_.GetWeakPtr());
 }
 
-void ScenicGpuService::Initialize(mojom::ScenicGpuHostPtr gpu_host) {
-  FulfillInterfaceRequest(std::move(gpu_host_request_), std::move(gpu_host));
+void ScenicGpuService::Initialize(
+    mojo::PendingRemote<mojom::ScenicGpuHost> gpu_host) {
+  FulfillPendingReceiver(std::move(gpu_host_receiver_), std::move(gpu_host));
 }
 
-void ScenicGpuService::AddBinding(mojom::ScenicGpuServiceRequest request) {
-  binding_set_.AddBinding(this, std::move(request));
+void ScenicGpuService::AddReceiver(
+    mojo::PendingReceiver<mojom::ScenicGpuService> receiver) {
+  receiver_set_.Add(this, std::move(receiver));
 }
 
 }  // namespace ui
diff --git a/ui/ozone/platform/scenic/scenic_gpu_service.h b/ui/ozone/platform/scenic/scenic_gpu_service.h
index b4d1915..6fff9f6 100644
--- a/ui/ozone/platform/scenic/scenic_gpu_service.h
+++ b/ui/ozone/platform/scenic/scenic_gpu_service.h
@@ -8,7 +8,9 @@
 #include "base/callback.h"
 #include "base/macros.h"
 #include "base/memory/weak_ptr.h"
-#include "mojo/public/cpp/bindings/binding_set.h"
+#include "mojo/public/cpp/bindings/pending_receiver.h"
+#include "mojo/public/cpp/bindings/pending_remote.h"
+#include "mojo/public/cpp/bindings/receiver_set.h"
 #include "ui/ozone/public/mojom/scenic_gpu_host.mojom.h"
 #include "ui/ozone/public/mojom/scenic_gpu_service.mojom.h"
 
@@ -22,23 +24,24 @@
 // so that surfaces can present to Scenic views managed by the browser.
 class ScenicGpuService : public mojom::ScenicGpuService {
  public:
-  ScenicGpuService(mojom::ScenicGpuHostRequest gpu_host_request);
+  ScenicGpuService(
+      mojo::PendingReceiver<mojom::ScenicGpuHost> gpu_host_receiver);
   ~ScenicGpuService() override;
 
-  base::RepeatingCallback<void(mojom::ScenicGpuServiceRequest)>
+  base::RepeatingCallback<void(mojo::PendingReceiver<mojom::ScenicGpuService>)>
   GetBinderCallback();
 
   // mojom::ScenicGpuService:
-  void Initialize(mojom::ScenicGpuHostPtr gpu_host) override;
+  void Initialize(mojo::PendingRemote<mojom::ScenicGpuHost> gpu_host) override;
 
  private:
-  void AddBinding(mojom::ScenicGpuServiceRequest request);
+  void AddReceiver(mojo::PendingReceiver<mojom::ScenicGpuService> receiver);
 
-  mojom::ScenicGpuHostRequest gpu_host_request_;
+  mojo::PendingReceiver<mojom::ScenicGpuHost> gpu_host_receiver_;
 
-  mojo::BindingSet<mojom::ScenicGpuService> binding_set_;
+  mojo::ReceiverSet<mojom::ScenicGpuService> receiver_set_;
 
-  base::WeakPtrFactory<ScenicGpuService> weak_ptr_factory_;
+  base::WeakPtrFactory<ScenicGpuService> weak_ptr_factory_{this};
 
   DISALLOW_COPY_AND_ASSIGN(ScenicGpuService);
 };
diff --git a/ui/ozone/public/mojom/scenic_gpu_service.mojom b/ui/ozone/public/mojom/scenic_gpu_service.mojom
index fdd94487..5742b35 100644
--- a/ui/ozone/public/mojom/scenic_gpu_service.mojom
+++ b/ui/ozone/public/mojom/scenic_gpu_service.mojom
@@ -15,5 +15,5 @@
   //
   // TODO(spang): Consider providing a way for GPU to request ScenicGpuHost from
   // service manager instead of returning one from a callback.
-  Initialize(ScenicGpuHost scenic_gpu_host);
+  Initialize(pending_remote<ScenicGpuHost> scenic_gpu_host);
 };