Temporarily re-enabling SizeAfterPrefChange test with traces.
[chromium-blink-merge.git] / mojo / public / cpp / application / application.h
blob99742faea94b47a47f1522766c0682d98e4b4463
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_
7 #include <vector>
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"
14 #if defined(WIN32)
15 #if !defined(CDECL)
16 #define CDECL __cdecl
17 #endif
18 #define APPLICATION_EXPORT __declspec(dllexport)
19 #else
20 #define CDECL
21 #define APPLICATION_EXPORT __attribute__((visibility("default")))
22 #endif
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);
32 namespace mojo {
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
41 // constructor.
43 // class FooImpl : public InterfaceImpl<Foo> {
44 // public:
45 // FooImpl() {}
46 // };
48 // or
50 // class BarImpl : public InterfaceImpl<Bar> {
51 // public:
52 // // context will remain valid for the lifetime of BarImpl.
53 // BarImpl(BarContext* context) : context_(context) {}
54 // private:
55 // BarContext* context;
56 // };
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 {
68 public:
69 Application();
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) {
81 AddServiceConnector(
82 new internal::ServiceConnector<Impl, Context>(Impl::Name_, context));
85 template <typename Impl>
86 void AddService() {
87 AddServiceConnector(
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);
100 protected:
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)
107 MOJO_OVERRIDE;
109 private:
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_;
127 } // namespace mojo
129 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_H_