File: service_instance_registry.cc

package info (click to toggle)
qtwebengine-opensource-src 5.15.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,891,008 kB
  • sloc: cpp: 12,231,790; ansic: 4,139,950; javascript: 590,747; python: 550,957; asm: 507,724; xml: 434,729; java: 166,199; objc: 79,696; perl: 72,973; sh: 70,983; cs: 30,332; makefile: 21,627; yacc: 8,867; tcl: 8,297; php: 5,896; pascal: 4,488; lex: 2,830; lisp: 2,703; sql: 1,810; ruby: 683; awk: 200; sed: 56
file content (208 lines) | stat: -rw-r--r-- 7,088 bytes parent folder | download | duplicates (7)
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
// Copyright 2019 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/service_manager/service_instance_registry.h"

#include <algorithm>

#include "services/service_manager/public/cpp/manifest.h"
#include "services/service_manager/service_instance.h"

namespace service_manager {

ServiceInstanceRegistry::Entry::Entry(const base::Token& guid,
                                      ServiceInstance* instance)
    : guid(guid), instance(instance) {
  DCHECK(!guid.is_zero());
  DCHECK(instance);
}

ServiceInstanceRegistry::Entry::Entry(const Entry&) = default;

ServiceInstanceRegistry::Entry::~Entry() = default;

ServiceInstanceRegistry::RegularInstanceKey::RegularInstanceKey(
    const std::string& service_name,
    const base::Token& instance_group,
    const base::Token& instance_id)
    : service_name(service_name),
      instance_group(instance_group),
      instance_id(instance_id) {}

ServiceInstanceRegistry::RegularInstanceKey::RegularInstanceKey(
    const RegularInstanceKey&) = default;

ServiceInstanceRegistry::RegularInstanceKey::~RegularInstanceKey() = default;

bool ServiceInstanceRegistry::RegularInstanceKey::operator==(
    const RegularInstanceKey& other) const {
  return service_name == other.service_name &&
         instance_group == other.instance_group &&
         instance_id == other.instance_id;
}

bool ServiceInstanceRegistry::RegularInstanceKey::operator<(
    const RegularInstanceKey& other) const {
  return std::tie(service_name, instance_group, instance_id) <
         std::tie(other.service_name, other.instance_group, other.instance_id);
}

ServiceInstanceRegistry::SharedInstanceKey::SharedInstanceKey(
    const std::string& service_name,
    const base::Token& instance_id)
    : service_name(service_name), instance_id(instance_id) {}

ServiceInstanceRegistry::SharedInstanceKey::SharedInstanceKey(
    const SharedInstanceKey&) = default;

ServiceInstanceRegistry::SharedInstanceKey::~SharedInstanceKey() = default;

bool ServiceInstanceRegistry::SharedInstanceKey::operator==(
    const SharedInstanceKey& other) const {
  return service_name == other.service_name && instance_id == other.instance_id;
}

bool ServiceInstanceRegistry::SharedInstanceKey::operator<(
    const SharedInstanceKey& other) const {
  if (service_name != other.service_name)
    return service_name < other.service_name;
  return instance_id < other.instance_id;
}

ServiceInstanceRegistry::ServiceInstanceRegistry() = default;

ServiceInstanceRegistry::~ServiceInstanceRegistry() = default;

void ServiceInstanceRegistry::Register(ServiceInstance* instance) {
  DCHECK_NE(instance, nullptr);

  const Identity& identity = instance->identity();
  DCHECK_EQ(FindMatching(identity), nullptr);

  switch (instance->manifest().options.instance_sharing_policy) {
    case Manifest::InstanceSharingPolicy::kNoSharing: {
      const RegularInstanceKey key{identity.name(), identity.instance_group(),
                                   identity.instance_id()};
      regular_instances_[key].emplace_back(identity.globally_unique_id(),
                                           instance);
      break;
    }

    case Manifest::InstanceSharingPolicy::kSharedAcrossGroups: {
      const SharedInstanceKey key{identity.name(), identity.instance_id()};
      shared_instances_[key].emplace_back(identity.globally_unique_id(),
                                          instance);
      break;
    }

    case Manifest::InstanceSharingPolicy::kSingleton:
      singleton_instances_[identity.name()].emplace_back(
          identity.globally_unique_id(), instance);
      break;

    default:
      NOTREACHED();
  }
}

bool ServiceInstanceRegistry::Unregister(ServiceInstance* instance) {
  DCHECK(instance);
  const Identity& identity = instance->identity();

  const RegularInstanceKey regular_key{
      identity.name(), identity.instance_group(), identity.instance_id()};
  auto regular_iter = regular_instances_.find(regular_key);
  if (regular_iter != regular_instances_.end()) {
    auto& entries = regular_iter->second;
    if (EraseEntry(identity.globally_unique_id(), &entries)) {
      if (entries.empty())
        regular_instances_.erase(regular_iter);
      return true;
    }
  }

  const SharedInstanceKey shared_key{identity.name(), identity.instance_id()};
  auto shared_iter = shared_instances_.find(shared_key);
  if (shared_iter != shared_instances_.end()) {
    auto& entries = shared_iter->second;
    if (EraseEntry(identity.globally_unique_id(), &entries)) {
      if (entries.empty())
        shared_instances_.erase(shared_iter);
      return true;
    }
  }

  auto singleton_iter = singleton_instances_.find(identity.name());
  if (singleton_iter != singleton_instances_.end()) {
    auto& entries = singleton_iter->second;
    if (EraseEntry(identity.globally_unique_id(), &entries)) {
      if (entries.empty())
        singleton_instances_.erase(singleton_iter);
      return true;
    }
  }

  return false;
}

ServiceInstance* ServiceInstanceRegistry::FindMatching(
    const ServiceFilter& filter) const {
  DCHECK(filter.instance_group());
  DCHECK(filter.instance_id());
  DCHECK(!filter.globally_unique_id() ||
         !filter.globally_unique_id()->is_zero());

  const RegularInstanceKey regular_key{
      filter.service_name(), *filter.instance_group(), *filter.instance_id()};
  auto regular_iter = regular_instances_.find(regular_key);
  if (regular_iter != regular_instances_.end()) {
    return FindMatchInEntries(regular_iter->second,
                              filter.globally_unique_id());
  }

  const SharedInstanceKey shared_key{filter.service_name(),
                                     *filter.instance_id()};
  auto shared_iter = shared_instances_.find(
      SharedInstanceKey(filter.service_name(), *filter.instance_id()));
  if (shared_iter != shared_instances_.end()) {
    return FindMatchInEntries(shared_iter->second, filter.globally_unique_id());
  }

  auto singleton_iter = singleton_instances_.find(filter.service_name());
  if (singleton_iter != singleton_instances_.end()) {
    return FindMatchInEntries(singleton_iter->second,
                              filter.globally_unique_id());
  }

  return nullptr;
}

ServiceInstance* ServiceInstanceRegistry::FindMatchInEntries(
    const std::vector<Entry>& entries,
    const base::Optional<base::Token>& guid) const {
  DCHECK(!entries.empty());
  if (!guid.has_value())
    return entries.front().instance;

  for (const auto& entry : entries) {
    if (entry.guid == *guid)
      return entry.instance;
  }

  return nullptr;
}

bool ServiceInstanceRegistry::EraseEntry(const base::Token& guid,
                                         std::vector<Entry>* entries) {
  auto it =
      std::find_if(entries->begin(), entries->end(),
                   [&guid](const Entry& entry) { return entry.guid == guid; });
  if (it == entries->end())
    return false;

  entries->erase(it);
  return true;
}

}  // namespace service_manager