Home | History | Annotate | Download | only in bindings
      1 // Copyright 2015 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_REQUEST_H_
      6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_REQUEST_H_
      7 
      8 #include <string>
      9 #include <utility>
     10 
     11 #include "base/macros.h"
     12 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
     13 
     14 namespace mojo {
     15 
     16 // AssociatedInterfaceRequest represents an associated interface request. It is
     17 // similar to InterfaceRequest except that it doesn't own a message pipe handle.
     18 template <typename Interface>
     19 class AssociatedInterfaceRequest {
     20  public:
     21   // Constructs an empty AssociatedInterfaceRequest, representing that the
     22   // client is not requesting an implementation of Interface.
     23   AssociatedInterfaceRequest() {}
     24   AssociatedInterfaceRequest(decltype(nullptr)) {}
     25 
     26   // Takes the interface endpoint handle from another
     27   // AssociatedInterfaceRequest.
     28   AssociatedInterfaceRequest(AssociatedInterfaceRequest&& other) {
     29     handle_ = std::move(other.handle_);
     30   }
     31   AssociatedInterfaceRequest& operator=(AssociatedInterfaceRequest&& other) {
     32     if (this != &other)
     33       handle_ = std::move(other.handle_);
     34     return *this;
     35   }
     36 
     37   // Assigning to nullptr resets the AssociatedInterfaceRequest to an empty
     38   // state, closing the interface endpoint handle currently bound to it (if
     39   // any).
     40   AssociatedInterfaceRequest& operator=(decltype(nullptr)) {
     41     handle_.reset();
     42     return *this;
     43   }
     44 
     45   // Indicates whether the request currently contains a valid interface endpoint
     46   // handle.
     47   bool is_pending() const { return handle_.is_valid(); }
     48 
     49   void Bind(ScopedInterfaceEndpointHandle handle) {
     50     handle_ = std::move(handle);
     51   }
     52 
     53   ScopedInterfaceEndpointHandle PassHandle() {
     54     return std::move(handle_);
     55   }
     56 
     57   const ScopedInterfaceEndpointHandle& handle() const { return handle_; }
     58 
     59   bool Equals(const AssociatedInterfaceRequest& other) const {
     60     if (this == &other)
     61       return true;
     62 
     63     // Now that the two refer to different objects, they are equivalent if
     64     // and only if they are both invalid.
     65     return !is_pending() && !other.is_pending();
     66   }
     67 
     68   void ResetWithReason(uint32_t custom_reason, const std::string& description) {
     69     handle_.ResetWithReason(custom_reason, description);
     70   }
     71 
     72  private:
     73   ScopedInterfaceEndpointHandle handle_;
     74 
     75   DISALLOW_COPY_AND_ASSIGN(AssociatedInterfaceRequest);
     76 };
     77 
     78 // Makes an AssociatedInterfaceRequest bound to the specified associated
     79 // endpoint.
     80 template <typename Interface>
     81 AssociatedInterfaceRequest<Interface> MakeAssociatedRequest(
     82     ScopedInterfaceEndpointHandle handle) {
     83   AssociatedInterfaceRequest<Interface> request;
     84   request.Bind(std::move(handle));
     85   return request;
     86 }
     87 
     88 }  // namespace mojo
     89 
     90 #endif  // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_REQUEST_H_
     91