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

Unified Diff: mojo/public/cpp/bindings/thread_safe_associated_interface_ptr.h

Issue 2506383002: Mojo: adding a thread safe associated interface ptr. (Closed)
Patch Set: Fixed BUILD.gn 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: mojo/public/cpp/bindings/thread_safe_associated_interface_ptr.h
diff --git a/mojo/public/cpp/bindings/thread_safe_associated_interface_ptr.h b/mojo/public/cpp/bindings/thread_safe_associated_interface_ptr.h
new file mode 100644
index 0000000000000000000000000000000000000000..114a9e6890be47df907c1a3686f193affc072aa2
--- /dev/null
+++ b/mojo/public/cpp/bindings/thread_safe_associated_interface_ptr.h
@@ -0,0 +1,80 @@
+// 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 MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_H_
+#define MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_H_
+
+#include <memory>
+
+#include "base/macros.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "mojo/public/cpp/bindings/interface_ptr.h"
+#include "mojo/public/cpp/bindings/lib/interface_ptr_state.h"
+#include "mojo/public/cpp/bindings/lib/thread_safe_interface_ptr_base.h"
+#include "mojo/public/cpp/bindings/message.h"
+
+namespace mojo {
+
+// ThreadSafeAssociatedInterfacePtr is a version of AssociatedInterfacePtr that
+// lets caller invoke interface methods from any threads. Callbacks are called
+// on the thread that performed the interface call.
+// To create a ThreadSafeAssociatedInterfacePtr, create first a regular
+// AssociatedInterfacePtr that you then provide to
+// ThreadSafeAssociatedInterfacePtr::Create.
+// You can then call methods on the ThreadSafeAssociatedInterfacePtr from any
+// thread.
+//
+// Ex:
+// frob::FrobinatorAssociatedPtr frobinator;
+// connection_ptr->GetSender(GetProxy(&frobinator,
+// connection_ptr.associated_group()));
+// scoped_refptr<frob::ThreadSafeAssociatedFrobinatorPtr>
+// thread_safe_frobinator =
+// frob::ThreadSafeAssociatedFrobinatorPtr::Create(
+// std::move(frobinator));
+// <pass the thread_safe_frobinator to a different thread>
+// ...
+// (*thread_safe_frobinator)->FrobinateToTheMax();
+
+template <typename Interface>
+class ThreadSafeAssociatedInterfacePtr :
yzshen1 2016/11/17 17:58:12 It seems this code is the same as ThreadSafeInterf
Jay Civelli 2016/11/17 19:16:20 Oh, that's nice! My template fu was not that good
+ public ThreadSafeInterfacePtrBase<Interface> {
+ public:
+ static scoped_refptr<ThreadSafeAssociatedInterfacePtr<Interface>> Create(
+ AssociatedInterfacePtr<Interface> interface_ptr) {
+ if (!interface_ptr.is_bound()) {
+ LOG(ERROR) << "Attempting to create a ThreadSafeAssociatedInterfacePtr "
+ "from an unbound AssociatedInterfacePtr.";
+ return nullptr;
+ }
+ return new ThreadSafeAssociatedInterfacePtr(
+ std::move(interface_ptr),
+ base::ThreadTaskRunnerHandle::Get());
+ }
+
+ protected:
+ // ThreadSafeInterfacePtrBase implementation:
+ void AcceptOnInterfacePtrThread(Message message) override {
+ interface_ptr_.internal_state()->ForwardMessage(std::move(message));
+ }
+ void AcceptWithResponderOnInterfacePtrThread(
+ Message message, std::unique_ptr<MessageReceiver> responder) override {
+ interface_ptr_.internal_state()->ForwardMessageWithResponder(
+ std::move(message), std::move(responder));
+ }
+
+ private:
+ ThreadSafeAssociatedInterfacePtr(
+ AssociatedInterfacePtr<Interface> interface_ptr,
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner)
+ : ThreadSafeInterfacePtrBase<Interface>(task_runner),
+ interface_ptr_(std::move(interface_ptr)) {
+ }
+
+ AssociatedInterfacePtr<Interface> interface_ptr_;
+};
+
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_ASSOCIATED_INTERFACE_PTR_H_
« no previous file with comments | « mojo/public/cpp/bindings/tests/associated_interface_unittest.cc ('k') | mojo/public/cpp/bindings/thread_safe_interface_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698