Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / mojo / application / public / cpp / lib / application_impl.cc
blob62797030d395b929ef7c7b6bd7ca45eb82fb72d2
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
3 // found in the LICENSE file.
5 #include "mojo/application/public/cpp/application_impl.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/message_loop/message_loop.h"
11 #include "mojo/application/public/cpp/application_delegate.h"
12 #include "mojo/application/public/cpp/lib/service_registry.h"
13 #include "mojo/public/cpp/bindings/interface_ptr.h"
14 #include "mojo/public/cpp/environment/logging.h"
16 namespace mojo {
18 namespace {
20 void DefaultTerminationClosure() {
21 if (base::MessageLoop::current() &&
22 base::MessageLoop::current()->is_running())
23 base::MessageLoop::current()->Quit();
26 } // namespace
28 // TODO(beng): upstream this into mojo repo, array.h
29 template <typename E, typename T>
30 struct TypeConverter<std::set<E>, Array<T>> {
31 static std::set<E> Convert(const Array<T>& input) {
32 std::set<E> result;
33 if (!input.is_null()) {
34 for (size_t i = 0; i < input.size(); ++i)
35 result.insert(TypeConverter<E, T>::Convert(input[i]));
37 return result;
41 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
42 InterfaceRequest<Application> request)
43 : ApplicationImpl(delegate, request.Pass(),
44 base::Bind(&DefaultTerminationClosure)) {
47 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
48 InterfaceRequest<Application> request,
49 const Closure& termination_closure)
50 : delegate_(delegate),
51 binding_(this, request.Pass()),
52 termination_closure_(termination_closure),
53 app_lifetime_helper_(this),
54 quit_requested_(false),
55 weak_factory_(this) {}
57 ApplicationImpl::~ApplicationImpl() {
58 app_lifetime_helper_.OnQuit();
61 scoped_ptr<ApplicationConnection> ApplicationImpl::ConnectToApplication(
62 URLRequestPtr request) {
63 return ConnectToApplicationWithCapabilityFilter(request.Pass(), nullptr);
66 scoped_ptr<ApplicationConnection>
67 ApplicationImpl::ConnectToApplicationWithCapabilityFilter(
68 URLRequestPtr request,
69 CapabilityFilterPtr filter) {
70 if (!shell_)
71 return nullptr;
72 ServiceProviderPtr local_services;
73 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services);
74 ServiceProviderPtr remote_services;
75 std::string application_url = request->url.To<std::string>();
76 shell_->ConnectToApplication(request.Pass(), GetProxy(&remote_services),
77 local_services.Pass(), filter.Pass());
78 // We allow all interfaces on outgoing connections since we are presumably in
79 // a position to know who we're talking to.
80 // TODO(beng): is this a valid assumption or do we need to figure some way to
81 // filter here too?
82 std::set<std::string> allowed;
83 allowed.insert("*");
84 scoped_ptr<ApplicationConnection> registry(new internal::ServiceRegistry(
85 application_url, application_url, remote_services.Pass(),
86 local_request.Pass(), allowed));
87 if (!delegate_->ConfigureOutgoingConnection(registry.get()))
88 return nullptr;
89 return registry.Pass();
92 void ApplicationImpl::Initialize(ShellPtr shell, const mojo::String& url) {
93 shell_ = shell.Pass();
94 shell_.set_connection_error_handler([this]() { OnConnectionError(); });
95 url_ = url;
96 delegate_->Initialize(this);
99 void ApplicationImpl::Quit() {
100 // We can't quit immediately, since there could be in-flight requests from the
101 // shell. So check with it first.
102 if (shell_) {
103 quit_requested_ = true;
104 shell_->QuitApplication();
105 } else {
106 QuitNow();
110 void ApplicationImpl::AcceptConnection(
111 const String& requestor_url,
112 InterfaceRequest<ServiceProvider> services,
113 ServiceProviderPtr exposed_services,
114 Array<String> allowed_interfaces,
115 const String& url) {
116 scoped_ptr<ApplicationConnection> registry(new internal::ServiceRegistry(
117 url, requestor_url, exposed_services.Pass(), services.Pass(),
118 allowed_interfaces.To<std::set<std::string>>()));
119 if (!delegate_->ConfigureIncomingConnection(registry.get()))
120 return;
122 // If we were quitting because we thought there were no more services for this
123 // app in use, then that has changed so cancel the quit request.
124 if (quit_requested_)
125 quit_requested_ = false;
127 incoming_connections_.push_back(registry.Pass());
130 void ApplicationImpl::OnQuitRequested(const Callback<void(bool)>& callback) {
131 // If by the time we got the reply from the shell, more requests had come in
132 // then we don't want to quit the app anymore so we return false. Otherwise
133 // |quit_requested_| is true so we tell the shell to proceed with the quit.
134 callback.Run(quit_requested_);
135 if (quit_requested_)
136 QuitNow();
139 void ApplicationImpl::OnConnectionError() {
140 base::WeakPtr<ApplicationImpl> ptr(weak_factory_.GetWeakPtr());
142 // We give the delegate notice first, since it might want to do something on
143 // shell connection errors other than immediate termination of the run
144 // loop. The application might want to continue servicing connections other
145 // than the one to the shell.
146 bool quit_now = delegate_->OnShellConnectionError();
147 if (quit_now)
148 QuitNow();
149 if (!ptr)
150 return;
151 shell_ = nullptr;
154 void ApplicationImpl::QuitNow() {
155 delegate_->Quit();
156 termination_closure_.Run();
159 void ApplicationImpl::UnbindConnections(
160 InterfaceRequest<Application>* application_request,
161 ShellPtr* shell) {
162 *application_request = binding_.Unbind();
163 shell->Bind(shell_.PassInterface());
166 } // namespace mojo