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

Unified Diff: content/renderer/mojo/thread_safe_associated_interface_ptr_provider.h

Issue 2522333002: Provide a Mojo equivalent of ThreadSafeSender. (Closed)
Patch Set: Addressed comments and synced Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: content/renderer/mojo/thread_safe_associated_interface_ptr_provider.h
diff --git a/content/renderer/mojo/thread_safe_associated_interface_ptr_provider.h b/content/renderer/mojo/thread_safe_associated_interface_ptr_provider.h
new file mode 100644
index 0000000000000000000000000000000000000000..af78321aff96cb3c1ddacca0969b488d2c1fbc9c
--- /dev/null
+++ b/content/renderer/mojo/thread_safe_associated_interface_ptr_provider.h
@@ -0,0 +1,60 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_RENDERER_MOJO_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_PROVIDER_H_
+#define CONTENT_RENDERER_MOJO_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_PROVIDER_H_
+
+#include "base/bind.h"
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "ipc/ipc_channel.h"
+#include "ipc/ipc_channel_proxy.h"
+#include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
+
+namespace content {
+
+// This class provides a way to create ThreadSafeAssociatedInterfacePtr's from
+// the main thread that can be used right away (even though the backing
+// AssociatedInterfacePtr is created on the IO thread and the channel may not be
+// connected yet).
+class ThreadSafeAssociatedInterfacePtrProvider {
+ public:
+ // Note that this does not take ownership of |channel_proxy|. It's the
+ // caller responsibility to ensure |channel_proxy| outlives this object.
+ explicit ThreadSafeAssociatedInterfacePtrProvider(
+ IPC::ChannelProxy* channel_proxy)
+ : channel_proxy_(channel_proxy) {}
+
+ template <typename Interface>
+ scoped_refptr<mojo::ThreadSafeAssociatedInterfacePtr<Interface>>
+ CreateInterfacePtr() {
+ scoped_refptr<mojo::ThreadSafeAssociatedInterfacePtr<Interface>> ptr =
+ mojo::ThreadSafeAssociatedInterfacePtr<Interface>::CreateUnbound();
+ channel_proxy_->RunOnIOThread(base::Bind(
+ &ThreadSafeAssociatedInterfacePtrProvider::BindInterfacePtr<Interface>,
+ ptr));
+ return ptr;
+ }
+
+ private:
+ template <typename Interface>
+ static void BindInterfacePtr(
+ const scoped_refptr<mojo::ThreadSafeAssociatedInterfacePtr<Interface>>&
+ ptr,
+ IPC::Channel* channel) {
+ mojo::AssociatedInterfacePtr<Interface> interface_ptr;
+ channel->GetAssociatedInterfaceSupport()->GetRemoteAssociatedInterface(
+ &interface_ptr);
+ bool success = ptr->BindToCurrentThread(std::move(interface_ptr));
+ DCHECK(success);
+ }
+
+ IPC::ChannelProxy* channel_proxy_;
+
+ DISALLOW_COPY_AND_ASSIGN(ThreadSafeAssociatedInterfacePtrProvider);
+};
+
+} // namespace content
+
+#endif // CONTENT_RENDERER_MOJO_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_PROVIDER_H_

Powered by Google App Engine
This is Rietveld 408576698