Move WebIDBFactoryImpl to a non-channel-associated interface

Bug: 717812
Change-Id: Id90b1199c9702e7f03864cb40958fae858c1c3cd
Reviewed-on: https://chromium-review.googlesource.com/1146335
Commit-Queue: Chase Phillips <cmp@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Ken Buchanan <kenrb@chromium.org>
Reviewed-by: Chris Mumford <cmumford@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Reviewed-by: Daniel Murphy <dmurph@chromium.org>
Reviewed-by: Victor Costan <pwnall@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578062}
diff --git a/content/browser/indexed_db/indexed_db_dispatcher_host.cc b/content/browser/indexed_db/indexed_db_dispatcher_host.cc
index 034a34e..cc518bf 100644
--- a/content/browser/indexed_db/indexed_db_dispatcher_host.cc
+++ b/content/browser/indexed_db/indexed_db_dispatcher_host.cc
@@ -142,7 +142,7 @@
 }
 
 void IndexedDBDispatcherHost::AddBinding(
-    ::indexed_db::mojom::FactoryAssociatedRequest request) {
+    ::indexed_db::mojom::FactoryRequest request) {
   bindings_.AddBinding(this, std::move(request));
 }
 
diff --git a/content/browser/indexed_db/indexed_db_dispatcher_host.h b/content/browser/indexed_db/indexed_db_dispatcher_host.h
index dc17704..df583047bc 100644
--- a/content/browser/indexed_db/indexed_db_dispatcher_host.h
+++ b/content/browser/indexed_db/indexed_db_dispatcher_host.h
@@ -49,7 +49,7 @@
       scoped_refptr<IndexedDBContextImpl> indexed_db_context,
       scoped_refptr<ChromeBlobStorageContext> blob_storage_context);
 
-  void AddBinding(::indexed_db::mojom::FactoryAssociatedRequest request);
+  void AddBinding(::indexed_db::mojom::FactoryRequest request);
 
   void AddDatabaseBinding(
       std::unique_ptr<::indexed_db::mojom::Database> database,
@@ -117,7 +117,7 @@
   // Used to set file permissions for blob storage.
   const int ipc_process_id_;
 
-  mojo::AssociatedBindingSet<::indexed_db::mojom::Factory> bindings_;
+  mojo::BindingSet<::indexed_db::mojom::Factory> bindings_;
 
   mojo::StrongAssociatedBindingSet<::indexed_db::mojom::Database>
       database_bindings_;
diff --git a/content/browser/indexed_db/indexed_db_dispatcher_host_unittest.cc b/content/browser/indexed_db/indexed_db_dispatcher_host_unittest.cc
index ff71819..1ad304b 100644
--- a/content/browser/indexed_db/indexed_db_dispatcher_host_unittest.cc
+++ b/content/browser/indexed_db/indexed_db_dispatcher_host_unittest.cc
@@ -43,8 +43,7 @@
 using indexed_db::mojom::DatabaseCallbacks;
 using indexed_db::mojom::DatabaseCallbacksAssociatedPtrInfo;
 using indexed_db::mojom::Factory;
-using indexed_db::mojom::FactoryAssociatedPtr;
-using indexed_db::mojom::FactoryAssociatedRequest;
+using indexed_db::mojom::FactoryPtr;
 using indexed_db::mojom::KeyPath;
 using indexed_db::mojom::Value;
 using indexed_db::mojom::ValuePtr;
@@ -184,9 +183,7 @@
   }
 
   void SetUp() override {
-    FactoryAssociatedRequest request =
-        ::mojo::MakeRequestAssociatedWithDedicatedPipe(&idb_mojo_factory_);
-    host_->AddBinding(std::move(request));
+    host_->AddBinding(::mojo::MakeRequest(&idb_mojo_factory_));
   }
 
  protected:
@@ -199,7 +196,7 @@
   scoped_refptr<IndexedDBContextImpl> context_impl_;
   std::unique_ptr<IndexedDBDispatcherHost, BrowserThread::DeleteOnIOThread>
       host_;
-  FactoryAssociatedPtr idb_mojo_factory_;
+  FactoryPtr idb_mojo_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcherHostTest);
 };
diff --git a/content/browser/renderer_host/render_process_host_impl.cc b/content/browser/renderer_host/render_process_host_impl.cc
index 60ece39..5dcfede 100644
--- a/content/browser/renderer_host/render_process_host_impl.cc
+++ b/content/browser/renderer_host/render_process_host_impl.cc
@@ -1911,10 +1911,6 @@
 void RenderProcessHostImpl::RegisterMojoInterfaces() {
   auto registry = std::make_unique<service_manager::BinderRegistry>();
 
-  channel_->AddAssociatedInterfaceForIOThread(
-      base::Bind(&IndexedDBDispatcherHost::AddBinding,
-                 base::Unretained(indexed_db_factory_.get())));
-
   channel_->AddAssociatedInterfaceForIOThread(base::BindRepeating(
       &ServiceWorkerDispatcherHost::AddBinding,
       base::Unretained(service_worker_dispatcher_host_.get())));
@@ -1996,6 +1992,10 @@
   }
 
   registry->AddInterface(
+      base::BindRepeating(&IndexedDBDispatcherHost::AddBinding,
+                          base::Unretained(indexed_db_factory_.get())));
+
+  registry->AddInterface(
       base::Bind(
           &WebDatabaseHostImpl::Create, GetID(),
           base::WrapRefCounted(storage_partition_impl_->GetDatabaseTracker())),
diff --git a/content/public/app/mojo/content_browser_manifest.json b/content/public/app/mojo/content_browser_manifest.json
index 96f8e82..62ef1d7 100644
--- a/content/public/app/mojo/content_browser_manifest.json
+++ b/content/public/app/mojo/content_browser_manifest.json
@@ -61,6 +61,7 @@
           "device.mojom.BatteryMonitor",
           "device.mojom.GamepadHapticsManager",
           "discardable_memory.mojom.DiscardableSharedMemoryManager",
+          "indexed_db.mojom.Factory",
           "media.mojom.KeySystemSupport",
           "media.mojom.VideoCaptureHost",
           "media.mojom.VideoDecodePerfHistory",
diff --git a/content/renderer/indexed_db/webidbfactory_impl.cc b/content/renderer/indexed_db/webidbfactory_impl.cc
index 753c35a..a2b771b 100644
--- a/content/renderer/indexed_db/webidbfactory_impl.cc
+++ b/content/renderer/indexed_db/webidbfactory_impl.cc
@@ -20,21 +20,22 @@
 using blink::WebString;
 using indexed_db::mojom::CallbacksAssociatedPtrInfo;
 using indexed_db::mojom::DatabaseCallbacksAssociatedPtrInfo;
-using indexed_db::mojom::FactoryAssociatedPtr;
+using indexed_db::mojom::FactoryPtr;
+using indexed_db::mojom::FactoryPtrInfo;
 
 namespace content {
 
 class WebIDBFactoryImpl::IOThreadHelper {
  public:
-  IOThreadHelper(scoped_refptr<IPC::SyncMessageFilter> sync_message_filter);
+  IOThreadHelper();
   ~IOThreadHelper();
 
-  FactoryAssociatedPtr& GetService();
   CallbacksAssociatedPtrInfo GetCallbacksProxy(
       std::unique_ptr<IndexedDBCallbacksImpl> callbacks);
   DatabaseCallbacksAssociatedPtrInfo GetDatabaseCallbacksProxy(
       std::unique_ptr<IndexedDBDatabaseCallbacksImpl> callbacks);
 
+  void Bind(FactoryPtrInfo factory_info);
   void GetDatabaseNames(std::unique_ptr<IndexedDBCallbacksImpl> callbacks,
                         const url::Origin& origin);
   void Open(const base::string16& name,
@@ -49,17 +50,19 @@
                       bool force_close);
 
  private:
-  scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
-  FactoryAssociatedPtr service_;
+  FactoryPtr factory_;
 
   DISALLOW_COPY_AND_ASSIGN(IOThreadHelper);
 };
 
 WebIDBFactoryImpl::WebIDBFactoryImpl(
-    scoped_refptr<IPC::SyncMessageFilter> sync_message_filter,
+    FactoryPtrInfo factory_info,
     scoped_refptr<base::SingleThreadTaskRunner> io_runner)
-    : io_helper_(new IOThreadHelper(std::move(sync_message_filter))),
-      io_runner_(std::move(io_runner)) {}
+    : io_helper_(new IOThreadHelper()), io_runner_(std::move(io_runner)) {
+  io_runner_->PostTask(FROM_HERE, base::BindOnce(&IOThreadHelper::Bind,
+                                                 base::Unretained(io_helper_),
+                                                 std::move(factory_info)));
+}
 
 WebIDBFactoryImpl::~WebIDBFactoryImpl() {
   io_runner_->DeleteSoon(FROM_HERE, io_helper_);
@@ -117,16 +120,12 @@
                                 force_close));
 }
 
-WebIDBFactoryImpl::IOThreadHelper::IOThreadHelper(
-    scoped_refptr<IPC::SyncMessageFilter> sync_message_filter)
-    : sync_message_filter_(std::move(sync_message_filter)) {}
+WebIDBFactoryImpl::IOThreadHelper::IOThreadHelper() = default;
 
 WebIDBFactoryImpl::IOThreadHelper::~IOThreadHelper() {}
 
-FactoryAssociatedPtr& WebIDBFactoryImpl::IOThreadHelper::GetService() {
-  if (!service_)
-    sync_message_filter_->GetRemoteAssociatedInterface(&service_);
-  return service_;
+void WebIDBFactoryImpl::IOThreadHelper::Bind(FactoryPtrInfo factory_info) {
+  factory_.Bind(std::move(factory_info));
 }
 
 CallbacksAssociatedPtrInfo WebIDBFactoryImpl::IOThreadHelper::GetCallbacksProxy(
@@ -149,8 +148,7 @@
 void WebIDBFactoryImpl::IOThreadHelper::GetDatabaseNames(
     std::unique_ptr<IndexedDBCallbacksImpl> callbacks,
     const url::Origin& origin) {
-  GetService()->GetDatabaseNames(GetCallbacksProxy(std::move(callbacks)),
-                                 origin);
+  factory_->GetDatabaseNames(GetCallbacksProxy(std::move(callbacks)), origin);
 }
 
 void WebIDBFactoryImpl::IOThreadHelper::Open(
@@ -160,9 +158,9 @@
     std::unique_ptr<IndexedDBCallbacksImpl> callbacks,
     std::unique_ptr<IndexedDBDatabaseCallbacksImpl> database_callbacks,
     const url::Origin& origin) {
-  GetService()->Open(GetCallbacksProxy(std::move(callbacks)),
-                     GetDatabaseCallbacksProxy(std::move(database_callbacks)),
-                     origin, name, version, transaction_id);
+  factory_->Open(GetCallbacksProxy(std::move(callbacks)),
+                 GetDatabaseCallbacksProxy(std::move(database_callbacks)),
+                 origin, name, version, transaction_id);
 }
 
 void WebIDBFactoryImpl::IOThreadHelper::DeleteDatabase(
@@ -170,8 +168,8 @@
     std::unique_ptr<IndexedDBCallbacksImpl> callbacks,
     const url::Origin& origin,
     bool force_close) {
-  GetService()->DeleteDatabase(GetCallbacksProxy(std::move(callbacks)), origin,
-                               name, force_close);
+  factory_->DeleteDatabase(GetCallbacksProxy(std::move(callbacks)), origin,
+                           name, force_close);
 }
 
 }  // namespace content
diff --git a/content/renderer/indexed_db/webidbfactory_impl.h b/content/renderer/indexed_db/webidbfactory_impl.h
index 5195900..2dfeca4 100644
--- a/content/renderer/indexed_db/webidbfactory_impl.h
+++ b/content/renderer/indexed_db/webidbfactory_impl.h
@@ -17,15 +17,11 @@
 class WebString;
 }
 
-namespace IPC {
-class SyncMessageFilter;
-}
-
 namespace content {
 
 class WebIDBFactoryImpl : public blink::WebIDBFactory {
  public:
-  WebIDBFactoryImpl(scoped_refptr<IPC::SyncMessageFilter> sync_message_filter,
+  WebIDBFactoryImpl(indexed_db::mojom::FactoryPtrInfo factory_info,
                     scoped_refptr<base::SingleThreadTaskRunner> io_runner);
   ~WebIDBFactoryImpl() override;
 
diff --git a/content/renderer/renderer_blink_platform_impl.cc b/content/renderer/renderer_blink_platform_impl.cc
index f91cc4b..ede9e6c 100644
--- a/content/renderer/renderer_blink_platform_impl.cc
+++ b/content/renderer/renderer_blink_platform_impl.cc
@@ -33,6 +33,7 @@
 #include "content/child/thread_safe_sender.h"
 #include "content/common/frame_messages.h"
 #include "content/common/gpu_stream_constants.h"
+#include "content/common/indexed_db/indexed_db.mojom.h"
 #include "content/common/render_message_filter.mojom.h"
 #include "content/public/common/content_features.h"
 #include "content/public/common/content_switches.h"
@@ -74,8 +75,6 @@
 #include "gpu/command_buffer/client/gles2_interface.h"
 #include "gpu/config/gpu_info.h"
 #include "gpu/ipc/client/gpu_channel_host.h"
-#include "ipc/ipc_sync_channel.h"
-#include "ipc/ipc_sync_message_filter.h"
 #include "media/audio/audio_output_device.h"
 #include "media/blink/webcontentdecryptionmodule_impl.h"
 #include "media/filters/stream_parser_factory.h"
@@ -258,12 +257,8 @@
                      ->GetServiceManagerConnection()
                      ->GetConnector()
                      ->Clone();
-    sync_message_filter_ = RenderThreadImpl::current()->sync_message_filter();
     thread_safe_sender_ = RenderThreadImpl::current()->thread_safe_sender();
     blob_registry_.reset(new WebBlobRegistryImpl(thread_safe_sender_.get()));
-    web_idb_factory_.reset(new WebIDBFactoryImpl(
-        sync_message_filter_,
-        RenderThreadImpl::current()->GetIOTaskRunner().get()));
 #if defined(OS_LINUX)
     font_loader_ = sk_make_sp<font_service::FontLoader>(connector_.get());
     SkFontConfigInterface::SetGlobal(font_loader_);
@@ -293,6 +288,16 @@
 
   GetInterfaceProvider()->GetInterface(
       mojo::MakeRequest(&web_database_host_info_));
+
+  // RenderThread may not exist in some tests.
+  if (RenderThreadImpl::current()) {
+    indexed_db::mojom::FactoryPtrInfo web_idb_factory_host_info;
+    GetInterfaceProvider()->GetInterface(
+        mojo::MakeRequest(&web_idb_factory_host_info));
+    web_idb_factory_.reset(new WebIDBFactoryImpl(
+        std::move(web_idb_factory_host_info),
+        RenderThreadImpl::current()->GetIOTaskRunner()));
+  }
 }
 
 RendererBlinkPlatformImpl::~RendererBlinkPlatformImpl() {
diff --git a/content/renderer/renderer_blink_platform_impl.h b/content/renderer/renderer_blink_platform_impl.h
index 98afd27..54f59bc 100644
--- a/content/renderer/renderer_blink_platform_impl.h
+++ b/content/renderer/renderer_blink_platform_impl.h
@@ -34,10 +34,6 @@
 #include "third_party/skia/include/core/SkRefCnt.h"           // nogncheck
 #endif
 
-namespace IPC {
-class SyncMessageFilter;
-}
-
 namespace blink {
 namespace scheduler {
 class WebThreadScheduler;
@@ -274,7 +270,6 @@
   std::unique_ptr<blink::WebBlobRegistry> blob_registry_;
 
   scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
-  scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
   scoped_refptr<ThreadSafeSender> thread_safe_sender_;
 
   std::unique_ptr<WebDatabaseObserverImpl> web_database_observer_impl_;