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 #include "mojo/public/cpp/environment/logging.h" | 10 #include "mojo/public/cpp/environment/logging.h" |
11 | 11 |
12 namespace mojo { | 12 namespace mojo { |
13 | 13 |
14 class ApplicationImpl::ShellPtrWatcher : public ErrorHandler { | 14 class ApplicationImpl::ShellPtrWatcher : public ErrorHandler { |
15 public: | 15 public: |
16 ShellPtrWatcher(ApplicationImpl* impl) : impl_(impl) {} | 16 ShellPtrWatcher(ApplicationImpl* impl) : impl_(impl) {} |
17 | 17 |
18 ~ShellPtrWatcher() override {} | 18 ~ShellPtrWatcher() override {} |
19 | 19 |
20 void OnConnectionError() override { impl_->OnShellError(); } | 20 void OnConnectionError() override { impl_->OnShellError(); } |
21 | 21 |
22 private: | 22 private: |
23 ApplicationImpl* impl_; | 23 ApplicationImpl* impl_; |
24 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher); | 24 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher); |
25 }; | 25 }; |
26 | 26 |
27 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, ShellPtr shell) | 27 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, |
| 28 InterfaceRequest<Application> request) |
28 : initialized_(false), | 29 : initialized_(false), |
29 delegate_(delegate), | 30 delegate_(delegate), |
30 shell_(shell.Pass()), | 31 binding_(this, request.Pass()), |
31 shell_watch_(new ShellPtrWatcher(this)) { | 32 shell_watch_(nullptr) { |
32 shell_.set_client(this); | |
33 shell_.set_error_handler(shell_watch_); | |
34 } | 33 } |
35 | 34 |
36 bool ApplicationImpl::HasArg(const std::string& arg) const { | 35 bool ApplicationImpl::HasArg(const std::string& arg) const { |
37 return std::find(args_.begin(), args_.end(), arg) != args_.end(); | 36 return std::find(args_.begin(), args_.end(), arg) != args_.end(); |
38 } | 37 } |
39 | 38 |
40 void ApplicationImpl::ClearConnections() { | 39 void ApplicationImpl::ClearConnections() { |
41 for (ServiceRegistryList::iterator i(incoming_service_registries_.begin()); | 40 for (ServiceRegistryList::iterator i(incoming_service_registries_.begin()); |
42 i != incoming_service_registries_.end(); | 41 i != incoming_service_registries_.end(); |
43 ++i) | 42 ++i) |
44 delete *i; | 43 delete *i; |
45 for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin()); | 44 for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin()); |
46 i != outgoing_service_registries_.end(); | 45 i != outgoing_service_registries_.end(); |
47 ++i) | 46 ++i) |
48 delete *i; | 47 delete *i; |
49 incoming_service_registries_.clear(); | 48 incoming_service_registries_.clear(); |
50 outgoing_service_registries_.clear(); | 49 outgoing_service_registries_.clear(); |
51 } | 50 } |
52 | 51 |
53 ApplicationImpl::~ApplicationImpl() { | 52 ApplicationImpl::~ApplicationImpl() { |
54 ClearConnections(); | 53 ClearConnections(); |
55 delete shell_watch_; | 54 delete shell_watch_; |
56 } | 55 } |
57 | 56 |
58 ApplicationConnection* ApplicationImpl::ConnectToApplication( | 57 ApplicationConnection* ApplicationImpl::ConnectToApplication( |
59 const String& application_url) { | 58 const String& application_url) { |
60 MOJO_CHECK(initialized_); | 59 MOJO_CHECK(shell_); |
61 ServiceProviderPtr local_services; | 60 ServiceProviderPtr local_services; |
62 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); | 61 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); |
63 ServiceProviderPtr remote_services; | 62 ServiceProviderPtr remote_services; |
64 shell_->ConnectToApplication(application_url, GetProxy(&remote_services), | 63 shell_->ConnectToApplication(application_url, GetProxy(&remote_services), |
65 local_services.Pass()); | 64 local_services.Pass()); |
66 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 65 internal::ServiceRegistry* registry = new internal::ServiceRegistry( |
67 this, application_url, remote_services.Pass(), local_request.Pass()); | 66 this, application_url, remote_services.Pass(), local_request.Pass()); |
68 if (!delegate_->ConfigureOutgoingConnection(registry)) { | 67 if (!delegate_->ConfigureOutgoingConnection(registry)) { |
69 delete registry; | 68 delete registry; |
70 return nullptr; | 69 return nullptr; |
71 } | 70 } |
72 outgoing_service_registries_.push_back(registry); | 71 outgoing_service_registries_.push_back(registry); |
73 return registry; | 72 return registry; |
74 } | 73 } |
75 | 74 |
76 ShellPtr ApplicationImpl::UnbindShell() { | 75 void ApplicationImpl::Initialize(ShellPtr shell, Array<String> args) { |
77 MOJO_CHECK(shell_); | 76 shell_ = shell.Pass(); |
78 ShellPtr unbound_shell; | 77 shell_watch_ = new ShellPtrWatcher(this); |
79 unbound_shell.Bind(shell_.PassMessagePipe()); | 78 shell_.set_error_handler(shell_watch_); |
80 return unbound_shell.Pass(); | 79 args_ = args.To<std::vector<std::string>>(); |
| 80 delegate_->Initialize(this); |
81 } | 81 } |
82 | 82 |
83 void ApplicationImpl::Initialize(Array<String> args) { | 83 void ApplicationImpl::WaitForInitialize() { |
84 MOJO_CHECK(!initialized_); | 84 if (!shell_) |
85 initialized_ = true; | 85 binding_.WaitForIncomingMethodCall(); |
86 args_ = args.To<std::vector<std::string>>(); | 86 } |
87 delegate_->Initialize(this); | 87 |
| 88 void ApplicationImpl::UnbindConnections( |
| 89 InterfaceRequest<Application>* application_request, |
| 90 ShellPtr* shell) { |
| 91 *application_request = binding_.Unbind(); |
| 92 shell->Bind(shell_.PassMessagePipe()); |
88 } | 93 } |
89 | 94 |
90 void ApplicationImpl::AcceptConnection( | 95 void ApplicationImpl::AcceptConnection( |
91 const String& requestor_url, | 96 const String& requestor_url, |
92 InterfaceRequest<ServiceProvider> services, | 97 InterfaceRequest<ServiceProvider> services, |
93 ServiceProviderPtr exposed_services) { | 98 ServiceProviderPtr exposed_services) { |
94 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 99 internal::ServiceRegistry* registry = new internal::ServiceRegistry( |
95 this, requestor_url, exposed_services.Pass(), services.Pass()); | 100 this, requestor_url, exposed_services.Pass(), services.Pass()); |
96 if (!delegate_->ConfigureIncomingConnection(registry)) { | 101 if (!delegate_->ConfigureIncomingConnection(registry)) { |
97 delete registry; | 102 delete registry; |
98 return; | 103 return; |
99 } | 104 } |
100 incoming_service_registries_.push_back(registry); | 105 incoming_service_registries_.push_back(registry); |
101 } | 106 } |
102 | 107 |
103 void ApplicationImpl::RequestQuit() { | 108 void ApplicationImpl::RequestQuit() { |
104 Terminate(); | 109 Terminate(); |
105 } | 110 } |
106 | 111 |
107 } // namespace mojo | 112 } // namespace mojo |
OLD | NEW |