1// Copyright 2014 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_INTERFACE_REQUEST_H_ 6#define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ 7 8#include "mojo/public/cpp/bindings/interface_ptr.h" 9 10namespace mojo { 11 12// Used in methods that return instances of remote objects. 13template <typename Interface> 14class InterfaceRequest { 15 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InterfaceRequest, RValue) 16 public: 17 InterfaceRequest() {} 18 19 InterfaceRequest(RValue other) { 20 handle_ = other.object->handle_.Pass(); 21 } 22 InterfaceRequest& operator=(RValue other) { 23 handle_ = other.object->handle_.Pass(); 24 return *this; 25 } 26 27 // Returns true if the request has yet to be completed. 28 bool is_pending() const { return handle_.is_valid(); } 29 30 void Bind(ScopedMessagePipeHandle handle) { 31 handle_ = handle.Pass(); 32 } 33 34 ScopedMessagePipeHandle PassMessagePipe() { 35 return handle_.Pass(); 36 } 37 38 private: 39 ScopedMessagePipeHandle handle_; 40}; 41 42template <typename Interface> 43InterfaceRequest<Interface> MakeRequest(ScopedMessagePipeHandle handle) { 44 InterfaceRequest<Interface> request; 45 request.Bind(handle.Pass()); 46 return request.Pass(); 47} 48 49// Used to construct a request that synchronously binds an InterfacePtr<..>, 50// making it immediately usable upon return. The resulting request object may 51// then be later bound to an InterfaceImpl<..> via BindToRequest. 52// 53// Given the following interface: 54// 55// interface Foo { 56// CreateBar(Bar& bar); 57// } 58// 59// The caller of CreateBar would have code similar to the following: 60// 61// InterfacePtr<Foo> foo = ...; 62// InterfacePtr<Bar> bar; 63// foo->CreateBar(Get(&bar)); 64// 65// Upon return from CreateBar, |bar| is ready to have methods called on it. 66// 67template <typename Interface> 68InterfaceRequest<Interface> Get(InterfacePtr<Interface>* ptr) { 69 MessagePipe pipe; 70 ptr->Bind(pipe.handle0.Pass()); 71 return MakeRequest<Interface>(pipe.handle1.Pass()); 72} 73 74} // namespace mojo 75 76#endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_REQUEST_H_ 77