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"
10 #include "mojo/public/cpp/environment/logging.h"
14 class ApplicationImpl::ShellPtrWatcher
: public ErrorHandler
{
16 ShellPtrWatcher(ApplicationImpl
* impl
) : impl_(impl
) {}
18 virtual ~ShellPtrWatcher() {}
20 virtual void OnConnectionError() override
{ impl_
->OnShellError(); }
23 ApplicationImpl
* impl_
;
24 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher
);
27 ApplicationImpl::ApplicationImpl(ApplicationDelegate
* delegate
,
28 ScopedMessagePipeHandle shell_handle
)
29 : initialized_(false), delegate_(delegate
), shell_watch_(nullptr) {
30 BindShell(shell_handle
.Pass());
33 ApplicationImpl::ApplicationImpl(ApplicationDelegate
* delegate
,
34 MojoHandle shell_handle
)
35 : initialized_(false), delegate_(delegate
), shell_watch_(nullptr) {
36 BindShell(MakeScopedHandle(MessagePipeHandle(shell_handle
)));
39 void ApplicationImpl::ClearConnections() {
40 for (ServiceRegistryList::iterator
i(incoming_service_registries_
.begin());
41 i
!= incoming_service_registries_
.end();
44 for (ServiceRegistryList::iterator
i(outgoing_service_registries_
.begin());
45 i
!= outgoing_service_registries_
.end();
48 incoming_service_registries_
.clear();
49 outgoing_service_registries_
.clear();
52 ApplicationImpl::~ApplicationImpl() {
57 void ApplicationImpl::Initialize(Array
<String
> args
) {
58 MOJO_CHECK(!initialized_
);
60 args_
= args
.To
<std::vector
<std::string
>>();
61 delegate_
->Initialize(this);
64 ApplicationConnection
* ApplicationImpl::ConnectToApplication(
65 const String
& application_url
) {
66 MOJO_CHECK(initialized_
);
67 ServiceProviderPtr out_service_provider
;
68 shell_
->ConnectToApplication(application_url
,
69 GetProxy(&out_service_provider
));
70 internal::ServiceRegistry
* registry
= new internal::ServiceRegistry(
71 this, application_url
, out_service_provider
.Pass());
72 if (!delegate_
->ConfigureOutgoingConnection(registry
)) {
76 outgoing_service_registries_
.push_back(registry
);
80 bool ApplicationImpl::WaitForInitialize() {
81 MOJO_CHECK(!initialized_
);
82 bool result
= shell_
.WaitForIncomingMethodCall();
83 MOJO_CHECK(initialized_
|| !result
);
87 void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle
) {
88 shell_watch_
= new ShellPtrWatcher(this);
89 shell_
.Bind(shell_handle
.Pass());
90 shell_
.set_client(this);
91 shell_
.set_error_handler(shell_watch_
);
94 void ApplicationImpl::AcceptConnection(const String
& requestor_url
,
95 ServiceProviderPtr service_provider
) {
96 internal::ServiceRegistry
* registry
= new internal::ServiceRegistry(
97 this, requestor_url
, service_provider
.Pass());
98 if (!delegate_
->ConfigureIncomingConnection(registry
)) {
102 incoming_service_registries_
.push_back(registry
);