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/logging.h" | |
8 #include "base/process/process_handle.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "mojo/public/cpp/bindings/strong_binding.h" | |
11 #include "services/resource_coordinator/public/cpp/coordination_unit_id.h" | |
12 | |
13 namespace resource_coordinator { | |
14 | |
15 namespace { | |
16 | |
17 using CUIDMap = std::unordered_map<CoordinationUnitID, CoordinationUnitImpl*>; | |
18 | |
19 CUIDMap* g_cu_map() { | |
20 static CUIDMap* instance = new CUIDMap(); | |
21 return instance; | |
dcheng
2017/04/18 05:40:46
Consider returning a ref, since this is never null
oystein (OOO til 10th of July)
2017/04/18 20:55:33
Done.
| |
22 } | |
23 | |
24 } // namespace | |
25 | |
26 CoordinationUnitImpl::CoordinationUnitImpl( | |
27 const CoordinationUnitID& id, | |
28 std::unique_ptr<service_manager::ServiceContextRef> service_ref) { | |
29 if (!id.id) { | |
30 static int next_id = 0; | |
31 id_ = CoordinationUnitID(id.type, | |
32 base::IntToString(base::GetCurrentProcId()) + "." + | |
dcheng
2017/04/18 05:40:46
I don't entirely understand the purpose of buildin
oystein (OOO til 10th of July)
2017/04/18 20:55:33
UnguessableToken makes more sense; this was more a
| |
33 base::IntToString(++next_id)); | |
34 } else { | |
35 id_ = id; | |
36 } | |
37 | |
38 auto it = g_cu_map()->insert(std::make_pair(id_, this)); | |
39 DCHECK(it.second); // Inserted successfully | |
40 | |
41 service_ref_ = std::move(service_ref); | |
dcheng
2017/04/18 05:40:46
Nit: #include <utility>
oystein (OOO til 10th of July)
2017/04/18 20:55:33
Done.
| |
42 } | |
43 | |
44 CoordinationUnitImpl::~CoordinationUnitImpl() { | |
45 g_cu_map()->erase(id_); | |
46 | |
47 for (CoordinationUnitImpl* child : children_) { | |
48 child->RemoveParent(this); | |
49 } | |
50 | |
51 for (CoordinationUnitImpl* parent : parents_) { | |
52 parent->RemoveChild(this); | |
53 } | |
54 } | |
55 | |
56 bool CoordinationUnitImpl::SelfOrParentHasFlagSet(StateFlags state) { | |
57 const base::Optional<bool>& state_flag = state_flags_[state]; | |
58 if (state_flag && *state_flag) | |
59 return true; | |
60 | |
61 for (CoordinationUnitImpl* parent : parents_) { | |
62 if (parent->SelfOrParentHasFlagSet(state)) | |
63 return true; | |
64 } | |
65 | |
66 return false; | |
67 } | |
68 | |
69 void CoordinationUnitImpl::RecalcPolicy() { | |
70 for (CoordinationUnitImpl* child : children_) { | |
71 child->RecalcPolicy(); | |
72 } | |
73 | |
74 if (!policy_callback_) { | |
75 return; | |
76 } | |
77 | |
78 bool background_priority = !SelfOrParentHasFlagSet(TAB_VISIBLE) && | |
79 !SelfOrParentHasFlagSet(AUDIO_PLAYING); | |
80 | |
81 // Send the priority to the client if it's new or changed. | |
82 if (!current_policy_) | |
83 current_policy_ = mojom::Policy::New(); | |
84 else if ((current_policy_->use_background_priority == background_priority) && | |
85 !SelfOrParentHasFlagSet(StateFlags::TEST_STATE)) { | |
86 return; | |
87 } | |
88 | |
89 mojom::PolicyPtr policy = mojom::Policy::New(); | |
90 policy->use_background_priority = background_priority; | |
91 current_policy_->use_background_priority = background_priority; | |
92 | |
93 policy_callback_->SetPolicy(std::move(policy)); | |
94 } | |
95 | |
96 void CoordinationUnitImpl::SendEvent(mojom::EventPtr event) { | |
97 switch (event->type) { | |
98 case mojom::EventType::ON_WEBCONTENTS_SHOWN: | |
99 state_flags_[TAB_VISIBLE] = true; | |
100 break; | |
101 case mojom::EventType::ON_WEBCONTENTS_HIDDEN: | |
102 state_flags_[TAB_VISIBLE] = false; | |
103 break; | |
104 case mojom::EventType::ON_PROCESS_AUDIO_STARTED: | |
105 state_flags_[AUDIO_PLAYING] = true; | |
106 break; | |
107 case mojom::EventType::ON_PROCESS_AUDIO_STOPPED: | |
108 state_flags_[AUDIO_PLAYING] = false; | |
109 break; | |
110 case mojom::EventType::TEST_EVENT: | |
111 state_flags_[TEST_STATE] = true; | |
112 break; | |
113 default: | |
114 return; | |
115 } | |
116 | |
117 RecalcPolicy(); | |
118 } | |
119 | |
120 void CoordinationUnitImpl::GetID(const GetIDCallback& callback) { | |
121 callback.Run(id_); | |
122 } | |
123 | |
124 void CoordinationUnitImpl::Duplicate(mojom::CoordinationUnitRequest request) { | |
125 bindings_.AddBinding(this, std::move(request)); | |
126 } | |
127 | |
128 void CoordinationUnitImpl::AddChild(const CoordinationUnitID& child_id) { | |
129 if (child_id == id_) | |
130 return; | |
131 | |
132 auto child_iter = g_cu_map()->find(child_id); | |
133 if (child_iter != g_cu_map()->end()) { | |
134 CoordinationUnitImpl* child = child_iter->second; | |
135 if (HasParent(child) || HasChild(child)) | |
136 return; | |
137 | |
138 DCHECK(child->id_ == child_id); | |
139 DCHECK(child != this); | |
140 | |
141 if (AddChild(child)) { | |
142 child->AddParent(this); | |
143 } | |
144 } | |
145 } | |
146 | |
147 bool CoordinationUnitImpl::AddChild(CoordinationUnitImpl* child) { | |
148 return children_.count(child) ? false : children_.insert(child).second; | |
149 } | |
150 | |
151 void CoordinationUnitImpl::RemoveChild(CoordinationUnitImpl* child) { | |
152 size_t children_removed = children_.erase(child); | |
153 DCHECK_EQ(1u, children_removed); | |
154 } | |
155 | |
156 void CoordinationUnitImpl::AddParent(CoordinationUnitImpl* parent) { | |
157 DCHECK_EQ(0u, parents_.count(parent)); | |
158 parents_.insert(parent); | |
159 | |
160 RecalcPolicy(); | |
161 } | |
162 | |
163 void CoordinationUnitImpl::RemoveParent(CoordinationUnitImpl* parent) { | |
164 size_t parents_removed = parents_.erase(parent); | |
165 DCHECK_EQ(1u, parents_removed); | |
166 | |
167 RecalcPolicy(); | |
168 } | |
169 | |
170 bool CoordinationUnitImpl::HasParent(CoordinationUnitImpl* unit) { | |
171 for (CoordinationUnitImpl* parent : parents_) { | |
172 if (parent == unit || parent->HasParent(unit)) | |
173 return true; | |
174 } | |
175 | |
176 return false; | |
177 } | |
178 | |
179 bool CoordinationUnitImpl::HasChild(CoordinationUnitImpl* unit) { | |
180 for (CoordinationUnitImpl* child : children_) { | |
181 if (child == unit || child->HasChild(unit)) | |
182 return true; | |
183 } | |
184 | |
185 return false; | |
186 } | |
187 | |
188 void CoordinationUnitImpl::SetPolicyCallback( | |
189 mojom::PolicyCallbackPtr callback) { | |
190 callback.set_connection_error_handler(base::Bind( | |
191 &CoordinationUnitImpl::UnregisterPolicyCallback, base::Unretained(this))); | |
192 | |
193 policy_callback_ = std::move(callback); | |
194 | |
195 RecalcPolicy(); | |
196 } | |
197 | |
198 void CoordinationUnitImpl::UnregisterPolicyCallback() { | |
199 policy_callback_.reset(); | |
200 current_policy_.reset(); | |
201 } | |
202 | |
203 } // namespace resource_coordinator | |
OLD | NEW |