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 THIRD_PARTY_BLINK_PUBLIC_PLATFORM_INTERFACE_REGISTRY_H_ 6 #define THIRD_PARTY_BLINK_PUBLIC_PLATFORM_INTERFACE_REGISTRY_H_ 7 8 #include <utility> 9 10 #include "base/callback_forward.h" 11 #include "base/memory/scoped_refptr.h" 12 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" 13 #include "mojo/public/cpp/system/message_pipe.h" 14 #include "third_party/blink/public/platform/web_common.h" 15 16 #if INSIDE_BLINK 17 #include "mojo/public/cpp/bindings/pending_associated_receiver.h" 18 #include "mojo/public/cpp/bindings/pending_receiver.h" 19 #include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h" // nogncheck 20 #include "third_party/blink/renderer/platform/wtf/functional.h" // nogncheck 21 #endif 22 23 namespace base { 24 class SingleThreadTaskRunner; 25 } 26 27 namespace blink { 28 29 using InterfaceFactory = 30 base::RepeatingCallback<void(mojo::ScopedMessagePipeHandle)>; 31 using AssociatedInterfaceFactory = 32 base::RepeatingCallback<void(mojo::ScopedInterfaceEndpointHandle)>; 33 34 class BLINK_PLATFORM_EXPORT InterfaceRegistry { 35 public: 36 virtual void AddInterface( 37 const char* name, 38 const InterfaceFactory&, 39 scoped_refptr<base::SingleThreadTaskRunner> = nullptr) = 0; 40 // The usage of associated interfaces should be very limited. Please 41 // consult the owners of public/platform before adding one. 42 virtual void AddAssociatedInterface(const char* name, 43 const AssociatedInterfaceFactory&) = 0; 44 45 static InterfaceRegistry* GetEmptyInterfaceRegistry(); 46 47 #if INSIDE_BLINK 48 template <typename Interface> AddInterface(base::RepeatingCallback<void (mojo::PendingReceiver<Interface>)> factory)49 void AddInterface( 50 base::RepeatingCallback<void(mojo::PendingReceiver<Interface>)> factory) { 51 AddInterface( 52 Interface::Name_, 53 WTF::BindRepeating(&InterfaceRegistry::ForwardToInterfaceFactory< 54 mojo::PendingReceiver<Interface>>, 55 std::move(factory))); 56 } 57 58 template <typename Interface> AddInterface(base::RepeatingCallback<void (mojo::PendingReceiver<Interface>)> factory,scoped_refptr<base::SingleThreadTaskRunner> task_runner)59 void AddInterface( 60 base::RepeatingCallback<void(mojo::PendingReceiver<Interface>)> factory, 61 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 62 DCHECK(task_runner->RunsTasksInCurrentSequence()); 63 AddInterface( 64 Interface::Name_, 65 WTF::BindRepeating(&InterfaceRegistry::ForwardToInterfaceFactory< 66 mojo::PendingReceiver<Interface>>, 67 std::move(factory)), 68 std::move(task_runner)); 69 } 70 71 template <typename Interface> AddAssociatedInterface(base::RepeatingCallback<void (mojo::PendingAssociatedReceiver<Interface>)> factory)72 void AddAssociatedInterface( 73 base::RepeatingCallback<void(mojo::PendingAssociatedReceiver<Interface>)> 74 factory) { 75 AddAssociatedInterface( 76 Interface::Name_, 77 WTF::BindRepeating( 78 &InterfaceRegistry::ForwardToAssociatedInterfaceFactory< 79 mojo::PendingAssociatedReceiver<Interface>>, 80 std::move(factory))); 81 } 82 83 private: 84 template <typename MojoType> ForwardToInterfaceFactory(base::RepeatingCallback<void (MojoType)> factory,mojo::ScopedMessagePipeHandle handle)85 static void ForwardToInterfaceFactory( 86 base::RepeatingCallback<void(MojoType)> factory, 87 mojo::ScopedMessagePipeHandle handle) { 88 factory.Run(MojoType(std::move(handle))); 89 } 90 91 template <typename MojoType> ForwardToAssociatedInterfaceFactory(base::RepeatingCallback<void (MojoType)> factory,mojo::ScopedInterfaceEndpointHandle handle)92 static void ForwardToAssociatedInterfaceFactory( 93 base::RepeatingCallback<void(MojoType)> factory, 94 mojo::ScopedInterfaceEndpointHandle handle) { 95 factory.Run(MojoType(std::move(handle))); 96 } 97 98 #endif // INSIDE_BLINK 99 }; 100 101 } // namespace blink 102 103 #endif // THIRD_PARTY_BLINK_PUBLIC_PLATFORM_INTERFACE_REGISTRY_H_ 104