Revert of Mojo bindings environment: remove usage in gles2. (patchset #1 id:1 of https://codereview.chromium.org/1725123002/ )

Reason for revert:
This causes crashes: https://bugs.chromium.org/p/chromium/issues/detail?id=589964

The shell and the application belong to different modules, so they have different MessageLoop::current() / MessagePumpMojo::current() / etc. Although these values are in thread-local storage, we refer to them using global variables which are different in different modules.

Original issue's description:
> Mojo bindings environment: remove usage in gles2.
>
> BUG=585942
>
> Committed: https://crrev.com/67adb0d1a11573b9d0bd4fbe144f5e979327009d
> Cr-Commit-Position: refs/heads/master@{#377314}

TBR=sky@chromium.org
# Not skipping CQ checks because original CL landed more than 1 days ago.
BUG=585942

Review URL: https://codereview.chromium.org/1732373003

Cr-Commit-Position: refs/heads/master@{#377822}


CrOS-Libchrome-Original-Commit: 3f372c3212400b0a26cb6a3d6d28fa1d6584e3f2
diff --git a/mojo/gles2/command_buffer_client_impl.cc b/mojo/gles2/command_buffer_client_impl.cc
index fecd6aa..4813c42 100644
--- a/mojo/gles2/command_buffer_client_impl.cc
+++ b/mojo/gles2/command_buffer_client_impl.cc
@@ -68,6 +68,7 @@
 CommandBufferClientImpl::CommandBufferClientImpl(
     CommandBufferDelegate* delegate,
     const std::vector<int32_t>& attribs,
+    const MojoAsyncWaiter* async_waiter,
     mojo::ScopedMessagePipeHandle command_buffer_handle)
     : delegate_(delegate),
       attribs_(attribs),
@@ -78,9 +79,11 @@
       next_transfer_buffer_id_(0),
       next_image_id_(0),
       next_fence_sync_release_(1),
-      flushed_fence_sync_release_(0) {
+      flushed_fence_sync_release_(0),
+      async_waiter_(async_waiter) {
   command_buffer_.Bind(mojo::InterfacePtrInfo<mus::mojom::CommandBuffer>(
-      std::move(command_buffer_handle), 0u));
+                           std::move(command_buffer_handle), 0u),
+                       async_waiter);
   command_buffer_.set_connection_error_handler(
       [this]() { Destroyed(gpu::error::kUnknown, gpu::error::kLostContext); });
 }
@@ -101,7 +104,7 @@
   shared_state()->Initialize();
 
   mus::mojom::CommandBufferClientPtr client_ptr;
-  client_binding_.Bind(GetProxy(&client_ptr));
+  client_binding_.Bind(GetProxy(&client_ptr), async_waiter_);
 
   mus::mojom::CommandBufferInitializeResultPtr initialize_result;
   command_buffer_->Initialize(
diff --git a/mojo/gles2/command_buffer_client_impl.h b/mojo/gles2/command_buffer_client_impl.h
index 995ff1c..473c228 100644
--- a/mojo/gles2/command_buffer_client_impl.h
+++ b/mojo/gles2/command_buffer_client_impl.h
@@ -41,6 +41,7 @@
   explicit CommandBufferClientImpl(
       CommandBufferDelegate* delegate,
       const std::vector<int32_t>& attribs,
+      const MojoAsyncWaiter* async_waiter,
       mojo::ScopedMessagePipeHandle command_buffer_handle);
   ~CommandBufferClientImpl() override;
 
@@ -114,6 +115,8 @@
 
   uint64_t next_fence_sync_release_;
   uint64_t flushed_fence_sync_release_;
+
+  const MojoAsyncWaiter* async_waiter_;
 };
 
 }  // gles2
diff --git a/mojo/gles2/gles2_context.cc b/mojo/gles2/gles2_context.cc
index 90ba14e..c9c5a81 100644
--- a/mojo/gles2/gles2_context.cc
+++ b/mojo/gles2/gles2_context.cc
@@ -25,10 +25,14 @@
 }
 
 GLES2Context::GLES2Context(const std::vector<int32_t>& attribs,
+                           const MojoAsyncWaiter* async_waiter,
                            mojo::ScopedMessagePipeHandle command_buffer_handle,
                            MojoGLES2ContextLost lost_callback,
                            void* closure)
-    : command_buffer_(this, attribs, std::move(command_buffer_handle)),
+    : command_buffer_(this,
+                      attribs,
+                      async_waiter,
+                      std::move(command_buffer_handle)),
       lost_callback_(lost_callback),
       closure_(closure) {}
 
diff --git a/mojo/gles2/gles2_context.h b/mojo/gles2/gles2_context.h
index 684b485..ecb7af5 100644
--- a/mojo/gles2/gles2_context.h
+++ b/mojo/gles2/gles2_context.h
@@ -31,6 +31,7 @@
                      public MojoGLES2ContextPrivate {
  public:
   explicit GLES2Context(const std::vector<int32_t>& attribs,
+                        const MojoAsyncWaiter* async_waiter,
                         mojo::ScopedMessagePipeHandle command_buffer_handle,
                         MojoGLES2ContextLost lost_callback,
                         void* closure);
diff --git a/mojo/gles2/gles2_impl.cc b/mojo/gles2/gles2_impl.cc
index c1875f4..bb117f5 100644
--- a/mojo/gles2/gles2_impl.cc
+++ b/mojo/gles2/gles2_impl.cc
@@ -31,7 +31,8 @@
 MojoGLES2Context MojoGLES2CreateContext(MojoHandle handle,
                                         const int32_t* attrib_list,
                                         MojoGLES2ContextLost lost_callback,
-                                        void* closure) {
+                                        void* closure,
+                                        const MojoAsyncWaiter* async_waiter) {
   mojo::MessagePipeHandle mph(handle);
   mojo::ScopedMessagePipeHandle scoped_handle(mph);
   std::vector<int32_t> attribs;
@@ -43,7 +44,7 @@
   }
   attribs.push_back(kNone);
   scoped_ptr<GLES2Context> client(new GLES2Context(
-      attribs, std::move(scoped_handle), lost_callback, closure));
+      attribs, async_waiter, std::move(scoped_handle), lost_callback, closure));
   if (!client->Initialize())
     client.reset();
   return client.release();
diff --git a/mojo/public/c/gles2/gles2.h b/mojo/public/c/gles2/gles2.h
index 6c0b44e..247084d 100644
--- a/mojo/public/c/gles2/gles2.h
+++ b/mojo/public/c/gles2/gles2.h
@@ -10,6 +10,7 @@
 #include <GLES2/gl2.h>
 #include <stdint.h>
 
+#include "mojo/public/c/environment/async_waiter.h"
 #include "mojo/public/c/gles2/gles2_export.h"
 #include "mojo/public/c/gles2/gles2_types.h"
 #include "mojo/public/c/system/types.h"
@@ -19,10 +20,11 @@
 #endif
 
 MOJO_GLES2_EXPORT MojoGLES2Context
-MojoGLES2CreateContext(MojoHandle handle,
-                       const int32_t* attrib_list,
-                       MojoGLES2ContextLost lost_callback,
-                       void* closure);
+    MojoGLES2CreateContext(MojoHandle handle,
+                           const int32_t* attrib_list,
+                           MojoGLES2ContextLost lost_callback,
+                           void* closure,
+                           const MojoAsyncWaiter* async_waiter);
 MOJO_GLES2_EXPORT void MojoGLES2DestroyContext(MojoGLES2Context context);
 MOJO_GLES2_EXPORT void MojoGLES2MakeCurrent(MojoGLES2Context context);
 MOJO_GLES2_EXPORT void MojoGLES2SwapBuffers(void);
diff --git a/mojo/public/platform/native/gles2_thunks.cc b/mojo/public/platform/native/gles2_thunks.cc
index 6d84279..2350fce 100644
--- a/mojo/public/platform/native/gles2_thunks.cc
+++ b/mojo/public/platform/native/gles2_thunks.cc
@@ -17,10 +17,11 @@
 MojoGLES2Context MojoGLES2CreateContext(MojoHandle handle,
                                         const int32_t* attrib_list,
                                         MojoGLES2ContextLost lost_callback,
-                                        void* closure) {
+                                        void* closure,
+                                        const MojoAsyncWaiter* async_waiter) {
   assert(g_control_thunks.GLES2CreateContext);
-  return g_control_thunks.GLES2CreateContext(handle, attrib_list, lost_callback,
-                                             closure);
+  return g_control_thunks.GLES2CreateContext(
+      handle, attrib_list, lost_callback, closure, async_waiter);
 }
 
 void MojoGLES2DestroyContext(MojoGLES2Context context) {
diff --git a/mojo/public/platform/native/gles2_thunks.h b/mojo/public/platform/native/gles2_thunks.h
index 4a56550..cd9cc75 100644
--- a/mojo/public/platform/native/gles2_thunks.h
+++ b/mojo/public/platform/native/gles2_thunks.h
@@ -22,7 +22,8 @@
   MojoGLES2Context (*GLES2CreateContext)(MojoHandle handle,
                                          const int32_t* attrib_list,
                                          MojoGLES2ContextLost lost_callback,
-                                         void* closure);
+                                         void* closure,
+                                         const MojoAsyncWaiter* async_waiter);
   void (*GLES2DestroyContext)(MojoGLES2Context context);
   void (*GLES2MakeCurrent)(MojoGLES2Context context);
   void (*GLES2SwapBuffers)();