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 <algorithm> | 5 #include <algorithm> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "mojo/converters/network/network_type_converters.h" | 10 #include "mojo/converters/network/network_type_converters.h" |
11 #include "mojo/public/cpp/bindings/interface_ptr.h" | 11 #include "mojo/public/cpp/bindings/interface_ptr.h" |
12 #include "mojo/public/cpp/environment/logging.h" | 12 #include "mojo/public/cpp/environment/logging.h" |
13 #include "mojo/shell/public/cpp/application_impl.h" | |
14 #include "mojo/shell/public/cpp/lib/connection_impl.h" | 13 #include "mojo/shell/public/cpp/lib/connection_impl.h" |
15 #include "mojo/shell/public/cpp/shell_client.h" | 14 #include "mojo/shell/public/cpp/shell_client.h" |
| 15 #include "mojo/shell/public/cpp/shell_connection.h" |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 void DefaultTerminationClosure() { | 21 void DefaultTerminationClosure() { |
22 if (base::MessageLoop::current() && | 22 if (base::MessageLoop::current() && |
23 base::MessageLoop::current()->is_running()) | 23 base::MessageLoop::current()->is_running()) |
24 base::MessageLoop::current()->QuitWhenIdle(); | 24 base::MessageLoop::current()->QuitWhenIdle(); |
25 } | 25 } |
26 | 26 |
27 } // namespace | 27 } // namespace |
28 | 28 |
29 ApplicationImpl::ConnectParams::ConnectParams(const std::string& url) | 29 ShellConnection::ConnectParams::ConnectParams(const std::string& url) |
30 : ConnectParams(URLRequest::From(url)) {} | 30 : ConnectParams(URLRequest::From(url)) {} |
31 ApplicationImpl::ConnectParams::ConnectParams(URLRequestPtr request) | 31 ShellConnection::ConnectParams::ConnectParams(URLRequestPtr request) |
32 : request_(std::move(request)), | 32 : request_(std::move(request)), |
33 filter_(shell::mojom::CapabilityFilter::New()) { | 33 filter_(shell::mojom::CapabilityFilter::New()) { |
34 filter_->filter.mark_non_null(); | 34 filter_->filter.mark_non_null(); |
35 } | 35 } |
36 ApplicationImpl::ConnectParams::~ConnectParams() {} | 36 ShellConnection::ConnectParams::~ConnectParams() {} |
37 | 37 |
38 ApplicationImpl::ApplicationImpl( | 38 ShellConnection::ShellConnection( |
39 ShellClient* client, | 39 mojo::ShellClient* client, |
40 InterfaceRequest<shell::mojom::Application> request) | 40 InterfaceRequest<shell::mojom::ShellClient> request) |
41 : ApplicationImpl(client, | 41 : ShellConnection(client, |
42 std::move(request), | 42 std::move(request), |
43 base::Bind(&DefaultTerminationClosure)) {} | 43 base::Bind(&DefaultTerminationClosure)) {} |
44 | 44 |
45 ApplicationImpl::ApplicationImpl( | 45 ShellConnection::ShellConnection( |
46 ShellClient* client, | 46 mojo::ShellClient* client, |
47 InterfaceRequest<shell::mojom::Application> request, | 47 InterfaceRequest<shell::mojom::ShellClient> request, |
48 const Closure& termination_closure) | 48 const Closure& termination_closure) |
49 : client_(client), | 49 : client_(client), |
50 binding_(this, std::move(request)), | 50 binding_(this, std::move(request)), |
51 termination_closure_(termination_closure), | 51 termination_closure_(termination_closure), |
52 app_lifetime_helper_(this), | 52 app_lifetime_helper_(this), |
53 quit_requested_(false), | 53 quit_requested_(false), |
54 weak_factory_(this) {} | 54 weak_factory_(this) {} |
55 | 55 |
56 ApplicationImpl::~ApplicationImpl() { | 56 ShellConnection::~ShellConnection() { |
57 app_lifetime_helper_.OnQuit(); | 57 app_lifetime_helper_.OnQuit(); |
58 } | 58 } |
59 | 59 |
60 void ApplicationImpl::WaitForInitialize() { | 60 void ShellConnection::WaitForInitialize() { |
61 DCHECK(!shell_.is_bound()); | 61 DCHECK(!shell_.is_bound()); |
62 binding_.WaitForIncomingMethodCall(); | 62 binding_.WaitForIncomingMethodCall(); |
63 } | 63 } |
64 | 64 |
65 scoped_ptr<Connection> ApplicationImpl::Connect(const std::string& url) { | 65 scoped_ptr<Connection> ShellConnection::Connect(const std::string& url) { |
66 ConnectParams params(url); | 66 ConnectParams params(url); |
67 params.set_filter(CreatePermissiveCapabilityFilter()); | 67 params.set_filter(CreatePermissiveCapabilityFilter()); |
68 return Connect(¶ms); | 68 return Connect(¶ms); |
69 } | 69 } |
70 | 70 |
71 scoped_ptr<Connection> ApplicationImpl::Connect(ConnectParams* params) { | 71 scoped_ptr<Connection> ShellConnection::Connect(ConnectParams* params) { |
72 if (!shell_) | 72 if (!shell_) |
73 return nullptr; | 73 return nullptr; |
74 DCHECK(params); | 74 DCHECK(params); |
75 URLRequestPtr request = params->TakeRequest(); | 75 URLRequestPtr request = params->TakeRequest(); |
76 ServiceProviderPtr local_services; | 76 ServiceProviderPtr local_services; |
77 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); | 77 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); |
78 ServiceProviderPtr remote_services; | 78 ServiceProviderPtr remote_services; |
79 std::string application_url = request->url.To<std::string>(); | 79 std::string application_url = request->url.To<std::string>(); |
80 // We allow all interfaces on outgoing connections since we are presumably in | 80 // We allow all interfaces on outgoing connections since we are presumably in |
81 // a position to know who we're talking to. | 81 // a position to know who we're talking to. |
82 // TODO(beng): is this a valid assumption or do we need to figure some way to | 82 // TODO(beng): is this a valid assumption or do we need to figure some way to |
83 // filter here too? | 83 // filter here too? |
84 std::set<std::string> allowed; | 84 std::set<std::string> allowed; |
85 allowed.insert("*"); | 85 allowed.insert("*"); |
86 InterfaceRequest<ServiceProvider> remote_services_proxy = | 86 InterfaceRequest<ServiceProvider> remote_services_proxy = |
87 GetProxy(&remote_services); | 87 GetProxy(&remote_services); |
88 scoped_ptr<internal::ConnectionImpl> registry(new internal::ConnectionImpl( | 88 scoped_ptr<internal::ConnectionImpl> registry(new internal::ConnectionImpl( |
89 application_url, application_url, | 89 application_url, application_url, |
90 shell::mojom::Shell::kInvalidApplicationID, std::move(remote_services), | 90 shell::mojom::Shell::kInvalidApplicationID, std::move(remote_services), |
91 std::move(local_request), allowed)); | 91 std::move(local_request), allowed)); |
92 shell_->ConnectToApplication(std::move(request), | 92 shell_->ConnectToApplication(std::move(request), |
93 std::move(remote_services_proxy), | 93 std::move(remote_services_proxy), |
94 std::move(local_services), params->TakeFilter(), | 94 std::move(local_services), params->TakeFilter(), |
95 registry->GetConnectToApplicationCallback()); | 95 registry->GetConnectToApplicationCallback()); |
96 return std::move(registry); | 96 return std::move(registry); |
97 } | 97 } |
98 | 98 |
99 void ApplicationImpl::Quit() { | 99 void ShellConnection::Quit() { |
100 // We can't quit immediately, since there could be in-flight requests from the | 100 // We can't quit immediately, since there could be in-flight requests from the |
101 // shell. So check with it first. | 101 // shell. So check with it first. |
102 if (shell_) { | 102 if (shell_) { |
103 quit_requested_ = true; | 103 quit_requested_ = true; |
104 shell_->QuitApplication(); | 104 shell_->QuitApplication(); |
105 } else { | 105 } else { |
106 QuitNow(); | 106 QuitNow(); |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 scoped_ptr<AppRefCount> ApplicationImpl::CreateAppRefCount() { | 110 scoped_ptr<AppRefCount> ShellConnection::CreateAppRefCount() { |
111 return app_lifetime_helper_.CreateAppRefCount(); | 111 return app_lifetime_helper_.CreateAppRefCount(); |
112 } | 112 } |
113 | 113 |
114 void ApplicationImpl::Initialize(shell::mojom::ShellPtr shell, | 114 void ShellConnection::Initialize(shell::mojom::ShellPtr shell, |
115 const mojo::String& url, | 115 const mojo::String& url, |
116 uint32_t id) { | 116 uint32_t id) { |
117 shell_ = std::move(shell); | 117 shell_ = std::move(shell); |
118 shell_.set_connection_error_handler([this]() { OnConnectionError(); }); | 118 shell_.set_connection_error_handler([this]() { OnConnectionError(); }); |
119 client_->Initialize(this, url, id); | 119 client_->Initialize(this, url, id); |
120 } | 120 } |
121 | 121 |
122 void ApplicationImpl::AcceptConnection( | 122 void ShellConnection::AcceptConnection( |
123 const String& requestor_url, | 123 const String& requestor_url, |
124 uint32_t requestor_id, | 124 uint32_t requestor_id, |
125 InterfaceRequest<ServiceProvider> services, | 125 InterfaceRequest<ServiceProvider> services, |
126 ServiceProviderPtr exposed_services, | 126 ServiceProviderPtr exposed_services, |
127 Array<String> allowed_interfaces, | 127 Array<String> allowed_interfaces, |
128 const String& url) { | 128 const String& url) { |
129 scoped_ptr<Connection> registry(new internal::ConnectionImpl( | 129 scoped_ptr<Connection> registry(new internal::ConnectionImpl( |
130 url, requestor_url, requestor_id, std::move(exposed_services), | 130 url, requestor_url, requestor_id, std::move(exposed_services), |
131 std::move(services), allowed_interfaces.To<std::set<std::string>>())); | 131 std::move(services), allowed_interfaces.To<std::set<std::string>>())); |
132 if (!client_->AcceptConnection(registry.get())) | 132 if (!client_->AcceptConnection(registry.get())) |
133 return; | 133 return; |
134 | 134 |
135 // If we were quitting because we thought there were no more services for this | 135 // If we were quitting because we thought there were no more services for this |
136 // app in use, then that has changed so cancel the quit request. | 136 // app in use, then that has changed so cancel the quit request. |
137 if (quit_requested_) | 137 if (quit_requested_) |
138 quit_requested_ = false; | 138 quit_requested_ = false; |
139 | 139 |
140 incoming_connections_.push_back(std::move(registry)); | 140 incoming_connections_.push_back(std::move(registry)); |
141 } | 141 } |
142 | 142 |
143 void ApplicationImpl::OnQuitRequested(const Callback<void(bool)>& callback) { | 143 void ShellConnection::OnQuitRequested(const Callback<void(bool)>& callback) { |
144 // If by the time we got the reply from the shell, more requests had come in | 144 // If by the time we got the reply from the shell, more requests had come in |
145 // then we don't want to quit the app anymore so we return false. Otherwise | 145 // then we don't want to quit the app anymore so we return false. Otherwise |
146 // |quit_requested_| is true so we tell the shell to proceed with the quit. | 146 // |quit_requested_| is true so we tell the shell to proceed with the quit. |
147 callback.Run(quit_requested_); | 147 callback.Run(quit_requested_); |
148 if (quit_requested_) | 148 if (quit_requested_) |
149 QuitNow(); | 149 QuitNow(); |
150 } | 150 } |
151 | 151 |
152 void ApplicationImpl::OnConnectionError() { | 152 void ShellConnection::OnConnectionError() { |
153 base::WeakPtr<ApplicationImpl> ptr(weak_factory_.GetWeakPtr()); | 153 base::WeakPtr<ShellConnection> ptr(weak_factory_.GetWeakPtr()); |
154 | 154 |
155 // We give the client notice first, since it might want to do something on | 155 // We give the client notice first, since it might want to do something on |
156 // shell connection errors other than immediate termination of the run | 156 // shell connection errors other than immediate termination of the run |
157 // loop. The application might want to continue servicing connections other | 157 // loop. The application might want to continue servicing connections other |
158 // than the one to the shell. | 158 // than the one to the shell. |
159 bool quit_now = client_->ShellConnectionLost(); | 159 bool quit_now = client_->ShellConnectionLost(); |
160 if (quit_now) | 160 if (quit_now) |
161 QuitNow(); | 161 QuitNow(); |
162 if (!ptr) | 162 if (!ptr) |
163 return; | 163 return; |
164 shell_ = nullptr; | 164 shell_ = nullptr; |
165 } | 165 } |
166 | 166 |
167 void ApplicationImpl::QuitNow() { | 167 void ShellConnection::QuitNow() { |
168 client_->Quit(); | 168 client_->Quit(); |
169 termination_closure_.Run(); | 169 termination_closure_.Run(); |
170 } | 170 } |
171 | 171 |
172 void ApplicationImpl::UnbindConnections( | 172 void ShellConnection::UnbindConnections( |
173 InterfaceRequest<shell::mojom::Application>* application_request, | 173 InterfaceRequest<shell::mojom::ShellClient>* request, |
174 shell::mojom::ShellPtr* shell) { | 174 shell::mojom::ShellPtr* shell) { |
175 *application_request = binding_.Unbind(); | 175 *request = binding_.Unbind(); |
176 shell->Bind(shell_.PassInterface()); | 176 shell->Bind(shell_.PassInterface()); |
177 } | 177 } |
178 | 178 |
179 shell::mojom::CapabilityFilterPtr CreatePermissiveCapabilityFilter() { | 179 shell::mojom::CapabilityFilterPtr CreatePermissiveCapabilityFilter() { |
180 shell::mojom::CapabilityFilterPtr filter( | 180 shell::mojom::CapabilityFilterPtr filter( |
181 shell::mojom::CapabilityFilter::New()); | 181 shell::mojom::CapabilityFilter::New()); |
182 Array<String> all_interfaces; | 182 Array<String> all_interfaces; |
183 all_interfaces.push_back("*"); | 183 all_interfaces.push_back("*"); |
184 filter->filter.insert("*", std::move(all_interfaces)); | 184 filter->filter.insert("*", std::move(all_interfaces)); |
185 return filter; | 185 return filter; |
186 } | 186 } |
187 | 187 |
188 } // namespace mojo | 188 } // namespace mojo |
OLD | NEW |