OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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_SHELL_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_ | 5 #ifndef SERVICES_SHELL_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_ |
6 #define SERVICES_SHELL_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_ | 6 #define SERVICES_SHELL_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_ |
7 | 7 |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 explicit CallbackBinder( | 24 explicit CallbackBinder( |
25 const BindCallback& callback, | 25 const BindCallback& callback, |
26 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 26 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
27 : callback_(callback), task_runner_(task_runner) {} | 27 : callback_(callback), task_runner_(task_runner) {} |
28 ~CallbackBinder() override {} | 28 ~CallbackBinder() override {} |
29 | 29 |
30 private: | 30 private: |
31 // InterfaceBinder: | 31 // InterfaceBinder: |
32 void BindInterface(Connection* connection, | 32 void BindInterface(Connection* connection, |
33 const std::string& interface_name, | 33 const std::string& interface_name, |
34 mojo::ScopedMessagePipeHandle client_handle) override { | 34 mojo::ScopedMessagePipeHandle handle) override { |
| 35 mojo::InterfaceRequest<Interface> request = |
| 36 mojo::MakeRequest<Interface>(std::move(handle)); |
35 if (task_runner_) { | 37 if (task_runner_) { |
36 task_runner_->PostTask( | 38 task_runner_->PostTask( |
37 FROM_HERE, | 39 FROM_HERE, |
38 base::Bind(&CallbackBinder::RunCallbackOnTaskRunner, callback_, | 40 base::Bind(&CallbackBinder::RunCallbackOnTaskRunner, callback_, |
39 base::Passed(&client_handle))); | 41 base::Passed(&request))); |
40 return; | 42 return; |
41 } | 43 } |
42 callback_.Run(std::move(client_handle)); | 44 callback_.Run(std::move(request)); |
43 } | 45 } |
44 | 46 |
45 static void RunCallbackOnTaskRunner( | 47 static void RunCallbackOnTaskRunner( |
46 const BindCallback& callback, | 48 const BindCallback& callback, |
47 mojo::ScopedMessagePipeHandle client_handle) { | 49 mojo::InterfaceRequest<Interface> client) { |
48 callback.Run(std::move(client_handle)); | 50 callback.Run(std::move(client)); |
49 } | 51 } |
50 | 52 |
51 const BindCallback callback_; | 53 const BindCallback callback_; |
52 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 54 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
53 DISALLOW_COPY_AND_ASSIGN(CallbackBinder); | 55 DISALLOW_COPY_AND_ASSIGN(CallbackBinder); |
54 }; | 56 }; |
55 | 57 |
| 58 class GenericCallbackBinder : public InterfaceBinder { |
| 59 public: |
| 60 // Method that binds a request for Interface. |
| 61 using BindCallback = base::Callback<void(mojo::ScopedMessagePipeHandle)>; |
| 62 |
| 63 explicit GenericCallbackBinder( |
| 64 const BindCallback& callback, |
| 65 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); |
| 66 ~GenericCallbackBinder() override; |
| 67 |
| 68 private: |
| 69 // InterfaceBinder: |
| 70 void BindInterface( |
| 71 Connection* connection, |
| 72 const std::string& interface_name, |
| 73 mojo::ScopedMessagePipeHandle handle) override; |
| 74 |
| 75 static void RunCallbackOnTaskRunner( |
| 76 const BindCallback& callback, |
| 77 mojo::ScopedMessagePipeHandle client_handle); |
| 78 |
| 79 const BindCallback callback_; |
| 80 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 81 DISALLOW_COPY_AND_ASSIGN(GenericCallbackBinder); |
| 82 }; |
| 83 |
56 } // namespace internal | 84 } // namespace internal |
57 } // namespace shell | 85 } // namespace shell |
58 | 86 |
59 #endif // SERVICES_SHELL_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_ | 87 #endif // SERVICES_SHELL_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_ |
OLD | NEW |