| 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_registry.h" |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/callback_forward.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/threading/thread_checker.h" |
| 13 #include "services/service_manager/public/cpp/bind_source_info.h" |
| 14 |
| 15 namespace { |
| 16 tracing::AgentRegistry* g_agent_registry; |
| 17 } |
| 18 |
| 19 namespace tracing { |
| 20 |
| 21 AgentRegistry::AgentEntry::AgentEntry(size_t id, |
| 22 AgentRegistry* agent_registry, |
| 23 mojom::AgentPtr agent, |
| 24 const std::string& label, |
| 25 mojom::TraceDataType type, |
| 26 bool supports_explicit_clock_sync) |
| 27 : id_(id), |
| 28 agent_registry_(agent_registry), |
| 29 agent_(std::move(agent)), |
| 30 label_(label), |
| 31 type_(type), |
| 32 supports_explicit_clock_sync_(supports_explicit_clock_sync) { |
| 33 DCHECK(!label.empty()); |
| 34 agent_.set_connection_error_handler(base::BindRepeating( |
| 35 &AgentRegistry::AgentEntry::OnConnectionError, base::Unretained(this))); |
| 36 } |
| 37 |
| 38 AgentRegistry::AgentEntry::~AgentEntry() = default; |
| 39 |
| 40 void AgentRegistry::AgentEntry::SetDisconnectClosure( |
| 41 base::OnceClosure closure) { |
| 42 DCHECK(closure_.is_null()); |
| 43 closure_ = std::move(closure); |
| 44 } |
| 45 |
| 46 bool AgentRegistry::AgentEntry::RemoveDisconnectClosure() { |
| 47 bool closure_was_set = !closure_.is_null(); |
| 48 closure_.Reset(); |
| 49 return closure_was_set; |
| 50 } |
| 51 |
| 52 void AgentRegistry::AgentEntry::OnConnectionError() { |
| 53 // Run the disconnect closure if it is set. We should mark |closure_| as |
| 54 // movable so that the version of |Run| that takes an rvalue reference is |
| 55 // selected not the version that takes a const reference. The former is for |
| 56 // once callbacks and the latter is for repeating callbacks. |
| 57 if (!closure_.is_null()) |
| 58 std::move(closure_).Run(); |
| 59 agent_registry_->UnregisterAgent(id_); |
| 60 } |
| 61 |
| 62 // static |
| 63 AgentRegistry* AgentRegistry::GetInstance() { |
| 64 return g_agent_registry; |
| 65 } |
| 66 |
| 67 AgentRegistry::AgentRegistry() { |
| 68 DCHECK(!g_agent_registry); |
| 69 g_agent_registry = this; |
| 70 } |
| 71 |
| 72 AgentRegistry::~AgentRegistry() { |
| 73 // For testing only. |
| 74 g_agent_registry = nullptr; |
| 75 } |
| 76 |
| 77 void AgentRegistry::BindAgentRegistryRequest( |
| 78 const service_manager::BindSourceInfo& source_info, |
| 79 mojom::AgentRegistryRequest request) { |
| 80 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 81 bindings_.AddBinding(this, std::move(request)); |
| 82 } |
| 83 |
| 84 void AgentRegistry::SetAgentInitializationCallback( |
| 85 const AgentInitializationCallback& callback) { |
| 86 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 87 agent_initialization_callback_ = callback; |
| 88 ForAllAgents([this](AgentEntry* agent_entry) { |
| 89 agent_initialization_callback_.Run(agent_entry); |
| 90 }); |
| 91 } |
| 92 |
| 93 void AgentRegistry::RemoveAgentInitializationCallback() { |
| 94 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 95 agent_initialization_callback_.Reset(); |
| 96 } |
| 97 |
| 98 void AgentRegistry::RegisterAgent(mojom::AgentPtr agent, |
| 99 const std::string& label, |
| 100 mojom::TraceDataType type, |
| 101 bool supports_explicit_clock_sync) { |
| 102 auto id = next_agent_id_++; |
| 103 auto entry = base::MakeUnique<AgentEntry>(id, this, std::move(agent), label, |
| 104 type, supports_explicit_clock_sync); |
| 105 if (!agent_initialization_callback_.is_null()) |
| 106 agent_initialization_callback_.Run(entry.get()); |
| 107 auto result = agents_.insert(std::make_pair(id, std::move(entry))); |
| 108 DCHECK(result.second); |
| 109 } |
| 110 |
| 111 void AgentRegistry::UnregisterAgent(size_t agent_id) { |
| 112 size_t num_deleted = agents_.erase(agent_id); |
| 113 DCHECK_EQ(1u, num_deleted); |
| 114 } |
| 115 |
| 116 } // namespace tracing |
| OLD | NEW |