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 | 11 |
11 namespace mojo { | 12 namespace mojo { |
12 | 13 |
13 class ApplicationImpl::ShellPtrWatcher : public ErrorHandler { | 14 class ApplicationImpl::ShellPtrWatcher : public ErrorHandler { |
14 public: | 15 public: |
15 ShellPtrWatcher(ApplicationImpl* impl) | 16 ShellPtrWatcher(ApplicationImpl* impl) |
16 : impl_(impl) {} | 17 : impl_(impl) {} |
17 | 18 |
18 virtual ~ShellPtrWatcher() {} | 19 virtual ~ShellPtrWatcher() {} |
19 | 20 |
20 virtual void OnConnectionError() MOJO_OVERRIDE { | 21 virtual void OnConnectionError() MOJO_OVERRIDE { |
21 impl_->OnShellError(); | 22 impl_->OnShellError(); |
22 } | 23 } |
23 | 24 |
24 private: | 25 private: |
25 ApplicationImpl* impl_; | 26 ApplicationImpl* impl_; |
26 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher); | 27 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellPtrWatcher); |
27 }; | 28 }; |
28 | 29 |
29 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | 30 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, |
30 ScopedMessagePipeHandle shell_handle) | 31 ScopedMessagePipeHandle shell_handle) |
31 : delegate_(delegate), shell_watch_(NULL) { | 32 : initialized_(false), delegate_(delegate), shell_watch_(NULL) { |
32 BindShell(shell_handle.Pass()); | 33 BindShell(shell_handle.Pass()); |
33 } | 34 } |
34 | 35 |
35 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | 36 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, |
36 MojoHandle shell_handle) | 37 MojoHandle shell_handle) |
37 : delegate_(delegate), shell_watch_(NULL) { | 38 : initialized_(false), delegate_(delegate), shell_watch_(NULL) { |
38 BindShell(MakeScopedHandle(MessagePipeHandle(shell_handle))); | 39 BindShell(MakeScopedHandle(MessagePipeHandle(shell_handle))); |
39 } | 40 } |
40 | 41 |
41 void ApplicationImpl::ClearConnections() { | 42 void ApplicationImpl::ClearConnections() { |
42 for (ServiceRegistryList::iterator i(incoming_service_registries_.begin()); | 43 for (ServiceRegistryList::iterator i(incoming_service_registries_.begin()); |
43 i != incoming_service_registries_.end(); ++i) | 44 i != incoming_service_registries_.end(); ++i) |
44 delete *i; | 45 delete *i; |
45 for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin()); | 46 for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin()); |
46 i != outgoing_service_registries_.end(); ++i) | 47 i != outgoing_service_registries_.end(); ++i) |
47 delete *i; | 48 delete *i; |
48 incoming_service_registries_.clear(); | 49 incoming_service_registries_.clear(); |
49 outgoing_service_registries_.clear(); | 50 outgoing_service_registries_.clear(); |
50 } | 51 } |
51 | 52 |
52 ApplicationImpl::~ApplicationImpl() { | 53 ApplicationImpl::~ApplicationImpl() { |
53 ClearConnections(); | 54 ClearConnections(); |
54 delete shell_watch_; | 55 delete shell_watch_; |
55 } | 56 } |
56 | 57 |
| 58 void ApplicationImpl::Initialize(Array<String> args) { |
| 59 MOJO_CHECK(!initialized_); |
| 60 initialized_ = true; |
| 61 args_ = args.Pass(); |
| 62 delegate_->Initialize(this); |
| 63 } |
| 64 |
57 ApplicationConnection* ApplicationImpl::ConnectToApplication( | 65 ApplicationConnection* ApplicationImpl::ConnectToApplication( |
58 const String& application_url) { | 66 const String& application_url) { |
| 67 MOJO_CHECK(initialized_); |
59 ServiceProviderPtr out_service_provider; | 68 ServiceProviderPtr out_service_provider; |
60 shell_->ConnectToApplication(application_url, Get(&out_service_provider)); | 69 shell_->ConnectToApplication(application_url, Get(&out_service_provider)); |
61 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 70 internal::ServiceRegistry* registry = new internal::ServiceRegistry( |
62 this, | 71 this, |
63 application_url, | 72 application_url, |
64 out_service_provider.Pass()); | 73 out_service_provider.Pass()); |
65 if (!delegate_->ConfigureOutgoingConnection(registry)) { | 74 if (!delegate_->ConfigureOutgoingConnection(registry)) { |
66 delete registry; | 75 delete registry; |
67 return NULL; | 76 return NULL; |
68 } | 77 } |
69 outgoing_service_registries_.push_back(registry); | 78 outgoing_service_registries_.push_back(registry); |
70 return registry; | 79 return registry; |
71 } | 80 } |
72 | 81 |
73 void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) { | 82 void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) { |
74 shell_watch_ = new ShellPtrWatcher(this); | 83 shell_watch_ = new ShellPtrWatcher(this); |
75 shell_.Bind(shell_handle.Pass()); | 84 shell_.Bind(shell_handle.Pass()); |
76 shell_.set_client(this); | 85 shell_.set_client(this); |
77 shell_.set_error_handler(shell_watch_); | 86 shell_.set_error_handler(shell_watch_); |
78 delegate_->Initialize(this); | |
79 } | 87 } |
80 | 88 |
81 void ApplicationImpl::AcceptConnection(const String& requestor_url, | 89 void ApplicationImpl::AcceptConnection(const String& requestor_url, |
82 ServiceProviderPtr service_provider) { | 90 ServiceProviderPtr service_provider) { |
83 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 91 internal::ServiceRegistry* registry = new internal::ServiceRegistry( |
84 this, requestor_url, service_provider.Pass()); | 92 this, requestor_url, service_provider.Pass()); |
85 if (!delegate_->ConfigureIncomingConnection(registry)) { | 93 if (!delegate_->ConfigureIncomingConnection(registry)) { |
86 delete registry; | 94 delete registry; |
87 return; | 95 return; |
88 } | 96 } |
89 incoming_service_registries_.push_back(registry); | 97 incoming_service_registries_.push_back(registry); |
90 } | 98 } |
91 | 99 |
92 } // namespace mojo | 100 } // namespace mojo |
OLD | NEW |