OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "services/resource_coordinator/coordination_unit/coordination_unit_impl .h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/process/process_handle.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/unguessable_token.h" | |
14 #include "mojo/public/cpp/bindings/strong_binding.h" | |
15 #include "services/resource_coordinator/public/cpp/coordination_unit_id.h" | |
16 | |
17 namespace resource_coordinator { | |
18 | |
19 namespace { | |
20 | |
21 using CUIDMap = std::unordered_map<CoordinationUnitID, CoordinationUnitImpl*>; | |
22 | |
23 CUIDMap& g_cu_map() { | |
24 static CUIDMap* instance = new CUIDMap(); | |
25 return *instance; | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 CoordinationUnitImpl::CoordinationUnitImpl( | |
31 const CoordinationUnitID& id, | |
32 std::unique_ptr<service_manager::ServiceContextRef> service_ref) { | |
33 if (!id.id) { | |
34 id_ = CoordinationUnitID(id.type, | |
35 base::UnguessableToken().Create().ToString()); | |
36 } else { | |
37 id_ = id; | |
38 } | |
39 | |
40 auto it = g_cu_map().insert(std::make_pair(id_, this)); | |
41 DCHECK(it.second); // Inserted successfully | |
42 | |
43 service_ref_ = std::move(service_ref); | |
44 } | |
45 | |
46 CoordinationUnitImpl::~CoordinationUnitImpl() { | |
47 g_cu_map().erase(id_); | |
48 | |
49 for (CoordinationUnitImpl* child : children_) { | |
50 child->RemoveParent(this); | |
51 } | |
52 | |
53 for (CoordinationUnitImpl* parent : parents_) { | |
54 parent->RemoveChild(this); | |
55 } | |
56 } | |
57 | |
58 bool CoordinationUnitImpl::SelfOrParentHasFlagSet(StateFlags state) { | |
59 const base::Optional<bool>& state_flag = state_flags_[state]; | |
60 if (state_flag && *state_flag) { | |
61 return true; | |
62 } | |
63 | |
64 for (CoordinationUnitImpl* parent : parents_) { | |
65 if (parent->SelfOrParentHasFlagSet(state)) { | |
66 return true; | |
67 } | |
68 } | |
69 | |
70 return false; | |
71 } | |
72 | |
73 void CoordinationUnitImpl::RecalcCoordinationPolicy() { | |
74 for (CoordinationUnitImpl* child : children_) { | |
75 child->RecalcCoordinationPolicy(); | |
76 } | |
77 | |
78 if (!policy_callback_) { | |
79 return; | |
80 } | |
81 | |
82 bool background_priority = !SelfOrParentHasFlagSet(kTabVisible) && | |
83 !SelfOrParentHasFlagSet(kAudioPlaying); | |
84 | |
85 // Send the priority to the client if it's new or changed. | |
86 if (!current_policy_) { | |
87 current_policy_ = mojom::CoordinationPolicy::New(); | |
88 } else if ((current_policy_->use_background_priority == | |
89 background_priority) && | |
90 !SelfOrParentHasFlagSet(StateFlags::kTestState)) { | |
91 return; | |
92 } | |
93 | |
94 // current_policy_ should be kept in sync with the policy we | |
95 // send to the client, to avoid redundant updates. | |
96 mojom::CoordinationPolicyPtr policy = mojom::CoordinationPolicy::New(); | |
nasko
2017/04/25 21:40:56
nit: Since this is supposed to be a copy, it might
oystein (OOO til 10th of July)
2017/04/26 18:15:00
Acknowledged, added TODO.
| |
97 policy->use_background_priority = background_priority; | |
98 current_policy_->use_background_priority = background_priority; | |
99 | |
100 policy_callback_->SetCoordinationPolicy(std::move(policy)); | |
101 } | |
102 | |
103 void CoordinationUnitImpl::SendEvent(mojom::EventPtr event) { | |
104 switch (event->type) { | |
105 case mojom::EventType::kOnWebContentsShown: | |
106 state_flags_[kTabVisible] = true; | |
107 break; | |
108 case mojom::EventType::kOnWebContentsHidden: | |
109 state_flags_[kTabVisible] = false; | |
110 break; | |
111 case mojom::EventType::kOnProcessAudioStarted: | |
112 state_flags_[kAudioPlaying] = true; | |
113 break; | |
114 case mojom::EventType::kOnProcessAudioStopped: | |
115 state_flags_[kAudioPlaying] = false; | |
116 break; | |
117 case mojom::EventType::kTestEvent: | |
118 state_flags_[kTestState] = true; | |
119 break; | |
120 default: | |
121 return; | |
122 } | |
123 | |
124 RecalcCoordinationPolicy(); | |
125 } | |
126 | |
127 void CoordinationUnitImpl::GetID(const GetIDCallback& callback) { | |
128 callback.Run(id_); | |
129 } | |
130 | |
131 void CoordinationUnitImpl::Duplicate(mojom::CoordinationUnitRequest request) { | |
132 bindings_.AddBinding(this, std::move(request)); | |
133 } | |
134 | |
135 void CoordinationUnitImpl::AddChild(const CoordinationUnitID& child_id) { | |
136 if (child_id == id_) { | |
137 return; | |
138 } | |
139 | |
140 auto child_iter = g_cu_map().find(child_id); | |
141 if (child_iter != g_cu_map().end()) { | |
142 CoordinationUnitImpl* child = child_iter->second; | |
143 if (HasParent(child) || HasChild(child)) { | |
144 return; | |
145 } | |
146 | |
147 DCHECK(child->id_ == child_id); | |
148 DCHECK(child != this); | |
149 | |
150 if (AddChild(child)) { | |
151 child->AddParent(this); | |
152 } | |
153 } else { | |
154 LOG(ERROR) << "CoordinationUnitImpl::AddChild failed due to child_id " | |
nasko
2017/04/25 21:40:56
Is this intended to stay and be committed?
oystein (OOO til 10th of July)
2017/04/26 18:15:00
Whoops, nope. Removed.
| |
155 "lookup failing."; | |
156 } | |
157 } | |
158 | |
159 bool CoordinationUnitImpl::AddChild(CoordinationUnitImpl* child) { | |
160 // We don't recalculate the policy here as policies are only dependent | |
161 // on the current CU or its parents, not its children. In other words, | |
162 // policies only bubble down. | |
163 return children_.count(child) ? false : children_.insert(child).second; | |
164 } | |
165 | |
166 void CoordinationUnitImpl::RemoveChild(CoordinationUnitImpl* child) { | |
167 size_t children_removed = children_.erase(child); | |
168 DCHECK_EQ(1u, children_removed); | |
169 } | |
170 | |
171 void CoordinationUnitImpl::AddParent(CoordinationUnitImpl* parent) { | |
172 DCHECK_EQ(0u, parents_.count(parent)); | |
173 parents_.insert(parent); | |
174 | |
175 RecalcCoordinationPolicy(); | |
176 } | |
177 | |
178 void CoordinationUnitImpl::RemoveParent(CoordinationUnitImpl* parent) { | |
179 size_t parents_removed = parents_.erase(parent); | |
180 DCHECK_EQ(1u, parents_removed); | |
181 | |
182 RecalcCoordinationPolicy(); | |
183 } | |
184 | |
185 bool CoordinationUnitImpl::HasParent(CoordinationUnitImpl* unit) { | |
186 for (CoordinationUnitImpl* parent : parents_) { | |
187 if (parent == unit || parent->HasParent(unit)) { | |
188 return true; | |
189 } | |
190 } | |
191 | |
192 return false; | |
193 } | |
194 | |
195 bool CoordinationUnitImpl::HasChild(CoordinationUnitImpl* unit) { | |
196 for (CoordinationUnitImpl* child : children_) { | |
197 if (child == unit || child->HasChild(unit)) { | |
198 return true; | |
199 } | |
200 } | |
201 | |
202 return false; | |
203 } | |
204 | |
205 void CoordinationUnitImpl::SetCoordinationPolicyCallback( | |
206 mojom::CoordinationPolicyCallbackPtr callback) { | |
207 callback.set_connection_error_handler( | |
208 base::Bind(&CoordinationUnitImpl::UnregisterCoordinationPolicyCallback, | |
209 base::Unretained(this))); | |
210 | |
211 policy_callback_ = std::move(callback); | |
212 | |
213 RecalcCoordinationPolicy(); | |
214 } | |
215 | |
216 void CoordinationUnitImpl::UnregisterCoordinationPolicyCallback() { | |
217 policy_callback_.reset(); | |
218 current_policy_.reset(); | |
219 } | |
220 | |
221 } // namespace resource_coordinator | |
OLD | NEW |