| 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_APPLICATION_IMPL_H_ | 5 #ifndef MOJO_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ |
| 6 #define MOJO_SHELL_PUBLIC_CPP_APPLICATION_IMPL_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/application.mojom.h" | |
| 21 #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" |
| 22 | 22 |
| 23 namespace mojo { | 23 namespace mojo { |
| 24 | 24 |
| 25 // TODO(beng): This comment is hilariously out of date. | 25 // TODO(beng): This comment is hilariously out of date. |
| 26 // Utility class for communicating with the Shell, and providing Services | 26 // Utility class for communicating with the Shell, and providing Services |
| 27 // to clients. | 27 // to clients. |
| 28 // | 28 // |
| 29 // To use define a class that implements your specific server api, e.g. FooImpl | 29 // To use define a class that implements your specific server api, e.g. FooImpl |
| 30 // to implement a service named Foo. | 30 // to implement a service named Foo. |
| 31 // That class must subclass an InterfaceImpl specialization. | 31 // That class must subclass an InterfaceImpl specialization. |
| 32 // | 32 // |
| 33 // If there is context that is to be shared amongst all instances, define a | 33 // If there is context that is to be shared amongst all instances, define a |
| 34 // constructor with that class as its only argument, otherwise define an empty | 34 // constructor with that class as its only argument, otherwise define an empty |
| 35 // constructor. | 35 // constructor. |
| 36 // | 36 // |
| 37 // class FooImpl : public InterfaceImpl<Foo> { | 37 // class FooImpl : public InterfaceImpl<Foo> { |
| 38 // public: | 38 // public: |
| 39 // FooImpl(ApplicationContext* app_context) {} | 39 // FooImpl(ApplicationContext* app_context) {} |
| 40 // }; | 40 // }; |
| 41 // | 41 // |
| 42 // or | 42 // or |
| 43 // | 43 // |
| 44 // class BarImpl : public InterfaceImpl<Bar> { | 44 // class BarImpl : public InterfaceImpl<Bar> { |
| 45 // public: | 45 // public: |
| 46 // // contexts will remain valid for the lifetime of BarImpl. | 46 // // contexts will remain valid for the lifetime of BarImpl. |
| 47 // BarImpl(ApplicationContext* app_context, BarContext* service_context) | 47 // BarImpl(ApplicationContext* app_context, BarContext* service_context) |
| 48 // : app_context_(app_context), servicecontext_(context) {} | 48 // : app_context_(app_context), servicecontext_(context) {} |
| 49 // | 49 // |
| 50 // Create an ApplicationImpl instance that collects any service implementations. | 50 // Create an ShellConnection instance that collects any service implementations. |
| 51 // | 51 // |
| 52 // ApplicationImpl app(service_provider_handle); | 52 // ShellConnection app(service_provider_handle); |
| 53 // app.AddService<FooImpl>(); | 53 // app.AddService<FooImpl>(); |
| 54 // | 54 // |
| 55 // BarContext context; | 55 // BarContext context; |
| 56 // app.AddService<BarImpl>(&context); | 56 // app.AddService<BarImpl>(&context); |
| 57 // | 57 // |
| 58 // | 58 // |
| 59 class ApplicationImpl : public Shell, public shell::mojom::Application { | 59 class ShellConnection : public Shell, public shell::mojom::ShellClient { |
| 60 public: | 60 public: |
| 61 class TestApi { | 61 class TestApi { |
| 62 public: | 62 public: |
| 63 explicit TestApi(ApplicationImpl* application) | 63 explicit TestApi(ShellConnection* shell_connection) |
| 64 : application_(application) {} | 64 : shell_connection_(shell_connection) {} |
| 65 | 65 |
| 66 void UnbindConnections( | 66 void UnbindConnections( |
| 67 InterfaceRequest<shell::mojom::Application>* application_request, | 67 InterfaceRequest<shell::mojom::ShellClient>* request, |
| 68 shell::mojom::ShellPtr* shell) { | 68 shell::mojom::ShellPtr* shell) { |
| 69 application_->UnbindConnections(application_request, shell); | 69 shell_connection_->UnbindConnections(request, shell); |
| 70 } | 70 } |
| 71 | 71 |
| 72 private: | 72 private: |
| 73 ApplicationImpl* application_; | 73 ShellConnection* shell_connection_; |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 // Does not take ownership of |delegate|, which must remain valid for the | 76 // Does not take ownership of |delegate|, which must remain valid for the |
| 77 // lifetime of ApplicationImpl. | 77 // lifetime of ShellConnection. |
| 78 ApplicationImpl(ShellClient* client, | 78 ShellConnection(mojo::ShellClient* client, |
| 79 InterfaceRequest<shell::mojom::Application> request); | 79 InterfaceRequest<shell::mojom::ShellClient> request); |
| 80 // Constructs an ApplicationImpl with a custom termination closure. This | 80 // Constructs an ShellConnection with a custom termination closure. This |
| 81 // closure is invoked on Quit() instead of the default behavior of quitting | 81 // closure is invoked on Quit() instead of the default behavior of quitting |
| 82 // the current base::MessageLoop. | 82 // the current base::MessageLoop. |
| 83 ApplicationImpl(ShellClient* client, | 83 ShellConnection(mojo::ShellClient* client, |
| 84 InterfaceRequest<shell::mojom::Application> request, | 84 InterfaceRequest<shell::mojom::ShellClient> request, |
| 85 const Closure& termination_closure); | 85 const Closure& termination_closure); |
| 86 ~ApplicationImpl() override; | 86 ~ShellConnection() override; |
| 87 | |
| 88 // The Mojo shell. This will return a valid pointer after Initialize() has | |
| 89 // been invoked. It will remain valid until UnbindConnections() is invoked or | |
| 90 // the ApplicationImpl is destroyed. | |
| 91 shell::mojom::Shell* shell() const { return shell_.get(); } | |
| 92 | 87 |
| 93 // Block the calling thread until the Initialize() method is called by the | 88 // Block the calling thread until the Initialize() method is called by the |
| 94 // shell. | 89 // shell. |
| 95 void WaitForInitialize(); | 90 void WaitForInitialize(); |
| 96 | 91 |
| 97 // Shell. | 92 // Shell. |
| 98 scoped_ptr<Connection> Connect(const std::string& url) override; | 93 scoped_ptr<Connection> Connect(const std::string& url) override; |
| 99 scoped_ptr<Connection> Connect(ConnectParams* params) override; | 94 scoped_ptr<Connection> Connect(ConnectParams* params) override; |
| 100 void Quit() override; | 95 void Quit() override; |
| 101 scoped_ptr<AppRefCount> CreateAppRefCount() override; | 96 scoped_ptr<AppRefCount> CreateAppRefCount() override; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 113 const String& url) override; | 108 const String& url) override; |
| 114 void OnQuitRequested(const Callback<void(bool)>& callback) override; | 109 void OnQuitRequested(const Callback<void(bool)>& callback) override; |
| 115 | 110 |
| 116 void OnConnectionError(); | 111 void OnConnectionError(); |
| 117 | 112 |
| 118 // Called from Quit() when there is no Shell connection, or asynchronously | 113 // Called from Quit() when there is no Shell connection, or asynchronously |
| 119 // from Quit() once the Shell has OK'ed shutdown. | 114 // from Quit() once the Shell has OK'ed shutdown. |
| 120 void QuitNow(); | 115 void QuitNow(); |
| 121 | 116 |
| 122 // Unbinds the Shell and Application connections. Can be used to re-bind the | 117 // Unbinds the Shell and Application connections. Can be used to re-bind the |
| 123 // handles to another implementation of ApplicationImpl, for instance when | 118 // handles to another implementation of ShellConnection, for instance when |
| 124 // running apptests. | 119 // running apptests. |
| 125 void UnbindConnections( | 120 void UnbindConnections(InterfaceRequest<shell::mojom::ShellClient>* request, |
| 126 InterfaceRequest<shell::mojom::Application>* application_request, | 121 shell::mojom::ShellPtr* shell); |
| 127 shell::mojom::ShellPtr* shell); | |
| 128 | 122 |
| 129 // We track the lifetime of incoming connection registries as it more | 123 // We track the lifetime of incoming connection registries as it more |
| 130 // convenient for the client. | 124 // convenient for the client. |
| 131 ScopedVector<Connection> incoming_connections_; | 125 ScopedVector<Connection> incoming_connections_; |
| 132 ShellClient* client_; | 126 mojo::ShellClient* client_; |
| 133 Binding<shell::mojom::Application> binding_; | 127 Binding<shell::mojom::ShellClient> binding_; |
| 134 shell::mojom::ShellPtr shell_; | 128 shell::mojom::ShellPtr shell_; |
| 135 Closure termination_closure_; | 129 Closure termination_closure_; |
| 136 AppLifetimeHelper app_lifetime_helper_; | 130 AppLifetimeHelper app_lifetime_helper_; |
| 137 bool quit_requested_; | 131 bool quit_requested_; |
| 138 base::WeakPtrFactory<ApplicationImpl> weak_factory_; | 132 base::WeakPtrFactory<ShellConnection> weak_factory_; |
| 139 | 133 |
| 140 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); | 134 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellConnection); |
| 141 }; | 135 }; |
| 142 | 136 |
| 143 } // namespace mojo | 137 } // namespace mojo |
| 144 | 138 |
| 145 #endif // MOJO_SHELL_PUBLIC_CPP_APPLICATION_IMPL_H_ | 139 #endif // MOJO_SHELL_PUBLIC_CPP_SHELL_CONNECTION_H_ |
| OLD | NEW |