| 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 MOJO_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ | 5 #ifndef MOJO_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ |
| 6 #define MOJO_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ | 6 #define MOJO_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ |
| 7 | 7 |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "mojo/public/cpp/bindings/binding.h" | 14 #include "mojo/public/cpp/bindings/binding.h" |
| 15 #include "mojo/public/cpp/bindings/callback.h" | 15 #include "mojo/public/cpp/bindings/callback.h" |
| 16 #include "mojo/public/cpp/system/core.h" | 16 #include "mojo/public/cpp/system/core.h" |
| 17 #include "mojo/shell/public/cpp/app_lifetime_helper.h" | 17 #include "mojo/shell/public/cpp/app_lifetime_helper.h" |
| 18 #include "mojo/shell/public/cpp/shell.h" | 18 #include "mojo/shell/public/cpp/shell.h" |
| 19 #include "mojo/shell/public/cpp/shell_client.h" | 19 #include "mojo/shell/public/cpp/shell_client.h" |
| 20 #include "mojo/shell/public/interfaces/shell.mojom.h" | 20 #include "mojo/shell/public/interfaces/shell.mojom.h" |
| 21 #include "mojo/shell/public/interfaces/shell_client.mojom.h" | 21 #include "mojo/shell/public/interfaces/shell_client.mojom.h" |
| 22 | 22 |
| 23 namespace mojo { | 23 namespace mojo { |
| 24 | 24 |
| 25 // TODO(beng): This comment is hilariously out of date. | 25 // Encapsulates a connection to the Mojo Shell in two parts: |
| 26 // Utility class for communicating with the Shell, and providing Services | 26 // - a bound InterfacePtr to mojo::shell::mojom::Shell, the primary mechanism |
| 27 // to clients. | 27 // by which the instantiating application interacts with other services |
| 28 // brokered by the Mojo Shell. |
| 29 // - a bound InterfaceRequest of mojo::shell::mojom::ShellClient, an interface |
| 30 // used by the Mojo Shell to inform this application of lifecycle events and |
| 31 // inbound connections brokered by it. |
| 28 // | 32 // |
| 29 // To use define a class that implements your specific server api, e.g. FooImpl | 33 // This class should be used in two scenarios: |
| 30 // to implement a service named Foo. | 34 // - During early startup to bind the mojo::shell::mojom::ShellClientRequest |
| 31 // That class must subclass an InterfaceImpl specialization. | 35 // obtained from the Mojo Shell. This is typically in response to either |
| 36 // MojoMain() or main(). |
| 37 // - In an implementation of mojo::shell::mojom::ContentHandler to bind the |
| 38 // mojo::shell::mojom::ShellClientRequest passed via StartApplication. In this |
| 39 // scenario there can be many instances of this class per process. |
| 32 // | 40 // |
| 33 // If there is context that is to be shared amongst all instances, define a | 41 // Instances of this class are constructed with an implementation of the Shell |
| 34 // constructor with that class as its only argument, otherwise define an empty | 42 // Client Lib's mojo::ShellClient interface. See documentation in shell_client.h |
| 35 // constructor. | 43 // for details. |
| 36 // | 44 // |
| 37 // class FooImpl : public InterfaceImpl<Foo> { | 45 // Though this class provides the canonical implementation of the Shell Client |
| 38 // public: | 46 // lib's mojo::Shell interface, this interface should not be reached through |
| 39 // FooImpl(ApplicationContext* app_context) {} | 47 // pointers to this type. |
| 40 // }; | |
| 41 // | |
| 42 // or | |
| 43 // | |
| 44 // class BarImpl : public InterfaceImpl<Bar> { | |
| 45 // public: | |
| 46 // // contexts will remain valid for the lifetime of BarImpl. | |
| 47 // BarImpl(ApplicationContext* app_context, BarContext* service_context) | |
| 48 // : app_context_(app_context), servicecontext_(context) {} | |
| 49 // | |
| 50 // Create an ShellConnection instance that collects any service implementations. | |
| 51 // | |
| 52 // ShellConnection app(service_provider_handle); | |
| 53 // app.AddService<FooImpl>(); | |
| 54 // | |
| 55 // BarContext context; | |
| 56 // app.AddService<BarImpl>(&context); | |
| 57 // | |
| 58 // | |
| 59 class ShellConnection : public Shell, public shell::mojom::ShellClient { | 48 class ShellConnection : public Shell, public shell::mojom::ShellClient { |
| 60 public: | 49 public: |
| 61 class TestApi { | 50 class TestApi { |
| 62 public: | 51 public: |
| 63 explicit TestApi(ShellConnection* shell_connection) | 52 explicit TestApi(ShellConnection* shell_connection) |
| 64 : shell_connection_(shell_connection) {} | 53 : shell_connection_(shell_connection) {} |
| 65 | 54 |
| 66 void UnbindConnections( | 55 void UnbindConnections( |
| 67 InterfaceRequest<shell::mojom::ShellClient>* request, | 56 InterfaceRequest<shell::mojom::ShellClient>* request, |
| 68 shell::mojom::ShellPtr* shell) { | 57 shell::mojom::ShellPtr* shell) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 82 // the current base::MessageLoop. | 71 // the current base::MessageLoop. |
| 83 ShellConnection(mojo::ShellClient* client, | 72 ShellConnection(mojo::ShellClient* client, |
| 84 InterfaceRequest<shell::mojom::ShellClient> request, | 73 InterfaceRequest<shell::mojom::ShellClient> request, |
| 85 const Closure& termination_closure); | 74 const Closure& termination_closure); |
| 86 ~ShellConnection() override; | 75 ~ShellConnection() override; |
| 87 | 76 |
| 88 // Block the calling thread until the Initialize() method is called by the | 77 // Block the calling thread until the Initialize() method is called by the |
| 89 // shell. | 78 // shell. |
| 90 void WaitForInitialize(); | 79 void WaitForInitialize(); |
| 91 | 80 |
| 92 // Shell. | 81 private: |
| 82 // Shell: |
| 93 scoped_ptr<Connection> Connect(const std::string& url) override; | 83 scoped_ptr<Connection> Connect(const std::string& url) override; |
| 94 scoped_ptr<Connection> Connect(ConnectParams* params) override; | 84 scoped_ptr<Connection> Connect(ConnectParams* params) override; |
| 95 void Quit() override; | 85 void Quit() override; |
| 96 scoped_ptr<AppRefCount> CreateAppRefCount() override; | 86 scoped_ptr<AppRefCount> CreateAppRefCount() override; |
| 97 | 87 |
| 98 private: | 88 // shell::mojom::ShellClient: |
| 99 // shell::mojom::Application implementation. | |
| 100 void Initialize(shell::mojom::ShellPtr shell, | 89 void Initialize(shell::mojom::ShellPtr shell, |
| 101 const mojo::String& url, | 90 const mojo::String& url, |
| 102 uint32_t id) override; | 91 uint32_t id) override; |
| 103 void AcceptConnection(const String& requestor_url, | 92 void AcceptConnection(const String& requestor_url, |
| 104 uint32_t requestor_id, | 93 uint32_t requestor_id, |
| 105 InterfaceRequest<InterfaceProvider> remote_interfaces, | 94 InterfaceRequest<InterfaceProvider> remote_interfaces, |
| 106 InterfaceProviderPtr local_interfaces, | 95 InterfaceProviderPtr local_interfaces, |
| 107 Array<String> allowed_interfaces, | 96 Array<String> allowed_interfaces, |
| 108 const String& url) override; | 97 const String& url) override; |
| 109 void OnQuitRequested(const Callback<void(bool)>& callback) override; | 98 void OnQuitRequested(const Callback<void(bool)>& callback) override; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 130 AppLifetimeHelper app_lifetime_helper_; | 119 AppLifetimeHelper app_lifetime_helper_; |
| 131 bool quit_requested_; | 120 bool quit_requested_; |
| 132 base::WeakPtrFactory<ShellConnection> weak_factory_; | 121 base::WeakPtrFactory<ShellConnection> weak_factory_; |
| 133 | 122 |
| 134 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellConnection); | 123 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellConnection); |
| 135 }; | 124 }; |
| 136 | 125 |
| 137 } // namespace mojo | 126 } // namespace mojo |
| 138 | 127 |
| 139 #endif // MOJO_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ | 128 #endif // MOJO_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ |
| OLD | NEW |