Mojo C++ bindings: fix a DCHECK in multiplex_router.h

BUG=None

Review-Url: https://codereview.chromium.org/2025053002
Cr-Commit-Position: refs/heads/master@{#396903}


CrOS-Libchrome-Original-Commit: 542452b027f688a5a02070dcc41ed7fcc9d057b8
diff --git a/mojo/public/cpp/bindings/lib/multiplex_router.cc b/mojo/public/cpp/bindings/lib/multiplex_router.cc
index d081b01..2f547bc 100644
--- a/mojo/public/cpp/bindings/lib/multiplex_router.cc
+++ b/mojo/public/cpp/bindings/lib/multiplex_router.cc
@@ -702,8 +702,6 @@
   bool inserted = false;
   InterfaceEndpoint* endpoint = FindOrInsertEndpoint(id, &inserted);
   if (inserted) {
-    DCHECK(!IsMasterInterfaceId(id));
-
     // Currently, it is legitimate to receive messages for an endpoint
     // that is not registered. For example, the endpoint is transferred in
     // a message that is discarded. Once we add support to specify all
@@ -711,7 +709,16 @@
     // this.
     UpdateEndpointStateMayRemove(endpoint, ENDPOINT_CLOSED);
 
-    control_message_proxy_.NotifyPeerEndpointClosed(id);
+    // It is also possible that this newly-inserted endpoint is the master
+    // endpoint. When the master InterfacePtr/Binding goes away, the message
+    // pipe is closed and we explicitly trigger a pipe connection error. The
+    // error updates all the endpoints, including the master endpoint, with
+    // PEER_ENDPOINT_CLOSED and removes the master endpoint from the
+    // registration. We continue to process remaining tasks in the queue, as
+    // long as there are refs keeping the router alive. If there are remaining
+    // messages for the master endpoint, we will get here.
+    if (!IsMasterInterfaceId(id))
+      control_message_proxy_.NotifyPeerEndpointClosed(id);
     return true;
   }
 
diff --git a/mojo/public/cpp/bindings/lib/pipe_control_message_proxy.cc b/mojo/public/cpp/bindings/lib/pipe_control_message_proxy.cc
index 492bc02..a7f340f 100644
--- a/mojo/public/cpp/bindings/lib/pipe_control_message_proxy.cc
+++ b/mojo/public/cpp/bindings/lib/pipe_control_message_proxy.cc
@@ -8,6 +8,7 @@
 #include <utility>
 
 #include "base/compiler_specific.h"
+#include "base/logging.h"
 #include "mojo/public/cpp/bindings/lib/message_builder.h"
 #include "mojo/public/cpp/bindings/lib/serialization.h"
 #include "mojo/public/cpp/bindings/message.h"
@@ -46,6 +47,7 @@
     : receiver_(receiver) {}
 
 void PipeControlMessageProxy::NotifyPeerEndpointClosed(InterfaceId id) {
+  DCHECK(!IsMasterInterfaceId(id));
   pipe_control::PeerAssociatedEndpointClosedEventPtr event(
       pipe_control::PeerAssociatedEndpointClosedEvent::New());
   event->id = id;
@@ -58,6 +60,7 @@
 }
 
 void PipeControlMessageProxy::NotifyEndpointClosedBeforeSent(InterfaceId id) {
+  DCHECK(!IsMasterInterfaceId(id));
   pipe_control::AssociatedEndpointClosedBeforeSentEventPtr event(
       pipe_control::AssociatedEndpointClosedBeforeSentEvent::New());
   event->id = id;