Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / application / lib / application_impl.cc
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
11 namespace mojo {
12
13 ApplicationImpl::ShellPtrWatcher::ShellPtrWatcher(ApplicationImpl* impl)
14     : impl_(impl) {}
15
16 ApplicationImpl::ShellPtrWatcher::~ShellPtrWatcher() {}
17
18 void ApplicationImpl::ShellPtrWatcher::OnConnectionError() {
19   impl_->OnShellError();
20 }
21
22 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate)
23     : delegate_(delegate), shell_watch_(this) {}
24
25 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
26                                  ScopedMessagePipeHandle shell_handle)
27     : delegate_(delegate), shell_watch_(this) {
28   BindShell(shell_handle.Pass());
29 }
30
31 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
32                                  MojoHandle shell_handle)
33     : delegate_(delegate), shell_watch_(this) {
34   BindShell(shell_handle);
35 }
36
37 void ApplicationImpl::ClearConnections() {
38   for (ServiceRegistryList::iterator i(incoming_service_registries_.begin());
39       i != incoming_service_registries_.end(); ++i)
40     delete *i;
41   for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin());
42       i != outgoing_service_registries_.end(); ++i)
43     delete *i;
44   incoming_service_registries_.clear();
45   outgoing_service_registries_.clear();
46 }
47
48 ApplicationImpl::~ApplicationImpl() {
49   ClearConnections();
50 }
51
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(
57       this,
58       application_url,
59       out_service_provider.Pass());
60   if (!delegate_->ConfigureOutgoingConnection(registry)) {
61     delete registry;
62     return NULL;
63   }
64   outgoing_service_registries_.push_back(registry);
65   return registry;
66 }
67
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);
73 }
74
75 void ApplicationImpl::BindShell(MojoHandle shell_handle) {
76   BindShell(mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)));
77 }
78
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)) {
84     delete registry;
85     return;
86   }
87   incoming_service_registries_.push_back(registry);
88 }
89
90 }  // namespace mojo