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 MEDIA_MOJO_SERVICES_STRONG_BINDING_SET_H_ | |
6 #define MEDIA_MOJO_SERVICES_STRONG_BINDING_SET_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/ptr_util.h" | |
15 #include "mojo/public/cpp/bindings/binding.h" | |
16 #include "mojo/public/cpp/bindings/interface_request.h" | |
17 | |
18 namespace media { | |
19 | |
20 // This class manages a set of bindings. When the pipe a binding is bound to is | |
21 // disconnected, the binding is automatically destroyed and removed from the | |
22 // set, and the interface implementation is deleted. When the StrongBindingSet | |
23 // is destructed, all outstanding bindings in the set are destroyed and all the | |
24 // bound interface implementations are automatically deleted. | |
25 template <typename Interface> | |
26 class StrongBindingSet { | |
xhwang
2016/11/23 22:27:09
rockot: Following our offline discussion. Please t
xhwang
2016/11/29 10:20:57
Test added.
| |
27 public: | |
28 using BindingId = size_t; | |
29 using RequestType = mojo::InterfaceRequest<Interface>; | |
30 | |
31 StrongBindingSet() {} | |
32 ~StrongBindingSet() {} | |
33 | |
34 void AddBinding(std::unique_ptr<Interface> impl, RequestType request) { | |
35 BindingId binding_id = next_binding_id_++; | |
36 std::unique_ptr<Entry> entry = base::MakeUnique<Entry>( | |
37 std::move(impl), std::move(request), this, binding_id); | |
38 bindings_.insert(std::make_pair(binding_id, std::move(entry))); | |
39 } | |
40 | |
41 private: | |
42 class Entry { | |
43 public: | |
44 Entry(std::unique_ptr<Interface> impl, | |
45 RequestType request, | |
46 StrongBindingSet* binding_set, | |
47 BindingId binding_id) | |
48 : impl_(std::move(impl)), | |
49 binding_(impl_.get(), std::move(request)), | |
50 binding_set_(binding_set), | |
51 binding_id_(binding_id) { | |
52 binding_.set_connection_error_with_reason_handler( | |
53 base::Bind(&Entry::OnConnectionError, base::Unretained(this))); | |
Ken Rockot(use gerrit already)
2016/11/23 22:33:31
nit, you could just bind directly to BindingSet::O
xhwang
2016/11/29 10:20:57
Done.
| |
54 } | |
55 | |
56 void OnConnectionError(uint32_t custom_reason, | |
57 const std::string& description) { | |
58 binding_set_->OnConnectionError(binding_id_, custom_reason, description); | |
59 } | |
60 | |
61 // Holds ownership of the interface implementation. | |
62 std::unique_ptr<Interface> impl_; | |
63 | |
64 mojo::Binding<Interface> binding_; | |
65 StrongBindingSet* const binding_set_; | |
66 const BindingId binding_id_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(Entry); | |
69 }; | |
70 | |
71 void OnConnectionError(BindingId binding_id, | |
72 uint32_t custom_reason, | |
73 const std::string& description) { | |
74 auto it = bindings_.find(binding_id); | |
75 DCHECK(it != bindings_.end()); | |
76 bindings_.erase(it); | |
77 } | |
78 | |
79 std::map<BindingId, std::unique_ptr<Entry>> bindings_; | |
80 BindingId next_binding_id_ = 0; | |
81 | |
82 DISALLOW_COPY_AND_ASSIGN(StrongBindingSet); | |
83 }; | |
84 | |
85 } // namespace media | |
86 | |
87 #endif // MEDIA_MOJO_SERVICES_STRONG_BINDING_SET_H_ | |
OLD | NEW |