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/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_;