application_impl.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "mojo/public/cpp/application/application_impl.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "mojo/public/cpp/application/application_delegate.h"
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "mojo/public/cpp/application/lib/service_registry.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "mojo/public/cpp/bindings/interface_ptr.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace mojo {
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ApplicationImpl::ShellPtrWatcher::ShellPtrWatcher(ApplicationImpl* impl)
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    : impl_(impl) {}
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ApplicationImpl::ShellPtrWatcher::~ShellPtrWatcher() {}
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ApplicationImpl::ShellPtrWatcher::OnConnectionError() {
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  impl_->OnShellError();
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
22116680a4aac90f2aa7413d9095a592090648e557Ben MurdochApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 ScopedMessagePipeHandle shell_handle)
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    : delegate_(delegate), shell_watch_(this) {
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  BindShell(shell_handle.Pass());
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
28116680a4aac90f2aa7413d9095a592090648e557Ben MurdochApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 MojoHandle shell_handle)
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    : delegate_(delegate), shell_watch_(this) {
3103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  BindShell(MakeScopedHandle(MessagePipeHandle(shell_handle)));
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ApplicationImpl::ClearConnections() {
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (ServiceRegistryList::iterator i(incoming_service_registries_.begin());
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      i != incoming_service_registries_.end(); ++i)
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    delete *i;
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin());
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      i != outgoing_service_registries_.end(); ++i)
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    delete *i;
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  incoming_service_registries_.clear();
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  outgoing_service_registries_.clear();
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ApplicationImpl::~ApplicationImpl() {
465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ClearConnections();
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
49116680a4aac90f2aa7413d9095a592090648e557Ben MurdochApplicationConnection* ApplicationImpl::ConnectToApplication(
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const String& application_url) {
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ServiceProviderPtr out_service_provider;
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  shell_->ConnectToApplication(application_url, Get(&out_service_provider));
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  internal::ServiceRegistry* registry = new internal::ServiceRegistry(
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      this,
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      application_url,
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      out_service_provider.Pass());
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!delegate_->ConfigureOutgoingConnection(registry)) {
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    delete registry;
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return NULL;
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  outgoing_service_registries_.push_back(registry);
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return registry;
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) {
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  shell_.Bind(shell_handle.Pass());
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  shell_.set_client(this);
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  shell_.set_error_handler(&shell_watch_);
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  delegate_->Initialize(this);
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ApplicationImpl::AcceptConnection(const String& requestor_url,
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                       ServiceProviderPtr service_provider) {
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  internal::ServiceRegistry* registry = new internal::ServiceRegistry(
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      this, requestor_url, service_provider.Pass());
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!delegate_->ConfigureIncomingConnection(registry)) {
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    delete registry;
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  incoming_service_registries_.push_back(registry);
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace mojo
84