| OLD | NEW |
| (Empty) |
| 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 SERVICES_SERVICE_MANAGER_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_ | |
| 6 #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_ | |
| 7 | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 12 #include "services/service_manager/public/cpp/interface_binder.h" | |
| 13 | |
| 14 namespace service_manager { | |
| 15 namespace internal { | |
| 16 | |
| 17 template <typename Interface> | |
| 18 class CallbackBinder : public InterfaceBinder { | |
| 19 public: | |
| 20 using BindCallback = base::Callback<void(const BindSourceInfo&, | |
| 21 mojo::InterfaceRequest<Interface>)>; | |
| 22 | |
| 23 CallbackBinder(const BindCallback& callback, | |
| 24 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | |
| 25 : callback_(callback), task_runner_(task_runner) {} | |
| 26 ~CallbackBinder() override {} | |
| 27 | |
| 28 private: | |
| 29 // InterfaceBinder: | |
| 30 void BindInterface(const BindSourceInfo& source_info, | |
| 31 const std::string& interface_name, | |
| 32 mojo::ScopedMessagePipeHandle handle) override { | |
| 33 mojo::InterfaceRequest<Interface> request = | |
| 34 mojo::MakeRequest<Interface>(std::move(handle)); | |
| 35 if (task_runner_) { | |
| 36 task_runner_->PostTask(FROM_HERE, | |
| 37 base::Bind(&CallbackBinder::RunCallback, callback_, | |
| 38 source_info, base::Passed(&request))); | |
| 39 } else { | |
| 40 RunCallback(callback_, source_info, std::move(request)); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 static void RunCallback(const BindCallback& callback, | |
| 45 const BindSourceInfo& source_info, | |
| 46 mojo::InterfaceRequest<Interface> request) { | |
| 47 callback.Run(source_info, std::move(request)); | |
| 48 } | |
| 49 | |
| 50 const BindCallback callback_; | |
| 51 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 52 DISALLOW_COPY_AND_ASSIGN(CallbackBinder); | |
| 53 }; | |
| 54 | |
| 55 class GenericCallbackBinder : public InterfaceBinder { | |
| 56 public: | |
| 57 using BindCallback = base::Callback<void(mojo::ScopedMessagePipeHandle)>; | |
| 58 | |
| 59 GenericCallbackBinder( | |
| 60 const BindCallback& callback, | |
| 61 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | |
| 62 ~GenericCallbackBinder() override; | |
| 63 | |
| 64 private: | |
| 65 // InterfaceBinder: | |
| 66 void BindInterface(const BindSourceInfo& source_info, | |
| 67 const std::string& interface_name, | |
| 68 mojo::ScopedMessagePipeHandle handle) override; | |
| 69 | |
| 70 static void RunCallback(const BindCallback& callback, | |
| 71 mojo::ScopedMessagePipeHandle client_handle); | |
| 72 | |
| 73 const BindCallback callback_; | |
| 74 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 75 DISALLOW_COPY_AND_ASSIGN(GenericCallbackBinder); | |
| 76 }; | |
| 77 | |
| 78 } // namespace internal | |
| 79 } // namespace service_manager | |
| 80 | |
| 81 #endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_ | |
| OLD | NEW |