Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: content/renderer/mojo/thread_safe_associated_interface_ptr_provider.h

Issue 2522333002: Provide a Mojo equivalent of ThreadSafeSender. (Closed)
Patch Set: Clean-up Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 _
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698