File: dns_sd_registry.cc

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (248 lines) | stat: -rw-r--r-- 7,149 bytes parent folder | download | duplicates (8)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h"

#include <algorithm>
#include <limits>
#include <utility>

#include "base/check.h"
#include "base/observer_list.h"
#include "chrome/browser/local_discovery/service_discovery_shared_client.h"  // nogncheck
#include "chrome/browser/media/router/discovery/mdns/dns_sd_device_lister.h"
#include "chrome/common/buildflags.h"

namespace media_router {

DnsSdRegistry::ServiceTypeData::ServiceTypeData(
    std::unique_ptr<DnsSdDeviceLister> lister)
    : ref_count(1), lister_(std::move(lister)) {}

DnsSdRegistry::ServiceTypeData::~ServiceTypeData() = default;

void DnsSdRegistry::ServiceTypeData::ListenerAdded() {
  ref_count++;
  CHECK(ref_count != std::numeric_limits<decltype(ref_count)>::max());
}

bool DnsSdRegistry::ServiceTypeData::ListenerRemoved() {
  return --ref_count == 0;
}

int DnsSdRegistry::ServiceTypeData::GetListenerCount() {
  return ref_count;
}

bool DnsSdRegistry::ServiceTypeData::UpdateService(
    bool added,
    const DnsSdService& service) {
  auto it = std::ranges::find(service_list_, service.service_name,
                              &DnsSdService::service_name);
  // Set to true when a service is updated in or added to the registry.
  bool updated_or_added = added;
  bool known = (it != service_list_.end());
  if (known) {
    // If added == true, but we still found the service in our cache, then just
    // update the existing entry, but this should not happen!
    DCHECK(!added);
    if (*it != service) {
      *it = service;
      updated_or_added = true;
    }
  } else if (added) {
    service_list_.push_back(service);
  }
  return updated_or_added;
}

bool DnsSdRegistry::ServiceTypeData::RemoveService(
    const std::string& service_name) {
  for (auto it = service_list_.begin(); it != service_list_.end(); ++it) {
    if ((*it).service_name == service_name) {
      service_list_.erase(it);
      return true;
    }
  }
  return false;
}

void DnsSdRegistry::ServiceTypeData::ResetAndDiscover() {
  lister_->Reset();
  ClearServices();
}

bool DnsSdRegistry::ServiceTypeData::ClearServices() {
  lister_->Discover();

  if (service_list_.empty()) {
    return false;
  }

  service_list_.clear();
  return true;
}

const DnsSdRegistry::DnsSdServiceList&
DnsSdRegistry::ServiceTypeData::GetServiceList() {
  return service_list_;
}

DnsSdRegistry::DnsSdRegistry() {
#if BUILDFLAG(ENABLE_SERVICE_DISCOVERY)
  service_discovery_client_ =
      local_discovery::ServiceDiscoverySharedClient::GetInstance();
#endif
}

DnsSdRegistry::DnsSdRegistry(
    local_discovery::ServiceDiscoverySharedClient* client) {
  service_discovery_client_ = client;
}

DnsSdRegistry::~DnsSdRegistry() {
  DCHECK(thread_checker_.CalledOnValidThread());
}

// static
DnsSdRegistry* DnsSdRegistry::GetInstance() {
  return base::Singleton<DnsSdRegistry,
                         base::LeakySingletonTraits<DnsSdRegistry>>::get();
}

void DnsSdRegistry::AddObserver(DnsSdObserver* observer) {
  DCHECK(thread_checker_.CalledOnValidThread());
  observers_.AddObserver(observer);
}

void DnsSdRegistry::RemoveObserver(DnsSdObserver* observer) {
  DCHECK(thread_checker_.CalledOnValidThread());
  observers_.RemoveObserver(observer);
}

DnsSdDeviceLister* DnsSdRegistry::CreateDnsSdDeviceLister(
    DnsSdDelegate* delegate,
    const std::string& service_type,
    local_discovery::ServiceDiscoverySharedClient* discovery_client) {
  return new DnsSdDeviceLister(discovery_client, delegate, service_type);
}

void DnsSdRegistry::Publish(const std::string& service_type) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DispatchApiEvent(service_type);
}

void DnsSdRegistry::ResetAndDiscover() {
  DCHECK(thread_checker_.CalledOnValidThread());
  for (const auto& next_service : service_data_map_) {
    next_service.second->ResetAndDiscover();
  }
}

void DnsSdRegistry::RegisterDnsSdListener(const std::string& service_type) {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (service_type.empty()) {
    return;
  }

  if (IsRegistered(service_type)) {
    service_data_map_[service_type]->ListenerAdded();
    DispatchApiEvent(service_type);
    return;
  }

  std::unique_ptr<DnsSdDeviceLister> dns_sd_device_lister(
      CreateDnsSdDeviceLister(this, service_type,
                              service_discovery_client_.get()));
  dns_sd_device_lister->Discover();
  service_data_map_[service_type] =
      std::make_unique<ServiceTypeData>(std::move(dns_sd_device_lister));
  DispatchApiEvent(service_type);
}

void DnsSdRegistry::UnregisterDnsSdListener(const std::string& service_type) {
  DCHECK(thread_checker_.CalledOnValidThread());
  auto it = service_data_map_.find(service_type);
  if (it == service_data_map_.end()) {
    return;
  }

  if (service_data_map_[service_type]->ListenerRemoved()) {
    service_data_map_.erase(it);
  }
}

void DnsSdRegistry::ResetForTest() {
  service_data_map_.clear();
  service_discovery_client_.reset();
}

void DnsSdRegistry::ServiceChanged(const std::string& service_type,
                                   bool added,
                                   const DnsSdService& service) {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (!IsRegistered(service_type)) {
    return;
  }

  // TODO(imcheng): This should be validated upstream in
  // dns_sd_device_lister.cc, i.e., |service.ip_address| should be a
  // valid net::IPAddress.
  net::IPAddress ip_address;
  if (!ip_address.AssignFromIPLiteral(service.ip_address)) {
    return;
  }
  bool is_updated =
      service_data_map_[service_type]->UpdateService(added, service);
  if (is_updated) {
    DispatchApiEvent(service_type);
  }
}

void DnsSdRegistry::ServiceRemoved(const std::string& service_type,
                                   const std::string& service_name) {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (!IsRegistered(service_type)) {
    return;
  }

  bool is_removed =
      service_data_map_[service_type]->RemoveService(service_name);
  if (is_removed) {
    DispatchApiEvent(service_type);
  }
}

void DnsSdRegistry::ServicesFlushed(const std::string& service_type) {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (!IsRegistered(service_type)) {
    return;
  }

  bool is_cleared = service_data_map_[service_type]->ClearServices();
  if (is_cleared) {
    DispatchApiEvent(service_type);
  }
}

void DnsSdRegistry::ServicesPermissionRejected() {
  for (auto& observer : observers_) {
    observer.OnDnsSdPermissionRejected();
  }
}

void DnsSdRegistry::DispatchApiEvent(const std::string& service_type) {
  DCHECK(thread_checker_.CalledOnValidThread());
  for (auto& observer : observers_) {
    observer.OnDnsSdEvent(service_type,
                          service_data_map_[service_type]->GetServiceList());
  }
}

bool DnsSdRegistry::IsRegistered(const std::string& service_type) {
  DCHECK(thread_checker_.CalledOnValidThread());
  return service_data_map_.find(service_type) != service_data_map_.end();
}

}  // namespace media_router