forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_manager.h
213 lines (176 loc) · 8.87 KB
/
service_manager.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
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
// Copyright 2014 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_SERVICE_MANAGER_SERVICE_MANAGER_H_
#define SERVICES_SERVICE_MANAGER_SERVICE_MANAGER_H_
#include <memory>
#include <set>
#include <vector>
#include "base/containers/unique_ptr_adapters.h"
#include "base/files/file_path.h"
#include "base/process/process.h"
#include "base/token.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "sandbox/policy/sandbox_type.h"
#include "services/service_manager/catalog.h"
#include "services/service_manager/public/cpp/identity.h"
#include "services/service_manager/public/cpp/manifest.h"
#include "services/service_manager/public/cpp/service.h"
#include "services/service_manager/public/cpp/service_receiver.h"
#include "services/service_manager/public/mojom/connector.mojom.h"
#include "services/service_manager/public/mojom/interface_provider.mojom.h"
#include "services/service_manager/public/mojom/service.mojom.h"
#include "services/service_manager/public/mojom/service_manager.mojom.h"
#include "services/service_manager/service_instance_registry.h"
#include "services/service_manager/service_process_host.h"
namespace service_manager {
class ServiceInstance;
class ServiceManager : public Service {
public:
// This is an interface a ServiceManager instance can use to delegate certain
// operations (like launching in-process services) to its embedding runtime
// environment.
class Delegate {
public:
virtual ~Delegate() {}
// Asks for a new concrete instance of the service identified by |identity|
// to be created in the calling (i.e. the Service Manager's) process. If
// created, the instance should bind to |receiver|. Returns |true| if the
// instance was created or |false| otherwise (e.g. the service was unknown
// or in-process instances are not supported by the runtime environment).
//
// This is only called for services with the ExecutionMode
// |kInProcessBuiltin|.
virtual bool RunBuiltinServiceInstanceInCurrentProcess(
const Identity& identity,
mojo::PendingReceiver<mojom::Service> receiver) = 0;
// Creates a new ServiceProcessHost to host an out-of-process service
// instance for a service using ExecuteMode |kOutOfProcessBuiltin|.
//
// May return null if builtin out-of-process services are not supported by
// the runtime environment.
//
// TODO(https://crbug.com/895615): Process launching should be fully the
// responsibility of the Service Manager. This exists because much of the
// Chromium process launching logic today is still buried in the Content
// layer.
virtual std::unique_ptr<ServiceProcessHost>
CreateProcessHostForBuiltinServiceInstance(const Identity& identity) = 0;
// Creates a new ServiceProcessHost to host an out-of-process service
// instance for a service using a standalone executable (i.e. ExecuteMode in
// the manifest is |kStandaloneExecutable|).
//
// May return null if service executables are not supported by the runtime
// environment.
//
// TODO(https://crbug.com/895615): Process launching should be fully the
// responsibility of the Service Manager. This exists because much of the
// Chromium process launching logic today is still buried in the Content
// layer.
virtual std::unique_ptr<ServiceProcessHost>
CreateProcessHostForServiceExecutable(
const base::FilePath& executable_path) = 0;
};
// Indicates whether standalone service executables are supported by this
// ServiceManager instance. Only used when an explicit Delegate is not
// specified at construction time.
enum class ServiceExecutablePolicy {
kSupported,
kNotSupported,
};
// Constructs a new ServiceManager instance. |delegate| is used to augment
// default Service Manager behavior.
//
// |manifests| is the complete list of manifests for all services available to
// the runtime environment.
ServiceManager(const std::vector<Manifest>& manifests,
std::unique_ptr<Delegate> delegate);
// Like above but uses a default internal Delegate implementation. With the
// default implementation, only packaged services, manually registered service
// instances, or (policy permitting) service executables are supported. No
// builtin (in-process or out-of-process) services are supported unless
// manually registered with |RegisterService()| below.
ServiceManager(const std::vector<Manifest>& manifests,
ServiceExecutablePolicy service_executable_policy);
ServiceManager(const ServiceManager&) = delete;
ServiceManager& operator=(const ServiceManager&) = delete;
~ServiceManager() override;
// Directly requests that the Service Manager start a new instance for
// |service_name| if one is not already running.
//
// TODO(https://crbug.com/904240): Remove this method.
void StartService(const std::string& service_name);
// Creates a service instance for |identity|. This is intended for use by the
// Service Manager's embedder to register instances directly, without
// requiring a Connector.
//
// |metadata_receiver| may be null, in which case the Service Manager assumes
// the new service is running in the calling process.
//
// Returns |true| if registration succeeded, or |false| otherwise.
bool RegisterService(
const Identity& identity,
mojo::PendingRemote<mojom::Service> service,
mojo::PendingReceiver<mojom::ProcessMetadata> metadata_receiver);
// Determine information about |service_name| from its manifests. Returns
// false if the identity does not have a catalog entry.
bool QueryCatalog(const std::string& service_name,
const base::Token& instance_group,
std::string* sandbox_type);
// Attempts to locate a ServiceInstance as a target for a connection request
// from |source_instance| by matching against |partial_target_filter|. If a
// suitable instance exists it is returned, otherwise the Service Manager
// attempts to create a new suitable instance.
//
// Returns null if a matching instance did not exist and could not be created,
// otherwise returns a valid ServiceInstance which matches
// |partial_target_filter| from |source_instance|'s perspective.
ServiceInstance* FindOrCreateMatchingTargetInstance(
const ServiceInstance& source_instance,
const ServiceFilter& partial_target_filter);
private:
friend class ServiceInstance;
// Erases |instance| from the instance registry. Following this call it is
// impossible for any call to GetExistingInstance() to return |instance| even
// though the instance may continue to exist and send requests to the Service
// Manager.
void MakeInstanceUnreachable(ServiceInstance* instance);
// Called when |instance| no longer has any connections to the remote service
// instance, or when some other fatal error is encountered in managing the
// instance. Deletes |instance|.
void DestroyInstance(ServiceInstance* instance);
// Called by a ServiceInstance as it's being destroyed.
void OnInstanceStopped(const Identity& identity);
// Returns a running instance identified by |identity|.
ServiceInstance* GetExistingInstance(const Identity& identity) const;
void NotifyServiceCreated(const ServiceInstance& instance);
void NotifyServiceStarted(const Identity& identity, base::ProcessId pid);
void NotifyServiceFailedToStart(const Identity& identity);
void NotifyServicePIDReceived(const Identity& identity, base::ProcessId pid);
ServiceInstance* CreateServiceInstance(const Identity& identity,
const Manifest& manifest);
// Called from the instance implementing mojom::ServiceManager.
void AddListener(mojo::PendingRemote<mojom::ServiceManagerListener> listener);
// Service:
void OnBindInterface(const BindSourceInfo& source_info,
const std::string& interface_name,
mojo::ScopedMessagePipeHandle receiving_pipe) override;
const std::unique_ptr<Delegate> delegate_;
ServiceReceiver service_receiver_{this};
// Ownership of all ServiceInstances.
using InstanceMap =
std::set<std::unique_ptr<ServiceInstance>, base::UniquePtrComparator>;
InstanceMap instances_;
Catalog catalog_;
// Maps service identities to reachable instances, allowing for lookup of
// running instances by ServiceFilter.
ServiceInstanceRegistry instance_registry_;
// Always points to the ServiceManager's own Instance. Note that this
// ServiceInstance still has an entry in |instances_|.
ServiceInstance* service_manager_instance_;
mojo::RemoteSet<mojom::ServiceManagerListener> listeners_;
};
} // namespace service_manager
#endif // SERVICES_SERVICE_MANAGER_SERVICE_MANAGER_H_