Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Unified Diff: services/service_manager/public/cpp/service.h

Issue 2487573002: Service Manager: Remove ServiceContext* arg from Service::OnStart() (Closed)
Patch Set: rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: services/service_manager/public/cpp/service.h
diff --git a/services/service_manager/public/cpp/service.h b/services/service_manager/public/cpp/service.h
index 958f10f16bb9971ce42c0729cdffd37dda03c3fe..2b8e780d0b815c9bfad0241c168def0431467a70 100644
--- a/services/service_manager/public/cpp/service.h
+++ b/services/service_manager/public/cpp/service.h
@@ -12,33 +12,26 @@ class ServiceContext;
struct ServiceInfo;
// The primary contract between a Service and the Service Manager, receiving
-// lifecycle notifications and connection requests. Every Service must minimally
-// implement OnConnect().
+// lifecycle notifications and connection requests.
class Service {
public:
+ Service();
virtual ~Service();
- // Called once a bidirectional connection with the Service Manager has been
- // established.
- //
- // |context| is the ServiceContext for this instance of the service. It's
- // guaranteed to outlive the Service instance and therefore may be retained by
- // it.
- //
- // Use the context to retrieve information about the service instance, make
- // outgoing service connections, and issue other requests to the Service
- // Manager on behalf of this instance.
- //
- // Called exactly once before any calls to OnConnect().
- virtual void OnStart(ServiceContext* context);
+ // Called exactly once, when a bidirectional connection with the Service
+ // Manager has been established. No calls to OnConnect() will be received
+ // before this.
+ virtual void OnStart();
// Called each time a connection to this service is brokered by the Service
// Manager. Implement this to expose interfaces to other services.
//
// Return true if the connection should succeed or false if the connection
// should be rejected.
+ //
+ // The default implementation returns false.
virtual bool OnConnect(const ServiceInfo& remote_info,
- InterfaceRegistry* registry) = 0;
+ InterfaceRegistry* registry);
// Called when the Service Manager has stopped tracking this instance. The
// service should use this as a signal to shut down, and in fact its process
@@ -56,6 +49,40 @@ class Service {
// OnConnect() is invoked, neither will be invoked at any point after this
// OnStop().
virtual bool OnStop();
+
+ protected:
+ // Access the ServiceContext associated with this Service. Note that this is
+ // only valid to call during or after OnStart(), but never before! As such,
+ // it's always safe to call in OnStart() and OnConnect(), but should generally
+ // be avoided in OnStop().
+ ServiceContext* context() const;
+
+ private:
+ friend class ForwardingService;
+ friend class ServiceContext;
+
+ // NOTE: This is guaranteed to be called before OnStart().
+ void set_context(ServiceContext* context) { service_context_ = context; }
+
+ ServiceContext* service_context_ = nullptr;
+};
+
+// TODO(rockot): Remove this. It's here to satisfy a few remaining use cases
+// where a Service impl is owned by something other than its ServiceContext.
+class ForwardingService : public Service {
+ public:
+ // |target| must outlive this object.
+ explicit ForwardingService(Service* target);
+ ~ForwardingService() override;
+
+ private:
+ // Service:
+ void OnStart() override;
+ bool OnConnect(const ServiceInfo& remote_info,
+ InterfaceRegistry* registry) override;
+ bool OnStop() override;
+
+ Service* const target_ = nullptr;
};
} // namespace service_manager
« no previous file with comments | « services/service_manager/public/cpp/lib/service_test.cc ('k') | services/service_manager/public/cpp/service_test.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698