Home | History | Annotate | Download | only in application
      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_PUBLIC_APPLICATION_APPLICATION_H_
      6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_H_
      7 #include <vector>
      8 
      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"
     13 
     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
     23 
     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);
     31 
     32 namespace mojo {
     33 
     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.
     38 //
     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.
     42 //
     43 // class FooImpl : public InterfaceImpl<Foo> {
     44 //  public:
     45 //   FooImpl() {}
     46 // };
     47 //
     48 // or
     49 //
     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 // };
     57 //
     58 // Create an Application instance that collects any service implementations.
     59 //
     60 // Application app(service_provider_handle);
     61 // app.AddService<FooImpl>();
     62 //
     63 // BarContext context;
     64 // app.AddService<BarImpl>(&context);
     65 //
     66 //
     67 class Application {
     68  public:
     69   Application();
     70   explicit Application(ScopedMessagePipeHandle service_provider_handle);
     71   explicit Application(MojoHandle service_provider_handle);
     72   virtual ~Application();
     73 
     74   // Override this method to control what urls are allowed to connect to a
     75   // service.
     76   virtual bool AllowIncomingConnection(const mojo::String& service_name,
     77                                        const mojo::String& requestor_url);
     78 
     79   template <typename Impl, typename Context>
     80   void AddService(Context* context) {
     81     service_registry_.AddServiceConnector(
     82         new internal::ServiceConnector<Impl, Context>(Impl::Name_, context));
     83   }
     84 
     85   template <typename Impl>
     86   void AddService() {
     87     service_registry_.AddServiceConnector(
     88         new internal::ServiceConnector<Impl, void>(Impl::Name_, NULL));
     89   }
     90 
     91   template <typename Interface>
     92   void ConnectTo(const std::string& url, InterfacePtr<Interface>* ptr) {
     93     mojo::ConnectToService(service_provider(), url, ptr);
     94   }
     95 
     96   ServiceProvider* service_provider() {
     97     return service_registry_.remote_service_provider();
     98   }
     99 
    100   void BindServiceProvider(ScopedMessagePipeHandle service_provider_handle);
    101 
    102  protected:
    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();
    107 
    108  private:
    109   friend MojoResult (::MojoMain)(MojoHandle);
    110 
    111   // Implement this method to create the specific subclass of Application.
    112   static Application* Create();
    113 
    114   internal::ServiceRegistry service_registry_;
    115 
    116   MOJO_DISALLOW_COPY_AND_ASSIGN(Application);
    117 };
    118 
    119 }  // namespace mojo
    120 
    121 #endif  // MOJO_PUBLIC_APPLICATION_APPLICATION_H_
    122