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/tracing/agent_registry.h" | |
| 6 | |
| 7 #include "base/callback_forward.h" | |
| 8 #include "base/threading/thread_checker.h" | |
| 9 #include "services/service_manager/public/cpp/bind_source_info.h" | |
| 10 | |
| 11 namespace { | |
| 12 tracing::AgentRegistry* g_agent_registry; | |
| 13 } | |
| 14 | |
| 15 namespace tracing { | |
| 16 | |
| 17 AgentRegistry::Entry::Entry(size_t id, | |
| 18 AgentRegistry* agent_registry, | |
| 19 mojom::AgentPtr agent, | |
| 20 const std::string& label, | |
| 21 mojom::TraceDataType type, | |
| 22 bool supports_explicit_clock_sync) | |
| 23 : id_(id), | |
| 24 agent_registry_(agent_registry), | |
| 25 agent_(std::move(agent)), | |
| 26 label_(label), | |
| 27 type_(type), | |
| 28 supports_explicit_clock_sync_(supports_explicit_clock_sync) { | |
| 29 DCHECK(label.size()); | |
|
Primiano Tucci (use gerrit)
2017/05/25 13:24:28
!label.empty() might be slightly more readable
chiniforooshan
2017/05/26 15:46:31
Done.
| |
| 30 agent_.set_connection_error_handler(base::BindRepeating( | |
| 31 &AgentRegistry::Entry::OnConnectionError, base::Unretained(this))); | |
| 32 } | |
| 33 | |
| 34 AgentRegistry::Entry::~Entry() {} | |
|
oystein (OOO til 10th of July)
2017/05/25 17:16:08
nit: = default;
chiniforooshan
2017/05/26 15:46:31
Done.
| |
| 35 | |
| 36 void AgentRegistry::Entry::SetDisconnectClosure(base::OnceClosure closure) { | |
| 37 DCHECK(closure_.is_null()); | |
|
oystein (OOO til 10th of July)
2017/05/25 17:16:08
nit: "base/logging.h"
chiniforooshan
2017/05/26 15:46:31
Done.
| |
| 38 closure_ = std::move(closure); | |
| 39 } | |
| 40 | |
| 41 bool AgentRegistry::Entry::RemoveDisconnectClosure() { | |
| 42 bool closure_was_set = !closure_.is_null(); | |
| 43 closure_.Reset(); | |
| 44 return closure_was_set; | |
|
oystein (OOO til 10th of July)
2017/05/25 17:16:08
just curious: why is this needing to be returned?
chiniforooshan
2017/05/26 15:46:31
Coordinator will use disconnect closures to make s
oystein (OOO til 10th of July)
2017/05/26 17:07:43
Yep absolutely, thanks for the explanation!
| |
| 45 } | |
| 46 | |
| 47 void AgentRegistry::Entry::OnConnectionError() { | |
| 48 if (!closure_.is_null()) | |
| 49 std::move(closure_).Run(); | |
|
oystein (OOO til 10th of July)
2017/05/25 17:16:08
This line confuses me :). Is this moving closure_
chiniforooshan
2017/05/26 15:46:31
It marks |closure_| as movable so that the version
oystein (OOO til 10th of July)
2017/05/26 17:07:43
Ouch, that's obscure. If there's no way to write t
| |
| 50 agent_registry_->UnregisterAgent(id_); | |
| 51 } | |
| 52 | |
| 53 // static | |
| 54 AgentRegistry* AgentRegistry::GetInstance() { | |
| 55 return g_agent_registry; | |
| 56 } | |
| 57 | |
| 58 AgentRegistry::AgentRegistry() { | |
| 59 DCHECK(!g_agent_registry); | |
| 60 g_agent_registry = this; | |
| 61 } | |
| 62 | |
| 63 AgentRegistry::~AgentRegistry() { | |
| 64 g_agent_registry = nullptr; | |
|
Primiano Tucci (use gerrit)
2017/05/25 13:24:28
maybe add a comment saying // for testing only (is
chiniforooshan
2017/05/26 15:46:31
Done.
| |
| 65 } | |
| 66 | |
| 67 void AgentRegistry::BindAgentRegistryRequest( | |
| 68 const service_manager::BindSourceInfo& source_info, | |
| 69 mojom::AgentRegistryRequest request) { | |
| 70 bindings_.AddBinding(this, std::move(request)); | |
| 71 } | |
| 72 | |
| 73 void AgentRegistry::SetAgentInitializationCallback( | |
| 74 const AgentInitializationCallback& callback) { | |
| 75 agent_initialization_callback_ = callback; | |
| 76 ForAllAgents( | |
| 77 [this](Entry* entry) { agent_initialization_callback_.Run(entry); }); | |
| 78 } | |
| 79 | |
| 80 void AgentRegistry::RemoveAgentInitializationCallback() { | |
| 81 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 82 agent_initialization_callback_.Reset(); | |
| 83 } | |
| 84 | |
| 85 void AgentRegistry::RegisterAgent(mojom::AgentPtr agent, | |
| 86 const std::string& label, | |
| 87 mojom::TraceDataType type, | |
| 88 bool supports_explicit_clock_sync) { | |
| 89 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 90 auto id = next_agent_id_++; | |
| 91 auto entry = base::MakeUnique<Entry>(id, this, std::move(agent), label, type, | |
| 92 supports_explicit_clock_sync); | |
| 93 if (!agent_initialization_callback_.is_null()) | |
| 94 agent_initialization_callback_.Run(entry.get()); | |
| 95 auto result = agents_.insert(std::make_pair(id, std::move(entry))); | |
| 96 DCHECK(result.second); | |
| 97 } | |
| 98 | |
| 99 void AgentRegistry::UnregisterAgent(size_t agent_id) { | |
| 100 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 101 size_t num_deleted = agents_.erase(agent_id); | |
| 102 DCHECK_EQ(1u, num_deleted); | |
| 103 } | |
| 104 | |
| 105 } // namespace tracing | |
| OLD | NEW |