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 MOJO_SHELL_PUBLIC_CPP_APPLICATION_IMPL_H_ | |
6 #define MOJO_SHELL_PUBLIC_CPP_APPLICATION_IMPL_H_ | |
7 | |
8 #include <utility> | |
9 #include <vector> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/memory/scoped_vector.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "mojo/public/cpp/bindings/binding.h" | |
15 #include "mojo/public/cpp/bindings/callback.h" | |
16 #include "mojo/public/cpp/system/core.h" | |
17 #include "mojo/shell/public/cpp/app_lifetime_helper.h" | |
18 #include "mojo/shell/public/cpp/shell.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" | |
22 | |
23 namespace mojo { | |
24 | |
25 // TODO(beng): This comment is hilariously out of date. | |
26 // Utility class for communicating with the Shell, and providing Services | |
27 // to clients. | |
28 // | |
29 // To use define a class that implements your specific server api, e.g. FooImpl | |
30 // to implement a service named Foo. | |
31 // That class must subclass an InterfaceImpl specialization. | |
32 // | |
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 | |
35 // constructor. | |
36 // | |
37 // class FooImpl : public InterfaceImpl<Foo> { | |
38 // public: | |
39 // FooImpl(ApplicationContext* app_context) {} | |
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 ApplicationImpl instance that collects any service implementations. | |
51 // | |
52 // ApplicationImpl app(service_provider_handle); | |
53 // app.AddService<FooImpl>(); | |
54 // | |
55 // BarContext context; | |
56 // app.AddService<BarImpl>(&context); | |
57 // | |
58 // | |
59 class ApplicationImpl : public Shell, public shell::mojom::Application { | |
60 public: | |
61 class TestApi { | |
62 public: | |
63 explicit TestApi(ApplicationImpl* application) | |
64 : application_(application) {} | |
65 | |
66 void UnbindConnections( | |
67 InterfaceRequest<shell::mojom::Application>* application_request, | |
68 shell::mojom::ShellPtr* shell) { | |
69 application_->UnbindConnections(application_request, shell); | |
70 } | |
71 | |
72 private: | |
73 ApplicationImpl* application_; | |
74 }; | |
75 | |
76 // Does not take ownership of |delegate|, which must remain valid for the | |
77 // lifetime of ApplicationImpl. | |
78 ApplicationImpl(ShellClient* client, | |
79 InterfaceRequest<shell::mojom::Application> request); | |
80 // Constructs an ApplicationImpl with a custom termination closure. This | |
81 // closure is invoked on Quit() instead of the default behavior of quitting | |
82 // the current base::MessageLoop. | |
83 ApplicationImpl(ShellClient* client, | |
84 InterfaceRequest<shell::mojom::Application> request, | |
85 const Closure& termination_closure); | |
86 ~ApplicationImpl() 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 | |
93 // Block the calling thread until the Initialize() method is called by the | |
94 // shell. | |
95 void WaitForInitialize(); | |
96 | |
97 // Shell. | |
98 scoped_ptr<Connection> Connect(const std::string& url) override; | |
99 scoped_ptr<Connection> Connect(ConnectParams* params) override; | |
100 void Quit() override; | |
101 scoped_ptr<AppRefCount> CreateAppRefCount() override; | |
102 | |
103 private: | |
104 // shell::mojom::Application implementation. | |
105 void Initialize(shell::mojom::ShellPtr shell, | |
106 const mojo::String& url, | |
107 uint32_t id) override; | |
108 void AcceptConnection(const String& requestor_url, | |
109 uint32_t requestor_id, | |
110 InterfaceRequest<ServiceProvider> services, | |
111 ServiceProviderPtr exposed_services, | |
112 Array<String> allowed_interfaces, | |
113 const String& url) override; | |
114 void OnQuitRequested(const Callback<void(bool)>& callback) override; | |
115 | |
116 void OnConnectionError(); | |
117 | |
118 // Called from Quit() when there is no Shell connection, or asynchronously | |
119 // from Quit() once the Shell has OK'ed shutdown. | |
120 void QuitNow(); | |
121 | |
122 // Unbinds the Shell and Application connections. Can be used to re-bind the | |
123 // handles to another implementation of ApplicationImpl, for instance when | |
124 // running apptests. | |
125 void UnbindConnections( | |
126 InterfaceRequest<shell::mojom::Application>* application_request, | |
127 shell::mojom::ShellPtr* shell); | |
128 | |
129 // We track the lifetime of incoming connection registries as it more | |
130 // convenient for the client. | |
131 ScopedVector<Connection> incoming_connections_; | |
132 ShellClient* client_; | |
133 Binding<shell::mojom::Application> binding_; | |
134 shell::mojom::ShellPtr shell_; | |
135 Closure termination_closure_; | |
136 AppLifetimeHelper app_lifetime_helper_; | |
137 bool quit_requested_; | |
138 base::WeakPtrFactory<ApplicationImpl> weak_factory_; | |
139 | |
140 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); | |
141 }; | |
142 | |
143 } // namespace mojo | |
144 | |
145 #endif // MOJO_SHELL_PUBLIC_CPP_APPLICATION_IMPL_H_ | |
OLD | NEW |