• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_IMPL_H_
6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_
7 #include <vector>
8 
9 #include "mojo/public/cpp/application/application_connection.h"
10 #include "mojo/public/cpp/application/lib/service_connector.h"
11 #include "mojo/public/cpp/application/lib/service_registry.h"
12 #include "mojo/public/cpp/system/core.h"
13 #include "mojo/public/interfaces/application/application.mojom.h"
14 #include "mojo/public/interfaces/application/shell.mojom.h"
15 
16 namespace mojo {
17 
18 class ApplicationDelegate;
19 
20 // Utility class for communicating with the Shell, and providing Services
21 // to clients.
22 //
23 // To use define a class that implements your specific server api, e.g. FooImpl
24 // to implement a service named Foo.
25 // That class must subclass an InterfaceImpl specialization.
26 //
27 // If there is context that is to be shared amongst all instances, define a
28 // constructor with that class as its only argument, otherwise define an empty
29 // constructor.
30 //
31 // class FooImpl : public InterfaceImpl<Foo> {
32 //  public:
33 //   FooImpl(ApplicationContext* app_context) {}
34 // };
35 //
36 // or
37 //
38 // class BarImpl : public InterfaceImpl<Bar> {
39 //  public:
40 //   // contexts will remain valid for the lifetime of BarImpl.
41 //   BarImpl(ApplicationContext* app_context, BarContext* service_context)
42 //          : app_context_(app_context), servicecontext_(context) {}
43 //
44 // Create an ApplicationImpl instance that collects any service implementations.
45 //
46 // ApplicationImpl app(service_provider_handle);
47 // app.AddService<FooImpl>();
48 //
49 // BarContext context;
50 // app.AddService<BarImpl>(&context);
51 //
52 //
53 class ApplicationImpl : public InterfaceImpl<Application> {
54  public:
55   ApplicationImpl(ApplicationDelegate* delegate,
56                   ScopedMessagePipeHandle shell_handle);
57   ApplicationImpl(ApplicationDelegate* delegate,
58                   MojoHandle shell_handle);
59   virtual ~ApplicationImpl();
60 
shell()61   Shell* shell() const { return shell_.get(); }
62 
63   // Returns any initial configuration arguments, passed by the Shell.
args()64   const Array<String>& args() { return args_; }
65 
66   // Establishes a new connection to an application. Caller does not own.
67   ApplicationConnection* ConnectToApplication(const String& application_url);
68 
69   // Connect to application identified by |application_url| and connect to the
70   // service implementation of the interface identified by |Interface|.
71   template <typename Interface>
ConnectToService(const std::string & application_url,InterfacePtr<Interface> * ptr)72   void ConnectToService(const std::string& application_url,
73                         InterfacePtr<Interface>* ptr) {
74     ConnectToApplication(application_url)->ConnectToService(ptr);
75   }
76 
77  private:
78   class ShellPtrWatcher;
79 
80   void BindShell(ScopedMessagePipeHandle shell_handle);
81   void ClearConnections();
OnShellError()82   void OnShellError() {
83     ClearConnections();
84     Terminate();
85   };
86 
87   // Quits the main run loop for this application.
88   static void Terminate();
89 
90   // Application implementation.
91   virtual void Initialize(Array<String> args) MOJO_OVERRIDE;
92   virtual void AcceptConnection(const String& requestor_url,
93                                 ServiceProviderPtr provider) MOJO_OVERRIDE;
94 
95   typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList;
96 
97   bool initialized_;
98   ServiceRegistryList incoming_service_registries_;
99   ServiceRegistryList outgoing_service_registries_;
100   ApplicationDelegate* delegate_;
101   ShellPtr shell_;
102   ShellPtrWatcher* shell_watch_;
103   Array<String> args_;
104 
105   MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl);
106 };
107 
108 }  // namespace mojo
109 
110 #endif  // MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_
111