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/public/cpp/application/application_impl.h"
7 #include "mojo/public/cpp/application/application_delegate.h"
8 #include "mojo/public/cpp/application/lib/service_registry.h"
9 #include "mojo/public/cpp/bindings/interface_ptr.h"
13 ApplicationImpl::ShellPtrWatcher::ShellPtrWatcher(ApplicationImpl* impl)
16 ApplicationImpl::ShellPtrWatcher::~ShellPtrWatcher() {}
18 void ApplicationImpl::ShellPtrWatcher::OnConnectionError() {
19 impl_->OnShellError();
22 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate)
23 : delegate_(delegate), shell_watch_(this) {}
25 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
26 ScopedMessagePipeHandle shell_handle)
27 : delegate_(delegate), shell_watch_(this) {
28 BindShell(shell_handle.Pass());
31 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
32 MojoHandle shell_handle)
33 : delegate_(delegate), shell_watch_(this) {
34 BindShell(shell_handle);
37 void ApplicationImpl::ClearConnections() {
38 for (ServiceRegistryList::iterator i(incoming_service_registries_.begin());
39 i != incoming_service_registries_.end(); ++i)
41 for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin());
42 i != outgoing_service_registries_.end(); ++i)
44 incoming_service_registries_.clear();
45 outgoing_service_registries_.clear();
48 ApplicationImpl::~ApplicationImpl() {
52 ApplicationConnection* ApplicationImpl::ConnectToApplication(
53 const String& application_url) {
54 ServiceProviderPtr out_service_provider;
55 shell_->ConnectToApplication(application_url, Get(&out_service_provider));
56 internal::ServiceRegistry* registry = new internal::ServiceRegistry(
59 out_service_provider.Pass());
60 if (!delegate_->ConfigureOutgoingConnection(registry)) {
64 outgoing_service_registries_.push_back(registry);
68 void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) {
69 shell_.Bind(shell_handle.Pass());
70 shell_.set_client(this);
71 shell_.set_error_handler(&shell_watch_);
72 delegate_->Initialize(this);
75 void ApplicationImpl::BindShell(MojoHandle shell_handle) {
76 BindShell(mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)));
79 void ApplicationImpl::AcceptConnection(const String& requestor_url,
80 ServiceProviderPtr service_provider) {
81 internal::ServiceRegistry* registry = new internal::ServiceRegistry(
82 this, requestor_url, service_provider.Pass());
83 if (!delegate_->ConfigureIncomingConnection(registry)) {
87 incoming_service_registries_.push_back(registry);