Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: mojo/public/cpp/bindings/interface_ptr.h

Issue 265793015: Mojo: Replace RemotePtr with InterfacePtr and InterfaceImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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_PTR_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_
7
8 #include <assert.h>
9
10 #include <algorithm>
11
12 #include "mojo/public/cpp/bindings/error_handler.h"
13 #include "mojo/public/cpp/bindings/lib/interface_ptr_internal.h"
14 #include "mojo/public/cpp/system/macros.h"
15
16 namespace mojo {
17 class ErrorHandler;
18
19 // InterfacePtr represents a proxy to a remote instance of an interface.
20 template <typename Interface>
21 class InterfacePtr {
22 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InterfacePtr, RValue)
23 public:
24 InterfacePtr() {}
25
26 InterfacePtr(RValue other) {
27 other.object->internal_state_.Swap(&internal_state_);
28 }
29 InterfacePtr& operator=(RValue other) {
30 other.object->internal_state_.Swap(&internal_state_);
31 return *this;
32 }
33
34 ~InterfacePtr() {}
35
36 Interface* get() const {
37 return internal_state_.instance();
38 }
39 Interface* operator->() const { return get(); }
40 Interface* operator*() const { return get(); }
41
42 void reset() {
43 State doomed;
44 internal_state_.Swap(&doomed);
45 }
46
47 // This method configures the InterfacePtr<..> to be a proxy to a remote
48 // object on the other end of the given pipe.
49 //
50 // The proxy is bound to the current thread, which means its methods may
51 // only be called on the current thread.
52 //
53 // To move a bound InterfacePtr<..> to another thread, call
54 // ResetAndReturnMessagePipe. Then create a new InterfacePtr<..> on another
55 // thread, and bind the new InterfacePtr<..> to the message pipe on that
56 // thread.
57 void Bind(ScopedMessagePipeHandle handle,
58 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
59 reset();
60 internal_state_.ConfigureProxy(handle.Pass(), waiter);
61 }
62
63 // This method may be called to query if the underlying pipe has encountered
64 // an error. If true, this means method calls made on this interface will be
65 // dropped (and may have already been dropped) on the floor.
66 bool encountered_error() const {
67 assert(internal_state_.router());
68 return internal_state_.router()->encountered_error();
69 }
70
71 // This method may be called to register an ErrorHandler to observe a
72 // connection error on the underlying pipe. The callback runs asynchronously
73 // from the current message loop.
74 void set_error_handler(ErrorHandler* error_handler) {
75 assert(internal_state_.router());
76 internal_state_.router()->set_error_handler(error_handler);
77 }
78
79 // Returns the underlying message pipe handle (if any) and resets the
80 // InterfacePtr<..> to its uninitialized state. This method is helpful if you
81 // need to move a proxy to another thread. See related notes for Bind.
82 ScopedMessagePipeHandle ResetAndReturnMessagePipe() {
83 State state;
84 internal_state_.Swap(&state);
85 return state.router() ?
86 state.router()->ReleaseMessagePipe() : ScopedMessagePipeHandle();
87 }
88
89 // DO NOT USE. Exposed only for internal use and for testing.
90 internal::InterfacePtrState<Interface>* internal_state() {
91 return &internal_state_;
92 }
93
94 private:
95 typedef internal::InterfacePtrState<Interface> State;
96 State internal_state_;
97 };
98
99 // Takes a handle to the proxy end-point of a pipe. On the other end is
100 // presumed to be an interface implementation of type |Interface|. Returns a
101 // generated proxy to that interface, which may be used on the current thread.
102 // It is valid to call SetClient on the returned Interface to set an instance
103 // of Interface::Client.
104 template <typename Interface>
105 InterfacePtr<Interface> MakeProxy(
106 ScopedMessagePipeHandle handle,
107 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
108 InterfacePtr<Interface> ptr;
109 if (handle.is_valid())
110 ptr.Bind(handle.Pass(), waiter);
111 return ptr.Pass();
112 }
113
114 } // namespace mojo
115
116 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698