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

Side by Side Diff: services/shell/public/cpp/lib/callback_binder.h

Issue 2419723002: Move services/shell to services/service_manager (Closed)
Patch Set: rebase Created 4 years, 2 months 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 unified diff | Download patch
OLDNEW
(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_SHELL_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_
6 #define SERVICES_SHELL_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/shell/public/cpp/interface_binder.h"
13
14 namespace shell {
15 namespace internal {
16
17 template <typename Interface>
18 class CallbackBinder : public InterfaceBinder {
19 public:
20 // Method that binds a request for Interface.
21 using BindCallback =
22 base::Callback<void(mojo::InterfaceRequest<Interface>)>;
23
24 explicit CallbackBinder(
25 const BindCallback& callback,
26 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
27 : callback_(callback), task_runner_(task_runner) {}
28 ~CallbackBinder() override {}
29
30 private:
31 // InterfaceBinder:
32 void BindInterface(
33 const Identity& remote_identity,
34 const std::string& interface_name,
35 mojo::ScopedMessagePipeHandle handle) override {
36 mojo::InterfaceRequest<Interface> request =
37 mojo::MakeRequest<Interface>(std::move(handle));
38 if (task_runner_) {
39 task_runner_->PostTask(
40 FROM_HERE,
41 base::Bind(&CallbackBinder::RunCallbackOnTaskRunner, callback_,
42 base::Passed(&request)));
43 return;
44 }
45 callback_.Run(std::move(request));
46 }
47
48 static void RunCallbackOnTaskRunner(
49 const BindCallback& callback,
50 mojo::InterfaceRequest<Interface> client) {
51 callback.Run(std::move(client));
52 }
53
54 const BindCallback callback_;
55 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
56 DISALLOW_COPY_AND_ASSIGN(CallbackBinder);
57 };
58
59 class GenericCallbackBinder : public InterfaceBinder {
60 public:
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(const shell::Identity& remote_identity,
71 const std::string& interface_name,
72 mojo::ScopedMessagePipeHandle handle) override;
73
74 static void RunCallbackOnTaskRunner(
75 const BindCallback& callback,
76 mojo::ScopedMessagePipeHandle client_handle);
77
78 const BindCallback callback_;
79 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
80 DISALLOW_COPY_AND_ASSIGN(GenericCallbackBinder);
81 };
82
83 } // namespace internal
84 } // namespace shell
85
86 #endif // SERVICES_SHELL_PUBLIC_CPP_LIB_CALLBACK_BINDER_H_
OLDNEW
« no previous file with comments | « services/shell/public/cpp/interface_registry.h ('k') | services/shell/public/cpp/lib/callback_binder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698