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