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 | 7 |
| 8 #include <memory> |
8 #include <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "mojo/public/cpp/application/application_delegate.h" | 12 #include "mojo/public/cpp/application/application_delegate.h" |
12 #include "mojo/public/cpp/application/lib/service_registry.h" | 13 #include "mojo/public/cpp/application/lib/service_registry.h" |
13 #include "mojo/public/cpp/system/core.h" | 14 #include "mojo/public/cpp/system/core.h" |
14 #include "mojo/public/interfaces/application/application.mojom.h" | 15 #include "mojo/public/interfaces/application/application.mojom.h" |
| 16 #include "mojo/public/interfaces/application/application_connector.mojom.h" |
15 #include "mojo/public/interfaces/application/shell.mojom.h" | 17 #include "mojo/public/interfaces/application/shell.mojom.h" |
16 | 18 |
17 namespace mojo { | 19 namespace mojo { |
18 | 20 |
19 class ApplicationConnection; | 21 class ApplicationConnection; |
20 | 22 |
21 // Implements the Application interface, which the shell uses for basic | 23 // Implements the Application interface, which the shell uses for basic |
22 // communication with an application (e.g., to connect clients to services | 24 // communication with an application (e.g., to connect clients to services |
23 // provided by an application). Also provides the application access to the | 25 // provided by an application). Also provides the application access to the |
24 // Shell, which, e.g., may be used by an application to connect to other | 26 // Shell, which, e.g., may be used by an application to connect to other |
25 // services. | 27 // services. |
26 // | 28 // |
27 // Typically, you create one or more classes implementing your APIs (e.g., | 29 // Typically, you create one or more classes implementing your APIs (e.g., |
28 // FooImpl implementing Foo). See bindings/binding.h for more information. Then | 30 // FooImpl implementing Foo). See bindings/binding.h for more information. Then |
29 // you implement an mojo::ApplicationDelegate that either is or owns a | 31 // you implement an mojo::ApplicationDelegate that either is or owns a |
30 // mojo::InterfaceFactory<Foo> and whose ConfigureIncomingConnection() adds that | 32 // mojo::InterfaceFactory<Foo> and whose ConfigureIncomingConnection() adds that |
31 // factory to each connection. Finally, you instantiate your delegate and pass | 33 // factory to each connection. Finally, you instantiate your delegate and pass |
32 // it to an ApplicationRunner, which will create the ApplicationImpl and then | 34 // it to an ApplicationRunner, which will create the ApplicationImpl and then |
33 // run a message (or run) loop. | 35 // run a message (or run) loop. |
34 class ApplicationImpl : public Application { | 36 class ApplicationImpl : public Application { |
35 public: | 37 public: |
36 // Does not take ownership of |delegate|, which must remain valid for the | 38 // Does not take ownership of |delegate|, which must remain valid for the |
37 // lifetime of ApplicationImpl. | 39 // lifetime of ApplicationImpl. |
38 ApplicationImpl(ApplicationDelegate* delegate, | 40 ApplicationImpl(ApplicationDelegate* delegate, |
39 InterfaceRequest<Application> request); | 41 InterfaceRequest<Application> request); |
40 ~ApplicationImpl() override; | 42 ~ApplicationImpl() override; |
41 | 43 |
| 44 // Quits the main run loop for this application. |
| 45 // TODO(vtl): This is implemented in application_runner.cc (for example). Its |
| 46 // presence here is pretty dubious. |
| 47 static void Terminate(); |
| 48 |
42 // The Mojo shell. This will return a valid pointer after Initialize() has | 49 // The Mojo shell. This will return a valid pointer after Initialize() has |
43 // been invoked. It will remain valid until UnbindConnections() is invoked or | 50 // been invoked. It will remain valid until UnbindConnections() is invoked or |
44 // the ApplicationImpl is destroyed. | 51 // the ApplicationImpl is destroyed. |
45 Shell* shell() const { return shell_.get(); } | 52 Shell* shell() const { return shell_.get(); } |
46 | 53 |
47 const std::string& url() const { return url_; } | 54 const std::string& url() const { return url_; } |
48 | 55 |
49 // Returns any initial configuration arguments, passed by the Shell. | 56 // Returns any initial configuration arguments, passed by the Shell. |
50 const std::vector<std::string>& args() const { return args_; } | 57 const std::vector<std::string>& args() const { return args_; } |
51 bool HasArg(const std::string& arg) const; | 58 bool HasArg(const std::string& arg) const; |
52 | 59 |
| 60 // Creates a new |ApplicationConnector|. The result can be bound to an |
| 61 // |ApplicationConnectorPtr| and used to connect to other applications. (It |
| 62 // returns an |InterfacePtrInfo| instead of an |InterfacePtr| to facilitate |
| 63 // passing it to another thread.) |
| 64 InterfacePtrInfo<ApplicationConnector> CreateApplicationConnector(); |
| 65 |
53 // Requests a new connection to an application. Returns a pointer to the | 66 // Requests a new connection to an application. Returns a pointer to the |
54 // connection if the connection is permitted by this application's delegate, | 67 // connection if the connection is permitted by this application's delegate, |
55 // or nullptr otherwise. Caller does not take ownership. The pointer remains | 68 // or nullptr otherwise. Caller does not take ownership. The pointer remains |
56 // valid until an error occurs on the connection with the Shell, or until the | 69 // valid until an error occurs on the connection with the Shell, or until the |
57 // ApplicationImpl is destroyed, whichever occurs first. | 70 // ApplicationImpl is destroyed, whichever occurs first. |
| 71 // TODO(vtl): Deprecate/remove this. |
58 ApplicationConnection* ConnectToApplication(const String& application_url); | 72 ApplicationConnection* ConnectToApplication(const String& application_url); |
59 | 73 |
60 // Connect to application identified by |application_url| and connect to the | 74 // Connect to application identified by |application_url| and connect to the |
61 // service implementation of the interface identified by |Interface|. | 75 // service implementation of the interface identified by |Interface|. |
| 76 // TODO(vtl): Deprecate/remove this. |
62 template <typename Interface> | 77 template <typename Interface> |
63 void ConnectToService(const std::string& application_url, | 78 void ConnectToService(const std::string& application_url, |
64 InterfacePtr<Interface>* ptr) { | 79 InterfacePtr<Interface>* ptr) { |
65 ConnectToApplication(application_url)->ConnectToService(ptr); | 80 ConnectToApplication(application_url)->ConnectToService(ptr); |
66 } | 81 } |
67 | 82 |
68 // Application implementation. | 83 // Blocks until the |Application| is initialized (i.e., |Initialize()| is |
69 void Initialize(ShellPtr shell, | 84 // received), if it is not already. |
70 Array<String> args, | |
71 const mojo::String& url) override; | |
72 | |
73 // Block until the Application is initialized, if it is not already. | |
74 void WaitForInitialize(); | 85 void WaitForInitialize(); |
75 | 86 |
76 // Unbinds the Shell and Application connections. Can be used to re-bind the | 87 // Unbinds the Shell and Application connections. Can be used to re-bind the |
77 // handles to another implementation of ApplicationImpl, for instance when | 88 // handles to another implementation of ApplicationImpl, for instance when |
78 // running apptests. | 89 // running apptests. |
79 void UnbindConnections(InterfaceRequest<Application>* application_request, | 90 void UnbindConnections(InterfaceRequest<Application>* application_request, |
80 ShellPtr* shell); | 91 ShellPtr* shell); |
81 | 92 |
82 // Quits the main run loop for this application. | 93 // |Application| implementation. |
83 static void Terminate(); | 94 void Initialize(ShellPtr shell, |
84 | 95 Array<String> args, |
85 protected: | 96 const mojo::String& url) override; |
86 // Application implementation. | |
87 void AcceptConnection(const String& requestor_url, | 97 void AcceptConnection(const String& requestor_url, |
88 InterfaceRequest<ServiceProvider> services, | 98 InterfaceRequest<ServiceProvider> services, |
89 ServiceProviderPtr exposed_services, | 99 ServiceProviderPtr exposed_services, |
90 const String& url) override; | 100 const String& url) override; |
| 101 void RequestQuit() override; |
91 | 102 |
92 private: | 103 private: |
93 void ClearConnections(); | 104 using ServiceRegistryList = |
94 | 105 std::vector<std::unique_ptr<internal::ServiceRegistry>>; |
95 void OnShellError() { | |
96 delegate_->Quit(); | |
97 ClearConnections(); | |
98 Terminate(); | |
99 } | |
100 | |
101 // Application implementation. | |
102 void RequestQuit() override; | |
103 | |
104 typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList; | |
105 | |
106 ServiceRegistryList incoming_service_registries_; | 106 ServiceRegistryList incoming_service_registries_; |
107 ServiceRegistryList outgoing_service_registries_; | 107 ServiceRegistryList outgoing_service_registries_; |
108 ApplicationDelegate* delegate_; | 108 ApplicationDelegate* delegate_; |
109 Binding<Application> binding_; | 109 Binding<Application> binding_; |
110 ShellPtr shell_; | 110 ShellPtr shell_; |
111 std::string url_; | 111 std::string url_; |
112 std::vector<std::string> args_; | 112 std::vector<std::string> args_; |
113 | 113 |
114 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); | 114 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); |
115 }; | 115 }; |
116 | 116 |
117 } // namespace mojo | 117 } // namespace mojo |
118 | 118 |
119 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ | 119 #endif // MOJO_PUBLIC_APPLICATION_APPLICATION_IMPL_H_ |
OLD | NEW |