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/tracing/agent_set_impl.h" |
| 6 |
| 7 #include "base/callback_forward.h" |
| 8 #include "services/service_manager/public/cpp/bind_source_info.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 resource_coordinator::tracing::AgentSetImpl* g_agent_set_impl; |
| 13 } |
| 14 |
| 15 namespace resource_coordinator { |
| 16 namespace tracing { |
| 17 |
| 18 AgentSetImpl::Entry::Entry(size_t id, |
| 19 AgentSetImpl* agent_set, |
| 20 mojom::AgentPtr agent, |
| 21 const std::string& label, |
| 22 mojom::TraceDataType type, |
| 23 bool supports_explicit_clock_sync) |
| 24 : id_(id), |
| 25 agent_set_(agent_set), |
| 26 agent_(std::move(agent)), |
| 27 label_(label), |
| 28 type_(type), |
| 29 supports_explicit_clock_sync_(supports_explicit_clock_sync) { |
| 30 DCHECK(label.size()); |
| 31 agent_.set_connection_error_handler(base::BindRepeating( |
| 32 &AgentSetImpl::Entry::OnConnectionError, base::Unretained(this))); |
| 33 } |
| 34 |
| 35 AgentSetImpl::Entry::~Entry() {} |
| 36 |
| 37 void AgentSetImpl::Entry::AddDisconnectClosure(const std::string& closure_type, |
| 38 base::OnceClosure closure) { |
| 39 if (closures_.find(closure_type) != closures_.end()) { |
| 40 NOTREACHED() << "Cannot add two disconnect closures of the same type."; |
| 41 return; |
| 42 } |
| 43 closures_[closure_type] = std::move(closure); |
| 44 } |
| 45 |
| 46 bool AgentSetImpl::Entry::RemoveDisconnectClosure( |
| 47 const std::string& closure_type) { |
| 48 return closures_.erase(closure_type) > 0; |
| 49 } |
| 50 |
| 51 void AgentSetImpl::Entry::OnConnectionError() { |
| 52 for (auto& key_value : closures_) { |
| 53 std::move(key_value.second).Run(); |
| 54 } |
| 55 agent_set_->UnregisterAgent(id_); |
| 56 } |
| 57 |
| 58 // static |
| 59 AgentSetImpl* AgentSetImpl::GetInstance() { |
| 60 return g_agent_set_impl; |
| 61 } |
| 62 |
| 63 AgentSetImpl::AgentSetImpl() { |
| 64 DCHECK(!g_agent_set_impl); |
| 65 g_agent_set_impl = this; |
| 66 } |
| 67 |
| 68 AgentSetImpl::~AgentSetImpl() { |
| 69 g_agent_set_impl = nullptr; |
| 70 } |
| 71 |
| 72 void AgentSetImpl::BindAgentSetRequest( |
| 73 const service_manager::BindSourceInfo& source_info, |
| 74 mojom::AgentSetRequest request) { |
| 75 bindings_.AddBinding(this, std::move(request)); |
| 76 } |
| 77 |
| 78 void AgentSetImpl::SetAgentProcessingCallback( |
| 79 const AgentProcessingCallback& callback) { |
| 80 agent_processing_callback_ = callback; |
| 81 ForAllAgents([this](Entry* entry) { agent_processing_callback_.Run(entry); }); |
| 82 } |
| 83 |
| 84 void AgentSetImpl::ResetAgentProcessingCallback() { |
| 85 agent_processing_callback_.Reset(); |
| 86 } |
| 87 |
| 88 void AgentSetImpl::RegisterAgent(mojom::AgentPtr agent, |
| 89 const std::string& label, |
| 90 mojom::TraceDataType type, |
| 91 bool supports_explicit_clock_sync) { |
| 92 auto id = next_agent_id_++; |
| 93 auto entry = base::MakeUnique<Entry>(id, this, std::move(agent), label, type, |
| 94 supports_explicit_clock_sync); |
| 95 if (!agent_processing_callback_.is_null()) |
| 96 agent_processing_callback_.Run(entry.get()); |
| 97 auto result = agents_.insert(std::make_pair(id, std::move(entry))); |
| 98 DCHECK(result.second); |
| 99 } |
| 100 |
| 101 void AgentSetImpl::UnregisterAgent(size_t agent_id) { |
| 102 size_t num_deleted = agents_.erase(agent_id); |
| 103 DCHECK_EQ(static_cast<size_t>(1), num_deleted); |
| 104 } |
| 105 |
| 106 } // namespace tracing |
| 107 } // namespace resource_coordinator |
OLD | NEW |