service_instance.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2019 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. #ifndef SERVICES_SERVICE_MANAGER_SERVICE_INSTANCE_H_
  5. #define SERVICES_SERVICE_MANAGER_SERVICE_INSTANCE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include "base/containers/unique_ptr_adapters.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/process/process_handle.h"
  14. #include "build/build_config.h"
  15. #include "mojo/public/cpp/bindings/associated_receiver.h"
  16. #include "mojo/public/cpp/bindings/pending_associated_receiver.h"
  17. #include "mojo/public/cpp/bindings/pending_receiver.h"
  18. #include "mojo/public/cpp/bindings/pending_remote.h"
  19. #include "mojo/public/cpp/bindings/receiver.h"
  20. #include "mojo/public/cpp/bindings/receiver_set.h"
  21. #include "mojo/public/cpp/bindings/remote.h"
  22. #include "services/service_manager/public/cpp/identity.h"
  23. #include "services/service_manager/public/cpp/manifest.h"
  24. #include "services/service_manager/public/mojom/connector.mojom.h"
  25. #include "services/service_manager/public/mojom/service.mojom.h"
  26. #include "services/service_manager/public/mojom/service_control.mojom.h"
  27. #include "services/service_manager/public/mojom/service_manager.mojom.h"
  28. #include "third_party/abseil-cpp/absl/types/optional.h"
  29. namespace sandbox {
  30. namespace mojom {
  31. enum class Sandbox;
  32. }
  33. } // namespace sandbox
  34. namespace service_manager {
  35. class ServiceManager;
  36. class ServiceProcessHost;
  37. // ServiceInstance is the Service Manager-side representation of a service
  38. // instance running in the system. All communication between the Service
  39. // Manager and a running service instance is facilitated by a single
  40. // corresponding ServiceInstance object in the Service Manager, dedicated to
  41. // that instance.
  42. class ServiceInstance : public mojom::Connector,
  43. public mojom::ProcessMetadata,
  44. public mojom::ServiceControl,
  45. public mojom::ServiceManager {
  46. public:
  47. // |service_manager| must outlive this ServiceInstance.
  48. ServiceInstance(service_manager::ServiceManager* service_manager,
  49. const Identity& identity,
  50. const Manifest& manifest);
  51. ServiceInstance(const ServiceInstance&) = delete;
  52. ServiceInstance& operator=(const ServiceInstance&) = delete;
  53. ~ServiceInstance() override;
  54. const Identity& identity() const { return identity_; }
  55. const Manifest& manifest() const { return manifest_; }
  56. // mojom::ProcessMetadata:
  57. void SetPID(base::ProcessId pid) override;
  58. // Starts this instance using an already-established Service pipe.
  59. void StartWithRemote(mojo::PendingRemote<mojom::Service> remote);
  60. #if !BUILDFLAG(IS_IOS)
  61. // Starts this instance from a path to a service executable on disk.
  62. bool StartWithProcessHost(std::unique_ptr<ServiceProcessHost> host,
  63. sandbox::mojom::Sandbox sandbox_type);
  64. #endif // !BUILDFLAG(IS_IOS)
  65. // Binds an endpoint for this instance to receive metadata about its
  66. // corresponding service process, if any.
  67. void BindProcessMetadataReceiver(
  68. mojo::PendingReceiver<mojom::ProcessMetadata> receiver);
  69. // Forwards a BindInterface request from |source_instance| to this instance,
  70. // iff it should be allowed based on manifest constraints. Returns |true| if
  71. // the request was allowed, or |false| otherwise.
  72. bool MaybeAcceptConnectionRequest(
  73. const ServiceInstance& source_instance,
  74. const std::string& interface_name,
  75. mojo::ScopedMessagePipeHandle receiving_pipe,
  76. mojom::BindInterfacePriority priority);
  77. // Asks this service instance to bind a new concrete service implementation
  78. // for |packaged_instance_identity|. The packaged instance will always
  79. // correspond to a service packaged in this service's manifest.
  80. bool CreatePackagedServiceInstance(
  81. const Identity& packaged_instance_identity,
  82. mojo::PendingReceiver<mojom::Service> receiver,
  83. mojo::PendingRemote<mojom::ProcessMetadata> metadata);
  84. // Stops receiving any new messages from the service instance and renders the
  85. // instance permanently unreachable. Note that this does NOT make any attempt
  86. // to join the service instance's process if any exists.
  87. void Stop();
  88. // Creates a structure of metadata describing this instance, to be passed to
  89. // ServiceManagerListener clients observing running instances in the system.
  90. mojom::RunningServiceInfoPtr CreateRunningServiceInfo() const;
  91. // Binds a ServiceManager interface receiver for this instance. Instances may
  92. // connect to this interface on the Service Manager if sufficiently privileged
  93. // to observe Service Manager state according to manifest declarations.
  94. void BindServiceManagerReceiver(
  95. mojo::PendingReceiver<mojom::ServiceManager> receiver);
  96. private:
  97. class InterfaceFilter;
  98. friend class InterfaceFilter;
  99. void OnStartCompleted(
  100. mojo::PendingReceiver<mojom::Connector> connector_receiver,
  101. mojo::PendingAssociatedReceiver<mojom::ServiceControl> control_receiver);
  102. void OnConnectRequestAcknowledged();
  103. void MarkUnreachable();
  104. void MaybeNotifyPidAvailable();
  105. void OnServiceDisconnected();
  106. void OnConnectorDisconnected();
  107. void HandleServiceOrConnectorDisconnection();
  108. // Examines an interface connection request coming from this service instance
  109. // and determines whether it should be allowed to reach any designated target
  110. // instance. Returns |true| if so, or |false| otherwise.
  111. //
  112. // If |target_interface_name| is null, it is sufficient for this (the source)
  113. // service to have access to *any* arbitrary interface on the target service.
  114. bool CanConnectToOtherInstance(
  115. const ServiceFilter& target_filter,
  116. const absl::optional<std::string>& target_interface_name);
  117. // mojom::Connector:
  118. void BindInterface(const ServiceFilter& target_filter,
  119. const std::string& interface_name,
  120. mojo::ScopedMessagePipeHandle receiving_pipe,
  121. mojom::BindInterfacePriority priority,
  122. BindInterfaceCallback callback) override;
  123. void QueryService(const std::string& service_name,
  124. QueryServiceCallback callback) override;
  125. void WarmService(const ServiceFilter& target_filter,
  126. WarmServiceCallback callback) override;
  127. void RegisterServiceInstance(
  128. const Identity& identity,
  129. mojo::ScopedMessagePipeHandle service_remote_handle,
  130. mojo::PendingReceiver<mojom::ProcessMetadata> metadata_receiver,
  131. RegisterServiceInstanceCallback callback) override;
  132. void Clone(mojo::PendingReceiver<mojom::Connector> receiver) override;
  133. // mojom::ServiceControl:
  134. void RequestQuit() override;
  135. // mojom::ServiceManager:
  136. void AddListener(
  137. mojo::PendingRemote<mojom::ServiceManagerListener> listener) override;
  138. // Always owns |this|.
  139. const raw_ptr<service_manager::ServiceManager> service_manager_;
  140. // A unique identity for this instance. Distinct from PID, as a single process
  141. // may host multiple service instances. Globally unique across time and space.
  142. const Identity identity_;
  143. // The static service manifest provided for this service at system
  144. // initialization time.
  145. const Manifest manifest_;
  146. // Indicates if this instance is allowed to communicate with all service
  147. // instances in the system.
  148. const bool can_contact_all_services_;
  149. #if !BUILDFLAG(IS_IOS)
  150. std::unique_ptr<ServiceProcessHost> process_host_;
  151. #endif
  152. // The Service remote used to control the instance.
  153. mojo::Remote<mojom::Service> service_remote_;
  154. // Receivers for the various interfaces implemented by this object. These all
  155. // receive calls directly from the service instance itself or some trusted
  156. // representative thereof.
  157. mojo::Receiver<mojom::ProcessMetadata> process_metadata_receiver_{this};
  158. mojo::ReceiverSet<mojom::Connector> connector_receivers_;
  159. mojo::ReceiverSet<mojom::ServiceManager> service_manager_receivers_;
  160. mojo::AssociatedReceiver<mojom::ServiceControl> control_receiver_{this};
  161. // The PID of the process running the service instance, if known.
  162. base::ProcessId pid_ = base::kNullProcessId;
  163. // The current lifecycle state of the service, e.g. whether it's currently
  164. // starting, running, etc.
  165. mojom::InstanceState state_ = mojom::InstanceState::kCreated;
  166. // Indicates if the instance is permanently stopped.
  167. bool stopped_ = false;
  168. // The number of outstanding OnBindingInterface requests currently in flight
  169. // for this instance. This is the total number of OnBindInterface requests
  170. // sent to the instance, minus the number of acks received so far. If non-zero
  171. // clean instance termination is impossible at the moment.
  172. int pending_service_connections_ = 0;
  173. base::WeakPtrFactory<ServiceInstance> weak_ptr_factory_{this};
  174. };
  175. } // namespace service_manager
  176. #endif // SERVICES_SERVICE_MANAGER_SERVICE_INSTANCE_H_