OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ | 5 #ifndef MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ |
6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ | 6 #define MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "mojo/public/cpp/application/application_connection.h" | 9 #include "mojo/public/cpp/application/application_connection.h" |
10 #include "mojo/public/cpp/application/lib/service_connector.h" | 10 #include "mojo/public/cpp/application/lib/service_connector.h" |
11 #include "mojo/public/cpp/application/lib/service_registry.h" | 11 #include "mojo/public/cpp/application/lib/service_registry.h" |
12 #include "mojo/public/cpp/system/core.h" | 12 #include "mojo/public/cpp/system/core.h" |
13 #include "mojo/public/interfaces/application/application.mojom.h" | 13 #include "mojo/public/interfaces/application/application.mojom.h" |
14 #include "mojo/public/interfaces/application/shell.mojom.h" | 14 #include "mojo/public/interfaces/application/shell.mojom.h" |
15 | 15 |
16 #if defined(WIN32) | |
17 #if !defined(CDECL) | |
18 #define CDECL __cdecl | |
19 #endif | |
20 #define APPLICATION_EXPORT __declspec(dllexport) | |
21 #else | |
22 #define CDECL | |
23 #define APPLICATION_EXPORT __attribute__((visibility("default"))) | |
24 #endif | |
25 | |
26 // DSOs can either implement MojoMain directly or include | |
27 // mojo_main_{standalone|chromium}.cc in their project and implement | |
28 // ApplicationImpl::Create(); | |
29 // TODO(davemoore): Establish this as part of our SDK for third party mojo | |
30 // application writers. | |
31 extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain( | |
32 MojoHandle service_provider_handle); | |
33 | |
34 namespace mojo { | 16 namespace mojo { |
35 | 17 |
36 class ApplicationDelegate; | 18 class ApplicationDelegate; |
37 | 19 |
38 // Utility class for communicating with the Shell, and providing Services | 20 // Utility class for communicating with the Shell, and providing Services |
39 // to clients. | 21 // to clients. |
40 // | 22 // |
41 // To use define a class that implements your specific server api, e.g. FooImpl | 23 // To use define a class that implements your specific server api, e.g. FooImpl |
42 // to implement a service named Foo. | 24 // to implement a service named Foo. |
43 // That class must subclass an InterfaceImpl specialization. | 25 // That class must subclass an InterfaceImpl specialization. |
(...skipping 19 matching lines...) Expand all Loading... |
63 // | 45 // |
64 // ApplicationImpl app(service_provider_handle); | 46 // ApplicationImpl app(service_provider_handle); |
65 // app.AddService<FooImpl>(); | 47 // app.AddService<FooImpl>(); |
66 // | 48 // |
67 // BarContext context; | 49 // BarContext context; |
68 // app.AddService<BarImpl>(&context); | 50 // app.AddService<BarImpl>(&context); |
69 // | 51 // |
70 // | 52 // |
71 class ApplicationImpl : public InterfaceImpl<Application> { | 53 class ApplicationImpl : public InterfaceImpl<Application> { |
72 public: | 54 public: |
73 explicit ApplicationImpl(ApplicationDelegate* delegate); | |
74 ApplicationImpl(ApplicationDelegate* delegate, | 55 ApplicationImpl(ApplicationDelegate* delegate, |
75 ScopedMessagePipeHandle shell_handle); | 56 ScopedMessagePipeHandle shell_handle); |
76 ApplicationImpl(ApplicationDelegate* delegate, | 57 ApplicationImpl(ApplicationDelegate* delegate, |
77 MojoHandle shell_handle); | 58 MojoHandle shell_handle); |
78 virtual ~ApplicationImpl(); | 59 virtual ~ApplicationImpl(); |
79 | 60 |
80 // Establishes a new connection to an application. Caller does not own. | 61 // Establishes a new connection to an application. Caller does not own. |
81 ApplicationConnection* ConnectToApplication(const String& application_url); | 62 ApplicationConnection* ConnectToApplication(const String& application_url); |
82 | 63 |
83 // Connect to application identified by |application_url| and connect to the | 64 // Connect to application identified by |application_url| and connect to the |
84 // service implementation of the interface identified by |Interface|. | 65 // service implementation of the interface identified by |Interface|. |
85 template <typename Interface> | 66 template <typename Interface> |
86 void ConnectToService(const std::string& application_url, | 67 void ConnectToService(const std::string& application_url, |
87 InterfacePtr<Interface>* ptr) { | 68 InterfacePtr<Interface>* ptr) { |
88 ConnectToApplication(application_url)->ConnectToService(ptr); | 69 ConnectToApplication(application_url)->ConnectToService(ptr); |
89 } | 70 } |
90 | 71 |
91 private: | 72 private: |
92 class ShellPtrWatcher : public ErrorHandler { | 73 class ShellPtrWatcher : public ErrorHandler { |
93 public: | 74 public: |
94 explicit ShellPtrWatcher(ApplicationImpl* impl); | 75 explicit ShellPtrWatcher(ApplicationImpl* impl); |
95 virtual ~ShellPtrWatcher(); | 76 virtual ~ShellPtrWatcher(); |
96 virtual void OnConnectionError() MOJO_OVERRIDE; | 77 virtual void OnConnectionError() MOJO_OVERRIDE; |
97 private: | 78 private: |
98 ApplicationImpl* impl_; | 79 ApplicationImpl* impl_; |
99 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher); | 80 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher); |
100 }; | 81 }; |
101 | 82 |
102 friend MojoResult (::MojoMain)(MojoHandle); | |
103 | |
104 void BindShell(ScopedMessagePipeHandle shell_handle); | 83 void BindShell(ScopedMessagePipeHandle shell_handle); |
105 void BindShell(MojoHandle shell_handle); | |
106 void ClearConnections(); | 84 void ClearConnections(); |
107 void OnShellError() { ClearConnections(); Terminate(); }; | 85 void OnShellError() { ClearConnections(); Terminate(); }; |
108 | 86 |
109 // Quits the main run loop for this application. | 87 // Quits the main run loop for this application. |
110 void Terminate(); | 88 static void Terminate(); |
111 | 89 |
112 // Application implementation. | 90 // Application implementation. |
113 virtual void AcceptConnection(const String& requestor_url, | 91 virtual void AcceptConnection(const String& requestor_url, |
114 ServiceProviderPtr provider) MOJO_OVERRIDE; | 92 ServiceProviderPtr provider) MOJO_OVERRIDE; |
115 | 93 |
116 typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList; | 94 typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList; |
117 ServiceRegistryList incoming_service_registries_; | 95 ServiceRegistryList incoming_service_registries_; |
118 ServiceRegistryList outgoing_service_registries_; | 96 ServiceRegistryList outgoing_service_registries_; |
119 ApplicationDelegate* delegate_; | 97 ApplicationDelegate* delegate_; |
120 ShellPtr shell_; | 98 ShellPtr shell_; |
121 ShellPtrWatcher shell_watch_; | 99 ShellPtrWatcher shell_watch_; |
122 | 100 |
123 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); | 101 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); |
124 }; | 102 }; |
125 | 103 |
126 } // namespace mojo | 104 } // namespace mojo |
127 | 105 |
128 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ | 106 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ |
OLD | NEW |