| 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 CONTENT_RENDERER_MOJO_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_PROVIDER_H_ |
| 6 #define CONTENT_RENDERER_MOJO_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_PROVIDER_H_ |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "ipc/ipc_channel_proxy.h" |
| 12 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 // This class provides a way to create ThreadSafeAssociatedInterfacePtr's from |
| 17 // the main thread that can be used right away (even though the backing |
| 18 // AssociatedInterfacePtr is created on the IO thread and the channel may not be |
| 19 // connected yet). |
| 20 class ThreadSafeAssociatedInterfacePtrProvider { |
| 21 public: |
| 22 // Note that this does not take ownership of |channel_proxy|. It's the |
| 23 // caller responsibility to ensure |channel_proxy| outlives this object. |
| 24 explicit ThreadSafeAssociatedInterfacePtrProvider( |
| 25 IPC::ChannelProxy* channel_proxy) |
| 26 : channel_proxy_(channel_proxy) {} |
| 27 |
| 28 template <typename Interface> |
| 29 scoped_refptr<mojo::ThreadSafeAssociatedInterfacePtr<Interface>> |
| 30 CreateInterfacePtr() { |
| 31 scoped_refptr<mojo::ThreadSafeAssociatedInterfacePtr<Interface>> ptr = |
| 32 mojo::ThreadSafeAssociatedInterfacePtr<Interface>::CreateUnbound(); |
| 33 channel_proxy_->RetrieveAssociatedInterfaceOnIOThread<Interface>(base::Bind( |
| 34 &ThreadSafeAssociatedInterfacePtrProvider::BindInterfacePtr<Interface>, |
| 35 ptr)); |
| 36 return ptr; |
| 37 } |
| 38 |
| 39 private: |
| 40 template <typename Interface> |
| 41 static void BindInterfacePtr( |
| 42 const scoped_refptr<mojo::ThreadSafeAssociatedInterfacePtr<Interface>>& |
| 43 ptr, |
| 44 mojo::AssociatedInterfacePtr<Interface> interface_ptr) { |
| 45 bool success = ptr->Bind(std::move(interface_ptr)); |
| 46 DCHECK(success); |
| 47 } |
| 48 |
| 49 IPC::ChannelProxy* channel_proxy_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(ThreadSafeAssociatedInterfacePtrProvider); |
| 52 }; |
| 53 |
| 54 } // namespace content |
| 55 |
| 56 #endif // CONTENT_RENDERER_MOJO_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_PROVIDER_H
_ |
| OLD | NEW |