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