application_impl.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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.
4
5#include "mojo/public/cpp/application/application_impl.h"
6
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"
10
11namespace mojo {
12
13ApplicationImpl::ShellPtrWatcher::ShellPtrWatcher(ApplicationImpl* impl)
14    : impl_(impl) {}
15
16ApplicationImpl::ShellPtrWatcher::~ShellPtrWatcher() {}
17
18void ApplicationImpl::ShellPtrWatcher::OnConnectionError() {
19  impl_->OnShellError();
20}
21
22ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
23                                 ScopedMessagePipeHandle shell_handle)
24    : delegate_(delegate), shell_watch_(this) {
25  BindShell(shell_handle.Pass());
26}
27
28ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
29                                 MojoHandle shell_handle)
30    : delegate_(delegate), shell_watch_(this) {
31  BindShell(MakeScopedHandle(MessagePipeHandle(shell_handle)));
32}
33
34void ApplicationImpl::ClearConnections() {
35  for (ServiceRegistryList::iterator i(incoming_service_registries_.begin());
36      i != incoming_service_registries_.end(); ++i)
37    delete *i;
38  for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin());
39      i != outgoing_service_registries_.end(); ++i)
40    delete *i;
41  incoming_service_registries_.clear();
42  outgoing_service_registries_.clear();
43}
44
45ApplicationImpl::~ApplicationImpl() {
46  ClearConnections();
47}
48
49ApplicationConnection* ApplicationImpl::ConnectToApplication(
50    const String& application_url) {
51  ServiceProviderPtr out_service_provider;
52  shell_->ConnectToApplication(application_url, Get(&out_service_provider));
53  internal::ServiceRegistry* registry = new internal::ServiceRegistry(
54      this,
55      application_url,
56      out_service_provider.Pass());
57  if (!delegate_->ConfigureOutgoingConnection(registry)) {
58    delete registry;
59    return NULL;
60  }
61  outgoing_service_registries_.push_back(registry);
62  return registry;
63}
64
65void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) {
66  shell_.Bind(shell_handle.Pass());
67  shell_.set_client(this);
68  shell_.set_error_handler(&shell_watch_);
69  delegate_->Initialize(this);
70}
71
72void ApplicationImpl::AcceptConnection(const String& requestor_url,
73                                       ServiceProviderPtr service_provider) {
74  internal::ServiceRegistry* registry = new internal::ServiceRegistry(
75      this, requestor_url, service_provider.Pass());
76  if (!delegate_->ConfigureIncomingConnection(registry)) {
77    delete registry;
78    return;
79  }
80  incoming_service_registries_.push_back(registry);
81}
82
83}  // namespace mojo
84