OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_BINDER_H_ | 5 #ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_BINDER_H_ |
6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_BINDER_H_ | 6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_BINDER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
| 9 #include <utility> |
9 | 10 |
| 11 #include "base/bind.h" |
| 12 #include "mojo/public/cpp/bindings/interface_request.h" |
10 #include "mojo/public/cpp/system/message_pipe.h" | 13 #include "mojo/public/cpp/system/message_pipe.h" |
11 | 14 |
12 namespace service_manager { | 15 namespace service_manager { |
13 | 16 |
14 struct BindSourceInfo; | 17 struct BindSourceInfo; |
15 | 18 |
16 class InterfaceBinder { | 19 class InterfaceBinder { |
17 public: | 20 public: |
18 virtual ~InterfaceBinder() {} | 21 virtual ~InterfaceBinder() {} |
19 | 22 |
20 // Asks the InterfaceBinder to bind an implementation of the specified | 23 // Asks the InterfaceBinder to bind an implementation of the specified |
21 // interface to the request passed via |handle|. If the InterfaceBinder binds | 24 // interface to the request passed via |handle|. If the InterfaceBinder binds |
22 // an implementation it must take ownership of the request handle. | 25 // an implementation it must take ownership of the request handle. |
23 virtual void BindInterface(const BindSourceInfo& source_info, | 26 virtual void BindInterface(const BindSourceInfo& source_info, |
24 const std::string& interface_name, | 27 const std::string& interface_name, |
25 mojo::ScopedMessagePipeHandle handle) = 0; | 28 mojo::ScopedMessagePipeHandle handle) = 0; |
26 }; | 29 }; |
27 | 30 |
| 31 template <typename Interface> |
| 32 class CallbackBinder : public InterfaceBinder { |
| 33 public: |
| 34 using BindCallback = base::Callback<void(const BindSourceInfo&, |
| 35 mojo::InterfaceRequest<Interface>)>; |
| 36 |
| 37 CallbackBinder(const BindCallback& callback, |
| 38 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 39 : callback_(callback), task_runner_(task_runner) {} |
| 40 ~CallbackBinder() override {} |
| 41 |
| 42 private: |
| 43 // InterfaceBinder: |
| 44 void BindInterface(const BindSourceInfo& source_info, |
| 45 const std::string& interface_name, |
| 46 mojo::ScopedMessagePipeHandle handle) override { |
| 47 mojo::InterfaceRequest<Interface> request = |
| 48 mojo::MakeRequest<Interface>(std::move(handle)); |
| 49 if (task_runner_) { |
| 50 task_runner_->PostTask(FROM_HERE, |
| 51 base::Bind(&CallbackBinder::RunCallback, callback_, |
| 52 source_info, base::Passed(&request))); |
| 53 } else { |
| 54 RunCallback(callback_, source_info, std::move(request)); |
| 55 } |
| 56 } |
| 57 |
| 58 static void RunCallback(const BindCallback& callback, |
| 59 const BindSourceInfo& source_info, |
| 60 mojo::InterfaceRequest<Interface> request) { |
| 61 callback.Run(source_info, std::move(request)); |
| 62 } |
| 63 |
| 64 const BindCallback callback_; |
| 65 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 66 DISALLOW_COPY_AND_ASSIGN(CallbackBinder); |
| 67 }; |
| 68 |
| 69 class GenericCallbackBinder : public InterfaceBinder { |
| 70 public: |
| 71 using BindCallback = base::Callback<void(mojo::ScopedMessagePipeHandle)>; |
| 72 |
| 73 GenericCallbackBinder( |
| 74 const BindCallback& callback, |
| 75 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); |
| 76 ~GenericCallbackBinder() override; |
| 77 |
| 78 private: |
| 79 // InterfaceBinder: |
| 80 void BindInterface(const BindSourceInfo& source_info, |
| 81 const std::string& interface_name, |
| 82 mojo::ScopedMessagePipeHandle handle) override; |
| 83 |
| 84 static void RunCallback(const BindCallback& callback, |
| 85 mojo::ScopedMessagePipeHandle client_handle); |
| 86 |
| 87 const BindCallback callback_; |
| 88 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 89 DISALLOW_COPY_AND_ASSIGN(GenericCallbackBinder); |
| 90 }; |
| 91 |
28 } // namespace service_manager | 92 } // namespace service_manager |
29 | 93 |
30 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_BINDER_H_ | 94 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_INTERFACE_BINDER_H_ |
OLD | NEW |