Use per-frame task runners instead of per-thread task runners at pepper module

This CL is based on Alexander's CL: https://chromium-review.googlesource.com/c/chromium/src/+/1367329

Bug: 870606
Change-Id: I2ca696efeb203d7cb48c9f33034163c2d9c6277d
Reviewed-on: https://chromium-review.googlesource.com/c/1379607
Reviewed-by: Kentaro Hara <[email protected]>
Reviewed-by: Alexander Timin <[email protected]>
Reviewed-by: Bill Budge <[email protected]>
Commit-Queue: Hajime Hoshi <[email protected]>
Cr-Commit-Position: refs/heads/master@{#620986}
diff --git a/content/renderer/pepper/host_dispatcher_wrapper.cc b/content/renderer/pepper/host_dispatcher_wrapper.cc
index b9c04fca..b8f882b3 100644
--- a/content/renderer/pepper/host_dispatcher_wrapper.cc
+++ b/content/renderer/pepper/host_dispatcher_wrapper.cc
@@ -33,10 +33,12 @@
 
 HostDispatcherWrapper::~HostDispatcherWrapper() {}
 
-bool HostDispatcherWrapper::Init(const IPC::ChannelHandle& channel_handle,
-                                 PP_GetInterface_Func local_get_interface,
-                                 const ppapi::Preferences& preferences,
-                                 scoped_refptr<PepperHungPluginFilter> filter) {
+bool HostDispatcherWrapper::Init(
+    const IPC::ChannelHandle& channel_handle,
+    PP_GetInterface_Func local_get_interface,
+    const ppapi::Preferences& preferences,
+    scoped_refptr<PepperHungPluginFilter> filter,
+    scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
   if (!channel_handle.is_mojo_channel_handle())
     return false;
 
@@ -50,11 +52,10 @@
   // Guarantee the hung_plugin_filter_ outlives |dispatcher_|.
   hung_plugin_filter_ = filter;
 
-  if (!dispatcher_->InitHostWithChannel(dispatcher_delegate_.get(),
-                                        peer_pid_,
+  if (!dispatcher_->InitHostWithChannel(dispatcher_delegate_.get(), peer_pid_,
                                         channel_handle,
                                         true,  // Client.
-                                        preferences)) {
+                                        preferences, task_runner)) {
     dispatcher_.reset();
     dispatcher_delegate_.reset();
     return false;
diff --git a/content/renderer/pepper/host_dispatcher_wrapper.h b/content/renderer/pepper/host_dispatcher_wrapper.h
index f308ffbd..bea8527 100644
--- a/content/renderer/pepper/host_dispatcher_wrapper.h
+++ b/content/renderer/pepper/host_dispatcher_wrapper.h
@@ -36,7 +36,8 @@
   bool Init(const IPC::ChannelHandle& channel_handle,
             PP_GetInterface_Func local_get_interface,
             const ppapi::Preferences& preferences,
-            scoped_refptr<PepperHungPluginFilter> filter);
+            scoped_refptr<PepperHungPluginFilter> filter,
+            scoped_refptr<base::SingleThreadTaskRunner> task_runner);
 
   // Implements GetInterface for the proxied plugin.
   const void* GetProxiedInterface(const char* name);
diff --git a/content/renderer/pepper/pepper_plugin_instance_impl.cc b/content/renderer/pepper/pepper_plugin_instance_impl.cc
index 85176ba..d2f5b3ca 100644
--- a/content/renderer/pepper/pepper_plugin_instance_impl.cc
+++ b/content/renderer/pepper/pepper_plugin_instance_impl.cc
@@ -3121,13 +3121,10 @@
       module_->CreateModuleForExternalPluginInstance());
 
   RendererPpapiHostImpl* renderer_ppapi_host =
-      external_plugin_module->CreateOutOfProcessModule(render_frame_,
-                                                       file_path,
-                                                       permissions,
-                                                       channel_handle,
-                                                       plugin_pid,
-                                                       plugin_child_id,
-                                                       true);
+      external_plugin_module->CreateOutOfProcessModule(
+          render_frame_, file_path, permissions, channel_handle, plugin_pid,
+          plugin_child_id, true,
+          render_frame_->GetTaskRunner(blink::TaskType::kInternalDefault));
   if (!renderer_ppapi_host) {
     DLOG(ERROR) << "CreateExternalPluginModule() failed";
     return PP_EXTERNAL_PLUGIN_ERROR_MODULE;
diff --git a/content/renderer/pepper/plugin_module.cc b/content/renderer/pepper/plugin_module.cc
index 1cf8307..79c84a7 100644
--- a/content/renderer/pepper/plugin_module.cc
+++ b/content/renderer/pepper/plugin_module.cc
@@ -712,7 +712,8 @@
     const IPC::ChannelHandle& channel_handle,
     base::ProcessId peer_pid,
     int plugin_child_id,
-    bool is_external) {
+    bool is_external,
+    scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
   scoped_refptr<PepperHungPluginFilter> hung_filter(new PepperHungPluginFilter(
       path, render_frame->GetRoutingID(), plugin_child_id));
   std::unique_ptr<HostDispatcherWrapper> dispatcher(new HostDispatcherWrapper(
@@ -732,7 +733,7 @@
                         ppapi::Preferences(PpapiPreferencesBuilder::Build(
                             render_frame->render_view()->webkit_preferences(),
                             gpu_feature_info)),
-                        hung_filter.get())) {
+                        hung_filter.get(), task_runner)) {
     return nullptr;
   }
 
@@ -769,7 +770,8 @@
     RenderFrameImpl* render_frame,
     const WebPluginInfo& webplugin_info,
     const base::Optional<url::Origin>& origin_lock,
-    bool* pepper_plugin_was_registered) {
+    bool* pepper_plugin_was_registered,
+    scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
   *pepper_plugin_was_registered = true;
 
   // See if a module has already been loaded for this plugin.
@@ -818,13 +820,10 @@
   PepperPluginRegistry::GetInstance()->AddLiveModule(path, origin_lock,
                                                      module.get());
 
-  if (!module->CreateOutOfProcessModule(render_frame,
-                                        path,
-                                        permissions,
-                                        channel_handle,
-                                        peer_pid,
-                                        plugin_child_id,
-                                        false))  // is_external = false
+  if (!module->CreateOutOfProcessModule(render_frame, path, permissions,
+                                        channel_handle, peer_pid,
+                                        plugin_child_id, false,
+                                        task_runner))  // is_external = false
     return scoped_refptr<PluginModule>();
 
   return module;
diff --git a/content/renderer/pepper/plugin_module.h b/content/renderer/pepper/plugin_module.h
index a8dcfab..57fadde 100644
--- a/content/renderer/pepper/plugin_module.h
+++ b/content/renderer/pepper/plugin_module.h
@@ -17,6 +17,7 @@
 #include "base/native_library.h"
 #include "base/optional.h"
 #include "base/process/process.h"
+#include "base/single_thread_task_runner.h"
 #include "content/common/content_export.h"
 #include "content/public/common/pepper_plugin_info.h"
 #include "ppapi/c/pp_bool.h"
@@ -195,7 +196,8 @@
       const IPC::ChannelHandle& channel_handle,
       base::ProcessId plugin_pid,
       int plugin_child_id,
-      bool is_external);
+      bool is_external,
+      scoped_refptr<base::SingleThreadTaskRunner> task_runner);
 
   // In production we purposely leak the HostGlobals object but in unittest
   // code, this can interfere with subsequent tests. This deletes the
@@ -216,7 +218,8 @@
       RenderFrameImpl* render_frame,
       const WebPluginInfo& webplugin_info,
       const base::Optional<url::Origin>& origin_lock,
-      bool* pepper_plugin_was_registered);
+      bool* pepper_plugin_was_registered,
+      scoped_refptr<base::SingleThreadTaskRunner> task_runner);
 
  private:
   friend class base::RefCounted<PluginModule>;
diff --git a/content/renderer/render_frame_impl.cc b/content/renderer/render_frame_impl.cc
index f25ea89dd..d3d99b6 100644
--- a/content/renderer/render_frame_impl.cc
+++ b/content/renderer/render_frame_impl.cc
@@ -2979,7 +2979,8 @@
 
   bool pepper_plugin_was_registered = false;
   scoped_refptr<PluginModule> pepper_module(PluginModule::Create(
-      this, info, origin_lock, &pepper_plugin_was_registered));
+      this, info, origin_lock, &pepper_plugin_was_registered,
+      GetTaskRunner(blink::TaskType::kInternalDefault)));
   if (pepper_plugin_was_registered) {
     if (pepper_module.get()) {
       return new PepperWebPluginImpl(
diff --git a/ppapi/proxy/broker_dispatcher.cc b/ppapi/proxy/broker_dispatcher.cc
index 7187852..37734a1 100644
--- a/ppapi/proxy/broker_dispatcher.cc
+++ b/ppapi/proxy/broker_dispatcher.cc
@@ -25,7 +25,8 @@
     const IPC::ChannelHandle& channel_handle,
     bool is_client) {
   return ProxyChannel::InitWithChannel(delegate, peer_pid, channel_handle,
-                                       is_client);
+                                       is_client,
+                                       base::ThreadTaskRunnerHandle::Get());
 }
 
 bool BrokerDispatcher::OnMessageReceived(const IPC::Message& msg) {
diff --git a/ppapi/proxy/host_dispatcher.cc b/ppapi/proxy/host_dispatcher.cc
index 3eb4b87a..3a7562d0 100644
--- a/ppapi/proxy/host_dispatcher.cc
+++ b/ppapi/proxy/host_dispatcher.cc
@@ -91,9 +91,10 @@
     base::ProcessId peer_pid,
     const IPC::ChannelHandle& channel_handle,
     bool is_client,
-    const ppapi::Preferences& preferences) {
+    const ppapi::Preferences& preferences,
+    scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
   if (!Dispatcher::InitWithChannel(delegate, peer_pid, channel_handle,
-                                   is_client))
+                                   is_client, task_runner))
     return false;
   Send(new PpapiMsg_SetPreferences(preferences));
   return true;
diff --git a/ppapi/proxy/host_dispatcher.h b/ppapi/proxy/host_dispatcher.h
index 47d1f34..2a64556 100644
--- a/ppapi/proxy/host_dispatcher.h
+++ b/ppapi/proxy/host_dispatcher.h
@@ -61,11 +61,13 @@
   // You must call this function before anything else. Returns true on success.
   // The delegate pointer must outlive this class, ownership is not
   // transferred.
-  virtual bool InitHostWithChannel(Delegate* delegate,
-                                   base::ProcessId peer_pid,
-                                   const IPC::ChannelHandle& channel_handle,
-                                   bool is_client,
-                                   const Preferences& preferences);
+  virtual bool InitHostWithChannel(
+      Delegate* delegate,
+      base::ProcessId peer_pid,
+      const IPC::ChannelHandle& channel_handle,
+      bool is_client,
+      const Preferences& preferences,
+      scoped_refptr<base::SingleThreadTaskRunner> task_runner);
 
   // The host side maintains a mapping from PP_Instance to Dispatcher so
   // that we can send the messages to the right channel.
diff --git a/ppapi/proxy/plugin_dispatcher.cc b/ppapi/proxy/plugin_dispatcher.cc
index 67c9c3b..cdac7403 100644
--- a/ppapi/proxy/plugin_dispatcher.cc
+++ b/ppapi/proxy/plugin_dispatcher.cc
@@ -217,7 +217,8 @@
     const IPC::ChannelHandle& channel_handle,
     bool is_client) {
   if (!Dispatcher::InitWithChannel(delegate, peer_pid, channel_handle,
-                                   is_client))
+                                   is_client,
+                                   base::ThreadTaskRunnerHandle::Get()))
     return false;
   plugin_delegate_ = delegate;
   plugin_dispatcher_id_ = plugin_delegate_->Register(this);
diff --git a/ppapi/proxy/ppapi_proxy_test.cc b/ppapi/proxy/ppapi_proxy_test.cc
index 0b76f79a..8d33951 100644
--- a/ppapi/proxy/ppapi_proxy_test.cc
+++ b/ppapi/proxy/ppapi_proxy_test.cc
@@ -465,9 +465,9 @@
       &MockGetInterface,
       PpapiPermissions::AllPermissions()));
   ppapi::Preferences preferences;
-  host_dispatcher_->InitHostWithChannel(&delegate_mock_,
-                                        base::kNullProcessId, channel_handle,
-                                        is_client, preferences);
+  host_dispatcher_->InitHostWithChannel(&delegate_mock_, base::kNullProcessId,
+                                        channel_handle, is_client, preferences,
+                                        base::ThreadTaskRunnerHandle::Get());
   HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
 }
 
diff --git a/ppapi/proxy/proxy_channel.cc b/ppapi/proxy/proxy_channel.cc
index 6aa566f..990df28 100644
--- a/ppapi/proxy/proxy_channel.cc
+++ b/ppapi/proxy/proxy_channel.cc
@@ -26,18 +26,21 @@
   DVLOG(1) << "ProxyChannel::~ProxyChannel()";
 }
 
-bool ProxyChannel::InitWithChannel(Delegate* delegate,
-                                   base::ProcessId peer_pid,
-                                   const IPC::ChannelHandle& channel_handle,
-                                   bool is_client) {
+bool ProxyChannel::InitWithChannel(
+    Delegate* delegate,
+    base::ProcessId peer_pid,
+    const IPC::ChannelHandle& channel_handle,
+    bool is_client,
+    scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
   delegate_ = delegate;
   peer_pid_ = peer_pid;
   IPC::Channel::Mode mode = is_client
       ? IPC::Channel::MODE_CLIENT
       : IPC::Channel::MODE_SERVER;
-  channel_ = IPC::SyncChannel::Create(
-      channel_handle, mode, this, delegate->GetIPCTaskRunner(),
-      base::ThreadTaskRunnerHandle::Get(), true, delegate->GetShutdownEvent());
+  DCHECK(task_runner->BelongsToCurrentThread());
+  channel_ = IPC::SyncChannel::Create(channel_handle, mode, this,
+                                      delegate->GetIPCTaskRunner(), task_runner,
+                                      true, delegate->GetShutdownEvent());
   return true;
 }
 
diff --git a/ppapi/proxy/proxy_channel.h b/ppapi/proxy/proxy_channel.h
index ca41f58..6987b39 100644
--- a/ppapi/proxy/proxy_channel.h
+++ b/ppapi/proxy/proxy_channel.h
@@ -124,10 +124,12 @@
   // You must call this function before anything else. Returns true on success.
   // The delegate pointer must outlive this class, ownership is not
   // transferred.
-  virtual bool InitWithChannel(Delegate* delegate,
-                               base::ProcessId peer_pid,
-                               const IPC::ChannelHandle& channel_handle,
-                               bool is_client);
+  virtual bool InitWithChannel(
+      Delegate* delegate,
+      base::ProcessId peer_pid,
+      const IPC::ChannelHandle& channel_handle,
+      bool is_client,
+      scoped_refptr<base::SingleThreadTaskRunner> task_runner);
 
   ProxyChannel::Delegate* delegate() const {
     return delegate_;