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 include
25 // mojo_main_{standalone|chromium}.cc in their project 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);
70 explicit Application(ScopedMessagePipeHandle service_provider_handle);
71 explicit Application(MojoHandle service_provider_handle);
72 virtual ~Application();
74 // Override this method to control what urls are allowed to connect to a
76 virtual bool AllowIncomingConnection(const mojo::String& service_name,
77 const mojo::String& requestor_url);
79 template <typename Impl, typename Context>
80 void AddService(Context* context) {
81 service_registry_.AddServiceConnector(
82 new internal::ServiceConnector<Impl, Context>(Impl::Name_, context));
85 template <typename Impl>
87 service_registry_.AddServiceConnector(
88 new internal::ServiceConnector<Impl, void>(Impl::Name_, NULL));
91 template <typename Interface>
92 void ConnectTo(const std::string& url, InterfacePtr<Interface>* ptr) {
93 mojo::ConnectToService(service_provider(), url, ptr);
96 ServiceProvider* service_provider() {
97 return service_registry_.remote_service_provider();
100 void BindServiceProvider(ScopedMessagePipeHandle service_provider_handle);
103 // Override this to do any necessary initialization. There's no need to call
104 // Application's implementation.
105 // The service_provider will be bound to its pipe before this is called.
106 virtual void Initialize();
109 friend MojoResult (::MojoMain)(MojoHandle);
111 // Implement this method to create the specific subclass of Application.
112 static Application* Create();
114 internal::ServiceRegistry service_registry_;
116 MOJO_DISALLOW_COPY_AND_ASSIGN(Application);
121 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_H_