OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/interface_provider.h" | 5 #include "services/shell/public/cpp/interface_provider.h" |
6 | 6 |
7 #include "base/macros.h" | |
8 #include "mojo/public/cpp/bindings/strong_binding.h" | |
9 | |
10 namespace shell { | 7 namespace shell { |
11 | 8 |
12 InterfaceProvider::InterfaceProvider() : weak_factory_(this) { | 9 InterfaceProvider::InterfaceProvider() : weak_factory_(this) { |
13 pending_request_ = GetProxy(&interface_provider_); | 10 pending_request_ = GetProxy(&interface_provider_); |
14 } | 11 } |
15 | |
16 InterfaceProvider::~InterfaceProvider() {} | 12 InterfaceProvider::~InterfaceProvider() {} |
17 | 13 |
18 void InterfaceProvider::Bind(mojom::InterfaceProviderPtr interface_provider) { | 14 void InterfaceProvider::Bind(mojom::InterfaceProviderPtr interface_provider) { |
19 DCHECK(pending_request_.is_pending()); | 15 DCHECK(pending_request_.is_pending()); |
20 DCHECK(forward_callback_.is_null()); | |
21 mojo::FuseInterface(std::move(pending_request_), | 16 mojo::FuseInterface(std::move(pending_request_), |
22 interface_provider.PassInterface()); | 17 interface_provider.PassInterface()); |
23 } | 18 } |
24 | 19 |
25 void InterfaceProvider::Forward(const ForwardCallback& callback) { | |
26 DCHECK(pending_request_.is_pending()); | |
27 DCHECK(forward_callback_.is_null()); | |
28 interface_provider_.reset(); | |
29 pending_request_.PassMessagePipe().reset(); | |
30 forward_callback_ = callback; | |
31 } | |
32 | |
33 void InterfaceProvider::SetConnectionLostClosure( | 20 void InterfaceProvider::SetConnectionLostClosure( |
34 const base::Closure& connection_lost_closure) { | 21 const base::Closure& connection_lost_closure) { |
35 interface_provider_.set_connection_error_handler(connection_lost_closure); | 22 interface_provider_.set_connection_error_handler(connection_lost_closure); |
36 } | 23 } |
37 | 24 |
38 base::WeakPtr<InterfaceProvider> InterfaceProvider::GetWeakPtr() { | 25 base::WeakPtr<InterfaceProvider> InterfaceProvider::GetWeakPtr() { |
39 return weak_factory_.GetWeakPtr(); | 26 return weak_factory_.GetWeakPtr(); |
40 } | 27 } |
41 | 28 |
42 void InterfaceProvider::GetInterface( | 29 void InterfaceProvider::GetInterface( |
43 const std::string& name, | 30 const std::string& name, |
44 mojo::ScopedMessagePipeHandle request_handle) { | 31 mojo::ScopedMessagePipeHandle request_handle) { |
45 // Local binders can be registered via TestApi. | 32 // Local binders can be registered via TestApi. |
46 auto it = binders_.find(name); | 33 auto it = binders_.find(name); |
47 if (it != binders_.end()) { | 34 if (it != binders_.end()) { |
48 it->second.Run(std::move(request_handle)); | 35 it->second.Run(std::move(request_handle)); |
49 return; | 36 return; |
50 } | 37 } |
51 | 38 interface_provider_->GetInterface(name, std::move(request_handle)); |
52 if (!forward_callback_.is_null()) { | |
53 DCHECK(!interface_provider_.is_bound()); | |
54 forward_callback_.Run(name, std::move(request_handle)); | |
55 } else { | |
56 DCHECK(interface_provider_.is_bound()); | |
57 interface_provider_->GetInterface(name, std::move(request_handle)); | |
58 } | |
59 } | 39 } |
60 | 40 |
61 void InterfaceProvider::ClearBinders() { | 41 void InterfaceProvider::ClearBinders() { |
62 binders_.clear(); | 42 binders_.clear(); |
63 } | 43 } |
64 | 44 |
65 } // namespace shell | 45 } // namespace shell |
OLD | NEW |