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_PUBLIC_CPP_SERVICE_CONTEXT_H_ | |
6 #define SERVICES_SHELL_PUBLIC_CPP_SERVICE_CONTEXT_H_ | |
7 | |
8 #include <memory> | |
9 #include <utility> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/macros.h" | |
14 #include "mojo/public/cpp/bindings/binding.h" | |
15 #include "mojo/public/cpp/system/core.h" | |
16 #include "services/shell/public/cpp/connector.h" | |
17 #include "services/shell/public/cpp/service.h" | |
18 #include "services/shell/public/interfaces/connector.mojom.h" | |
19 #include "services/shell/public/interfaces/service.mojom.h" | |
20 | |
21 namespace shell { | |
22 | |
23 // Encapsulates a connection to the Service Manager in two parts: | |
24 // - a bound InterfacePtr to mojom::Connector, the primary mechanism | |
25 // by which the instantiating service connects to other services, | |
26 // brokered by the Service Manager. | |
27 // - a bound InterfaceRequest of mojom::Service, an interface used by the | |
28 // Service Manager to inform this service of lifecycle events and | |
29 // inbound connections brokered by it. | |
30 // | |
31 // This class should be used in two scenarios: | |
32 // - During early startup to bind the mojom::ServiceRequest obtained from | |
33 // the Service Manager, typically in response to either ServiceMain() or | |
34 // main(). | |
35 // - In an implementation of mojom::ServiceFactory to bind the | |
36 // mojom::ServiceRequest passed via CreateService. In this scenario there can | |
37 // be many instances of this class per process. | |
38 // | |
39 // Instances of this class are constructed with an implementation of the Service | |
40 // Manager Client Lib's Service interface. See documentation in service.h | |
41 // for details. | |
42 // | |
43 class ServiceContext : public mojom::Service { | |
44 public: | |
45 // Creates a new ServiceContext bound to |request|. This connection may be | |
46 // used immediately to make outgoing connections via connector(). Does not | |
47 // take ownership of |service|, which must remain valid for the lifetime of | |
48 // ServiceContext. If either |connector| or |connector_request| is non-null | |
49 // both must be non-null. If both are null, the connection will create its own | |
50 // Connector and request to pass to the Service Manager on initialization. | |
51 ServiceContext(shell::Service* service, | |
52 mojom::ServiceRequest request, | |
53 std::unique_ptr<Connector> connector = nullptr, | |
54 mojom::ConnectorRequest connector_request = nullptr); | |
55 | |
56 ~ServiceContext() override; | |
57 | |
58 Connector* connector() { return connector_.get(); } | |
59 const Identity& identity() { return identity_; } | |
60 | |
61 // Specify a function to be called when the connection to the shell is lost. | |
62 // Note that if connection has already been lost, then |closure| is called | |
63 // immediately. | |
64 void SetConnectionLostClosure(const base::Closure& closure); | |
65 | |
66 private: | |
67 // mojom::Service: | |
68 void OnStart(const Identity& identity, | |
69 const OnStartCallback& callback) override; | |
70 void OnConnect(const Identity& source, | |
71 mojom::InterfaceProviderRequest interfaces, | |
72 const Interfaces& allowed_interfaces, | |
73 const Classes& allowed_classes) override; | |
74 | |
75 void OnConnectionError(); | |
76 | |
77 // A callback called when OnStart() is run. | |
78 base::Closure initialize_handler_; | |
79 | |
80 // We track the lifetime of incoming connection registries as it more | |
81 // convenient for the client. | |
82 std::vector<std::unique_ptr<InterfaceRegistry>> incoming_connections_; | |
83 | |
84 // A pending Connector request which will eventually be passed to the Service | |
85 // Manager. | |
86 mojom::ConnectorRequest pending_connector_request_; | |
87 | |
88 shell::Service* service_; | |
89 mojo::Binding<mojom::Service> binding_; | |
90 std::unique_ptr<Connector> connector_; | |
91 shell::Identity identity_; | |
92 bool should_run_connection_lost_closure_ = false; | |
93 | |
94 base::Closure connection_lost_closure_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(ServiceContext); | |
97 }; | |
98 | |
99 } // namespace shell | |
100 | |
101 #endif // SERVICES_SHELL_PUBLIC_CPP_SERVICE_CONTEXT_H_ | |
OLD | NEW |