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)
19 virtual ~ShellPtrWatcher() {}
21 virtual void OnConnectionError() MOJO_OVERRIDE {
22 impl_->OnShellError();
26 ApplicationImpl* impl_;
27 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher);
30 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
31 ScopedMessagePipeHandle shell_handle)
32 : initialized_(false), delegate_(delegate), shell_watch_(NULL) {
33 BindShell(shell_handle.Pass());
36 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
37 MojoHandle shell_handle)
38 : initialized_(false), delegate_(delegate), shell_watch_(NULL) {
39 BindShell(MakeScopedHandle(MessagePipeHandle(shell_handle)));
42 void ApplicationImpl::ClearConnections() {
43 for (ServiceRegistryList::iterator i(incoming_service_registries_.begin());
44 i != incoming_service_registries_.end(); ++i)
46 for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin());
47 i != outgoing_service_registries_.end(); ++i)
49 incoming_service_registries_.clear();
50 outgoing_service_registries_.clear();
53 ApplicationImpl::~ApplicationImpl() {
58 void ApplicationImpl::Initialize(Array<String> args) {
59 MOJO_CHECK(!initialized_);
62 delegate_->Initialize(this);
65 ApplicationConnection* ApplicationImpl::ConnectToApplication(
66 const String& application_url) {
67 MOJO_CHECK(initialized_);
68 ServiceProviderPtr out_service_provider;
69 shell_->ConnectToApplication(application_url, Get(&out_service_provider));
70 internal::ServiceRegistry* registry = new internal::ServiceRegistry(
73 out_service_provider.Pass());
74 if (!delegate_->ConfigureOutgoingConnection(registry)) {
78 outgoing_service_registries_.push_back(registry);
82 void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) {
83 shell_watch_ = new ShellPtrWatcher(this);
84 shell_.Bind(shell_handle.Pass());
85 shell_.set_client(this);
86 shell_.set_error_handler(shell_watch_);
89 void ApplicationImpl::AcceptConnection(const String& requestor_url,
90 ServiceProviderPtr service_provider) {
91 internal::ServiceRegistry* registry = new internal::ServiceRegistry(
92 this, requestor_url, service_provider.Pass());
93 if (!delegate_->ConfigureIncomingConnection(registry)) {
97 incoming_service_registries_.push_back(registry);