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_->ipc_task_runner()); | |
34 channel_proxy_->RetrieveAssociatedInterfaceOnIOThread<Interface>(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 mojo::AssociatedInterfacePtr<Interface> interface_ptr) { | |
46 bool success = ptr->Bind(std::move(interface_ptr)); | |
47 DCHECK(success); | |
48 } | |
49 | |
50 IPC::ChannelProxy* channel_proxy_; | |
51 | |
52 DISALLOW_COPY_AND_ASSIGN(ThreadSafeAssociatedInterfacePtrProvider); | |
53 }; | |
54 | |
55 } // namespace content | |
56 | |
57 #endif // CONTENT_RENDERER_MOJO_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_PROVIDER_H
_ | |
OLD | NEW |