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> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
9 #include "mojo/application/public/cpp/application_delegate.h" | 11 #include "mojo/application/public/cpp/application_delegate.h" |
10 #include "mojo/application/public/cpp/lib/service_registry.h" | 12 #include "mojo/application/public/cpp/lib/service_registry.h" |
11 #include "mojo/public/cpp/bindings/interface_ptr.h" | 13 #include "mojo/public/cpp/bindings/interface_ptr.h" |
12 #include "mojo/public/cpp/environment/logging.h" | 14 #include "mojo/public/cpp/environment/logging.h" |
13 | 15 |
14 namespace mojo { | 16 namespace mojo { |
15 | 17 |
16 namespace { | 18 namespace { |
(...skipping 12 matching lines...) Expand all Loading... |
29 } | 31 } |
30 | 32 |
31 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | 33 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, |
32 InterfaceRequest<Application> request, | 34 InterfaceRequest<Application> request, |
33 const base::Closure& termination_closure) | 35 const base::Closure& termination_closure) |
34 : delegate_(delegate), | 36 : delegate_(delegate), |
35 binding_(this, request.Pass()), | 37 binding_(this, request.Pass()), |
36 termination_closure_(termination_closure), | 38 termination_closure_(termination_closure), |
37 app_lifetime_helper_(this), | 39 app_lifetime_helper_(this), |
38 quit_requested_(false), | 40 quit_requested_(false), |
| 41 in_destructor_(false), |
39 weak_factory_(this) { | 42 weak_factory_(this) { |
40 } | 43 } |
41 | 44 |
42 void ApplicationImpl::ClearConnections() { | 45 void ApplicationImpl::ClearConnections() { |
43 for (ServiceRegistryList::iterator i(incoming_service_registries_.begin()); | 46 // Copy the ServiceRegistryLists because they will be mutated by |
44 i != incoming_service_registries_.end(); | 47 // ApplicationConnection::CloseConnection. |
45 ++i) | 48 ServiceRegistryList incoming_service_registries(incoming_service_registries_); |
46 delete *i; | 49 for (internal::ServiceRegistry* registry : incoming_service_registries) |
47 for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin()); | 50 registry->CloseConnection(); |
48 i != outgoing_service_registries_.end(); | 51 DCHECK(incoming_service_registries_.empty()); |
49 ++i) | 52 |
50 delete *i; | 53 ServiceRegistryList outgoing_service_registries(outgoing_service_registries_); |
51 incoming_service_registries_.clear(); | 54 for (internal::ServiceRegistry* registry : outgoing_service_registries) |
52 outgoing_service_registries_.clear(); | 55 registry->CloseConnection(); |
| 56 DCHECK(outgoing_service_registries_.empty()); |
53 } | 57 } |
54 | 58 |
55 ApplicationImpl::~ApplicationImpl() { | 59 ApplicationImpl::~ApplicationImpl() { |
| 60 DCHECK(!in_destructor_); |
| 61 in_destructor_ = true; |
56 ClearConnections(); | 62 ClearConnections(); |
57 app_lifetime_helper_.ApplicationTerminated(); | 63 app_lifetime_helper_.ApplicationTerminated(); |
58 } | 64 } |
59 | 65 |
60 ApplicationConnection* ApplicationImpl::ConnectToApplication( | 66 ApplicationConnection* ApplicationImpl::ConnectToApplication( |
61 mojo::URLRequestPtr request) { | 67 mojo::URLRequestPtr request) { |
62 MOJO_CHECK(shell_); | 68 MOJO_CHECK(shell_); |
63 ServiceProviderPtr local_services; | 69 ServiceProviderPtr local_services; |
64 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); | 70 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); |
65 ServiceProviderPtr remote_services; | 71 ServiceProviderPtr remote_services; |
66 std::string application_url = request->url.To<std::string>(); | 72 std::string application_url = request->url.To<std::string>(); |
67 shell_->ConnectToApplication(request.Pass(), GetProxy(&remote_services), | 73 shell_->ConnectToApplication(request.Pass(), GetProxy(&remote_services), |
68 local_services.Pass()); | 74 local_services.Pass()); |
69 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 75 internal::ServiceRegistry* registry = new internal::ServiceRegistry( |
70 this, application_url, application_url, remote_services.Pass(), | 76 this, application_url, application_url, remote_services.Pass(), |
71 local_request.Pass()); | 77 local_request.Pass()); |
72 if (!delegate_->ConfigureOutgoingConnection(registry)) { | 78 if (!delegate_->ConfigureOutgoingConnection(registry)) { |
73 delete registry; | 79 registry->CloseConnection(); |
74 return nullptr; | 80 return nullptr; |
75 } | 81 } |
76 outgoing_service_registries_.push_back(registry); | 82 outgoing_service_registries_.push_back(registry); |
77 return registry; | 83 return registry; |
78 } | 84 } |
79 | 85 |
| 86 void ApplicationImpl::CloseConnection(ApplicationConnection* connection) { |
| 87 if (!in_destructor_) |
| 88 delegate_->OnWillCloseConnection(connection); |
| 89 auto outgoing_it = std::find(outgoing_service_registries_.begin(), |
| 90 outgoing_service_registries_.end(), |
| 91 connection); |
| 92 if (outgoing_it != outgoing_service_registries_.end()) { |
| 93 outgoing_service_registries_.erase(outgoing_it); |
| 94 return; |
| 95 } |
| 96 auto incoming_it = std::find(incoming_service_registries_.begin(), |
| 97 incoming_service_registries_.end(), |
| 98 connection); |
| 99 if (incoming_it != incoming_service_registries_.end()) |
| 100 incoming_service_registries_.erase(incoming_it); |
| 101 } |
| 102 |
80 void ApplicationImpl::Initialize(ShellPtr shell, const mojo::String& url) { | 103 void ApplicationImpl::Initialize(ShellPtr shell, const mojo::String& url) { |
81 shell_ = shell.Pass(); | 104 shell_ = shell.Pass(); |
82 shell_.set_error_handler(this); | 105 shell_.set_error_handler(this); |
83 url_ = url; | 106 url_ = url; |
84 delegate_->Initialize(this); | 107 delegate_->Initialize(this); |
85 } | 108 } |
86 | 109 |
87 void ApplicationImpl::WaitForInitialize() { | 110 void ApplicationImpl::WaitForInitialize() { |
88 if (!shell_) | 111 if (!shell_) |
89 binding_.WaitForIncomingMethodCall(); | 112 binding_.WaitForIncomingMethodCall(); |
(...skipping 23 matching lines...) Expand all Loading... |
113 } | 136 } |
114 | 137 |
115 void ApplicationImpl::AcceptConnection( | 138 void ApplicationImpl::AcceptConnection( |
116 const String& requestor_url, | 139 const String& requestor_url, |
117 InterfaceRequest<ServiceProvider> services, | 140 InterfaceRequest<ServiceProvider> services, |
118 ServiceProviderPtr exposed_services, | 141 ServiceProviderPtr exposed_services, |
119 const String& url) { | 142 const String& url) { |
120 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 143 internal::ServiceRegistry* registry = new internal::ServiceRegistry( |
121 this, url, requestor_url, exposed_services.Pass(), services.Pass()); | 144 this, url, requestor_url, exposed_services.Pass(), services.Pass()); |
122 if (!delegate_->ConfigureIncomingConnection(registry)) { | 145 if (!delegate_->ConfigureIncomingConnection(registry)) { |
123 delete registry; | 146 registry->CloseConnection(); |
124 return; | 147 return; |
125 } | 148 } |
126 incoming_service_registries_.push_back(registry); | 149 incoming_service_registries_.push_back(registry); |
127 | 150 |
128 // If we were quitting because we thought there were no more services for this | 151 // If we were quitting because we thought there were no more services for this |
129 // app in use, then that has changed so cancel the quit request. | 152 // app in use, then that has changed so cancel the quit request. |
130 if (quit_requested_) | 153 if (quit_requested_) |
131 quit_requested_ = false; | 154 quit_requested_ = false; |
132 } | 155 } |
133 | 156 |
134 void ApplicationImpl::OnQuitRequested(const Callback<void(bool)>& callback) { | 157 void ApplicationImpl::OnQuitRequested(const Callback<void(bool)>& callback) { |
135 // If by the time we got the reply from the shell, more requests had come in | 158 // If by the time we got the reply from the shell, more requests had come in |
136 // then we don't want to quit the app anymore so we return false. Otherwise | 159 // then we don't want to quit the app anymore so we return false. Otherwise |
137 // |quit_requested_| is true so we tell the shell to proceed with the quit. | 160 // |quit_requested_| is true so we tell the shell to proceed with the quit. |
138 callback.Run(quit_requested_); | 161 callback.Run(quit_requested_); |
139 if (quit_requested_) | 162 if (quit_requested_) |
140 QuitNow(); | 163 QuitNow(); |
141 } | 164 } |
142 | 165 |
143 void ApplicationImpl::OnConnectionError() { | 166 void ApplicationImpl::OnConnectionError() { |
144 base::WeakPtr<ApplicationImpl> ptr(weak_factory_.GetWeakPtr()); | 167 base::WeakPtr<ApplicationImpl> ptr(weak_factory_.GetWeakPtr()); |
145 QuitNow(); | 168 QuitNow(); |
146 if (!ptr) | 169 if (!ptr) |
147 return; | 170 return; |
148 shell_ = nullptr; | 171 shell_ = nullptr; |
149 } | 172 } |
150 | 173 |
151 } // namespace mojo | 174 } // namespace mojo |
OLD | NEW |