From bcc673eda87d37a39484b339c9c06fbb2ec85dba Mon Sep 17 00:00:00 2001
From: Lingqi Chi <lingqi@chromium.org>
Date: Wed, 16 Sep 2020 03:12:03 +0000
Subject: [PATCH] blink: Let OneShotBackgroundSyncService be obtained via
 ExecutionContent-scoped BIB

Migrated OneShotBackgroundSyncService interface; let it be obtained via
an ExecutionContent-scoped BrowserInterfaceBroker instead of a
Process-wide one. Then it can be controllable when prerendering a page.

Also fixed an error in webtest comments to make it be in accordance with
the error message.

Change-Id: I94ccdc65586b895f52f02972971338926e3b63a0
Bug: 1126758
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2395995
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Reviewed-by: Peter Beverloo <peter@chromium.org>
Reviewed-by: Matt Falkenhagen <falken@chromium.org>
Commit-Queue: Lingqi Chi <lingqi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807317}
---
 content/browser/browser_interface_binders.cc           |  8 ++++++++
 .../browser/renderer_host/render_process_host_impl.cc  |  5 +----
 .../browser/renderer_host/render_process_host_impl.h   | 10 +++++++---
 content/public/browser/render_process_host.h           |  4 ++++
 content/public/test/mock_render_process_host.h         |  3 +++
 .../renderer/modules/background_sync/sync_manager.cc   |  4 ++--
 .../web_tests/http/tests/background_sync/oneshot.html  |  2 +-
 7 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/content/browser/browser_interface_binders.cc b/content/browser/browser_interface_binders.cc
index c80f820b2b70d..d86a7e2302dac 100644
--- a/content/browser/browser_interface_binders.cc
+++ b/content/browser/browser_interface_binders.cc
@@ -74,6 +74,7 @@
 #include "third_party/blink/public/common/features.h"
 #include "third_party/blink/public/mojom/appcache/appcache.mojom.h"
 #include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom.h"
+#include "third_party/blink/public/mojom/background_sync/background_sync.mojom.h"
 #include "third_party/blink/public/mojom/badging/badging.mojom.h"
 #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom.h"
 #include "third_party/blink/public/mojom/cache_storage/cache_storage.mojom.h"
@@ -679,6 +680,10 @@ void PopulateFrameBinders(RenderFrameHostImpl* host, mojo::BinderMap* map) {
                           base::Unretained(host)));
 #endif
 
+  map->Add<blink::mojom::OneShotBackgroundSyncService>(
+      base::BindRepeating(&RenderProcessHost::CreateOneShotSyncService,
+                          base::Unretained(host->GetProcess())));
+
   map->Add<media::mojom::VideoDecodePerfHistory>(
       base::BindRepeating(&RenderProcessHost::BindVideoDecodePerfHistory,
                           base::Unretained(host->GetProcess())));
@@ -1023,6 +1028,9 @@ void PopulateServiceWorkerBinders(ServiceWorkerHost* host,
   // render process host binders
   map->Add<media::mojom::VideoDecodePerfHistory>(BindServiceWorkerReceiver(
       &RenderProcessHostImpl::BindVideoDecodePerfHistory, host));
+  map->Add<blink::mojom::OneShotBackgroundSyncService>(
+      BindServiceWorkerReceiver(
+          &RenderProcessHostImpl::CreateOneShotSyncService, host));
 }
 
 void PopulateBinderMapWithContext(
diff --git a/content/browser/renderer_host/render_process_host_impl.cc b/content/browser/renderer_host/render_process_host_impl.cc
index ff226c9dd88f3..e2f7673de2204 100644
--- a/content/browser/renderer_host/render_process_host_impl.cc
+++ b/content/browser/renderer_host/render_process_host_impl.cc
@@ -2311,10 +2311,6 @@ void RenderProcessHostImpl::RegisterMojoInterfaces() {
       base::BindRepeating(&RenderProcessHostImpl::BindCompositingModeReporter,
                           weak_factory_.GetWeakPtr()));
 
-  AddUIThreadInterface(
-      registry.get(),
-      base::BindRepeating(&RenderProcessHostImpl::CreateOneShotSyncService,
-                          weak_factory_.GetWeakPtr()));
   AddUIThreadInterface(
       registry.get(),
       base::BindRepeating(&RenderProcessHostImpl::CreatePeriodicSyncService,
@@ -2665,6 +2661,7 @@ void RenderProcessHostImpl::BindAecDumpManager(
 void RenderProcessHostImpl::CreateOneShotSyncService(
     mojo::PendingReceiver<blink::mojom::OneShotBackgroundSyncService>
         receiver) {
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
   storage_partition_impl_->GetBackgroundSyncContext()->CreateOneShotSyncService(
       std::move(receiver));
 }
diff --git a/content/browser/renderer_host/render_process_host_impl.h b/content/browser/renderer_host/render_process_host_impl.h
index d640c30630c26..5200d9d672c56 100644
--- a/content/browser/renderer_host/render_process_host_impl.h
+++ b/content/browser/renderer_host/render_process_host_impl.h
@@ -584,6 +584,13 @@ class CONTENT_EXPORT RenderProcessHostImpl
       mojo::PendingReceiver<media::mojom::VideoDecodePerfHistory> receiver)
       override;
 
+  // Binds |receiver| to a OneShotBackgroundSyncService instance owned by the
+  // StoragePartition associated with the render process host, and is used by
+  // frames and service workers via BrowserInterfaceBroker.
+  void CreateOneShotSyncService(
+      mojo::PendingReceiver<blink::mojom::OneShotBackgroundSyncService>
+          receiver) override;
+
   // Binds |receiver| to a QuotaManagerHost instance indirectly owned by the
   // StoragePartition associated with the render process host. Used by frames
   // and workers via BrowserInterfaceBroker.
@@ -780,9 +787,6 @@ class CONTENT_EXPORT RenderProcessHostImpl
       mojo::PendingReceiver<blink::mojom::WebDatabaseHost> receiver);
   void BindAecDumpManager(
       mojo::PendingReceiver<blink::mojom::AecDumpManager> receiver);
-  void CreateOneShotSyncService(
-      mojo::PendingReceiver<blink::mojom::OneShotBackgroundSyncService>
-          receiver);
   void CreatePeriodicSyncService(
       mojo::PendingReceiver<blink::mojom::PeriodicBackgroundSyncService>
           receiver);
diff --git a/content/public/browser/render_process_host.h b/content/public/browser/render_process_host.h
index 5ab867956923f..a8e6e4646e6e5 100644
--- a/content/public/browser/render_process_host.h
+++ b/content/public/browser/render_process_host.h
@@ -33,6 +33,7 @@
 #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/background_sync/background_sync.mojom.h"
 #include "third_party/blink/public/mojom/cache_storage/cache_storage.mojom-forward.h"
 #include "third_party/blink/public/mojom/filesystem/file_system.mojom-forward.h"
 #include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom-forward.h"
@@ -503,6 +504,9 @@ class CONTENT_EXPORT RenderProcessHost : public IPC::Sender,
           receiver) = 0;
   virtual void BindVideoDecodePerfHistory(
       mojo::PendingReceiver<media::mojom::VideoDecodePerfHistory> receiver) = 0;
+  virtual void CreateOneShotSyncService(
+      mojo::PendingReceiver<blink::mojom::OneShotBackgroundSyncService>
+          receiver) = 0;
   virtual void BindQuotaManagerHost(
       int render_frame_id,
       const url::Origin& origin,
diff --git a/content/public/test/mock_render_process_host.h b/content/public/test/mock_render_process_host.h
index 6c3fd892aba0a..9f065d2a50f03 100644
--- a/content/public/test/mock_render_process_host.h
+++ b/content/public/test/mock_render_process_host.h
@@ -199,6 +199,9 @@ class MockRenderProcessHost : public RenderProcessHost {
       int render_frame_id,
       const url::Origin& origin,
       mojo::PendingReceiver<blink::mojom::LockManager> receiver) override {}
+  void CreateOneShotSyncService(
+      mojo::PendingReceiver<blink::mojom::OneShotBackgroundSyncService>
+          receiver) override {}
   void CreatePermissionService(
       const url::Origin& origin,
       mojo::PendingReceiver<blink::mojom::PermissionService> receiver)
diff --git a/third_party/blink/renderer/modules/background_sync/sync_manager.cc b/third_party/blink/renderer/modules/background_sync/sync_manager.cc
index 9011c23011bcd..42cb8d3182275 100644
--- a/third_party/blink/renderer/modules/background_sync/sync_manager.cc
+++ b/third_party/blink/renderer/modules/background_sync/sync_manager.cc
@@ -4,7 +4,7 @@
 
 #include "third_party/blink/renderer/modules/background_sync/sync_manager.h"
 
-#include "third_party/blink/public/common/thread_safe_browser_interface_broker_proxy.h"
+#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
 #include "third_party/blink/public/platform/platform.h"
 #include "third_party/blink/renderer/bindings/core/v8/callback_promise_adapter.h"
 #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
@@ -25,7 +25,7 @@ SyncManager::SyncManager(ServiceWorkerRegistration* registration,
     : registration_(registration),
       background_sync_service_(registration->GetExecutionContext()) {
   DCHECK(registration);
-  Platform::Current()->GetBrowserInterfaceBroker()->GetInterface(
+  registration->GetExecutionContext()->GetBrowserInterfaceBroker().GetInterface(
       background_sync_service_.BindNewPipeAndPassReceiver(task_runner));
 }
 
diff --git a/third_party/blink/web_tests/http/tests/background_sync/oneshot.html b/third_party/blink/web_tests/http/tests/background_sync/oneshot.html
index 35ac7db36f0e3..82502eb9246ce 100644
--- a/third_party/blink/web_tests/http/tests/background_sync/oneshot.html
+++ b/third_party/blink/web_tests/http/tests/background_sync/oneshot.html
@@ -14,7 +14,7 @@ promise_test(function(t) {
   const scope = '/resources/scope/background_sync/' + iframe_scope;
   var sync_manager;
 
-  // This test verifies that registration of one-shots fails from an iframe.
+  // This test verifies that one-shot syncs can be registered from an iframe.
   return PermissionsHelper.setPermission('background-sync', 'granted')
     .then(function() {
         return service_worker_unregister_and_register(t, url, scope);