OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/public/cpp/application/application_impl.h" | 5 #include "mojo/public/cpp/application/application_impl.h" |
6 | 6 |
7 #include "mojo/public/cpp/application/application_delegate.h" | 7 #include "mojo/public/cpp/application/application_delegate.h" |
8 #include "mojo/public/cpp/application/lib/service_registry.h" | 8 #include "mojo/public/cpp/application/lib/service_registry.h" |
9 #include "mojo/public/cpp/bindings/interface_ptr.h" | 9 #include "mojo/public/cpp/bindings/interface_ptr.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 | 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) | 13 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate) |
23 : delegate_(delegate), shell_watch_(this) {} | 14 : delegate_(delegate) {} |
24 | 15 |
25 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | 16 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, |
26 ScopedMessagePipeHandle shell_handle) | 17 ScopedMessagePipeHandle shell_handle) |
27 : delegate_(delegate), shell_watch_(this) { | 18 : delegate_(delegate) { |
28 BindShell(shell_handle.Pass()); | 19 BindShell(shell_handle.Pass()); |
29 } | 20 } |
30 | 21 |
31 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | 22 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, |
32 MojoHandle shell_handle) | 23 MojoHandle shell_handle) |
33 : delegate_(delegate), shell_watch_(this) { | 24 : delegate_(delegate) { |
34 BindShell(shell_handle); | 25 BindShell(shell_handle); |
35 } | 26 } |
36 | 27 |
37 void ApplicationImpl::ClearConnections() { | 28 ApplicationImpl::~ApplicationImpl() { |
38 for (ServiceRegistryList::iterator i(incoming_service_registries_.begin()); | 29 for (ServiceRegistryList::iterator i(incoming_service_registries_.begin()); |
39 i != incoming_service_registries_.end(); ++i) | 30 i != incoming_service_registries_.end(); ++i) |
40 delete *i; | 31 delete *i; |
41 for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin()); | 32 for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin()); |
42 i != outgoing_service_registries_.end(); ++i) | 33 i != outgoing_service_registries_.end(); ++i) |
43 delete *i; | 34 delete *i; |
44 incoming_service_registries_.clear(); | |
45 outgoing_service_registries_.clear(); | |
46 } | |
47 | |
48 ApplicationImpl::~ApplicationImpl() { | |
49 ClearConnections(); | |
50 } | 35 } |
51 | 36 |
52 ApplicationConnection* ApplicationImpl::ConnectToApplication( | 37 ApplicationConnection* ApplicationImpl::ConnectToApplication( |
53 const String& application_url) { | 38 const String& application_url) { |
54 ServiceProviderPtr out_service_provider; | 39 ServiceProviderPtr out_service_provider; |
55 shell_->ConnectToApplication(application_url, Get(&out_service_provider)); | 40 shell_->ConnectToApplication(application_url, Get(&out_service_provider)); |
56 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 41 internal::ServiceRegistry* registry = new internal::ServiceRegistry( |
57 this, | 42 this, |
58 application_url, | 43 application_url, |
59 out_service_provider.Pass()); | 44 out_service_provider.Pass()); |
60 if (!delegate_->ConfigureOutgoingConnection(registry)) { | 45 if (!delegate_->ConfigureOutgoingConnection(registry)) { |
61 delete registry; | 46 delete registry; |
62 return NULL; | 47 return NULL; |
63 } | 48 } |
64 outgoing_service_registries_.push_back(registry); | 49 outgoing_service_registries_.push_back(registry); |
65 return registry; | 50 return registry; |
66 } | 51 } |
67 | 52 |
68 void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) { | 53 void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) { |
69 shell_.Bind(shell_handle.Pass()); | 54 shell_.Bind(shell_handle.Pass()); |
70 shell_.set_client(this); | 55 shell_.set_client(this); |
71 shell_.set_error_handler(&shell_watch_); | |
72 delegate_->Initialize(this); | 56 delegate_->Initialize(this); |
73 } | 57 } |
74 | 58 |
75 void ApplicationImpl::BindShell(MojoHandle shell_handle) { | 59 void ApplicationImpl::BindShell(MojoHandle shell_handle) { |
76 BindShell(mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle))); | 60 BindShell(mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle))); |
77 } | 61 } |
78 | 62 |
79 void ApplicationImpl::AcceptConnection(const String& requestor_url, | 63 void ApplicationImpl::AcceptConnection(const String& requestor_url, |
80 ServiceProviderPtr service_provider) { | 64 ServiceProviderPtr service_provider) { |
81 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 65 internal::ServiceRegistry* registry = new internal::ServiceRegistry( |
82 this, requestor_url, service_provider.Pass()); | 66 this, requestor_url, service_provider.Pass()); |
83 if (!delegate_->ConfigureIncomingConnection(registry)) { | 67 if (!delegate_->ConfigureIncomingConnection(registry)) { |
84 delete registry; | 68 delete registry; |
85 return; | 69 return; |
86 } | 70 } |
87 incoming_service_registries_.push_back(registry); | 71 incoming_service_registries_.push_back(registry); |
88 } | 72 } |
89 | 73 |
90 } // namespace mojo | 74 } // namespace mojo |
OLD | NEW |