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