| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SERVICES_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ | 5 #ifndef SERVICES_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ |
| 6 #define SERVICES_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ | 6 #define SERVICES_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_vector.h" |
| 14 #include "mojo/public/cpp/bindings/binding.h" | 15 #include "mojo/public/cpp/bindings/binding.h" |
| 15 #include "mojo/public/cpp/system/core.h" | 16 #include "mojo/public/cpp/system/core.h" |
| 16 #include "services/shell/public/cpp/connector.h" | |
| 17 #include "services/shell/public/cpp/service.h" | 17 #include "services/shell/public/cpp/service.h" |
| 18 #include "services/shell/public/interfaces/connector.mojom.h" | 18 #include "services/shell/public/interfaces/connector.mojom.h" |
| 19 #include "services/shell/public/interfaces/service.mojom.h" | 19 #include "services/shell/public/interfaces/service.mojom.h" |
| 20 | 20 |
| 21 namespace shell { | 21 namespace shell { |
| 22 | 22 |
| 23 class Connector; |
| 24 |
| 23 // Encapsulates a connection to the Mojo Shell in two parts: | 25 // Encapsulates a connection to the Mojo Shell in two parts: |
| 24 // - a bound InterfacePtr to mojom::Shell, the primary mechanism | 26 // - a bound InterfacePtr to mojom::Shell, the primary mechanism |
| 25 // by which the instantiating application interacts with other services | 27 // by which the instantiating application interacts with other services |
| 26 // brokered by the Mojo Shell. | 28 // brokered by the Mojo Shell. |
| 27 // - a bound InterfaceRequest of mojom::Service, an interface | 29 // - a bound InterfaceRequest of mojom::Service, an interface |
| 28 // used by the Mojo Shell to inform this application of lifecycle events and | 30 // used by the Mojo Shell to inform this application of lifecycle events and |
| 29 // inbound connections brokered by it. | 31 // inbound connections brokered by it. |
| 30 // | 32 // |
| 31 // This class should be used in two scenarios: | 33 // This class should be used in two scenarios: |
| 32 // - During early startup to bind the mojom::ServiceRequest obtained from | 34 // - During early startup to bind the mojom::ServiceRequest obtained from |
| 33 // the Mojo Shell, typically in response to either MojoMain() or main(). | 35 // the Mojo Shell, typically in response to either MojoMain() or main(). |
| 34 // - In an implementation of mojom::ServiceFactory to bind the | 36 // - In an implementation of mojom::ServiceFactory to bind the |
| 35 // mojom::ServiceRequest passed via StartApplication. In this scenario | 37 // mojom::ServiceRequest passed via StartApplication. In this scenario |
| 36 // there can be many instances of this class per process. | 38 // there can be many instances of this class per process. |
| 37 // | 39 // |
| 38 // Instances of this class are constructed with an implementation of the Shell | 40 // Instances of this class are constructed with an implementation of the Shell |
| 39 // Client Lib's Service interface. See documentation in service.h | 41 // Client Lib's Service interface. See documentation in service.h |
| 40 // for details. | 42 // for details. |
| 41 // | 43 // |
| 42 class ShellConnection : public mojom::Service { | 44 class ShellConnection : public mojom::Service { |
| 43 public: | 45 public: |
| 44 // Creates a new ShellConnection bound to |request|. This connection may be | 46 // Creates a new ShellConnection bound to |request|. This connection may be |
| 45 // used immediately to make outgoing connections via connector(). Does not | 47 // used immediately to make outgoing connections via connector(). Does not |
| 46 // take ownership of |service|, which must remain valid for the lifetime of | 48 // take ownership of |client|, which must remain valid for the lifetime of |
| 47 // ShellConnection. If either |connector| or |connector_request| is non-null | 49 // ShellConnection. |
| 48 // both must be non-null. If both are null, the connection will create its own | 50 ShellConnection(shell::Service* client, |
| 49 // Connector and request to pass to the shell on initialization. | 51 mojom::ServiceRequest request); |
| 50 ShellConnection(shell::Service* service, | |
| 51 mojom::ServiceRequest request, | |
| 52 std::unique_ptr<Connector> connector = nullptr, | |
| 53 mojom::ConnectorRequest connector_request = nullptr); | |
| 54 | 52 |
| 55 ~ShellConnection() override; | 53 ~ShellConnection() override; |
| 56 | 54 |
| 57 Connector* connector() { return connector_.get(); } | 55 Connector* connector() { return connector_.get(); } |
| 58 const Identity& identity() { return identity_; } | 56 const Identity& identity() { return identity_; } |
| 59 | 57 |
| 58 // TODO(rockot): Remove this. http://crbug.com/594852. |
| 59 void set_initialize_handler(const base::Closure& callback); |
| 60 |
| 61 // TODO(rockot): Remove this once we get rid of app tests. |
| 62 void SetAppTestConnectorForTesting(mojom::ConnectorPtr connector); |
| 63 |
| 60 // Specify a function to be called when the connection to the shell is lost. | 64 // Specify a function to be called when the connection to the shell is lost. |
| 61 // Note that if connection has already been lost, then |closure| is called | 65 // Note that if connection has already been lost, then |closure| is called |
| 62 // immediately. | 66 // immediately. |
| 63 void SetConnectionLostClosure(const base::Closure& closure); | 67 void SetConnectionLostClosure(const base::Closure& closure); |
| 64 | 68 |
| 65 private: | 69 private: |
| 66 // mojom::Service: | 70 // mojom::Service: |
| 67 void OnStart(mojom::IdentityPtr identity, | 71 void OnStart(mojom::IdentityPtr identity, |
| 68 uint32_t id, | 72 uint32_t id, |
| 69 const OnStartCallback& callback) override; | 73 const OnStartCallback& callback) override; |
| 70 void OnConnect(mojom::IdentityPtr source, | 74 void OnConnect(mojom::IdentityPtr source, |
| 71 uint32_t source_id, | 75 uint32_t source_id, |
| 72 mojom::InterfaceProviderRequest remote_interfaces, | 76 mojom::InterfaceProviderRequest remote_interfaces, |
| 73 mojom::InterfaceProviderPtr local_interfaces, | 77 mojom::InterfaceProviderPtr local_interfaces, |
| 74 mojom::CapabilityRequestPtr allowed_capabilities, | 78 mojom::CapabilityRequestPtr allowed_capabilities, |
| 75 const mojo::String& name) override; | 79 const mojo::String& name) override; |
| 76 | 80 |
| 77 void OnConnectionError(); | 81 void OnConnectionError(); |
| 78 | 82 |
| 79 // A callback called when OnStart() is run. | 83 // A callback called when OnStart() is run. |
| 80 base::Closure initialize_handler_; | 84 base::Closure initialize_handler_; |
| 81 | 85 |
| 82 // We track the lifetime of incoming connection registries as it more | 86 // We track the lifetime of incoming connection registries as it more |
| 83 // convenient for the client. | 87 // convenient for the client. |
| 84 std::vector<std::unique_ptr<Connection>> incoming_connections_; | 88 ScopedVector<Connection> incoming_connections_; |
| 85 | 89 |
| 86 // A pending Connector request which will eventually be passed to the shell. | 90 // A pending Connector request which will eventually be passed to the shell. |
| 87 mojom::ConnectorRequest pending_connector_request_; | 91 mojom::ConnectorRequest pending_connector_request_; |
| 88 | 92 |
| 89 shell::Service* service_; | 93 shell::Service* client_; |
| 90 mojo::Binding<mojom::Service> binding_; | 94 mojo::Binding<mojom::Service> binding_; |
| 91 std::unique_ptr<Connector> connector_; | 95 std::unique_ptr<Connector> connector_; |
| 92 shell::Identity identity_; | 96 shell::Identity identity_; |
| 93 bool should_run_connection_lost_closure_ = false; | 97 bool should_run_connection_lost_closure_ = false; |
| 94 | 98 |
| 95 base::Closure connection_lost_closure_; | 99 base::Closure connection_lost_closure_; |
| 96 | 100 |
| 97 DISALLOW_COPY_AND_ASSIGN(ShellConnection); | 101 DISALLOW_COPY_AND_ASSIGN(ShellConnection); |
| 98 }; | 102 }; |
| 99 | 103 |
| 100 } // namespace shell | 104 } // namespace shell |
| 101 | 105 |
| 102 #endif // SERVICES_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ | 106 #endif // SERVICES_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ |
| OLD | NEW |