blob: 30aff51c1f5f305b1d3852294ed9a093695d0653 [file] [log] [blame]
tibellad0c0b32016-08-04 01:13:301// 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
Sam McNallyd482b4b2017-07-17 03:45:037#include "mojo/public/cpp/bindings/lib/task_runner_helper.h"
8
tibellad0c0b32016-08-04 01:13:309namespace mojo {
10namespace internal {
11
Marijn Kruisselbrink3325c22c2017-06-07 20:54:2612BindingStateBase::BindingStateBase() : weak_ptr_factory_(this) {}
tibellad0c0b32016-08-04 01:13:3013
yzshena87f4ac2016-11-30 19:03:2014BindingStateBase::~BindingStateBase() = default;
tibellad0c0b32016-08-04 01:13:3015
yzshena87f4ac2016-11-30 19:03:2016void BindingStateBase::AddFilter(std::unique_ptr<MessageReceiver> filter) {
rockot21bb88542016-08-26 21:21:3217 DCHECK(endpoint_client_);
18 endpoint_client_->AddFilter(std::move(filter));
19}
20
yzshena87f4ac2016-11-30 19:03:2021bool BindingStateBase::HasAssociatedInterfaces() const {
tibellad0c0b32016-08-04 01:13:3022 return router_ ? router_->HasAssociatedEndpoints() : false;
23}
24
yzshena87f4ac2016-11-30 19:03:2025void BindingStateBase::PauseIncomingMethodCallProcessing() {
tibellad0c0b32016-08-04 01:13:3026 DCHECK(router_);
27 router_->PauseIncomingMethodCallProcessing();
28}
Marijn Kruisselbrink3325c22c2017-06-07 20:54:2629
yzshena87f4ac2016-11-30 19:03:2030void BindingStateBase::ResumeIncomingMethodCallProcessing() {
tibellad0c0b32016-08-04 01:13:3031 DCHECK(router_);
32 router_->ResumeIncomingMethodCallProcessing();
33}
34
yzshena87f4ac2016-11-30 19:03:2035bool BindingStateBase::WaitForIncomingMethodCall(MojoDeadline deadline) {
tibellad0c0b32016-08-04 01:13:3036 DCHECK(router_);
37 return router_->WaitForIncomingMessage(deadline);
38}
39
yzshena87f4ac2016-11-30 19:03:2040void BindingStateBase::Close() {
tibellad0c0b32016-08-04 01:13:3041 if (!router_)
42 return;
43
44 endpoint_client_.reset();
45 router_->CloseMessagePipe();
46 router_ = nullptr;
yzshen24438d22016-09-13 17:03:5347}
48
yzshena87f4ac2016-11-30 19:03:2049void BindingStateBase::CloseWithReason(uint32_t custom_reason,
50 const std::string& description) {
yzshen24438d22016-09-13 17:03:5351 if (endpoint_client_)
yzshen8be41d3a2017-01-23 20:40:3752 endpoint_client_->CloseWithReason(custom_reason, description);
53
yzshen24438d22016-09-13 17:03:5354 Close();
tibellad0c0b32016-08-04 01:13:3055}
56
Marijn Kruisselbrink3325c22c2017-06-07 20:54:2657ReportBadMessageCallback BindingStateBase::GetBadMessageCallback() {
58 return base::Bind(
59 [](const ReportBadMessageCallback& inner_callback,
60 base::WeakPtr<BindingStateBase> binding, const std::string& error) {
61 inner_callback.Run(error);
62 if (binding)
63 binding->Close();
64 },
65 mojo::GetBadMessageCallback(), weak_ptr_factory_.GetWeakPtr());
66}
67
yzshena87f4ac2016-11-30 19:03:2068void BindingStateBase::FlushForTesting() {
yzshen2859a2ac2017-02-14 22:24:2569 endpoint_client_->FlushForTesting();
sammc462458f2016-09-01 09:52:5870}
71
yzshena87f4ac2016-11-30 19:03:2072void BindingStateBase::EnableTestingMode() {
tibellad0c0b32016-08-04 01:13:3073 DCHECK(is_bound());
74 router_->EnableTestingMode();
75}
76
Tim Becker6573d892017-08-04 17:24:3577scoped_refptr<internal::MultiplexRouter> BindingStateBase::RouterForTesting() {
78 return router_;
79}
80
yzshena87f4ac2016-11-30 19:03:2081void BindingStateBase::BindInternal(
tibellad0c0b32016-08-04 01:13:3082 ScopedMessagePipeHandle handle,
Sam McNallyd482b4b2017-07-17 03:45:0383 scoped_refptr<base::SingleThreadTaskRunner> runner,
tibellad0c0b32016-08-04 01:13:3084 const char* interface_name,
rockot222e7dd2016-08-24 23:37:1185 std::unique_ptr<MessageReceiver> request_validator,
yzshen97c656d2016-09-22 17:02:4086 bool passes_associated_kinds,
tibellad0c0b32016-08-04 01:13:3087 bool has_sync_methods,
sammc462458f2016-09-01 09:52:5888 MessageReceiverWithResponderStatus* stub,
89 uint32_t interface_version) {
tibellad0c0b32016-08-04 01:13:3090 DCHECK(!router_);
91
Sam McNallyd482b4b2017-07-17 03:45:0392 auto sequenced_runner =
93 GetTaskRunnerToUseFromUserProvidedTaskRunner(std::move(runner));
yzshen97c656d2016-09-22 17:02:4094 MultiplexRouter::Config config =
95 passes_associated_kinds
96 ? MultiplexRouter::MULTI_INTERFACE
97 : (has_sync_methods
98 ? MultiplexRouter::SINGLE_INTERFACE_WITH_SYNC_METHODS
99 : MultiplexRouter::SINGLE_INTERFACE);
Sam McNallyd482b4b2017-07-17 03:45:03100 router_ =
101 new MultiplexRouter(std::move(handle), config, false, sequenced_runner);
tibellad0c0b32016-08-04 01:13:30102 router_->SetMasterInterfaceName(interface_name);
103
104 endpoint_client_.reset(new InterfaceEndpointClient(
105 router_->CreateLocalEndpointHandle(kMasterInterfaceId), stub,
Sam McNallyd482b4b2017-07-17 03:45:03106 std::move(request_validator), has_sync_methods,
107 std::move(sequenced_runner), interface_version));
tibellad0c0b32016-08-04 01:13:30108}
109
110} // namesapce internal
111} // namespace mojo