| Index: mojo/public/cpp/bindings/binding.h
 | 
| diff --git a/mojo/public/cpp/bindings/binding.h b/mojo/public/cpp/bindings/binding.h
 | 
| index de9159fd32db3bcd0b894a4a84d4ec464987e18c..5d97f2862a940f37f3d39d5d0ff696101ca126e5 100644
 | 
| --- a/mojo/public/cpp/bindings/binding.h
 | 
| +++ b/mojo/public/cpp/bindings/binding.h
 | 
| @@ -5,19 +5,19 @@
 | 
|  #ifndef MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_
 | 
|  #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_
 | 
|  
 | 
| +#include "base/macros.h"
 | 
|  #include "mojo/public/c/environment/async_waiter.h"
 | 
|  #include "mojo/public/cpp/bindings/callback.h"
 | 
|  #include "mojo/public/cpp/bindings/interface_ptr.h"
 | 
|  #include "mojo/public/cpp/bindings/interface_ptr_info.h"
 | 
|  #include "mojo/public/cpp/bindings/interface_request.h"
 | 
| -#include "mojo/public/cpp/bindings/lib/filter_chain.h"
 | 
| -#include "mojo/public/cpp/bindings/lib/message_header_validator.h"
 | 
| -#include "mojo/public/cpp/bindings/lib/router.h"
 | 
| -#include "mojo/public/cpp/environment/logging.h"
 | 
| +#include "mojo/public/cpp/bindings/lib/binding_state.h"
 | 
|  #include "mojo/public/cpp/system/core.h"
 | 
|  
 | 
|  namespace mojo {
 | 
|  
 | 
| +class AssociatedGroup;
 | 
| +
 | 
|  // Represents the binding of an interface implementation to a message pipe.
 | 
|  // When the |Binding| object is destroyed, the binding between the message pipe
 | 
|  // and the interface is torn down and the message pipe is closed, leaving the
 | 
| @@ -62,7 +62,7 @@ class Binding {
 | 
|    // Constructs an incomplete binding that will use the implementation |impl|.
 | 
|    // The binding may be completed with a subsequent call to the |Bind| method.
 | 
|    // Does not take ownership of |impl|, which must outlive the binding.
 | 
| -  explicit Binding(Interface* impl) : impl_(impl) { stub_.set_sink(impl_); }
 | 
| +  explicit Binding(Interface* impl) : internal_state_(impl) {}
 | 
|  
 | 
|    // Constructs a completed binding of message pipe |handle| to implementation
 | 
|    // |impl|. Does not take ownership of |impl|, which must outlive the binding.
 | 
| @@ -100,10 +100,7 @@ class Binding {
 | 
|  
 | 
|    // Tears down the binding, closing the message pipe and leaving the interface
 | 
|    // implementation unbound.
 | 
| -  ~Binding() {
 | 
| -    if (internal_router_)
 | 
| -      Close();
 | 
| -  }
 | 
| +  ~Binding() {}
 | 
|  
 | 
|    // Completes a binding that was constructed with only an interface
 | 
|    // implementation. Takes ownership of |handle| and binds it to the previously
 | 
| @@ -111,16 +108,7 @@ class Binding {
 | 
|    void Bind(
 | 
|        ScopedMessagePipeHandle handle,
 | 
|        const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
 | 
| -    MOJO_DCHECK(!internal_router_);
 | 
| -    internal::FilterChain filters;
 | 
| -    filters.Append<internal::MessageHeaderValidator>();
 | 
| -    filters.Append<typename Interface::RequestValidator_>();
 | 
| -
 | 
| -    internal_router_ =
 | 
| -        new internal::Router(handle.Pass(), filters.Pass(), waiter);
 | 
| -    internal_router_->set_incoming_receiver(&stub_);
 | 
| -    internal_router_->set_connection_error_handler(
 | 
| -        [this]() { connection_error_handler_.Run(); });
 | 
| +    internal_state_.Bind(handle.Pass(), waiter);
 | 
|    }
 | 
|  
 | 
|    // Completes a binding that was constructed with only an interface
 | 
| @@ -154,84 +142,75 @@ class Binding {
 | 
|    // Outgoing messages are still sent.
 | 
|    //
 | 
|    // No errors are detected on the message pipe while paused.
 | 
| +  //
 | 
| +  // NOTE: Not supported (yet) if |Interface| has methods to pass associated
 | 
| +  // interface pointers/requests.
 | 
|    void PauseIncomingMethodCallProcessing() {
 | 
| -    MOJO_DCHECK(internal_router_);
 | 
| -    internal_router_->PauseIncomingMethodCallProcessing();
 | 
| +    internal_state_.PauseIncomingMethodCallProcessing();
 | 
|    }
 | 
| +  // NOTE: Not supported (yet) if |Interface| has methods to pass associated
 | 
| +  // interface pointers/requests.
 | 
|    void ResumeIncomingMethodCallProcessing() {
 | 
| -    MOJO_DCHECK(internal_router_);
 | 
| -    internal_router_->ResumeIncomingMethodCallProcessing();
 | 
| +    internal_state_.ResumeIncomingMethodCallProcessing();
 | 
|    }
 | 
|  
 | 
|    // Blocks the calling thread until either a call arrives on the previously
 | 
|    // bound message pipe, the deadline is exceeded, or an error occurs. Returns
 | 
|    // true if a method was successfully read and dispatched.
 | 
| +  //
 | 
| +  // NOTE: Not supported (yet) if |Interface| has methods to pass associated
 | 
| +  // interface pointers/requests.
 | 
|    bool WaitForIncomingMethodCall(
 | 
|        MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE) {
 | 
| -    MOJO_DCHECK(internal_router_);
 | 
| -    return internal_router_->WaitForIncomingMessage(deadline);
 | 
| +    return internal_state_.WaitForIncomingMethodCall(deadline);
 | 
|    }
 | 
|  
 | 
|    // Closes the message pipe that was previously bound. Put this object into a
 | 
|    // state where it can be rebound to a new pipe.
 | 
| -  void Close() {
 | 
| -    MOJO_DCHECK(internal_router_);
 | 
| -    internal_router_->CloseMessagePipe();
 | 
| -    DestroyRouter();
 | 
| -  }
 | 
| +  void Close() { internal_state_.Close(); }
 | 
|  
 | 
|    // Unbinds the underlying pipe from this binding and returns it so it can be
 | 
|    // used in another context, such as on another thread or with a different
 | 
|    // implementation. Put this object into a state where it can be rebound to a
 | 
|    // new pipe.
 | 
| -  InterfaceRequest<Interface> Unbind() {
 | 
| -    InterfaceRequest<Interface> request =
 | 
| -        MakeRequest<Interface>(internal_router_->PassMessagePipe());
 | 
| -    DestroyRouter();
 | 
| -    // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove
 | 
| -    // it once that's fixed.
 | 
| -    return request.Pass();
 | 
| -  }
 | 
| +  InterfaceRequest<Interface> Unbind() { return internal_state_.Unbind(); }
 | 
|  
 | 
|    // Sets an error handler that will be called if a connection error occurs on
 | 
|    // the bound message pipe.
 | 
|    void set_connection_error_handler(const Closure& error_handler) {
 | 
| -    connection_error_handler_ = error_handler;
 | 
| +    internal_state_.set_connection_error_handler(error_handler);
 | 
|    }
 | 
|  
 | 
|    // Returns the interface implementation that was previously specified. Caller
 | 
|    // does not take ownership.
 | 
| -  Interface* impl() { return impl_; }
 | 
| +  Interface* impl() { return internal_state_.impl(); }
 | 
|  
 | 
|    // Indicates whether the binding has been completed (i.e., whether a message
 | 
|    // pipe has been bound to the implementation).
 | 
| -  bool is_bound() const { return !!internal_router_; }
 | 
| +  bool is_bound() const { return internal_state_.is_bound(); }
 | 
|  
 | 
|    // Returns the value of the handle currently bound to this Binding which can
 | 
|    // be used to make explicit Wait/WaitMany calls. Requires that the Binding be
 | 
|    // bound. Ownership of the handle is retained by the Binding, it is not
 | 
|    // transferred to the caller.
 | 
| -  MessagePipeHandle handle() const {
 | 
| -    MOJO_DCHECK(is_bound());
 | 
| -    return internal_router_->handle();
 | 
| +  MessagePipeHandle handle() const { return internal_state_.handle(); }
 | 
| +
 | 
| +  // Returns the associated group that this object belongs to. Returns null if:
 | 
| +  //   - this object is not bound; or
 | 
| +  //   - the interface doesn't have methods to pass associated interface
 | 
| +  //     pointers or requests.
 | 
| +  AssociatedGroup* associated_group() {
 | 
| +    return internal_state_.associated_group();
 | 
|    }
 | 
|  
 | 
|    // Exposed for testing, should not generally be used.
 | 
| -  internal::Router* internal_router() { return internal_router_; }
 | 
| +  void EnableTestingMode() { internal_state_.EnableTestingMode(); }
 | 
|  
 | 
|   private:
 | 
| -  void DestroyRouter() {
 | 
| -    internal_router_->set_connection_error_handler(Closure());
 | 
| -    delete internal_router_;
 | 
| -    internal_router_ = nullptr;
 | 
| -  }
 | 
| -
 | 
| -  internal::Router* internal_router_ = nullptr;
 | 
| -  typename Interface::Stub_ stub_;
 | 
| -  Interface* impl_;
 | 
| -  Closure connection_error_handler_;
 | 
| +  internal::BindingState<Interface, Interface::PassesAssociatedKinds_>
 | 
| +      internal_state_;
 | 
|  
 | 
| -  MOJO_DISALLOW_COPY_AND_ASSIGN(Binding);
 | 
| +  DISALLOW_COPY_AND_ASSIGN(Binding);
 | 
|  };
 | 
|  
 | 
|  }  // namespace mojo
 | 
| 
 |