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"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/public/cpp/environment/logging.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace mojo {
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass ApplicationImpl::ShellPtrWatcher : public ErrorHandler {
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci public:
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ShellPtrWatcher(ApplicationImpl* impl)
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    : impl_(impl) {}
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual ~ShellPtrWatcher() {}
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void OnConnectionError() MOJO_OVERRIDE {
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    impl_->OnShellError();
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci private:
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ApplicationImpl* impl_;
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher);
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci};
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
30116680a4aac90f2aa7413d9095a592090648e557Ben MurdochApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 ScopedMessagePipeHandle shell_handle)
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : initialized_(false), delegate_(delegate), shell_watch_(NULL) {
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  BindShell(shell_handle.Pass());
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
36116680a4aac90f2aa7413d9095a592090648e557Ben MurdochApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 MojoHandle shell_handle)
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : initialized_(false), delegate_(delegate), shell_watch_(NULL) {
3903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  BindShell(MakeScopedHandle(MessagePipeHandle(shell_handle)));
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ApplicationImpl::ClearConnections() {
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (ServiceRegistryList::iterator i(incoming_service_registries_.begin());
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      i != incoming_service_registries_.end(); ++i)
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    delete *i;
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin());
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      i != outgoing_service_registries_.end(); ++i)
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    delete *i;
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  incoming_service_registries_.clear();
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  outgoing_service_registries_.clear();
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)ApplicationImpl::~ApplicationImpl() {
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ClearConnections();
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  delete shell_watch_;
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ApplicationImpl::Initialize(Array<String> args) {
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  MOJO_CHECK(!initialized_);
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  initialized_ = true;
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  args_ = args.Pass();
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  delegate_->Initialize(this);
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
65116680a4aac90f2aa7413d9095a592090648e557Ben MurdochApplicationConnection* ApplicationImpl::ConnectToApplication(
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const String& application_url) {
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  MOJO_CHECK(initialized_);
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ServiceProviderPtr out_service_provider;
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  shell_->ConnectToApplication(application_url, Get(&out_service_provider));
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  internal::ServiceRegistry* registry = new internal::ServiceRegistry(
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      this,
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      application_url,
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      out_service_provider.Pass());
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!delegate_->ConfigureOutgoingConnection(registry)) {
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    delete registry;
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return NULL;
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  outgoing_service_registries_.push_back(registry);
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return registry;
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) {
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  shell_watch_ = new ShellPtrWatcher(this);
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  shell_.Bind(shell_handle.Pass());
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  shell_.set_client(this);
861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  shell_.set_error_handler(shell_watch_);
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ApplicationImpl::AcceptConnection(const String& requestor_url,
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                       ServiceProviderPtr service_provider) {
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  internal::ServiceRegistry* registry = new internal::ServiceRegistry(
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      this, requestor_url, service_provider.Pass());
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!delegate_->ConfigureIncomingConnection(registry)) {
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    delete registry;
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  incoming_service_registries_.push_back(registry);
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace mojo
101