Support early associated interface binding on ChannelMojo

Changes the associated bindings implementation for ChannelMojo
such that remote interfaces can be acquired immediately upon
ChannelMojo construction rather than having to wait for connection
on the IO thread.

Simplifies the Channel bootstrapping process, removing a round-trip
Init message (and in fact the entire IPC::mojom::Boostrap interface)
since there's no need to actually exchange associated interface handles
over the pipe. Instead both sides can assume the other will use a fixed,
reserved endpoint ID for their IPC::mojom::Channel interface.

This also removes the restriction that associated interfaces must be
added to a Channel after Init. Instead the same constraints apply as
with AddFilter: an associated interface, like a filter, may be added
at any time as long as either Init hasn't been called OR the remote
process hasn't been launched.

The result of this CL is that any place it's safe to AddFilter,
it's also safe to AddAssociatedInterface; and any place it's safe to
Send, it's also safe to GetRemoteAssociatedInterface and begin using
any such remote interface immediately.

Remote interface requests as well as all messages to remote interfaces
retain FIFO with respect to any Send calls on the same thread. Local
interface request dispatch as well as all messages on locally bound
associated interfaces retain FIFO with respect to any OnMessageReceived
calls on the same thread.

BUG=612500,619202

Committed: https://crrev.com/e1037f997da9e1d44ca3b09d4ff32f0465673091
Committed: https://crrev.com/508da24622f957a01b076ccd058bfdccc79068a4
Review-Url: https://codereview.chromium.org/2163633003
Cr-Original-Original-Commit-Position: refs/heads/master@{#406720}
Cr-Original-Commit-Position: refs/heads/master@{#407050}
Cr-Commit-Position: refs/heads/master@{#407264}


CrOS-Libchrome-Original-Commit: 0e4de5f9a519c6cd206448a10eccc7a535e3db64
diff --git a/ipc/ipc_channel_mojo.h b/ipc/ipc_channel_mojo.h
index 940dcf0..cfadb50 100644
--- a/ipc/ipc_channel_mojo.h
+++ b/ipc/ipc_channel_mojo.h
@@ -42,7 +42,7 @@
 class IPC_EXPORT ChannelMojo
     : public Channel,
       public Channel::AssociatedInterfaceSupport,
-      public MojoBootstrap::Delegate,
+      public NON_EXPORTED_BASE(MojoBootstrap::Delegate),
       public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) {
  public:
   // Creates a ChannelMojo.
@@ -90,18 +90,17 @@
       mojo::Array<mojom::SerializedHandlePtr>* handles);
 
   // MojoBootstrapDelegate implementation
-  void OnPipesAvailable(mojom::ChannelAssociatedPtrInfo send_channel,
-                        mojom::ChannelAssociatedRequest receive_channel,
-                        int32_t peer_pid) override;
-  void OnBootstrapError() override;
+  void OnPipesAvailable(mojom::ChannelAssociatedPtr sender,
+                        mojom::ChannelAssociatedRequest receiver) override;
+
+  // MessagePipeReader::Delegate
+  void OnPeerPidReceived() override;
+  void OnMessageReceived(const Message& message) override;
+  void OnPipeError() override;
   void OnAssociatedInterfaceRequest(
       const std::string& name,
       mojo::ScopedInterfaceEndpointHandle handle) override;
 
-  // MessagePipeReader::Delegate
-  void OnMessageReceived(const Message& message) override;
-  void OnPipeError() override;
-
  private:
   ChannelMojo(
       mojo::ScopedMessagePipeHandle handle,
@@ -109,10 +108,6 @@
       Listener* listener,
       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
 
-  void InitMessageReader(mojom::ChannelAssociatedPtrInfo sender,
-                         mojom::ChannelAssociatedRequest receiver,
-                         base::ProcessId peer_pid);
-
   // Channel::AssociatedInterfaceSupport:
   mojo::AssociatedGroup* GetAssociatedGroup() override;
   void AddGenericAssociatedInterface(
@@ -121,13 +116,6 @@
   void GetGenericRemoteAssociatedInterface(
       const std::string& name,
       mojo::ScopedInterfaceEndpointHandle handle) override;
-  void SetProxyTaskRunner(
-      scoped_refptr<base::SingleThreadTaskRunner> task_runner) override;
-
-  // ChannelMojo needs to kill its MessagePipeReader in delayed manner
-  // because the channel wants to kill these readers during the
-  // notifications invoked by them.
-  typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
 
   // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
   scoped_refptr<base::TaskRunner> task_runner_;
@@ -136,15 +124,12 @@
   std::unique_ptr<MojoBootstrap> bootstrap_;
   Listener* listener_;
 
+  std::unique_ptr<internal::MessagePipeReader> message_reader_;
+
+  base::Lock associated_interface_lock_;
   std::map<std::string, GenericAssociatedInterfaceFactory>
       associated_interfaces_;
 
-  // Guards access to the fields below.
-  mutable base::Lock lock_;
-  std::unique_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_;
-  std::vector<std::unique_ptr<Message>> pending_messages_;
-  bool waiting_connect_;
-
   base::WeakPtrFactory<ChannelMojo> weak_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(ChannelMojo);