Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/threading/thread_task_runner_handle.h" | |
| 12 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 13 #include "mojo/public/cpp/bindings/lib/interface_ptr_state.h" | |
| 14 #include "mojo/public/cpp/bindings/lib/thread_safe_interface_ptr_base.h" | |
| 15 #include "mojo/public/cpp/bindings/message.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 | |
| 19 // ThreadSafeAssociatedInterfacePtr is a version of AssociatedInterfacePtr that | |
| 20 // lets caller invoke interface methods from any threads. Callbacks are called | |
| 21 // on the thread that performed the interface call. | |
| 22 // To create a ThreadSafeAssociatedInterfacePtr, create first a regular | |
| 23 // AssociatedInterfacePtr that you then provide to | |
| 24 // ThreadSafeAssociatedInterfacePtr::Create. | |
| 25 // You can then call methods on the ThreadSafeAssociatedInterfacePtr from any | |
| 26 // thread. | |
| 27 // | |
| 28 // Ex: | |
| 29 // frob::FrobinatorAssociatedPtr frobinator; | |
| 30 // connection_ptr->GetSender(GetProxy(&frobinator, | |
| 31 // connection_ptr.associated_group())); | |
| 32 // scoped_refptr<frob::ThreadSafeAssociatedFrobinatorPtr> | |
| 33 // thread_safe_frobinator = | |
| 34 // frob::ThreadSafeAssociatedFrobinatorPtr::Create( | |
| 35 // std::move(frobinator)); | |
| 36 // <pass the thread_safe_frobinator to a different thread> | |
| 37 // ... | |
| 38 // (*thread_safe_frobinator)->FrobinateToTheMax(); | |
| 39 | |
| 40 template <typename Interface> | |
| 41 class ThreadSafeAssociatedInterfacePtr : | |
|
yzshen1
2016/11/17 17:58:12
It seems this code is the same as ThreadSafeInterf
Jay Civelli
2016/11/17 19:16:20
Oh, that's nice! My template fu was not that good
| |
| 42 public ThreadSafeInterfacePtrBase<Interface> { | |
| 43 public: | |
| 44 static scoped_refptr<ThreadSafeAssociatedInterfacePtr<Interface>> Create( | |
| 45 AssociatedInterfacePtr<Interface> interface_ptr) { | |
| 46 if (!interface_ptr.is_bound()) { | |
| 47 LOG(ERROR) << "Attempting to create a ThreadSafeAssociatedInterfacePtr " | |
| 48 "from an unbound AssociatedInterfacePtr."; | |
| 49 return nullptr; | |
| 50 } | |
| 51 return new ThreadSafeAssociatedInterfacePtr( | |
| 52 std::move(interface_ptr), | |
| 53 base::ThreadTaskRunnerHandle::Get()); | |
| 54 } | |
| 55 | |
| 56 protected: | |
| 57 // ThreadSafeInterfacePtrBase implementation: | |
| 58 void AcceptOnInterfacePtrThread(Message message) override { | |
| 59 interface_ptr_.internal_state()->ForwardMessage(std::move(message)); | |
| 60 } | |
| 61 void AcceptWithResponderOnInterfacePtrThread( | |
| 62 Message message, std::unique_ptr<MessageReceiver> responder) override { | |
| 63 interface_ptr_.internal_state()->ForwardMessageWithResponder( | |
| 64 std::move(message), std::move(responder)); | |
| 65 } | |
| 66 | |
| 67 private: | |
| 68 ThreadSafeAssociatedInterfacePtr( | |
| 69 AssociatedInterfacePtr<Interface> interface_ptr, | |
| 70 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
| 71 : ThreadSafeInterfacePtrBase<Interface>(task_runner), | |
| 72 interface_ptr_(std::move(interface_ptr)) { | |
| 73 } | |
| 74 | |
| 75 AssociatedInterfacePtr<Interface> interface_ptr_; | |
| 76 }; | |
| 77 | |
| 78 } // namespace mojo | |
| 79 | |
| 80 #endif // MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_H_ | |
| OLD | NEW |