1 // Copyright 2017 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/associated_binding.h"
6 
7 #include "base/single_thread_task_runner.h"
8 #include "mojo/public/cpp/bindings/lib/task_runner_helper.h"
9 
10 namespace mojo {
11 
AssociatedBindingBase()12 AssociatedBindingBase::AssociatedBindingBase() {}
13 
~AssociatedBindingBase()14 AssociatedBindingBase::~AssociatedBindingBase() {}
15 
AddFilter(std::unique_ptr<MessageReceiver> filter)16 void AssociatedBindingBase::AddFilter(std::unique_ptr<MessageReceiver> filter) {
17   DCHECK(endpoint_client_);
18   endpoint_client_->AddFilter(std::move(filter));
19 }
20 
Close()21 void AssociatedBindingBase::Close() {
22   endpoint_client_.reset();
23 }
24 
CloseWithReason(uint32_t custom_reason,const std::string & description)25 void AssociatedBindingBase::CloseWithReason(uint32_t custom_reason,
26                                             const std::string& description) {
27   if (endpoint_client_)
28     endpoint_client_->CloseWithReason(custom_reason, description);
29   Close();
30 }
31 
set_connection_error_handler(base::OnceClosure error_handler)32 void AssociatedBindingBase::set_connection_error_handler(
33     base::OnceClosure error_handler) {
34   DCHECK(is_bound());
35   endpoint_client_->set_connection_error_handler(std::move(error_handler));
36 }
37 
set_connection_error_with_reason_handler(ConnectionErrorWithReasonCallback error_handler)38 void AssociatedBindingBase::set_connection_error_with_reason_handler(
39     ConnectionErrorWithReasonCallback error_handler) {
40   DCHECK(is_bound());
41   endpoint_client_->set_connection_error_with_reason_handler(
42       std::move(error_handler));
43 }
44 
FlushForTesting()45 void AssociatedBindingBase::FlushForTesting() {
46   endpoint_client_->FlushForTesting();
47 }
48 
BindImpl(ScopedInterfaceEndpointHandle handle,MessageReceiverWithResponderStatus * receiver,std::unique_ptr<MessageReceiver> payload_validator,bool expect_sync_requests,scoped_refptr<base::SingleThreadTaskRunner> runner,uint32_t interface_version)49 void AssociatedBindingBase::BindImpl(
50     ScopedInterfaceEndpointHandle handle,
51     MessageReceiverWithResponderStatus* receiver,
52     std::unique_ptr<MessageReceiver> payload_validator,
53     bool expect_sync_requests,
54     scoped_refptr<base::SingleThreadTaskRunner> runner,
55     uint32_t interface_version) {
56   if (!handle.is_valid()) {
57     endpoint_client_.reset();
58     return;
59   }
60 
61   endpoint_client_.reset(new InterfaceEndpointClient(
62       std::move(handle), receiver, std::move(payload_validator),
63       expect_sync_requests,
64       internal::GetTaskRunnerToUseFromUserProvidedTaskRunner(std::move(runner)),
65       interface_version));
66 }
67 
68 }  // namespace mojo
69