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_PUBLIC_APPLICATION_APPLICATION_H_
6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_H_
9 #include "mojo/public/cpp/application/connect.h"
10 #include "mojo/public/cpp/application/lib/service_connector.h"
11 #include "mojo/public/cpp/system/core.h"
12 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
18 #define APPLICATION_EXPORT __declspec(dllexport)
21 #define APPLICATION_EXPORT __attribute__((visibility("default")))
24 // DSOs can either implement MojoMain directly or utilize the
25 // mojo_main_{standalone|chromium} gyp targets and implement
26 // Application::Create();
27 // TODO(davemoore): Establish this as part of our SDK for third party mojo
28 // application writers.
29 extern "C" APPLICATION_EXPORT MojoResult CDECL
MojoMain(
30 MojoHandle service_provider_handle
);
34 // Utility class for creating ServiceProviders that vend service instances.
35 // To use define a class that implements your specific server api, e.g. FooImpl
36 // to implement a service named Foo.
37 // That class must subclass an InterfaceImpl specialization.
39 // If there is context that is to be shared amongst all instances, define a
40 // constructor with that class as its only argument, otherwise define an empty
43 // class FooImpl : public InterfaceImpl<Foo> {
50 // class BarImpl : public InterfaceImpl<Bar> {
52 // // context will remain valid for the lifetime of BarImpl.
53 // BarImpl(BarContext* context) : context_(context) {}
55 // BarContext* context;
58 // Create an Application instance that collects any service implementations.
60 // Application app(service_provider_handle);
61 // app.AddService<FooImpl>();
63 // BarContext context;
64 // app.AddService<BarImpl>(&context);
67 class Application
: public internal::ServiceConnectorBase::Owner
{
70 explicit Application(ScopedMessagePipeHandle service_provider_handle
);
71 explicit Application(MojoHandle service_provider_handle
);
72 virtual ~Application();
74 // Override this to do any necessary initialization. There's no need to call
75 // Application's implementation.
76 // The service_provider will be bound to its pipe before this is called.
77 virtual void Initialize();
79 template <typename Impl
, typename Context
>
80 void AddService(Context
* context
) {
82 new internal::ServiceConnector
<Impl
, Context
>(Impl::Name_
, context
));
85 template <typename Impl
>
88 new internal::ServiceConnector
<Impl
, void>(Impl::Name_
, NULL
));
91 template <typename Interface
>
92 void ConnectTo(const std::string
& url
,
93 InterfacePtr
<Interface
>* ptr
) {
94 mojo::ConnectToService(service_provider(), url
, ptr
);
97 ServiceProvider
* service_provider() { return service_provider_
.get(); }
98 void BindServiceProvider(ScopedMessagePipeHandle service_provider_handle
);
101 // ServiceProvider methods.
102 // Override this to dispatch to correct service when there's more than one.
103 // TODO(davemoore): Augment this with name registration.
104 virtual void ConnectToService(const mojo::String
& url
,
105 const mojo::String
& name
,
106 ScopedMessagePipeHandle client_handle
)
110 friend MojoResult (::MojoMain
)(MojoHandle
);
112 // Implement this method to create the specific subclass of Application.
113 static Application
* Create();
115 // internal::ServiceConnectorBase::Owner methods.
116 // Takes ownership of |service_connector|.
117 virtual void AddServiceConnector(
118 internal::ServiceConnectorBase
* service_connector
) MOJO_OVERRIDE
;
119 virtual void RemoveServiceConnector(
120 internal::ServiceConnectorBase
* service_connector
) MOJO_OVERRIDE
;
122 typedef std::map
<std::string
, internal::ServiceConnectorBase
*>
123 NameToServiceConnectorMap
;
124 NameToServiceConnectorMap name_to_service_connector_
;
129 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_H_