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
3 // found in the LICENSE file.
4
5 #include "mojo/public/cpp/bindings/lib/binding_state.h"
6
7 #include "mojo/public/cpp/bindings/lib/task_runner_helper.h"
8
9 namespace mojo {
10 namespace internal {
11
BindingStateBase()12 BindingStateBase::BindingStateBase() : weak_ptr_factory_(this) {}
13
14 BindingStateBase::~BindingStateBase() = default;
15
AddFilter(std::unique_ptr<MessageReceiver> filter)16 void BindingStateBase::AddFilter(std::unique_ptr<MessageReceiver> filter) {
17 DCHECK(endpoint_client_);
18 endpoint_client_->AddFilter(std::move(filter));
19 }
20
HasAssociatedInterfaces() const21 bool BindingStateBase::HasAssociatedInterfaces() const {
22 return router_ ? router_->HasAssociatedEndpoints() : false;
23 }
24
PauseIncomingMethodCallProcessing()25 void BindingStateBase::PauseIncomingMethodCallProcessing() {
26 DCHECK(router_);
27 router_->PauseIncomingMethodCallProcessing();
28 }
29
ResumeIncomingMethodCallProcessing()30 void BindingStateBase::ResumeIncomingMethodCallProcessing() {
31 DCHECK(router_);
32 router_->ResumeIncomingMethodCallProcessing();
33 }
34
WaitForIncomingMethodCall(MojoDeadline deadline)35 bool BindingStateBase::WaitForIncomingMethodCall(MojoDeadline deadline) {
36 DCHECK(router_);
37 return router_->WaitForIncomingMessage(deadline);
38 }
39
Close()40 void BindingStateBase::Close() {
41 if (!router_)
42 return;
43
44 endpoint_client_.reset();
45 router_->CloseMessagePipe();
46 router_ = nullptr;
47 }
48
CloseWithReason(uint32_t custom_reason,const std::string & description)49 void BindingStateBase::CloseWithReason(uint32_t custom_reason,
50 const std::string& description) {
51 if (endpoint_client_)
52 endpoint_client_->CloseWithReason(custom_reason, description);
53
54 Close();
55 }
56
GetBadMessageCallback()57 ReportBadMessageCallback BindingStateBase::GetBadMessageCallback() {
58 return base::BindOnce(
59 [](ReportBadMessageCallback inner_callback,
60 base::WeakPtr<BindingStateBase> binding, const std::string& error) {
61 std::move(inner_callback).Run(error);
62 if (binding)
63 binding->Close();
64 },
65 mojo::GetBadMessageCallback(), weak_ptr_factory_.GetWeakPtr());
66 }
67
FlushForTesting()68 void BindingStateBase::FlushForTesting() {
69 endpoint_client_->FlushForTesting();
70 }
71
EnableTestingMode()72 void BindingStateBase::EnableTestingMode() {
73 DCHECK(is_bound());
74 router_->EnableTestingMode();
75 }
76
RouterForTesting()77 scoped_refptr<internal::MultiplexRouter> BindingStateBase::RouterForTesting() {
78 return router_;
79 }
80
BindInternal(ScopedMessagePipeHandle handle,scoped_refptr<base::SingleThreadTaskRunner> runner,const char * interface_name,std::unique_ptr<MessageReceiver> request_validator,bool passes_associated_kinds,bool has_sync_methods,MessageReceiverWithResponderStatus * stub,uint32_t interface_version)81 void BindingStateBase::BindInternal(
82 ScopedMessagePipeHandle handle,
83 scoped_refptr<base::SingleThreadTaskRunner> runner,
84 const char* interface_name,
85 std::unique_ptr<MessageReceiver> request_validator,
86 bool passes_associated_kinds,
87 bool has_sync_methods,
88 MessageReceiverWithResponderStatus* stub,
89 uint32_t interface_version) {
90 DCHECK(!is_bound()) << "Attempting to bind interface that is already bound: "
91 << interface_name;
92
93 auto sequenced_runner =
94 GetTaskRunnerToUseFromUserProvidedTaskRunner(std::move(runner));
95 MultiplexRouter::Config config =
96 passes_associated_kinds
97 ? MultiplexRouter::MULTI_INTERFACE
98 : (has_sync_methods
99 ? MultiplexRouter::SINGLE_INTERFACE_WITH_SYNC_METHODS
100 : MultiplexRouter::SINGLE_INTERFACE);
101 router_ =
102 new MultiplexRouter(std::move(handle), config, false, sequenced_runner);
103 router_->SetMasterInterfaceName(interface_name);
104
105 endpoint_client_.reset(new InterfaceEndpointClient(
106 router_->CreateLocalEndpointHandle(kMasterInterfaceId), stub,
107 std::move(request_validator), has_sync_methods,
108 std::move(sequenced_runner), interface_version));
109 }
110
111 } // namesapce internal
112 } // namespace mojo
113