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.
5 #ifndef MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_
6 #define MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_
10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "mojo/application/public/cpp/app_lifetime_helper.h"
13 #include "mojo/application/public/cpp/application_connection.h"
14 #include "mojo/application/public/cpp/application_delegate.h"
15 #include "mojo/application/public/cpp/lib/service_registry.h"
16 #include "mojo/application/public/interfaces/application.mojom.h"
17 #include "mojo/application/public/interfaces/shell.mojom.h"
18 #include "mojo/public/cpp/system/core.h"
22 // Utility class for communicating with the Shell, and providing Services
25 // To use define a class that implements your specific server api, e.g. FooImpl
26 // to implement a service named Foo.
27 // That class must subclass an InterfaceImpl specialization.
29 // If there is context that is to be shared amongst all instances, define a
30 // constructor with that class as its only argument, otherwise define an empty
33 // class FooImpl : public InterfaceImpl<Foo> {
35 // FooImpl(ApplicationContext* app_context) {}
40 // class BarImpl : public InterfaceImpl<Bar> {
42 // // contexts will remain valid for the lifetime of BarImpl.
43 // BarImpl(ApplicationContext* app_context, BarContext* service_context)
44 // : app_context_(app_context), servicecontext_(context) {}
46 // Create an ApplicationImpl instance that collects any service implementations.
48 // ApplicationImpl app(service_provider_handle);
49 // app.AddService<FooImpl>();
51 // BarContext context;
52 // app.AddService<BarImpl>(&context);
55 class ApplicationImpl
: public Application
{
57 // Does not take ownership of |delegate|, which must remain valid for the
58 // lifetime of ApplicationImpl.
59 ApplicationImpl(ApplicationDelegate
* delegate
,
60 InterfaceRequest
<Application
> request
);
61 // Constructs an ApplicationImpl with a custom termination closure. This
62 // closure is invoked on Terminate() instead of the default behavior of
63 // quitting the current MessageLoop.
64 ApplicationImpl(ApplicationDelegate
* delegate
,
65 InterfaceRequest
<Application
> request
,
66 const base::Closure
& termination_closure
);
67 ~ApplicationImpl() override
;
69 // The Mojo shell. This will return a valid pointer after Initialize() has
70 // been invoked. It will remain valid until UnbindConnections() is invoked or
71 // the ApplicationImpl is destroyed.
72 Shell
* shell() const { return shell_
.get(); }
74 const std::string
& url() const { return url_
; }
76 AppLifetimeHelper
* app_lifetime_helper() { return &app_lifetime_helper_
; }
78 // Requests a new connection to an application. Returns a pointer to the
79 // connection if the connection is permitted by this application's delegate,
80 // or nullptr otherwise. Caller does not take ownership. The pointer remains
81 // valid until an error occurs on the connection with the Shell, until the
82 // ApplicationImpl is destroyed, or until the connection is closed through a
83 // call to ApplicationConnection::CloseConnection.
84 // TODO(beng): consider replacing default value in a separate CL per style
86 ApplicationConnection
* ConnectToApplication(
87 URLRequestPtr request
,
88 CapabilityFilterPtr filter
= nullptr);
90 // Closes the |connection|.
91 void CloseConnection(ApplicationConnection
* connection
);
93 // Connect to application identified by |request->url| and connect to the
94 // service implementation of the interface identified by |Interface|.
95 template <typename Interface
>
96 void ConnectToService(mojo::URLRequestPtr request
,
97 InterfacePtr
<Interface
>* ptr
) {
98 ApplicationConnection
* connection
= ConnectToApplication(request
.Pass());
101 connection
->ConnectToService(ptr
);
104 // Application implementation.
105 void Initialize(ShellPtr shell
, const mojo::String
& url
) override
;
107 // Block until the Application is initialized, if it is not already.
108 void WaitForInitialize();
110 // Unbinds the Shell and Application connections. Can be used to re-bind the
111 // handles to another implementation of ApplicationImpl, for instance when
113 void UnbindConnections(InterfaceRequest
<Application
>* application_request
,
116 // Quits the main run loop for this application. It first checks with the
117 // shell to ensure there are no outstanding service requests.
120 // Quits without waiting to check with the shell.
124 // Application implementation.
125 void AcceptConnection(const String
& requestor_url
,
126 InterfaceRequest
<ServiceProvider
> services
,
127 ServiceProviderPtr exposed_services
,
128 Array
<String
> allowed_interfaces
,
129 const String
& url
) override
;
130 void OnQuitRequested(const Callback
<void(bool)>& callback
) override
;
132 void OnConnectionError();
134 void ClearConnections();
136 typedef std::vector
<internal::ServiceRegistry
*> ServiceRegistryList
;
138 ServiceRegistryList incoming_service_registries_
;
139 ServiceRegistryList outgoing_service_registries_
;
140 ApplicationDelegate
* delegate_
;
141 Binding
<Application
> binding_
;
144 base::Closure termination_closure_
;
145 AppLifetimeHelper app_lifetime_helper_
;
146 bool quit_requested_
;
148 base::WeakPtrFactory
<ApplicationImpl
> weak_factory_
;
150 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl
);
155 #endif // MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_