| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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_THREAD_SAFE_INTERFACE_PTR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 using ProxyType = typename Interface::Proxy_; | 53 using ProxyType = typename Interface::Proxy_; |
| 54 | 54 |
| 55 static scoped_refptr<ThreadSafeInterfacePtrBase<Interface, InterfacePtrType>> | 55 static scoped_refptr<ThreadSafeInterfacePtrBase<Interface, InterfacePtrType>> |
| 56 Create(InterfacePtrType<Interface> interface_ptr) { | 56 Create(InterfacePtrType<Interface> interface_ptr) { |
| 57 scoped_refptr<ThreadSafeInterfacePtrBase> ptr( | 57 scoped_refptr<ThreadSafeInterfacePtrBase> ptr( |
| 58 new ThreadSafeInterfacePtrBase()); | 58 new ThreadSafeInterfacePtrBase()); |
| 59 return ptr->Bind(std::move(interface_ptr)) ? ptr : nullptr; | 59 return ptr->Bind(std::move(interface_ptr)) ? ptr : nullptr; |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Creates a ThreadSafeInterfacePtrBase with no associated InterfacePtr. | 62 // Creates a ThreadSafeInterfacePtrBase with no associated InterfacePtr. |
| 63 // Call Bind() with the InterfacePtr once available. | 63 // Call Bind() with the InterfacePtr once available, which must be called on |
| 64 // the |bind_task_runner|. |
| 65 // Providing the TaskRunner here allows you to post a task to |
| 66 // |bind_task_runner| to do the bind and then immediately start calling |
| 67 // methods on the returned interface. |
| 64 static scoped_refptr<ThreadSafeInterfacePtrBase<Interface, InterfacePtrType>> | 68 static scoped_refptr<ThreadSafeInterfacePtrBase<Interface, InterfacePtrType>> |
| 65 CreateUnbound() { | 69 CreateUnbound( |
| 66 return new ThreadSafeInterfacePtrBase(); | 70 const scoped_refptr<base::SingleThreadTaskRunner>& bind_task_runner) { |
| 71 scoped_refptr<ThreadSafeInterfacePtrBase<Interface, InterfacePtrType>> ptr = |
| 72 new ThreadSafeInterfacePtrBase(); |
| 73 ptr->interface_ptr_task_runner_ = bind_task_runner; |
| 74 return ptr; |
| 67 } | 75 } |
| 68 | 76 |
| 69 // Binds a ThreadSafeInterfacePtrBase previously created with CreateUnbound(). | 77 // Binds a ThreadSafeInterfacePtrBase previously created with CreateUnbound(). |
| 70 // This must be called on the thread that |interface_ptr| should be used. | 78 // This must be called on the thread that |interface_ptr| should be used. |
| 79 // If created with CreateUnbound() that thread should be the same as the one |
| 80 // provided at creation time. |
| 71 bool Bind(InterfacePtrType<Interface> interface_ptr) { | 81 bool Bind(InterfacePtrType<Interface> interface_ptr) { |
| 72 DCHECK(!interface_ptr_task_runner_); | 82 DCHECK(!interface_ptr_task_runner_ || |
| 83 interface_ptr_task_runner_ == base::ThreadTaskRunnerHandle::Get()); |
| 73 if (!interface_ptr.is_bound()) { | 84 if (!interface_ptr.is_bound()) { |
| 74 LOG(ERROR) << "Attempting to bind a ThreadSafe[Associated]InterfacePtr " | 85 LOG(ERROR) << "Attempting to bind a ThreadSafe[Associated]InterfacePtr " |
| 75 "from an unbound InterfacePtr."; | 86 "from an unbound InterfacePtr."; |
| 76 return false; | 87 return false; |
| 77 } | 88 } |
| 78 interface_ptr_ = std::move(interface_ptr); | 89 interface_ptr_ = std::move(interface_ptr); |
| 79 interface_ptr_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 90 interface_ptr_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 80 return true; | 91 return true; |
| 81 } | 92 } |
| 82 | 93 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 using ThreadSafeAssociatedInterfacePtr = | 194 using ThreadSafeAssociatedInterfacePtr = |
| 184 ThreadSafeInterfacePtrBase<Interface, AssociatedInterfacePtr>; | 195 ThreadSafeInterfacePtrBase<Interface, AssociatedInterfacePtr>; |
| 185 | 196 |
| 186 template <typename Interface> | 197 template <typename Interface> |
| 187 using ThreadSafeInterfacePtr = | 198 using ThreadSafeInterfacePtr = |
| 188 ThreadSafeInterfacePtrBase<Interface, InterfacePtr>; | 199 ThreadSafeInterfacePtrBase<Interface, InterfacePtr>; |
| 189 | 200 |
| 190 } // namespace mojo | 201 } // namespace mojo |
| 191 | 202 |
| 192 #endif // MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ | 203 #endif // MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ |
| OLD | NEW |