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 // TODO(oysteine): Once this object becomes more complex, make |
| 97 // copying more robust. |
| 98 mojom::CoordinationPolicyPtr policy = mojom::CoordinationPolicy::New(); |
| 99 policy->use_background_priority = background_priority; |
| 100 current_policy_->use_background_priority = background_priority; |
| 101 |
| 102 policy_callback_->SetCoordinationPolicy(std::move(policy)); |
| 103 } |
| 104 |
| 105 void CoordinationUnitImpl::SendEvent(mojom::EventPtr event) { |
| 106 switch (event->type) { |
| 107 case mojom::EventType::kOnWebContentsShown: |
| 108 state_flags_[kTabVisible] = true; |
| 109 break; |
| 110 case mojom::EventType::kOnWebContentsHidden: |
| 111 state_flags_[kTabVisible] = false; |
| 112 break; |
| 113 case mojom::EventType::kOnProcessAudioStarted: |
| 114 state_flags_[kAudioPlaying] = true; |
| 115 break; |
| 116 case mojom::EventType::kOnProcessAudioStopped: |
| 117 state_flags_[kAudioPlaying] = false; |
| 118 break; |
| 119 case mojom::EventType::kTestEvent: |
| 120 state_flags_[kTestState] = true; |
| 121 break; |
| 122 default: |
| 123 return; |
| 124 } |
| 125 |
| 126 RecalcCoordinationPolicy(); |
| 127 } |
| 128 |
| 129 void CoordinationUnitImpl::GetID(const GetIDCallback& callback) { |
| 130 callback.Run(id_); |
| 131 } |
| 132 |
| 133 void CoordinationUnitImpl::Duplicate(mojom::CoordinationUnitRequest request) { |
| 134 bindings_.AddBinding(this, std::move(request)); |
| 135 } |
| 136 |
| 137 void CoordinationUnitImpl::AddChild(const CoordinationUnitID& child_id) { |
| 138 if (child_id == id_) { |
| 139 return; |
| 140 } |
| 141 |
| 142 auto child_iter = g_cu_map().find(child_id); |
| 143 if (child_iter != g_cu_map().end()) { |
| 144 CoordinationUnitImpl* child = child_iter->second; |
| 145 if (HasParent(child) || HasChild(child)) { |
| 146 return; |
| 147 } |
| 148 |
| 149 DCHECK(child->id_ == child_id); |
| 150 DCHECK(child != this); |
| 151 |
| 152 if (AddChild(child)) { |
| 153 child->AddParent(this); |
| 154 } |
| 155 } |
| 156 } |
| 157 |
| 158 bool CoordinationUnitImpl::AddChild(CoordinationUnitImpl* child) { |
| 159 // We don't recalculate the policy here as policies are only dependent |
| 160 // on the current CU or its parents, not its children. In other words, |
| 161 // policies only bubble down. |
| 162 return children_.count(child) ? false : children_.insert(child).second; |
| 163 } |
| 164 |
| 165 void CoordinationUnitImpl::RemoveChild(CoordinationUnitImpl* child) { |
| 166 size_t children_removed = children_.erase(child); |
| 167 DCHECK_EQ(1u, children_removed); |
| 168 } |
| 169 |
| 170 void CoordinationUnitImpl::AddParent(CoordinationUnitImpl* parent) { |
| 171 DCHECK_EQ(0u, parents_.count(parent)); |
| 172 parents_.insert(parent); |
| 173 |
| 174 RecalcCoordinationPolicy(); |
| 175 } |
| 176 |
| 177 void CoordinationUnitImpl::RemoveParent(CoordinationUnitImpl* parent) { |
| 178 size_t parents_removed = parents_.erase(parent); |
| 179 DCHECK_EQ(1u, parents_removed); |
| 180 |
| 181 RecalcCoordinationPolicy(); |
| 182 } |
| 183 |
| 184 bool CoordinationUnitImpl::HasParent(CoordinationUnitImpl* unit) { |
| 185 for (CoordinationUnitImpl* parent : parents_) { |
| 186 if (parent == unit || parent->HasParent(unit)) { |
| 187 return true; |
| 188 } |
| 189 } |
| 190 |
| 191 return false; |
| 192 } |
| 193 |
| 194 bool CoordinationUnitImpl::HasChild(CoordinationUnitImpl* unit) { |
| 195 for (CoordinationUnitImpl* child : children_) { |
| 196 if (child == unit || child->HasChild(unit)) { |
| 197 return true; |
| 198 } |
| 199 } |
| 200 |
| 201 return false; |
| 202 } |
| 203 |
| 204 void CoordinationUnitImpl::SetCoordinationPolicyCallback( |
| 205 mojom::CoordinationPolicyCallbackPtr callback) { |
| 206 callback.set_connection_error_handler( |
| 207 base::Bind(&CoordinationUnitImpl::UnregisterCoordinationPolicyCallback, |
| 208 base::Unretained(this))); |
| 209 |
| 210 policy_callback_ = std::move(callback); |
| 211 |
| 212 RecalcCoordinationPolicy(); |
| 213 } |
| 214 |
| 215 void CoordinationUnitImpl::UnregisterCoordinationPolicyCallback() { |
| 216 policy_callback_.reset(); |
| 217 current_policy_.reset(); |
| 218 } |
| 219 |
| 220 } // namespace resource_coordinator |
OLD | NEW |