Chromium Code Reviews| 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() { | |
|
nasko
2017/04/21 00:17:32
Can't we just use base::LazyInstance<CUIDMap>?
dcheng
2017/04/21 00:23:16
I'm technically not here, but the jury is out on t
oystein (OOO til 10th of July)
2017/04/21 17:57:55
Yeah I was originally using LazyInstance, but ther
| |
| 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 for (CoordinationUnitImpl* parent : parents_) { | |
| 64 if (parent->SelfOrParentHasFlagSet(state)) | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 void CoordinationUnitImpl::RecalcPolicy() { | |
| 72 for (CoordinationUnitImpl* child : children_) { | |
| 73 child->RecalcPolicy(); | |
| 74 } | |
| 75 | |
| 76 if (!policy_callback_) { | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 bool background_priority = !SelfOrParentHasFlagSet(TAB_VISIBLE) && | |
| 81 !SelfOrParentHasFlagSet(AUDIO_PLAYING); | |
| 82 | |
| 83 // Send the priority to the client if it's new or changed. | |
| 84 if (!current_policy_) | |
| 85 current_policy_ = mojom::Policy::New(); | |
| 86 else if ((current_policy_->use_background_priority == background_priority) && | |
| 87 !SelfOrParentHasFlagSet(StateFlags::TEST_STATE)) { | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 mojom::PolicyPtr policy = mojom::Policy::New(); | |
| 92 policy->use_background_priority = background_priority; | |
| 93 current_policy_->use_background_priority = background_priority; | |
| 94 | |
| 95 policy_callback_->SetPolicy(std::move(policy)); | |
| 96 } | |
| 97 | |
| 98 void CoordinationUnitImpl::SendEvent(mojom::EventPtr event) { | |
| 99 switch (event->type) { | |
| 100 case mojom::EventType::ON_WEBCONTENTS_SHOWN: | |
| 101 state_flags_[TAB_VISIBLE] = true; | |
| 102 break; | |
| 103 case mojom::EventType::ON_WEBCONTENTS_HIDDEN: | |
| 104 state_flags_[TAB_VISIBLE] = false; | |
| 105 break; | |
| 106 case mojom::EventType::ON_PROCESS_AUDIO_STARTED: | |
| 107 state_flags_[AUDIO_PLAYING] = true; | |
| 108 break; | |
| 109 case mojom::EventType::ON_PROCESS_AUDIO_STOPPED: | |
| 110 state_flags_[AUDIO_PLAYING] = false; | |
| 111 break; | |
| 112 case mojom::EventType::TEST_EVENT: | |
| 113 state_flags_[TEST_STATE] = true; | |
| 114 break; | |
| 115 default: | |
| 116 return; | |
| 117 } | |
| 118 | |
| 119 RecalcPolicy(); | |
| 120 } | |
| 121 | |
| 122 void CoordinationUnitImpl::GetID(const GetIDCallback& callback) { | |
| 123 callback.Run(id_); | |
| 124 } | |
| 125 | |
| 126 void CoordinationUnitImpl::Duplicate(mojom::CoordinationUnitRequest request) { | |
| 127 bindings_.AddBinding(this, std::move(request)); | |
| 128 } | |
| 129 | |
| 130 void CoordinationUnitImpl::AddChild(const CoordinationUnitID& child_id) { | |
| 131 if (child_id == id_) | |
| 132 return; | |
| 133 | |
| 134 auto child_iter = g_cu_map().find(child_id); | |
| 135 if (child_iter != g_cu_map().end()) { | |
| 136 CoordinationUnitImpl* child = child_iter->second; | |
| 137 if (HasParent(child) || HasChild(child)) | |
| 138 return; | |
| 139 | |
| 140 DCHECK(child->id_ == child_id); | |
| 141 DCHECK(child != this); | |
| 142 | |
| 143 if (AddChild(child)) { | |
| 144 child->AddParent(this); | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 bool CoordinationUnitImpl::AddChild(CoordinationUnitImpl* child) { | |
| 150 return children_.count(child) ? false : children_.insert(child).second; | |
| 151 } | |
| 152 | |
| 153 void CoordinationUnitImpl::RemoveChild(CoordinationUnitImpl* child) { | |
| 154 size_t children_removed = children_.erase(child); | |
| 155 DCHECK_EQ(1u, children_removed); | |
| 156 } | |
| 157 | |
| 158 void CoordinationUnitImpl::AddParent(CoordinationUnitImpl* parent) { | |
| 159 DCHECK_EQ(0u, parents_.count(parent)); | |
| 160 parents_.insert(parent); | |
| 161 | |
| 162 RecalcPolicy(); | |
| 163 } | |
| 164 | |
| 165 void CoordinationUnitImpl::RemoveParent(CoordinationUnitImpl* parent) { | |
| 166 size_t parents_removed = parents_.erase(parent); | |
| 167 DCHECK_EQ(1u, parents_removed); | |
| 168 | |
| 169 RecalcPolicy(); | |
| 170 } | |
| 171 | |
| 172 bool CoordinationUnitImpl::HasParent(CoordinationUnitImpl* unit) { | |
| 173 for (CoordinationUnitImpl* 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 (CoordinationUnitImpl* 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))); | |
| 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 |