| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 #ifndef SERVICES_SHELL_SERVICE_MANAGER_H_ | |
| 6 #define SERVICES_SHELL_SERVICE_MANAGER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <memory> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "mojo/public/cpp/bindings/binding_set.h" | |
| 15 #include "mojo/public/cpp/bindings/interface_ptr_set.h" | |
| 16 #include "services/shell/connect_params.h" | |
| 17 #include "services/shell/native_runner.h" | |
| 18 #include "services/shell/public/cpp/capabilities.h" | |
| 19 #include "services/shell/public/cpp/identity.h" | |
| 20 #include "services/shell/public/cpp/interface_factory.h" | |
| 21 #include "services/shell/public/cpp/service.h" | |
| 22 #include "services/shell/public/interfaces/connector.mojom.h" | |
| 23 #include "services/shell/public/interfaces/interface_provider.mojom.h" | |
| 24 #include "services/shell/public/interfaces/resolver.mojom.h" | |
| 25 #include "services/shell/public/interfaces/service.mojom.h" | |
| 26 #include "services/shell/public/interfaces/service_factory.mojom.h" | |
| 27 #include "services/shell/public/interfaces/service_manager.mojom.h" | |
| 28 #include "services/shell/service_overrides.h" | |
| 29 | |
| 30 namespace shell { | |
| 31 class ServiceContext; | |
| 32 | |
| 33 // Creates an identity for the Service Manager, used when the Service Manager | |
| 34 // connects to services. | |
| 35 Identity CreateServiceManagerIdentity(); | |
| 36 | |
| 37 class ServiceManager : public Service { | |
| 38 public: | |
| 39 // API for testing. | |
| 40 class TestAPI { | |
| 41 public: | |
| 42 explicit TestAPI(ServiceManager* service_manager); | |
| 43 ~TestAPI(); | |
| 44 | |
| 45 // Returns true if there is a Instance for this name. | |
| 46 bool HasRunningInstanceForName(const std::string& name) const; | |
| 47 private: | |
| 48 ServiceManager* service_manager_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(TestAPI); | |
| 51 }; | |
| 52 | |
| 53 // |native_runner_factory| is an instance of an object capable of vending | |
| 54 // implementations of NativeRunner, e.g. for in or out-of-process execution. | |
| 55 // See native_runner.h and RunNativeApplication(). | |
| 56 // |file_task_runner| provides access to a thread to perform file copy | |
| 57 // operations on. | |
| 58 ServiceManager(std::unique_ptr<NativeRunnerFactory> native_runner_factory, | |
| 59 mojom::ServicePtr catalog); | |
| 60 ~ServiceManager() override; | |
| 61 | |
| 62 // Sets overrides for service executable and package resolution. Must be | |
| 63 // called before any services are launched. | |
| 64 void SetServiceOverrides(std::unique_ptr<ServiceOverrides> overrides); | |
| 65 | |
| 66 // Provide a callback to be notified whenever an instance is destroyed. | |
| 67 // Typically the creator of the Service Manager will use this to determine | |
| 68 // when some set of services it created are destroyed, so it can shut down. | |
| 69 void SetInstanceQuitCallback(base::Callback<void(const Identity&)> callback); | |
| 70 | |
| 71 // Completes a connection between a source and target application as defined | |
| 72 // by |params|, exchanging InterfaceProviders between them. If no existing | |
| 73 // instance of the target application is running, one will be loaded. | |
| 74 void Connect(std::unique_ptr<ConnectParams> params); | |
| 75 | |
| 76 // Creates a new Instance identified as |name|. This is intended for use by | |
| 77 // the Service Manager's embedder to register itself. This must only be called | |
| 78 // once. | |
| 79 mojom::ServiceRequest StartEmbedderService(const std::string& name); | |
| 80 | |
| 81 private: | |
| 82 class Instance; | |
| 83 | |
| 84 // Service: | |
| 85 bool OnConnect(const Identity& remote_identity, | |
| 86 InterfaceRegistry* registry) override; | |
| 87 | |
| 88 void InitCatalog(mojom::ServicePtr catalog); | |
| 89 | |
| 90 // Returns the resolver to use for the specified identity. | |
| 91 // NOTE: Resolvers are cached to ensure we service requests in order. If | |
| 92 // we use a separate Resolver for each request ordering is not | |
| 93 // guaranteed and can lead to random flake. | |
| 94 mojom::Resolver* GetResolver(const Identity& identity); | |
| 95 | |
| 96 // Called when |instance| encounters an error. Deletes |instance|. | |
| 97 void OnInstanceError(Instance* instance); | |
| 98 | |
| 99 // Called when |instance| becomes unreachable to new connections because it | |
| 100 // no longer has any pipes to the ServiceManager. | |
| 101 void OnInstanceUnreachable(Instance* instance); | |
| 102 | |
| 103 // Called by an Instance as it's being destroyed. | |
| 104 void OnInstanceStopped(const Identity& identity); | |
| 105 | |
| 106 // Completes a connection between a source and target application as defined | |
| 107 // by |params|, exchanging InterfaceProviders between them. If no existing | |
| 108 // instance of the target application is running, one will be loaded. | |
| 109 // | |
| 110 // If |service| is not null, there must not be an instance of the target | |
| 111 // application already running. The Service Manager will create a new instance | |
| 112 // and use |service| to control it. | |
| 113 // | |
| 114 // If |instance| is not null, the lifetime of the connection request is | |
| 115 // bounded by that of |instance|. The connection will be cancelled dropped if | |
| 116 // |instance| is destroyed. | |
| 117 void Connect(std::unique_ptr<ConnectParams> params, | |
| 118 mojom::ServicePtr service, | |
| 119 base::WeakPtr<Instance> source_instance); | |
| 120 | |
| 121 // Returns a running instance matching |identity|. This might be an instance | |
| 122 // running as a different user if one is available that services all users. | |
| 123 Instance* GetExistingInstance(const Identity& identity) const; | |
| 124 | |
| 125 void NotifyPIDAvailable(const Identity& identity, base::ProcessId pid); | |
| 126 | |
| 127 // Attempt to complete the connection requested by |params| by connecting to | |
| 128 // an existing instance. If there is an existing instance, |params| is taken, | |
| 129 // and this function returns true. | |
| 130 bool ConnectToExistingInstance(std::unique_ptr<ConnectParams>* params); | |
| 131 | |
| 132 Instance* CreateInstance(const Identity& source, | |
| 133 const Identity& target, | |
| 134 const CapabilitySpec& spec); | |
| 135 | |
| 136 // Called from the instance implementing mojom::ServiceManager. | |
| 137 void AddListener(mojom::ServiceManagerListenerPtr listener); | |
| 138 | |
| 139 void CreateServiceWithFactory(const Identity& service_factory, | |
| 140 const std::string& name, | |
| 141 mojom::ServiceRequest request); | |
| 142 // Returns a running ServiceFactory for |service_factory_identity|. | |
| 143 // If there is not one running one is started for |source_identity|. | |
| 144 mojom::ServiceFactory* GetServiceFactory( | |
| 145 const Identity& service_factory_identity); | |
| 146 void OnServiceFactoryLost(const Identity& which); | |
| 147 | |
| 148 // Callback when remote Catalog resolves mojo:foo to mojo:bar. | |
| 149 // |params| are the params passed to Connect(). | |
| 150 // |service| if provided is a ServicePtr which should be used to manage the | |
| 151 // new application instance. This may be null. | |
| 152 // |result| contains the result of the resolve operation. | |
| 153 void OnGotResolvedName(std::unique_ptr<ConnectParams> params, | |
| 154 mojom::ServicePtr service, | |
| 155 bool has_source_instance, | |
| 156 base::WeakPtr<Instance> source_instance, | |
| 157 mojom::ResolveResultPtr result); | |
| 158 | |
| 159 base::WeakPtr<ServiceManager> GetWeakPtr(); | |
| 160 | |
| 161 std::unique_ptr<ServiceOverrides> service_overrides_; | |
| 162 | |
| 163 // Ownership of all root Instances. Non-root Instances are owned by their | |
| 164 // parent Instance. | |
| 165 using InstanceMap = std::map<Instance*, std::unique_ptr<Instance>>; | |
| 166 InstanceMap root_instances_; | |
| 167 | |
| 168 // Maps service identities to reachable instances. Note that the Instance* | |
| 169 // values here are NOT owned by this map. | |
| 170 std::map<Identity, Instance*> identity_to_instance_; | |
| 171 | |
| 172 // Always points to the ServiceManager's own Instance. Note that this | |
| 173 // Instance still has an entry in |root_instances_|. | |
| 174 Instance* service_manager_instance_; | |
| 175 | |
| 176 // Tracks the names of instances that are allowed to field connection requests | |
| 177 // from all users. | |
| 178 std::set<std::string> singletons_; | |
| 179 | |
| 180 std::map<Identity, mojom::ServiceFactoryPtr> service_factories_; | |
| 181 std::map<Identity, mojom::ResolverPtr> identity_to_resolver_; | |
| 182 mojo::InterfacePtrSet<mojom::ServiceManagerListener> listeners_; | |
| 183 base::Callback<void(const Identity&)> instance_quit_callback_; | |
| 184 std::unique_ptr<NativeRunnerFactory> native_runner_factory_; | |
| 185 std::unique_ptr<ServiceContext> service_context_; | |
| 186 base::WeakPtrFactory<ServiceManager> weak_ptr_factory_; | |
| 187 | |
| 188 DISALLOW_COPY_AND_ASSIGN(ServiceManager); | |
| 189 }; | |
| 190 | |
| 191 mojom::Connector::ConnectCallback EmptyConnectCallback(); | |
| 192 | |
| 193 } // namespace shell | |
| 194 | |
| 195 #endif // SERVICES_SHELL_SERVICE_MANAGER_H_ | |
| OLD | NEW |