Home | History | Annotate | Download | only in lib
      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/lib/associated_interface_ptr_state.h"
      6 
      7 #include "mojo/public/cpp/bindings/lib/task_runner_helper.h"
      8 
      9 namespace mojo {
     10 namespace internal {
     11 
     12 AssociatedInterfacePtrStateBase::AssociatedInterfacePtrStateBase() = default;
     13 
     14 AssociatedInterfacePtrStateBase::~AssociatedInterfacePtrStateBase() = default;
     15 
     16 void AssociatedInterfacePtrStateBase::QueryVersion(
     17     const base::Callback<void(uint32_t)>& callback) {
     18   // It is safe to capture |this| because the callback won't be run after this
     19   // object goes away.
     20   endpoint_client_->QueryVersion(
     21       base::Bind(&AssociatedInterfacePtrStateBase::OnQueryVersion,
     22                  base::Unretained(this), callback));
     23 }
     24 
     25 void AssociatedInterfacePtrStateBase::RequireVersion(uint32_t version) {
     26   if (version <= version_)
     27     return;
     28 
     29   version_ = version;
     30   endpoint_client_->RequireVersion(version);
     31 }
     32 
     33 void AssociatedInterfacePtrStateBase::OnQueryVersion(
     34     const base::Callback<void(uint32_t)>& callback,
     35     uint32_t version) {
     36   version_ = version;
     37   callback.Run(version);
     38 }
     39 
     40 void AssociatedInterfacePtrStateBase::FlushForTesting() {
     41   endpoint_client_->FlushForTesting();
     42 }
     43 
     44 void AssociatedInterfacePtrStateBase::CloseWithReason(
     45     uint32_t custom_reason,
     46     const std::string& description) {
     47   endpoint_client_->CloseWithReason(custom_reason, description);
     48 }
     49 
     50 void AssociatedInterfacePtrStateBase::Swap(
     51     AssociatedInterfacePtrStateBase* other) {
     52   using std::swap;
     53   swap(other->endpoint_client_, endpoint_client_);
     54   swap(other->version_, version_);
     55 }
     56 
     57 void AssociatedInterfacePtrStateBase::Bind(
     58     ScopedInterfaceEndpointHandle handle,
     59     uint32_t version,
     60     std::unique_ptr<MessageReceiver> validator,
     61     scoped_refptr<base::SequencedTaskRunner> runner) {
     62   DCHECK(!endpoint_client_);
     63   DCHECK_EQ(0u, version_);
     64   DCHECK(handle.is_valid());
     65 
     66   version_ = version;
     67   // The version is only queried from the client so the value passed here
     68   // will not be used.
     69   endpoint_client_ = std::make_unique<InterfaceEndpointClient>(
     70       std::move(handle), nullptr, std::move(validator), false,
     71       GetTaskRunnerToUseFromUserProvidedTaskRunner(std::move(runner)), 0u);
     72 }
     73 
     74 ScopedInterfaceEndpointHandle AssociatedInterfacePtrStateBase::PassHandle() {
     75   auto handle = endpoint_client_->PassHandle();
     76   endpoint_client_.reset();
     77   return handle;
     78 }
     79 
     80 }  // namespace internal
     81 }  // namespace mojo
     82