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 "base/hash.h" | |
8 #include "base/lazy_instance.h" | |
9 #include "base/logging.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "mojo/public/cpp/bindings/strong_binding.h" | |
12 #include "services/resource_coordinator/public/cpp/coordination_unit_events.h" | |
13 #include "services/service_manager/public/cpp/connection.h" | |
14 | |
15 namespace resource_coordinator { | |
16 | |
17 namespace { | |
18 | |
19 base::LazyInstance<std::unordered_map<int64_t, CoordinationUnitImpl*>>::Leaky | |
Primiano Tucci (use gerrit)
2017/04/06 18:09:46
Since we have thread-safe statics in C++11, I thin
oystein (OOO til 10th of July)
2017/04/10 20:02:58
Oh glad we can do this now; done!
| |
20 g_cu_map = LAZY_INSTANCE_INITIALIZER; | |
21 | |
22 int64_t CUIDHash(const CoordinationUnitID& id) { | |
23 return (((uint64_t)id.type) << 32) ^ id.id; | |
Primiano Tucci (use gerrit)
2017/04/06 18:09:46
(uint64_t) -> static_cast<uint64_t>.
I hate it and
oystein (OOO til 10th of July)
2017/04/10 20:02:59
Done and done.
| |
24 } | |
25 | |
26 } // namespace | |
27 | |
28 CoordinationUnitImpl::CoordinationUnitImpl( | |
Primiano Tucci (use gerrit)
2017/04/06 18:09:46
on which thread are these CUnits built and destroy
oystein (OOO til 10th of July)
2017/04/10 20:02:59
Mojo guarantees it'll all be run on the same taskr
Primiano Tucci (use gerrit)
2017/04/11 18:04:51
Acknowledged.
| |
29 const CoordinationUnitID& id, | |
30 std::unique_ptr<service_manager::ServiceContextRef> service_ref) | |
31 : id_(id) { | |
32 id_hash_ = CUIDHash(id); | |
33 | |
34 auto& cu_map = g_cu_map.Get(); | |
35 DCHECK(cu_map.end() == cu_map.find(id_hash_)); | |
Primiano Tucci (use gerrit)
2017/04/06 18:09:46
really a minor perf thingy, as this is debug only,
oystein (OOO til 10th of July)
2017/04/10 20:02:57
Done; didn't realize this but makes sense in hinds
Primiano Tucci (use gerrit)
2017/04/11 18:04:51
Acknowledged.
| |
36 | |
37 cu_map.insert(std::make_pair(id_hash_, this)); | |
38 service_ref_ = std::move(service_ref); | |
39 } | |
40 | |
41 CoordinationUnitImpl::~CoordinationUnitImpl() { | |
42 g_cu_map.Get().erase(id_hash_); | |
43 | |
44 for (auto* child : children_) { | |
45 child->RemoveParent(this); | |
46 } | |
47 | |
48 for (auto* parent : parents_) { | |
49 parent->RemoveChild(this); | |
50 } | |
51 } | |
52 | |
53 bool CoordinationUnitImpl::SelfOrParentHasFlagSet(StateFlags state) { | |
54 const base::Optional<bool>& state_flag = state_flags_[state]; | |
55 if (state_flag && *state_flag) | |
56 return true; | |
57 | |
58 for (auto* parent : parents_) { | |
59 if (parent->SelfOrParentHasFlagSet(state)) | |
60 return true; | |
61 } | |
62 | |
63 return false; | |
64 } | |
65 | |
66 void CoordinationUnitImpl::RecalcPolicy() { | |
67 for (auto* child : children_) { | |
68 child->RecalcPolicy(); | |
69 } | |
70 | |
71 if (!policy_callback_) { | |
72 return; | |
73 } | |
74 | |
75 bool background_priority = !SelfOrParentHasFlagSet(TAB_VISIBLE) && | |
76 !SelfOrParentHasFlagSet(AUDIO_PLAYING); | |
77 | |
78 // Send the priority to the client if it's new or changed. | |
79 if (!current_policy_) | |
80 current_policy_ = mojom::Policy::New(); | |
81 else if ((current_policy_->use_background_priority == background_priority) && | |
82 !SelfOrParentHasFlagSet(TEST_STATE)) { | |
83 return; | |
84 } | |
85 | |
86 mojom::PolicyPtr policy = mojom::Policy::New(); | |
87 policy->use_background_priority = background_priority; | |
88 current_policy_->use_background_priority = background_priority; | |
89 | |
90 policy_callback_->SetPolicy(std::move(policy)); | |
91 } | |
92 | |
93 void CoordinationUnitImpl::SendEvent(mojom::EventPtr event) { | |
94 switch (event->type) { | |
95 case ON_WEBCONTENTS_SHOWN: | |
96 state_flags_[TAB_VISIBLE] = true; | |
97 break; | |
98 case ON_WEBCONTENTS_HIDDEN: | |
99 state_flags_[TAB_VISIBLE] = false; | |
100 break; | |
101 case ON_PROCESS_AUDIO_STARTED: | |
102 state_flags_[AUDIO_PLAYING] = true; | |
103 break; | |
104 case ON_PROCESS_AUDIO_STOPPED: | |
105 state_flags_[AUDIO_PLAYING] = false; | |
106 break; | |
107 case TEST_EVENT: | |
108 state_flags_[TEST_STATE] = true; | |
109 break; | |
110 default: | |
111 return; | |
112 } | |
113 | |
114 RecalcPolicy(); | |
115 } | |
116 | |
117 void CoordinationUnitImpl::GetID(const GetIDCallback& callback) { | |
118 callback.Run(id_); | |
Primiano Tucci (use gerrit)
2017/04/06 18:09:46
uh? This might deserve some comment :)
why the cal
oystein (OOO til 10th of July)
2017/04/10 20:02:59
Added a comment in the .mojom.
It's mainly if you
| |
119 } | |
120 | |
121 void CoordinationUnitImpl::Duplicate(mojom::CoordinationUnitRequest request) { | |
122 bindings_.AddBinding(this, std::move(request)); | |
123 } | |
124 | |
125 void CoordinationUnitImpl::AddChild(const CoordinationUnitID& child_id) { | |
126 if (child_id == id_) | |
127 return; | |
128 | |
129 auto child_iter = g_cu_map.Get().find(CUIDHash(child_id)); | |
130 if (child_iter != g_cu_map.Get().end()) { | |
131 CoordinationUnitImpl* child = child_iter->second; | |
132 if (HasParent(child) || HasChild(child)) | |
133 return; | |
134 | |
135 DCHECK(child->id_ == child_id); | |
Primiano Tucci (use gerrit)
2017/04/06 18:09:46
Both DCHECK(a == b) or DCHECK_EQ(a, b) are fine, I
oystein (OOO til 10th of July)
2017/04/10 20:02:57
That was my first attempt at this check, but I get
| |
136 DCHECK(child != this); | |
137 | |
138 if (AddChild(child)) { | |
139 child->AddParent(this); | |
140 } | |
141 } | |
142 } | |
143 | |
144 bool CoordinationUnitImpl::AddChild(CoordinationUnitImpl* child) { | |
145 if (children_.find(child) != children_.end()) | |
Primiano Tucci (use gerrit)
2017/04/06 18:09:46
just
return children_.count(child) ? false : child
oystein (OOO til 10th of July)
2017/04/10 20:02:59
Done.
| |
146 return false; | |
147 | |
148 children_.insert(child); | |
149 | |
150 return true; | |
151 } | |
152 | |
153 void CoordinationUnitImpl::RemoveChild(CoordinationUnitImpl* child) { | |
154 size_t children_removed = children_.erase(child); | |
155 CHECK_EQ(1u, children_removed); | |
Primiano Tucci (use gerrit)
2017/04/06 18:09:46
just checking: is this really intended to be a che
oystein (OOO til 10th of July)
2017/04/10 20:02:59
Typo; fixed.
| |
156 } | |
157 | |
158 void CoordinationUnitImpl::AddParent(CoordinationUnitImpl* parent) { | |
159 DCHECK(parents_.find(parent) == parents_.end()); | |
160 parents_.insert(parent); | |
161 | |
162 RecalcPolicy(); | |
163 } | |
164 | |
165 void CoordinationUnitImpl::RemoveParent(CoordinationUnitImpl* parent) { | |
166 size_t parents_removed = parents_.erase(parent); | |
167 CHECK_EQ(1u, parents_removed); | |
168 | |
169 RecalcPolicy(); | |
170 } | |
171 | |
172 bool CoordinationUnitImpl::HasParent(CoordinationUnitImpl* unit) { | |
173 for (auto* parent : parents_) { | |
174 if (parent == unit || parent->HasParent(unit)) | |
175 return true; | |
176 } | |
177 | |
178 return false; | |
179 } | |
180 | |
181 bool CoordinationUnitImpl::HasChild(CoordinationUnitImpl* unit) { | |
182 for (auto* child : children_) { | |
183 if (child == unit || child->HasChild(unit)) | |
184 return true; | |
185 } | |
186 | |
187 return false; | |
188 } | |
189 | |
190 void CoordinationUnitImpl::SetPolicyCallback( | |
191 mojom::PolicyCallbackPtr callback) { | |
192 callback.set_connection_error_handler(base::Bind( | |
193 &CoordinationUnitImpl::UnregisterPolicyCallback, base::Unretained(this))); | |
Primiano Tucci (use gerrit)
2017/04/06 18:09:46
what happens if the CUI is destroyed in the meanwh
oystein (OOO til 10th of July)
2017/04/10 20:02:59
Mojo handles the lifespan and will only call this
| |
194 | |
195 policy_callback_ = std::move(callback); | |
196 | |
197 RecalcPolicy(); | |
198 } | |
199 | |
200 void CoordinationUnitImpl::UnregisterPolicyCallback() { | |
201 policy_callback_.reset(); | |
202 current_policy_.reset(); | |
203 } | |
204 | |
205 } // namespace resource_coordinator | |
OLD | NEW |