Index: services/resource_coordinator/tracing/agent_set_impl.cc |
diff --git a/services/resource_coordinator/tracing/agent_set_impl.cc b/services/resource_coordinator/tracing/agent_set_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bfb9432f10df9f746969b187501c87cf1a373ae1 |
--- /dev/null |
+++ b/services/resource_coordinator/tracing/agent_set_impl.cc |
@@ -0,0 +1,107 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "services/resource_coordinator/tracing/agent_set_impl.h" |
+ |
+#include "base/callback_forward.h" |
+#include "services/service_manager/public/cpp/bind_source_info.h" |
+ |
+namespace { |
+ |
+resource_coordinator::tracing::AgentSetImpl* g_agent_set_impl; |
+} |
+ |
+namespace resource_coordinator { |
+namespace tracing { |
+ |
+AgentSetImpl::Entry::Entry(size_t id, |
+ AgentSetImpl* agent_set, |
+ mojom::AgentPtr agent, |
+ const std::string& label, |
+ mojom::TraceDataType type, |
+ bool supports_explicit_clock_sync) |
+ : id_(id), |
+ agent_set_(agent_set), |
+ agent_(std::move(agent)), |
+ label_(label), |
+ type_(type), |
+ supports_explicit_clock_sync_(supports_explicit_clock_sync) { |
+ DCHECK(label.size()); |
+ agent_.set_connection_error_handler(base::BindRepeating( |
+ &AgentSetImpl::Entry::OnConnectionError, base::Unretained(this))); |
+} |
+ |
+AgentSetImpl::Entry::~Entry() {} |
+ |
+void AgentSetImpl::Entry::AddDisconnectClosure(const std::string& closure_type, |
+ base::OnceClosure closure) { |
+ if (closures_.find(closure_type) != closures_.end()) { |
+ NOTREACHED() << "Cannot add two disconnect closures of the same type."; |
+ return; |
+ } |
+ closures_[closure_type] = std::move(closure); |
+} |
+ |
+bool AgentSetImpl::Entry::RemoveDisconnectClosure( |
+ const std::string& closure_type) { |
+ return closures_.erase(closure_type) > 0; |
+} |
+ |
+void AgentSetImpl::Entry::OnConnectionError() { |
+ for (auto& key_value : closures_) { |
+ std::move(key_value.second).Run(); |
+ } |
+ agent_set_->UnregisterAgent(id_); |
+} |
+ |
+// static |
+AgentSetImpl* AgentSetImpl::GetInstance() { |
+ return g_agent_set_impl; |
+} |
+ |
+AgentSetImpl::AgentSetImpl() { |
+ DCHECK(!g_agent_set_impl); |
+ g_agent_set_impl = this; |
+} |
+ |
+AgentSetImpl::~AgentSetImpl() { |
+ g_agent_set_impl = nullptr; |
+} |
+ |
+void AgentSetImpl::BindAgentSetRequest( |
+ const service_manager::BindSourceInfo& source_info, |
+ mojom::AgentSetRequest request) { |
+ bindings_.AddBinding(this, std::move(request)); |
+} |
+ |
+void AgentSetImpl::SetAgentProcessingCallback( |
+ const AgentProcessingCallback& callback) { |
+ agent_processing_callback_ = callback; |
+ ForAllAgents([this](Entry* entry) { agent_processing_callback_.Run(entry); }); |
+} |
+ |
+void AgentSetImpl::ResetAgentProcessingCallback() { |
+ agent_processing_callback_.Reset(); |
+} |
+ |
+void AgentSetImpl::RegisterAgent(mojom::AgentPtr agent, |
+ const std::string& label, |
+ mojom::TraceDataType type, |
+ bool supports_explicit_clock_sync) { |
+ auto id = next_agent_id_++; |
+ auto entry = base::MakeUnique<Entry>(id, this, std::move(agent), label, type, |
+ supports_explicit_clock_sync); |
+ if (!agent_processing_callback_.is_null()) |
+ agent_processing_callback_.Run(entry.get()); |
+ auto result = agents_.insert(std::make_pair(id, std::move(entry))); |
+ DCHECK(result.second); |
+} |
+ |
+void AgentSetImpl::UnregisterAgent(size_t agent_id) { |
+ size_t num_deleted = agents_.erase(agent_id); |
+ DCHECK_EQ(static_cast<size_t>(1), num_deleted); |
+} |
+ |
+} // namespace tracing |
+} // namespace resource_coordinator |