forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_registry.h
126 lines (102 loc) · 3.99 KB
/
agent_registry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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.
#ifndef SERVICES_TRACING_AGENT_REGISTRY_H_
#define SERVICES_TRACING_AGENT_REGISTRY_H_
#include <map>
#include <memory>
#include <string>
#include "base/callback_forward.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "services/service_manager/public/cpp/identity.h"
#include "services/tracing/public/mojom/tracing.mojom.h"
namespace service_manager {
struct BindSourceInfo;
} // namespace service_manager
namespace tracing {
class AgentRegistry : public mojom::AgentRegistry {
public:
class AgentEntry : public base::SupportsWeakPtr<AgentEntry> {
public:
AgentEntry(size_t id,
AgentRegistry* agent_registry,
mojom::AgentPtr agent,
const std::string& label,
mojom::TraceDataType type,
bool supports_explicit_clock_sync,
base::ProcessId pid);
~AgentEntry();
void AddDisconnectClosure(const void* closure_name,
base::OnceClosure closure);
bool RemoveDisconnectClosure(const void* closure_name);
bool HasDisconnectClosure(const void* closure_name);
size_t num_disconnect_closures_for_testing() const {
return closures_.size();
}
mojom::Agent* agent() const { return agent_.get(); }
const std::string& label() const { return label_; }
mojom::TraceDataType type() const { return type_; }
bool supports_explicit_clock_sync() const {
return supports_explicit_clock_sync_;
}
bool is_tracing() const { return is_tracing_; }
void set_is_tracing(bool is_tracing) { is_tracing_ = is_tracing; }
base::ProcessId pid() const { return pid_; }
private:
void OnConnectionError();
const size_t id_;
AgentRegistry* agent_registry_;
mojom::AgentPtr agent_;
const std::string label_;
const mojom::TraceDataType type_;
const bool supports_explicit_clock_sync_;
const base::ProcessId pid_;
std::map<const void*, base::OnceClosure> closures_;
bool is_tracing_;
DISALLOW_COPY_AND_ASSIGN(AgentEntry);
};
// A function to be run for every agent that registers itself.
using AgentInitializationCallback =
base::RepeatingCallback<void(AgentEntry*)>;
AgentRegistry();
~AgentRegistry() override;
void BindAgentRegistryRequest(
scoped_refptr<base::SequencedTaskRunner> task_runner,
mojom::AgentRegistryRequest request,
const service_manager::BindSourceInfo& source_info);
void SetAgentInitializationCallback(
const AgentInitializationCallback& callback);
void RemoveAgentInitializationCallback();
bool HasDisconnectClosure(const void* closure_name);
template <typename FunctionType>
void ForAllAgents(FunctionType function) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (const auto& key_value : agents_) {
function(key_value.second.get());
}
}
private:
friend class AgentRegistryTest; // For testing.
friend class CoordinatorTest; // For testing.
void BindAgentRegistryRequestOnSequence(
mojom::AgentRegistryRequest request,
const service_manager::BindSourceInfo& source_info);
// mojom::AgentRegistry
void RegisterAgent(mojom::AgentPtr agent,
const std::string& label,
mojom::TraceDataType type,
bool supports_explicit_clock_sync,
base::ProcessId pid) override;
void UnregisterAgent(size_t agent_id);
mojo::BindingSet<mojom::AgentRegistry, service_manager::Identity> bindings_;
size_t next_agent_id_ = 0;
std::map<size_t, std::unique_ptr<AgentEntry>> agents_;
AgentInitializationCallback agent_initialization_callback_;
SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(AgentRegistry);
};
} // namespace tracing
#endif // SERVICES_TRACING_AGENT_REGISTRY_H_