Convert RestrictedCookieManager client to use BrowserInterfaceBroker

This change converts RestrictedCookieManager mojom interface client
in blink to use BrowserInterfaceBroker.

Bug: 936482
Change-Id: I2d8a4cee3b7f039ea350cffedfa15d96447c9df5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1844754
Commit-Queue: Miyoung Shin <myid.shin@igalia.com>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Reviewed-by: Dave Tapuska <dtapuska@chromium.org>
Reviewed-by: Oksana Zhuravlova <oksamyt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720870}
diff --git a/content/browser/browser_interface_binders.cc b/content/browser/browser_interface_binders.cc
index d0cb6e3..561079e7 100644
--- a/content/browser/browser_interface_binders.cc
+++ b/content/browser/browser_interface_binders.cc
@@ -49,6 +49,7 @@
 #include "services/device/public/mojom/constants.mojom.h"
 #include "services/device/public/mojom/sensor_provider.mojom.h"
 #include "services/device/public/mojom/vibration_manager.mojom.h"
+#include "services/network/public/mojom/restricted_cookie_manager.mojom.h"
 #include "services/service_manager/public/cpp/connector.h"
 #include "services/shape_detection/public/mojom/barcodedetection_provider.mojom.h"
 #include "services/shape_detection/public/mojom/facedetection_provider.mojom.h"
@@ -592,6 +593,10 @@
       base::BindRepeating(&RenderProcessHost::BindVideoDecodePerfHistory,
                           base::Unretained(host->GetProcess())));
 
+  map->Add<network::mojom::RestrictedCookieManager>(
+      base::BindRepeating(&RenderFrameHostImpl::BindRestrictedCookieManager,
+                          base::Unretained(host)));
+
   map->Add<shape_detection::mojom::BarcodeDetectionProvider>(
       base::BindRepeating(&BindBarcodeDetectionProvider));
 
@@ -888,6 +893,10 @@
           &RenderProcessHost::CreateNotificationService, host));
   map->Add<blink::mojom::WebSocketConnector>(BindServiceWorkerReceiverForOrigin(
       &RenderProcessHost::CreateWebSocketConnector, host));
+  map->Add<network::mojom::RestrictedCookieManager>(
+      BindServiceWorkerReceiverForOrigin(
+          &RenderProcessHost::BindRestrictedCookieManagerForServiceWorker,
+          host));
 
   // render process host binders taking a frame id and an origin
   map->Add<blink::mojom::IDBFactory>(
diff --git a/content/browser/frame_host/render_frame_host_impl.cc b/content/browser/frame_host/render_frame_host_impl.cc
index 6efdf5da..b9f9e3b 100644
--- a/content/browser/frame_host/render_frame_host_impl.cc
+++ b/content/browser/frame_host/render_frame_host_impl.cc
@@ -528,20 +528,6 @@
       bundle->Clone().release()));
 }
 
-void GetRestrictedCookieManager(
-    RenderFrameHostImpl* frame_host,
-    int process_id,
-    int frame_id,
-    StoragePartition* storage_partition,
-    mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver) {
-  storage_partition->CreateRestrictedCookieManager(
-      network::mojom::RestrictedCookieManagerRole::SCRIPT,
-      frame_host->GetLastCommittedOrigin(), frame_host->ComputeSiteForCookies(),
-      frame_host->ComputeTopFrameOrigin(frame_host->GetLastCommittedOrigin()),
-      /* is_service_worker = */ false, process_id, frame_id,
-      std::move(receiver));
-}
-
 // Helper method to download a URL on UI thread.
 void StartDownload(
     std::unique_ptr<download::DownloadUrlParameters> parameters,
@@ -4683,10 +4669,6 @@
       GetProcess()->GetID(),
       GetProcess()->GetStoragePartition()->GetFileSystemContext(),
       ChromeBlobStorageContext::GetFor(GetProcess()->GetBrowserContext())));
-
-  registry_->AddInterface(base::BindRepeating(
-      &GetRestrictedCookieManager, base::Unretained(this),
-      GetProcess()->GetID(), routing_id_, GetProcess()->GetStoragePartition()));
 }
 
 media::MediaMetricsProvider::RecordAggregateWatchTimeCallback
@@ -6634,6 +6616,16 @@
   SmsService::Create(fetcher, this, std::move(receiver));
 }
 
+void RenderFrameHostImpl::BindRestrictedCookieManager(
+    mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver) {
+  GetProcess()->GetStoragePartition()->CreateRestrictedCookieManager(
+      network::mojom::RestrictedCookieManagerRole::SCRIPT,
+      GetLastCommittedOrigin(), ComputeSiteForCookies(),
+      ComputeTopFrameOrigin(GetLastCommittedOrigin()),
+      /* is_service_worker = */ false, GetProcess()->GetID(), routing_id(),
+      std::move(receiver));
+}
+
 void RenderFrameHostImpl::GetInterface(
     const std::string& interface_name,
     mojo::ScopedMessagePipeHandle interface_pipe) {
diff --git a/content/browser/frame_host/render_frame_host_impl.h b/content/browser/frame_host/render_frame_host_impl.h
index 7fa2ffb7..5af70b1 100644
--- a/content/browser/frame_host/render_frame_host_impl.h
+++ b/content/browser/frame_host/render_frame_host_impl.h
@@ -1164,6 +1164,9 @@
   void BindSmsReceiverReceiver(
       mojo::PendingReceiver<blink::mojom::SmsReceiver> receiver);
 
+  void BindRestrictedCookieManager(
+      mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver);
+
   // Creates connections to WebUSB interfaces bound to this frame.
   void CreateWebUsbService(
       mojo::PendingReceiver<blink::mojom::WebUsbService> receiver);
diff --git a/content/browser/renderer_host/render_process_host_impl.cc b/content/browser/renderer_host/render_process_host_impl.cc
index 7348ec2..ef52b95 100644
--- a/content/browser/renderer_host/render_process_host_impl.cc
+++ b/content/browser/renderer_host/render_process_host_impl.cc
@@ -1937,6 +1937,16 @@
       std::move(receiver));
 }
 
+void RenderProcessHostImpl::BindRestrictedCookieManagerForServiceWorker(
+    const url::Origin& origin,
+    mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver) {
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
+  GetStoragePartition()->CreateRestrictedCookieManager(
+      network::mojom::RestrictedCookieManagerRole::SCRIPT, origin,
+      origin.GetURL(), origin, true /* is_service_worker */, GetID(),
+      MSG_ROUTING_NONE, std::move(receiver));
+}
+
 void RenderProcessHostImpl::BindVideoDecodePerfHistory(
     mojo::PendingReceiver<media::mojom::VideoDecodePerfHistory> receiver) {
   DCHECK_CURRENTLY_ON(BrowserThread::UI);
diff --git a/content/browser/renderer_host/render_process_host_impl.h b/content/browser/renderer_host/render_process_host_impl.h
index 590b67b4..6e25f20 100644
--- a/content/browser/renderer_host/render_process_host_impl.h
+++ b/content/browser/renderer_host/render_process_host_impl.h
@@ -559,6 +559,15 @@
     return file_system_manager_impl_.get();
   }
 
+  // Binds |receiver| to the RestrictedCookieManager instance owned by
+  // |storage_partition_impl_|, and is used by a service worker via
+  // BrowserInterfaceBroker. |receiver| belongs to the service worker at
+  // |origin| hosted by this process,
+  void BindRestrictedCookieManagerForServiceWorker(
+      const url::Origin& origin,
+      mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver)
+      override;
+
   // Binds |receiver| to the VideoDecodePerfHistory instance owned by the render
   // process host, and is used by workers via BrowserInterfaceBroker.
   void BindVideoDecodePerfHistory(
diff --git a/content/browser/service_worker/service_worker_provider_host.cc b/content/browser/service_worker/service_worker_provider_host.cc
index f60cae0..1bca528 100644
--- a/content/browser/service_worker/service_worker_provider_host.cc
+++ b/content/browser/service_worker/service_worker_provider_host.cc
@@ -48,18 +48,6 @@
   if (!process)
     return;
 
-  // RestrictedCookieManager creation is different between frames and service
-  // workers, so it's handled here.
-  if (interface_name == network::mojom::RestrictedCookieManager::Name_) {
-    mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver(
-        std::move(interface_pipe));
-    process->GetStoragePartition()->CreateRestrictedCookieManager(
-        network::mojom::RestrictedCookieManagerRole::SCRIPT, origin,
-        origin.GetURL(), origin, true /* is_service_worker */, process_id,
-        MSG_ROUTING_NONE, std::move(receiver));
-    return;
-  }
-
   BindWorkerInterface(interface_name, std::move(interface_pipe), process,
                       origin);
 }
diff --git a/content/public/app/content_browser_manifest.cc b/content/public/app/content_browser_manifest.cc
index 82f6bdf..1af6703 100644
--- a/content/public/app/content_browser_manifest.cc
+++ b/content/public/app/content_browser_manifest.cc
@@ -174,8 +174,7 @@
           .ExposeInterfaceFilterCapability_Deprecated(
               "navigation:service_worker", "renderer",
               std::set<const char*>{
-                  "blink.mojom.QuotaDispatcherHost",
-                  "network.mojom.RestrictedCookieManager"})
+                  "blink.mojom.QuotaDispatcherHost"})
           .ExposeInterfaceFilterCapability_Deprecated(
               "navigation:frame", "renderer",
               std::set<const char*>{
@@ -188,7 +187,6 @@
                   "content.mojom.RendererAudioInputStreamFactory",
                   "content.mojom.RendererAudioOutputStreamFactory",
                   "discardable_memory.mojom.DiscardableSharedMemoryManager",
-                  "network.mojom.RestrictedCookieManager",
                   "viz.mojom.Gpu"})
           .PackageService(content::GetManifest())
           .Build()};
diff --git a/content/public/browser/render_process_host.h b/content/public/browser/render_process_host.h
index a9497e1..f0cd3ca 100644
--- a/content/public/browser/render_process_host.h
+++ b/content/public/browser/render_process_host.h
@@ -29,6 +29,7 @@
 #include "net/base/network_isolation_key.h"
 #include "services/network/public/mojom/cross_origin_embedder_policy.mojom-forward.h"
 #include "services/network/public/mojom/network_context.mojom-forward.h"
+#include "services/network/public/mojom/restricted_cookie_manager.mojom-forward.h"
 #include "services/network/public/mojom/url_loader_factory.mojom-forward.h"
 #include "third_party/blink/public/mojom/appcache/appcache.mojom.h"
 #include "third_party/blink/public/mojom/cache_storage/cache_storage.mojom-forward.h"
@@ -511,6 +512,10 @@
       int render_frame_id,
       const url::Origin& origin,
       mojo::PendingReceiver<blink::mojom::IDBFactory> receiver) = 0;
+  virtual void BindRestrictedCookieManagerForServiceWorker(
+      const url::Origin& origin,
+      mojo::PendingReceiver<network::mojom::RestrictedCookieManager>
+          receiver) = 0;
   virtual void BindVideoDecodePerfHistory(
       mojo::PendingReceiver<media::mojom::VideoDecodePerfHistory> receiver) = 0;
   virtual void CreateLockManager(
diff --git a/content/public/test/mock_render_process_host.h b/content/public/test/mock_render_process_host.h
index b19229b..51346ae 100644
--- a/content/public/test/mock_render_process_host.h
+++ b/content/public/test/mock_render_process_host.h
@@ -186,6 +186,10 @@
       int render_frame_id,
       const url::Origin& origin,
       mojo::PendingReceiver<blink::mojom::IDBFactory> receiver) override;
+  void BindRestrictedCookieManagerForServiceWorker(
+      const url::Origin& origin,
+      mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver)
+      override {}
   void BindVideoDecodePerfHistory(
       mojo::PendingReceiver<media::mojom::VideoDecodePerfHistory> receiver)
       override {}
diff --git a/third_party/blink/renderer/core/loader/cookie_jar.cc b/third_party/blink/renderer/core/loader/cookie_jar.cc
index 2f68fd3..313fcf6e 100644
--- a/third_party/blink/renderer/core/loader/cookie_jar.cc
+++ b/third_party/blink/renderer/core/loader/cookie_jar.cc
@@ -4,7 +4,7 @@
 
 #include "third_party/blink/renderer/core/loader/cookie_jar.h"
 
-#include "services/service_manager/public/cpp/interface_provider.h"
+#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
 #include "third_party/blink/renderer/core/dom/document.h"
 #include "third_party/blink/renderer/platform/instrumentation/histogram.h"
 #include "third_party/blink/renderer/platform/weborigin/kurl.h"
@@ -54,7 +54,7 @@
 void CookieJar::RequestRestrictedCookieManagerIfNeeded() {
   if (!backend_.is_bound() || !backend_.is_connected()) {
     backend_.reset();
-    document_->GetInterfaceProvider()->GetInterface(
+    document_->GetBrowserInterfaceBroker().GetInterface(
         backend_.BindNewPipeAndPassReceiver());
   }
 }
diff --git a/third_party/blink/renderer/modules/cookie_store/global_cookie_store.cc b/third_party/blink/renderer/modules/cookie_store/global_cookie_store.cc
index 45e8ada..7426ca1 100644
--- a/third_party/blink/renderer/modules/cookie_store/global_cookie_store.cc
+++ b/third_party/blink/renderer/modules/cookie_store/global_cookie_store.cc
@@ -7,7 +7,7 @@
 #include <utility>
 
 #include "services/network/public/mojom/restricted_cookie_manager.mojom-blink.h"
-#include "services/service_manager/public/cpp/interface_provider.h"
+#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
 #include "third_party/blink/renderer/core/execution_context/execution_context.h"
 #include "third_party/blink/renderer/core/frame/local_dom_window.h"
 #include "third_party/blink/renderer/core/frame/local_frame.h"
@@ -46,15 +46,13 @@
   CookieStore* GetCookieStore(T& scope) {
     if (!cookie_store_) {
       ExecutionContext* execution_context = scope.GetExecutionContext();
-
-      service_manager::InterfaceProvider* interface_provider =
-          execution_context->GetInterfaceProvider();
-      if (!interface_provider)
+      if (!execution_context->GetInterfaceProvider())
         return nullptr;
 
       mojo::Remote<network::mojom::blink::RestrictedCookieManager> backend;
-      interface_provider->GetInterface(backend.BindNewPipeAndPassReceiver(
-          execution_context->GetTaskRunner(TaskType::kMiscPlatformAPI)));
+      execution_context->GetBrowserInterfaceBroker().GetInterface(
+          backend.BindNewPipeAndPassReceiver(
+              execution_context->GetTaskRunner(TaskType::kMiscPlatformAPI)));
       cookie_store_ = MakeGarbageCollected<CookieStore>(execution_context,
                                                         std::move(backend));
     }