| 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" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 } | 45 } |
| 46 | 46 |
| 47 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | 47 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, |
| 48 InterfaceRequest<Application> request, | 48 InterfaceRequest<Application> request, |
| 49 const Closure& termination_closure) | 49 const Closure& termination_closure) |
| 50 : delegate_(delegate), | 50 : delegate_(delegate), |
| 51 binding_(this, request.Pass()), | 51 binding_(this, request.Pass()), |
| 52 termination_closure_(termination_closure), | 52 termination_closure_(termination_closure), |
| 53 app_lifetime_helper_(this), | 53 app_lifetime_helper_(this), |
| 54 quit_requested_(false), | 54 quit_requested_(false), |
| 55 in_destructor_(false), | |
| 56 weak_factory_(this) {} | 55 weak_factory_(this) {} |
| 57 | 56 |
| 58 ApplicationImpl::~ApplicationImpl() { | 57 ApplicationImpl::~ApplicationImpl() { |
| 59 DCHECK(!in_destructor_); | |
| 60 in_destructor_ = true; | |
| 61 ClearConnections(); | |
| 62 app_lifetime_helper_.OnQuit(); | 58 app_lifetime_helper_.OnQuit(); |
| 63 } | 59 } |
| 64 | 60 |
| 65 ApplicationConnection* ApplicationImpl::ConnectToApplication( | 61 scoped_ptr<ApplicationConnection> ApplicationImpl::ConnectToApplication( |
| 66 mojo::URLRequestPtr request, | 62 mojo::URLRequestPtr request, |
| 67 CapabilityFilterPtr filter) { | 63 CapabilityFilterPtr filter) { |
| 68 if (!shell_) | 64 if (!shell_) |
| 69 return nullptr; | 65 return nullptr; |
| 70 ServiceProviderPtr local_services; | 66 ServiceProviderPtr local_services; |
| 71 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); | 67 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); |
| 72 ServiceProviderPtr remote_services; | 68 ServiceProviderPtr remote_services; |
| 73 std::string application_url = request->url.To<std::string>(); | 69 std::string application_url = request->url.To<std::string>(); |
| 74 shell_->ConnectToApplication(request.Pass(), GetProxy(&remote_services), | 70 shell_->ConnectToApplication(request.Pass(), GetProxy(&remote_services), |
| 75 local_services.Pass(), filter.Pass()); | 71 local_services.Pass(), filter.Pass()); |
| 76 // We allow all interfaces on outgoing connections since we are presumably in | 72 // We allow all interfaces on outgoing connections since we are presumably in |
| 77 // a position to know who we're talking to. | 73 // a position to know who we're talking to. |
| 78 // TODO(beng): is this a valid assumption or do we need to figure some way to | 74 // TODO(beng): is this a valid assumption or do we need to figure some way to |
| 79 // filter here too? | 75 // filter here too? |
| 80 std::set<std::string> allowed; | 76 std::set<std::string> allowed; |
| 81 allowed.insert("*"); | 77 allowed.insert("*"); |
| 82 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 78 scoped_ptr<ApplicationConnection> registry(new internal::ServiceRegistry( |
| 83 this, application_url, application_url, remote_services.Pass(), | 79 application_url, application_url, remote_services.Pass(), |
| 84 local_request.Pass(), allowed); | 80 local_request.Pass(), allowed)); |
| 85 if (!delegate_->ConfigureOutgoingConnection(registry)) { | 81 if (!delegate_->ConfigureOutgoingConnection(registry.get())) |
| 86 registry->CloseConnection(); | |
| 87 return nullptr; | 82 return nullptr; |
| 88 } | 83 return registry.Pass(); |
| 89 outgoing_service_registries_.push_back(registry); | |
| 90 return registry; | |
| 91 } | |
| 92 | |
| 93 void ApplicationImpl::CloseConnection(ApplicationConnection* connection) { | |
| 94 if (!in_destructor_) | |
| 95 delegate_->OnWillCloseConnection(connection); | |
| 96 auto outgoing_it = std::find(outgoing_service_registries_.begin(), | |
| 97 outgoing_service_registries_.end(), | |
| 98 connection); | |
| 99 if (outgoing_it != outgoing_service_registries_.end()) { | |
| 100 outgoing_service_registries_.erase(outgoing_it); | |
| 101 return; | |
| 102 } | |
| 103 auto incoming_it = std::find(incoming_service_registries_.begin(), | |
| 104 incoming_service_registries_.end(), | |
| 105 connection); | |
| 106 if (incoming_it != incoming_service_registries_.end()) | |
| 107 incoming_service_registries_.erase(incoming_it); | |
| 108 } | 84 } |
| 109 | 85 |
| 110 void ApplicationImpl::Initialize(ShellPtr shell, const mojo::String& url) { | 86 void ApplicationImpl::Initialize(ShellPtr shell, const mojo::String& url) { |
| 111 shell_ = shell.Pass(); | 87 shell_ = shell.Pass(); |
| 112 shell_.set_connection_error_handler([this]() { OnConnectionError(); }); | 88 shell_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 113 url_ = url; | 89 url_ = url; |
| 114 delegate_->Initialize(this); | 90 delegate_->Initialize(this); |
| 115 } | 91 } |
| 116 | 92 |
| 117 void ApplicationImpl::WaitForInitialize() { | 93 void ApplicationImpl::WaitForInitialize() { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 136 QuitNow(); | 112 QuitNow(); |
| 137 } | 113 } |
| 138 } | 114 } |
| 139 | 115 |
| 140 void ApplicationImpl::AcceptConnection( | 116 void ApplicationImpl::AcceptConnection( |
| 141 const String& requestor_url, | 117 const String& requestor_url, |
| 142 InterfaceRequest<ServiceProvider> services, | 118 InterfaceRequest<ServiceProvider> services, |
| 143 ServiceProviderPtr exposed_services, | 119 ServiceProviderPtr exposed_services, |
| 144 Array<String> allowed_interfaces, | 120 Array<String> allowed_interfaces, |
| 145 const String& url) { | 121 const String& url) { |
| 146 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 122 scoped_ptr<ApplicationConnection> registry(new internal::ServiceRegistry( |
| 147 this, url, requestor_url, exposed_services.Pass(), services.Pass(), | 123 url, requestor_url, exposed_services.Pass(), services.Pass(), |
| 148 allowed_interfaces.To<std::set<std::string>>()); | 124 allowed_interfaces.To<std::set<std::string>>())); |
| 149 if (!delegate_->ConfigureIncomingConnection(registry)) { | 125 if (!delegate_->ConfigureIncomingConnection(registry.get())) |
| 150 registry->CloseConnection(); | |
| 151 return; | 126 return; |
| 152 } | |
| 153 incoming_service_registries_.push_back(registry); | |
| 154 | 127 |
| 155 // If we were quitting because we thought there were no more services for this | 128 // If we were quitting because we thought there were no more services for this |
| 156 // app in use, then that has changed so cancel the quit request. | 129 // app in use, then that has changed so cancel the quit request. |
| 157 if (quit_requested_) | 130 if (quit_requested_) |
| 158 quit_requested_ = false; | 131 quit_requested_ = false; |
| 132 |
| 133 incoming_connections_.push_back(registry.Pass()); |
| 159 } | 134 } |
| 160 | 135 |
| 161 void ApplicationImpl::OnQuitRequested(const Callback<void(bool)>& callback) { | 136 void ApplicationImpl::OnQuitRequested(const Callback<void(bool)>& callback) { |
| 162 // If by the time we got the reply from the shell, more requests had come in | 137 // If by the time we got the reply from the shell, more requests had come in |
| 163 // then we don't want to quit the app anymore so we return false. Otherwise | 138 // then we don't want to quit the app anymore so we return false. Otherwise |
| 164 // |quit_requested_| is true so we tell the shell to proceed with the quit. | 139 // |quit_requested_| is true so we tell the shell to proceed with the quit. |
| 165 callback.Run(quit_requested_); | 140 callback.Run(quit_requested_); |
| 166 if (quit_requested_) | 141 if (quit_requested_) |
| 167 QuitNow(); | 142 QuitNow(); |
| 168 } | 143 } |
| 169 | 144 |
| 170 void ApplicationImpl::OnConnectionError() { | 145 void ApplicationImpl::OnConnectionError() { |
| 171 base::WeakPtr<ApplicationImpl> ptr(weak_factory_.GetWeakPtr()); | 146 base::WeakPtr<ApplicationImpl> ptr(weak_factory_.GetWeakPtr()); |
| 172 | 147 |
| 173 // We give the delegate notice first, since it might want to do something on | 148 // We give the delegate notice first, since it might want to do something on |
| 174 // shell connection errors other than immediate termination of the run | 149 // shell connection errors other than immediate termination of the run |
| 175 // loop. The application might want to continue servicing connections other | 150 // loop. The application might want to continue servicing connections other |
| 176 // than the one to the shell. | 151 // than the one to the shell. |
| 177 bool quit_now = delegate_->OnShellConnectionError(); | 152 bool quit_now = delegate_->OnShellConnectionError(); |
| 178 if (quit_now) | 153 if (quit_now) |
| 179 QuitNow(); | 154 QuitNow(); |
| 180 if (!ptr) | 155 if (!ptr) |
| 181 return; | 156 return; |
| 182 shell_ = nullptr; | 157 shell_ = nullptr; |
| 183 } | 158 } |
| 184 | 159 |
| 185 void ApplicationImpl::ClearConnections() { | |
| 186 // Copy the ServiceRegistryLists because they will be mutated by | |
| 187 // ApplicationConnection::CloseConnection. | |
| 188 ServiceRegistryList incoming_service_registries(incoming_service_registries_); | |
| 189 for (internal::ServiceRegistry* registry : incoming_service_registries) | |
| 190 registry->CloseConnection(); | |
| 191 DCHECK(incoming_service_registries_.empty()); | |
| 192 | |
| 193 ServiceRegistryList outgoing_service_registries(outgoing_service_registries_); | |
| 194 for (internal::ServiceRegistry* registry : outgoing_service_registries) | |
| 195 registry->CloseConnection(); | |
| 196 DCHECK(outgoing_service_registries_.empty()); | |
| 197 } | |
| 198 | |
| 199 void ApplicationImpl::QuitNow() { | 160 void ApplicationImpl::QuitNow() { |
| 200 delegate_->Quit(); | 161 delegate_->Quit(); |
| 201 termination_closure_.Run(); | 162 termination_closure_.Run(); |
| 202 } | 163 } |
| 203 | 164 |
| 204 } // namespace mojo | 165 } // namespace mojo |
| OLD | NEW |