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 "services/shell/public/cpp/service_context.h" | 5 #include "services/shell/public/cpp/service_context.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "mojo/public/cpp/bindings/interface_ptr.h" | 10 #include "mojo/public/cpp/bindings/interface_ptr.h" |
11 #include "mojo/public/cpp/bindings/interface_request.h" | 11 #include "mojo/public/cpp/bindings/interface_request.h" |
12 #include "services/shell/public/cpp/capabilities.h" | 12 #include "services/shell/public/cpp/capabilities.h" |
| 13 #include "services/shell/public/cpp/connector.h" |
13 #include "services/shell/public/cpp/lib/connection_impl.h" | 14 #include "services/shell/public/cpp/lib/connection_impl.h" |
14 #include "services/shell/public/cpp/lib/connector_impl.h" | 15 #include "services/shell/public/cpp/lib/connector_impl.h" |
15 #include "services/shell/public/cpp/service.h" | 16 #include "services/shell/public/cpp/service.h" |
16 | 17 |
17 namespace shell { | 18 namespace shell { |
18 | 19 |
19 //////////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////////// |
20 // ServiceContext, public: | 21 // ServiceContext, public: |
21 | 22 |
22 ServiceContext::ServiceContext(shell::Service* service, | 23 ServiceContext::ServiceContext(shell::Service* client, |
23 mojom::ServiceRequest request, | 24 mojom::ServiceRequest request) |
24 std::unique_ptr<Connector> connector, | 25 : client_(client), binding_(this) { |
25 mojom::ConnectorRequest connector_request) | 26 mojom::ConnectorPtr connector; |
26 : pending_connector_request_(std::move(connector_request)), | 27 pending_connector_request_ = GetProxy(&connector); |
27 service_(service), | 28 connector_.reset(new ConnectorImpl(std::move(connector))); |
28 binding_(this, std::move(request)), | 29 |
29 connector_(std::move(connector)) { | 30 DCHECK(request.is_pending()); |
30 DCHECK(binding_.is_bound()); | 31 binding_.Bind(std::move(request)); |
31 if (!connector_) { | |
32 connector_ = Connector::Create(&pending_connector_request_); | |
33 } else { | |
34 DCHECK(pending_connector_request_.is_pending()); | |
35 } | |
36 } | 32 } |
37 | 33 |
38 ServiceContext::~ServiceContext() {} | 34 ServiceContext::~ServiceContext() {} |
39 | 35 |
| 36 void ServiceContext::set_initialize_handler(const base::Closure& callback) { |
| 37 initialize_handler_ = callback; |
| 38 } |
| 39 |
| 40 void ServiceContext::SetAppTestConnectorForTesting( |
| 41 mojom::ConnectorPtr connector) { |
| 42 pending_connector_request_ = nullptr; |
| 43 connector_.reset(new ConnectorImpl(std::move(connector))); |
| 44 } |
| 45 |
40 void ServiceContext::SetConnectionLostClosure(const base::Closure& closure) { | 46 void ServiceContext::SetConnectionLostClosure(const base::Closure& closure) { |
41 connection_lost_closure_ = closure; | 47 connection_lost_closure_ = closure; |
42 if (should_run_connection_lost_closure_ && | 48 if (should_run_connection_lost_closure_ && |
43 !connection_lost_closure_.is_null()) | 49 !connection_lost_closure_.is_null()) |
44 connection_lost_closure_.Run(); | 50 connection_lost_closure_.Run(); |
45 } | 51 } |
46 | 52 |
47 //////////////////////////////////////////////////////////////////////////////// | 53 //////////////////////////////////////////////////////////////////////////////// |
48 // ServiceContext, mojom::Service implementation: | 54 // ServiceContext, mojom::Service implementation: |
49 | 55 |
50 void ServiceContext::OnStart(mojom::IdentityPtr identity, | 56 void ServiceContext::OnStart(mojom::IdentityPtr identity, |
51 uint32_t id, | 57 uint32_t id, |
52 const OnStartCallback& callback) { | 58 const OnStartCallback& callback) { |
53 identity_ = identity.To<Identity>(); | 59 identity_ = identity.To<Identity>(); |
54 if (!initialize_handler_.is_null()) | 60 if (!initialize_handler_.is_null()) |
55 initialize_handler_.Run(); | 61 initialize_handler_.Run(); |
56 | 62 |
57 callback.Run(std::move(pending_connector_request_)); | 63 callback.Run(std::move(pending_connector_request_)); |
58 | 64 |
59 DCHECK(binding_.is_bound()); | 65 DCHECK(binding_.is_bound()); |
60 binding_.set_connection_error_handler( | 66 binding_.set_connection_error_handler( |
61 base::Bind(&ServiceContext::OnConnectionError, base::Unretained(this))); | 67 base::Bind(&ServiceContext::OnConnectionError, base::Unretained(this))); |
62 | 68 |
63 service_->OnStart(connector_.get(), identity_, id); | 69 client_->OnStart(connector_.get(), identity_, id); |
64 } | 70 } |
65 | 71 |
66 void ServiceContext::OnConnect( | 72 void ServiceContext::OnConnect( |
67 mojom::IdentityPtr source, | 73 mojom::IdentityPtr source, |
68 uint32_t source_id, | 74 uint32_t source_id, |
69 mojom::InterfaceProviderRequest local_interfaces, | 75 mojom::InterfaceProviderRequest local_interfaces, |
70 mojom::InterfaceProviderPtr remote_interfaces, | 76 mojom::InterfaceProviderPtr remote_interfaces, |
71 mojom::CapabilityRequestPtr allowed_capabilities, | 77 mojom::CapabilityRequestPtr allowed_capabilities, |
72 const mojo::String& name) { | 78 const mojo::String& name) { |
73 std::unique_ptr<internal::ConnectionImpl> registry( | 79 std::unique_ptr<internal::ConnectionImpl> registry( |
74 new internal::ConnectionImpl(name, source.To<Identity>(), source_id, | 80 new internal::ConnectionImpl(name, source.To<Identity>(), source_id, |
75 allowed_capabilities.To<CapabilityRequest>(), | 81 allowed_capabilities.To<CapabilityRequest>(), |
76 Connection::State::CONNECTED)); | 82 Connection::State::CONNECTED)); |
77 | 83 |
78 std::unique_ptr<InterfaceRegistry> exposed_interfaces( | 84 InterfaceRegistry* exposed_interfaces = |
79 new InterfaceRegistry(registry.get())); | 85 client_->GetInterfaceRegistryForConnection(); |
80 exposed_interfaces->Bind(std::move(local_interfaces)); | 86 if (exposed_interfaces) { |
81 registry->SetExposedInterfaces(std::move(exposed_interfaces)); | 87 exposed_interfaces->Bind(std::move(local_interfaces)); |
| 88 registry->set_exposed_interfaces(exposed_interfaces); |
| 89 } else { |
| 90 std::unique_ptr<InterfaceRegistry> interfaces( |
| 91 new InterfaceRegistry(registry.get())); |
| 92 interfaces->Bind(std::move(local_interfaces)); |
| 93 registry->SetExposedInterfaces(std::move(interfaces)); |
| 94 } |
| 95 shell::InterfaceProvider* remote_interface_provider = |
| 96 client_->GetInterfaceProviderForConnection(); |
| 97 if (remote_interface_provider) { |
| 98 remote_interface_provider->Bind(std::move(remote_interfaces)); |
| 99 registry->set_remote_interfaces(remote_interface_provider); |
| 100 } else { |
| 101 std::unique_ptr<InterfaceProvider> interfaces(new InterfaceProvider); |
| 102 interfaces->Bind(std::move(remote_interfaces)); |
| 103 registry->SetRemoteInterfaces(std::move(interfaces)); |
| 104 } |
82 | 105 |
83 std::unique_ptr<InterfaceProvider> interfaces(new InterfaceProvider); | 106 if (!client_->OnConnect(registry.get())) |
84 interfaces->Bind(std::move(remote_interfaces)); | |
85 registry->SetRemoteInterfaces(std::move(interfaces)); | |
86 | |
87 if (!service_->OnConnect(registry.get())) | |
88 return; | 107 return; |
89 | 108 |
90 // TODO(beng): it appears we never prune this list. We should, when the | 109 // TODO(beng): it appears we never prune this list. We should, when the |
91 // connection's remote service provider pipe breaks. | 110 // connection's remote service provider pipe breaks. |
92 incoming_connections_.push_back(std::move(registry)); | 111 incoming_connections_.push_back(std::move(registry)); |
93 } | 112 } |
94 | 113 |
95 //////////////////////////////////////////////////////////////////////////////// | 114 //////////////////////////////////////////////////////////////////////////////// |
96 // ServiceContext, private: | 115 // ServiceContext, private: |
97 | 116 |
98 void ServiceContext::OnConnectionError() { | 117 void ServiceContext::OnConnectionError() { |
99 // Note that the Service doesn't technically have to quit now, it may live | 118 // Note that the Service doesn't technically have to quit now, it may live |
100 // on to service existing connections. All existing Connectors however are | 119 // on to service existing connections. All existing Connectors however are |
101 // invalid. | 120 // invalid. |
102 should_run_connection_lost_closure_ = service_->OnStop(); | 121 should_run_connection_lost_closure_ = client_->OnStop(); |
103 if (should_run_connection_lost_closure_ && | 122 if (should_run_connection_lost_closure_ && |
104 !connection_lost_closure_.is_null()) | 123 !connection_lost_closure_.is_null()) |
105 connection_lost_closure_.Run(); | 124 connection_lost_closure_.Run(); |
106 // We don't reset the connector as clients may have taken a raw pointer to it. | 125 // We don't reset the connector as clients may have taken a raw pointer to it. |
107 // Connect() will return nullptr if they try to connect to anything. | 126 // Connect() will return nullptr if they try to connect to anything. |
108 } | 127 } |
109 | 128 |
110 } // namespace shell | 129 } // namespace shell |
OLD | NEW |