| 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 <utility> | 9 #include <utility> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/callback.h" | 12 #include "base/callback.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/scoped_vector.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/bindings/callback.h" | 16 #include "mojo/public/cpp/bindings/callback.h" |
| 16 #include "mojo/public/cpp/system/core.h" | 17 #include "mojo/public/cpp/system/core.h" |
| 17 #include "services/shell/public/cpp/shell_client.h" | 18 #include "services/shell/public/cpp/shell_client.h" |
| 18 #include "services/shell/public/interfaces/connector.mojom.h" | 19 #include "services/shell/public/interfaces/connector.mojom.h" |
| 19 #include "services/shell/public/interfaces/shell_client.mojom.h" | 20 #include "services/shell/public/interfaces/shell_client.mojom.h" |
| 20 | 21 |
| 21 namespace mojo { | 22 namespace shell { |
| 22 | 23 |
| 23 class Connector; | 24 class Connector; |
| 24 | 25 |
| 25 // Encapsulates a connection to the Mojo Shell in two parts: | 26 // Encapsulates a connection to the Mojo Shell in two parts: |
| 26 // - a bound InterfacePtr to mojo::shell::mojom::Shell, the primary mechanism | 27 // - a bound InterfacePtr to mojom::Shell, the primary mechanism |
| 27 // by which the instantiating application interacts with other services | 28 // by which the instantiating application interacts with other services |
| 28 // brokered by the Mojo Shell. | 29 // brokered by the Mojo Shell. |
| 29 // - a bound InterfaceRequest of mojo::shell::mojom::ShellClient, an interface | 30 // - a bound InterfaceRequest of mojom::ShellClient, an interface |
| 30 // used by the Mojo Shell to inform this application of lifecycle events and | 31 // used by the Mojo Shell to inform this application of lifecycle events and |
| 31 // inbound connections brokered by it. | 32 // inbound connections brokered by it. |
| 32 // | 33 // |
| 33 // This class should be used in two scenarios: | 34 // This class should be used in two scenarios: |
| 34 // - During early startup to bind the mojo::shell::mojom::ShellClientRequest | 35 // - During early startup to bind the mojom::ShellClientRequest obtained from |
| 35 // obtained from the Mojo Shell. This is typically in response to either | 36 // the Mojo Shell, typically in response to either MojoMain() or main(). |
| 36 // MojoMain() or main(). | 37 // - In an implementation of mojom::ShellClientFactory to bind the |
| 37 // - In an implementation of mojo::shell::mojom::ContentHandler to bind the | 38 // mojom::ShellClientRequest passed via StartApplication. In this scenario |
| 38 // mojo::shell::mojom::ShellClientRequest passed via StartApplication. In this | 39 // there can be many instances of this class per process. |
| 39 // scenario there can be many instances of this class per process. | |
| 40 // | 40 // |
| 41 // Instances of this class are constructed with an implementation of the Shell | 41 // Instances of this class are constructed with an implementation of the Shell |
| 42 // Client Lib's mojo::ShellClient interface. See documentation in shell_client.h | 42 // Client Lib's ShellClient interface. See documentation in shell_client.h |
| 43 // for details. | 43 // for details. |
| 44 // | 44 // |
| 45 class ShellConnection : public shell::mojom::ShellClient { | 45 class ShellConnection : public mojom::ShellClient { |
| 46 public: | 46 public: |
| 47 // Creates a new ShellConnection bound to |request|. This connection may be | 47 // Creates a new ShellConnection bound to |request|. This connection may be |
| 48 // used immediately to make outgoing connections via connector(). Does not | 48 // used immediately to make outgoing connections via connector(). Does not |
| 49 // take ownership of |client|, which must remain valid for the lifetime of | 49 // take ownership of |client|, which must remain valid for the lifetime of |
| 50 // ShellConnection. | 50 // ShellConnection. |
| 51 ShellConnection(mojo::ShellClient* client, | 51 ShellConnection(shell::ShellClient* client, |
| 52 shell::mojom::ShellClientRequest request); | 52 mojom::ShellClientRequest request); |
| 53 | 53 |
| 54 ~ShellConnection() override; | 54 ~ShellConnection() override; |
| 55 | 55 |
| 56 Connector* connector() { return connector_.get(); } | 56 Connector* connector() { return connector_.get(); } |
| 57 | 57 |
| 58 // TODO(rockot): Remove this. http://crbug.com/594852. | 58 // TODO(rockot): Remove this. http://crbug.com/594852. |
| 59 void set_initialize_handler(const base::Closure& callback); | 59 void set_initialize_handler(const base::Closure& callback); |
| 60 | 60 |
| 61 // TODO(rockot): Remove this once we get rid of app tests. | 61 // TODO(rockot): Remove this once we get rid of app tests. |
| 62 void SetAppTestConnectorForTesting(shell::mojom::ConnectorPtr connector); | 62 void SetAppTestConnectorForTesting(mojom::ConnectorPtr connector); |
| 63 | 63 |
| 64 // 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. |
| 65 void set_connection_lost_closure(const base::Closure& closure) { | 65 void set_connection_lost_closure(const base::Closure& closure) { |
| 66 connection_lost_closure_ = closure; | 66 connection_lost_closure_ = closure; |
| 67 } | 67 } |
| 68 | 68 |
| 69 private: | 69 private: |
| 70 // shell::mojom::ShellClient: | 70 // mojom::ShellClient: |
| 71 void Initialize(shell::mojom::IdentityPtr identity, | 71 void Initialize(mojom::IdentityPtr identity, |
| 72 uint32_t id, | 72 uint32_t id, |
| 73 const InitializeCallback& callback) override; | 73 const InitializeCallback& callback) override; |
| 74 void AcceptConnection( | 74 void AcceptConnection(mojom::IdentityPtr source, |
| 75 shell::mojom::IdentityPtr source, | 75 uint32_t source_id, |
| 76 uint32_t source_id, | 76 mojom::InterfaceProviderRequest remote_interfaces, |
| 77 shell::mojom::InterfaceProviderRequest remote_interfaces, | 77 mojom::InterfaceProviderPtr local_interfaces, |
| 78 shell::mojom::InterfaceProviderPtr local_interfaces, | 78 mojom::CapabilityRequestPtr allowed_capabilities, |
| 79 shell::mojom::CapabilityRequestPtr allowed_capabilities, | 79 const mojo::String& name) override; |
| 80 const String& name) override; | |
| 81 | 80 |
| 82 void OnConnectionError(); | 81 void OnConnectionError(); |
| 83 | 82 |
| 84 // A callback called when Initialize() is run. | 83 // A callback called when Initialize() is run. |
| 85 base::Closure initialize_handler_; | 84 base::Closure initialize_handler_; |
| 86 | 85 |
| 87 // We track the lifetime of incoming connection registries as it more | 86 // We track the lifetime of incoming connection registries as it more |
| 88 // convenient for the client. | 87 // convenient for the client. |
| 89 ScopedVector<Connection> incoming_connections_; | 88 ScopedVector<Connection> incoming_connections_; |
| 90 | 89 |
| 91 // 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. |
| 92 shell::mojom::ConnectorRequest pending_connector_request_; | 91 mojom::ConnectorRequest pending_connector_request_; |
| 93 | 92 |
| 94 mojo::ShellClient* client_; | 93 shell::ShellClient* client_; |
| 95 Binding<shell::mojom::ShellClient> binding_; | 94 mojo::Binding<mojom::ShellClient> binding_; |
| 96 scoped_ptr<Connector> connector_; | 95 std::unique_ptr<Connector> connector_; |
| 97 | 96 |
| 98 base::Closure connection_lost_closure_; | 97 base::Closure connection_lost_closure_; |
| 99 | 98 |
| 100 DISALLOW_COPY_AND_ASSIGN(ShellConnection); | 99 DISALLOW_COPY_AND_ASSIGN(ShellConnection); |
| 101 }; | 100 }; |
| 102 | 101 |
| 103 } // namespace mojo | 102 } // namespace shell |
| 104 | 103 |
| 105 #endif // SERVICES_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ | 104 #endif // SERVICES_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ |
| OLD | NEW |