Chromium Code Reviews| Index: mojo/public/cpp/bindings/thread_safe_associated_interface_ptr.h |
| diff --git a/mojo/public/cpp/bindings/thread_safe_associated_interface_ptr.h b/mojo/public/cpp/bindings/thread_safe_associated_interface_ptr.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..114a9e6890be47df907c1a3686f193affc072aa2 |
| --- /dev/null |
| +++ b/mojo/public/cpp/bindings/thread_safe_associated_interface_ptr.h |
| @@ -0,0 +1,80 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_H_ |
| +#define MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_H_ |
| + |
| +#include <memory> |
| + |
| +#include "base/macros.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "mojo/public/cpp/bindings/interface_ptr.h" |
| +#include "mojo/public/cpp/bindings/lib/interface_ptr_state.h" |
| +#include "mojo/public/cpp/bindings/lib/thread_safe_interface_ptr_base.h" |
| +#include "mojo/public/cpp/bindings/message.h" |
| + |
| +namespace mojo { |
| + |
| +// ThreadSafeAssociatedInterfacePtr is a version of AssociatedInterfacePtr that |
| +// lets caller invoke interface methods from any threads. Callbacks are called |
| +// on the thread that performed the interface call. |
| +// To create a ThreadSafeAssociatedInterfacePtr, create first a regular |
| +// AssociatedInterfacePtr that you then provide to |
| +// ThreadSafeAssociatedInterfacePtr::Create. |
| +// You can then call methods on the ThreadSafeAssociatedInterfacePtr from any |
| +// thread. |
| +// |
| +// Ex: |
| +// frob::FrobinatorAssociatedPtr frobinator; |
| +// connection_ptr->GetSender(GetProxy(&frobinator, |
| +// connection_ptr.associated_group())); |
| +// scoped_refptr<frob::ThreadSafeAssociatedFrobinatorPtr> |
| +// thread_safe_frobinator = |
| +// frob::ThreadSafeAssociatedFrobinatorPtr::Create( |
| +// std::move(frobinator)); |
| +// <pass the thread_safe_frobinator to a different thread> |
| +// ... |
| +// (*thread_safe_frobinator)->FrobinateToTheMax(); |
| + |
| +template <typename Interface> |
| +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
|
| + public ThreadSafeInterfacePtrBase<Interface> { |
| + public: |
| + static scoped_refptr<ThreadSafeAssociatedInterfacePtr<Interface>> Create( |
| + AssociatedInterfacePtr<Interface> interface_ptr) { |
| + if (!interface_ptr.is_bound()) { |
| + LOG(ERROR) << "Attempting to create a ThreadSafeAssociatedInterfacePtr " |
| + "from an unbound AssociatedInterfacePtr."; |
| + return nullptr; |
| + } |
| + return new ThreadSafeAssociatedInterfacePtr( |
| + std::move(interface_ptr), |
| + base::ThreadTaskRunnerHandle::Get()); |
| + } |
| + |
| + protected: |
| + // ThreadSafeInterfacePtrBase implementation: |
| + void AcceptOnInterfacePtrThread(Message message) override { |
| + interface_ptr_.internal_state()->ForwardMessage(std::move(message)); |
| + } |
| + void AcceptWithResponderOnInterfacePtrThread( |
| + Message message, std::unique_ptr<MessageReceiver> responder) override { |
| + interface_ptr_.internal_state()->ForwardMessageWithResponder( |
| + std::move(message), std::move(responder)); |
| + } |
| + |
| + private: |
| + ThreadSafeAssociatedInterfacePtr( |
| + AssociatedInterfacePtr<Interface> interface_ptr, |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| + : ThreadSafeInterfacePtrBase<Interface>(task_runner), |
| + interface_ptr_(std::move(interface_ptr)) { |
| + } |
| + |
| + AssociatedInterfacePtr<Interface> interface_ptr_; |
| +}; |
| + |
| +} // namespace mojo |
| + |
| +#endif // MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_H_ |