OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ |
7 | 7 |
8 #include "mojo/public/cpp/bindings/lib/interface_impl_internal.h" | 8 #include "mojo/public/cpp/bindings/lib/interface_impl_internal.h" |
9 #include "mojo/public/cpp/system/macros.h" | 9 #include "mojo/public/cpp/system/macros.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 | 12 |
13 // InterfaceImpl<..> is designed to be the base class of an interface | 13 // InterfaceImpl<..> is designed to be the base class of an interface |
14 // implementation. It may be bound to a pipe or a proxy, see BindToPipe and | 14 // implementation. It may be bound to a pipe or a proxy, see BindToPipe and |
15 // BindToProxy. | 15 // BindToProxy. |
16 template <typename Interface> | 16 template <typename Interface> |
17 class InterfaceImpl : public internal::InterfaceImplBase<Interface> { | 17 class InterfaceImpl : public internal::InterfaceImplBase<Interface> { |
18 public: | 18 public: |
19 typedef typename Interface::Client Client; | 19 typedef typename Interface::Client Client; |
20 | 20 |
21 InterfaceImpl() : internal_state_(this) {} | 21 InterfaceImpl() : internal_state_(this) {} |
22 virtual ~InterfaceImpl() {} | 22 virtual ~InterfaceImpl() {} |
23 | 23 |
24 // Subclasses can override this to handle post connection initialization. | 24 // Returns a proxy to the client interface. This is null upon construction, |
| 25 // and becomes non-null after OnClientConnected. NOTE: It remains non-null |
| 26 // until this instance is deleted. |
| 27 Client* client() { return internal_state_.client(); } |
| 28 |
| 29 // Called when the client has connected to this instance. |
25 virtual void OnConnectionEstablished() {} | 30 virtual void OnConnectionEstablished() {} |
26 | 31 |
27 // Subclasses must handle connection errors. | 32 // Called when the client is no longer connected to this instance. NOTE: The |
28 virtual void OnConnectionError() = 0; | 33 // client() method continues to return a non-null pointer after this method |
29 | 34 // is called. After this method is called, any method calls made on client() |
30 // We override SetClient here so subclasses don't each have to. | 35 // will be silently ignored. |
31 virtual void SetClient(Client* client) MOJO_OVERRIDE { | 36 virtual void OnConnectionError() {} |
32 internal_state_.set_client(client); | |
33 } | |
34 Client* client() { return internal_state_.client(); } | |
35 | 37 |
36 // DO NOT USE. Exposed only for internal use and for testing. | 38 // DO NOT USE. Exposed only for internal use and for testing. |
37 internal::InterfaceImplState<Interface>* internal_state() { | 39 internal::InterfaceImplState<Interface>* internal_state() { |
38 return &internal_state_; | 40 return &internal_state_; |
39 } | 41 } |
40 | 42 |
41 private: | 43 private: |
| 44 virtual void SetClient(Client* client) MOJO_OVERRIDE { |
| 45 internal_state_.set_client(client); |
| 46 } |
42 internal::InterfaceImplState<Interface> internal_state_; | 47 internal::InterfaceImplState<Interface> internal_state_; |
43 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl); | 48 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImpl); |
44 }; | 49 }; |
45 | 50 |
46 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given | 51 // Takes an instance of an InterfaceImpl<..> subclass and binds it to the given |
47 // MessagePipe. The instance is returned for convenience in member initializer | 52 // MessagePipe. The instance is returned for convenience in member initializer |
48 // lists, etc. If the pipe is closed, the instance's OnConnectionError method | 53 // lists, etc. If the pipe is closed, the instance's OnConnectionError method |
49 // will be called. | 54 // will be called. |
50 // | 55 // |
51 // The instance is also bound to the current thread. Its methods will only be | 56 // The instance is also bound to the current thread. Its methods will only be |
(...skipping 25 matching lines...) Expand all Loading... |
77 Impl* BindToProxy(Impl* instance, | 82 Impl* BindToProxy(Impl* instance, |
78 InterfacePtr<Interface>* ptr, | 83 InterfacePtr<Interface>* ptr, |
79 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { | 84 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { |
80 instance->internal_state()->BindProxy(ptr, waiter); | 85 instance->internal_state()->BindProxy(ptr, waiter); |
81 return instance; | 86 return instance; |
82 } | 87 } |
83 | 88 |
84 } // namespace mojo | 89 } // namespace mojo |
85 | 90 |
86 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ | 91 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_IMPL_H_ |
OLD | NEW |