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 #include "mojo/application/public/cpp/application_impl.h" | 5 #include "mojo/application/public/cpp/application_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "mojo/application/public/cpp/application_delegate.h" | 11 #include "mojo/application/public/cpp/application_delegate.h" |
12 #include "mojo/application/public/cpp/lib/service_registry.h" | 12 #include "mojo/application/public/cpp/lib/service_registry.h" |
13 #include "mojo/public/cpp/bindings/interface_ptr.h" | 13 #include "mojo/public/cpp/bindings/interface_ptr.h" |
14 #include "mojo/public/cpp/environment/logging.h" | 14 #include "mojo/public/cpp/environment/logging.h" |
15 | 15 |
16 namespace mojo { | 16 namespace mojo { |
17 | 17 |
18 namespace { | |
19 | |
20 void DefaultTerminationClosure() { | |
21 if (base::MessageLoop::current() && | |
22 base::MessageLoop::current()->is_running()) | |
23 base::MessageLoop::current()->Quit(); | |
24 } | |
25 | |
26 } // namespace | |
27 | |
28 // TODO(beng): upstream this into mojo repo, array.h | 18 // TODO(beng): upstream this into mojo repo, array.h |
29 template <typename E, typename T> | 19 template <typename E, typename T> |
30 struct TypeConverter<std::set<E>, Array<T>> { | 20 struct TypeConverter<std::set<E>, Array<T>> { |
31 static std::set<E> Convert(const Array<T>& input) { | 21 static std::set<E> Convert(const Array<T>& input) { |
32 std::set<E> result; | 22 std::set<E> result; |
33 if (!input.is_null()) { | 23 if (!input.is_null()) { |
34 for (size_t i = 0; i < input.size(); ++i) | 24 for (size_t i = 0; i < input.size(); ++i) |
35 result.insert(TypeConverter<E, T>::Convert(input[i])); | 25 result.insert(TypeConverter<E, T>::Convert(input[i])); |
36 } | 26 } |
37 return result; | 27 return result; |
38 } | 28 } |
39 }; | 29 }; |
40 | 30 |
41 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | 31 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, |
42 InterfaceRequest<Application> request) | |
43 : ApplicationImpl(delegate, request.Pass(), | |
44 base::Bind(&DefaultTerminationClosure)) { | |
45 } | |
46 | |
47 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | |
48 InterfaceRequest<Application> request, | 32 InterfaceRequest<Application> request, |
49 const Closure& termination_closure) | 33 const Closure& termination_closure) |
50 : delegate_(delegate), | 34 : delegate_(delegate), |
51 binding_(this, request.Pass()), | 35 binding_(this, request.Pass()), |
52 termination_closure_(termination_closure), | 36 termination_closure_(termination_closure), |
53 app_lifetime_helper_(this), | 37 app_lifetime_helper_(this), |
54 quit_requested_(false), | 38 quit_requested_(false), |
55 in_destructor_(false), | 39 in_destructor_(false), |
56 weak_factory_(this) {} | 40 weak_factory_(this) {} |
57 | 41 |
58 void ApplicationImpl::ClearConnections() { | 42 void ApplicationImpl::ClearConnections() { |
59 // Copy the ServiceRegistryLists because they will be mutated by | 43 // Copy the ServiceRegistryLists because they will be mutated by |
60 // ApplicationConnection::CloseConnection. | 44 // ApplicationConnection::CloseConnection. |
61 ServiceRegistryList incoming_service_registries(incoming_service_registries_); | 45 ServiceRegistryList incoming_service_registries(incoming_service_registries_); |
62 for (internal::ServiceRegistry* registry : incoming_service_registries) | 46 for (internal::ServiceRegistry* registry : incoming_service_registries) |
63 registry->CloseConnection(); | 47 registry->CloseConnection(); |
64 DCHECK(incoming_service_registries_.empty()); | 48 DCHECK(incoming_service_registries_.empty()); |
65 | 49 |
66 ServiceRegistryList outgoing_service_registries(outgoing_service_registries_); | 50 ServiceRegistryList outgoing_service_registries(outgoing_service_registries_); |
67 for (internal::ServiceRegistry* registry : outgoing_service_registries) | 51 for (internal::ServiceRegistry* registry : outgoing_service_registries) |
68 registry->CloseConnection(); | 52 registry->CloseConnection(); |
69 DCHECK(outgoing_service_registries_.empty()); | 53 DCHECK(outgoing_service_registries_.empty()); |
70 } | 54 } |
71 | 55 |
72 ApplicationImpl::~ApplicationImpl() { | 56 ApplicationImpl::~ApplicationImpl() { |
73 DCHECK(!in_destructor_); | 57 DCHECK(!in_destructor_); |
74 in_destructor_ = true; | 58 in_destructor_ = true; |
75 ClearConnections(); | 59 ClearConnections(); |
76 app_lifetime_helper_.ApplicationTerminated(); | 60 app_lifetime_helper_.OnQuit(); |
77 } | 61 } |
78 | 62 |
79 ApplicationConnection* ApplicationImpl::ConnectToApplication( | 63 ApplicationConnection* ApplicationImpl::ConnectToApplication( |
80 mojo::URLRequestPtr request, | 64 mojo::URLRequestPtr request, |
81 CapabilityFilterPtr filter) { | 65 CapabilityFilterPtr filter) { |
82 if (!shell_) | 66 if (!shell_) |
83 return nullptr; | 67 return nullptr; |
84 ServiceProviderPtr local_services; | 68 ServiceProviderPtr local_services; |
85 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); | 69 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); |
86 ServiceProviderPtr remote_services; | 70 ServiceProviderPtr remote_services; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 binding_.WaitForIncomingMethodCall(); | 117 binding_.WaitForIncomingMethodCall(); |
134 } | 118 } |
135 | 119 |
136 void ApplicationImpl::UnbindConnections( | 120 void ApplicationImpl::UnbindConnections( |
137 InterfaceRequest<Application>* application_request, | 121 InterfaceRequest<Application>* application_request, |
138 ShellPtr* shell) { | 122 ShellPtr* shell) { |
139 *application_request = binding_.Unbind(); | 123 *application_request = binding_.Unbind(); |
140 shell->Bind(shell_.PassInterface()); | 124 shell->Bind(shell_.PassInterface()); |
141 } | 125 } |
142 | 126 |
143 void ApplicationImpl::Terminate() { | 127 void ApplicationImpl::Quit() { |
144 // We can't quit immediately, since there could be in-flight requests from the | 128 // We can't quit immediately, since there could be in-flight requests from the |
145 // shell. So check with it first. | 129 // shell. So check with it first. |
146 if (shell_) { | 130 if (shell_) { |
147 quit_requested_ = true; | 131 quit_requested_ = true; |
148 shell_->QuitApplication(); | 132 shell_->QuitApplication(); |
149 } else { | 133 } else { |
150 QuitNow(); | 134 QuitNow(); |
151 } | 135 } |
152 } | 136 } |
153 | 137 |
154 void ApplicationImpl::QuitNow() { | |
155 delegate_->Quit(); | |
156 termination_closure_.Run(); | |
157 } | |
158 | |
159 void ApplicationImpl::AcceptConnection( | 138 void ApplicationImpl::AcceptConnection( |
160 const String& requestor_url, | 139 const String& requestor_url, |
161 InterfaceRequest<ServiceProvider> services, | 140 InterfaceRequest<ServiceProvider> services, |
162 ServiceProviderPtr exposed_services, | 141 ServiceProviderPtr exposed_services, |
163 Array<String> allowed_interfaces, | 142 Array<String> allowed_interfaces, |
164 const String& url) { | 143 const String& url) { |
165 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 144 internal::ServiceRegistry* registry = new internal::ServiceRegistry( |
166 this, url, requestor_url, exposed_services.Pass(), services.Pass(), | 145 this, url, requestor_url, exposed_services.Pass(), services.Pass(), |
167 allowed_interfaces.To<std::set<std::string>>()); | 146 allowed_interfaces.To<std::set<std::string>>()); |
168 if (!delegate_->ConfigureIncomingConnection(registry)) { | 147 if (!delegate_->ConfigureIncomingConnection(registry)) { |
(...skipping 25 matching lines...) Expand all Loading... |
194 // loop. The application might want to continue servicing connections other | 173 // loop. The application might want to continue servicing connections other |
195 // than the one to the shell. | 174 // than the one to the shell. |
196 bool quit_now = delegate_->OnShellConnectionError(); | 175 bool quit_now = delegate_->OnShellConnectionError(); |
197 if (quit_now) | 176 if (quit_now) |
198 QuitNow(); | 177 QuitNow(); |
199 if (!ptr) | 178 if (!ptr) |
200 return; | 179 return; |
201 shell_ = nullptr; | 180 shell_ = nullptr; |
202 } | 181 } |
203 | 182 |
| 183 void ApplicationImpl::QuitNow() { |
| 184 delegate_->Quit(); |
| 185 termination_closure_.Run(); |
| 186 } |
| 187 |
204 } // namespace mojo | 188 } // namespace mojo |
OLD | NEW |