// mojo/public/mojom/base/binder.mojom.cc is auto generated by mojom_bindings_generator.py, do not edit

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-private-field"
#endif

#include "mojo/public/mojom/base/binder.mojom.h"
#include <math.h>
#include <stdint.h>
#include <utility>
#include "base/debug/alias.h"
#include "base/notreached.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/typed_macros.h"
#include "mojo/public/cpp/bindings/lib/default_construct_tag_internal.h"
#include "mojo/public/cpp/bindings/lib/generated_code_util.h"
#include "mojo/public/cpp/bindings/lib/message_internal.h"
#include "mojo/public/cpp/bindings/lib/proxy_to_responder.h"
#include "mojo/public/cpp/bindings/lib/send_message_helper.h"
#include "mojo/public/cpp/bindings/lib/serialization.h"
#include "mojo/public/cpp/bindings/lib/unserialized_message_context.h"
#include "mojo/public/cpp/bindings/lib/validate_params.h"
#include "mojo/public/cpp/bindings/lib/validation_errors.h"
#include "mojo/public/cpp/bindings/mojo_buildflags.h"
#include "mojo/public/cpp/bindings/urgent_message_scope.h"
#include "mojo/public/interfaces/bindings/interface_control_messages.mojom.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value.h"

#include "mojo/public/mojom/base/binder.mojom-params-data.h"
#include "mojo/public/mojom/base/binder.mojom-shared-message-ids.h"

#include "mojo/public/mojom/base/binder.mojom-import-headers.h"
#include "mojo/public/mojom/base/binder.mojom-test-utils.h"


namespace mojo_base::mojom {
// The declaration includes the definition on other builds.

Binder::IPCStableHashFunction Binder::MessageToMethodInfo_(mojo::Message& message) {
#if !BUILDFLAG(IS_FUCHSIA)
  switch (static_cast<messages::Binder>(message.name())) {
    case messages::Binder::kBind: {
      return &Binder::Bind_Sym::IPCStableHash;
    }
  }
#endif  // !BUILDFLAG(IS_FUCHSIA)
  return nullptr;
}


const char* Binder::MessageToMethodName_(mojo::Message& message) {
#if BUILDFLAG(MOJO_TRACE_ENABLED)
  bool is_response = message.has_flag(mojo::Message::kFlagIsResponse);
  if (!is_response) {
    switch (static_cast<messages::Binder>(message.name())) {
      case messages::Binder::kBind:
            return "Receive mojo_base::mojom::Binder::Bind";
    }
  } else {
    switch (static_cast<messages::Binder>(message.name())) {
      case messages::Binder::kBind:
            return "Receive reply mojo_base::mojom::Binder::Bind";
    }
  }
  return "Receive unknown mojo message";
#else
  bool is_response = message.has_flag(mojo::Message::kFlagIsResponse);
  if (is_response) {
    return "Receive mojo reply";
  } else {
    return "Receive mojo message";
  }
#endif // BUILDFLAG(MOJO_TRACE_ENABLED)
}

#if !BUILDFLAG(IS_FUCHSIA)
uint32_t Binder::Bind_Sym::IPCStableHash() {
  // This method's address is used for identifying the mojo method name after
  // symbolization. So each IPCStableHash should have a unique address.
  // We cannot use NO_CODE_FOLDING() here - it relies on the uniqueness of
  // __LINE__ value, which is not unique across different mojo modules.
  // The code below is very similar to NO_CODE_FOLDING, but it uses a unique
  // hash instead of __LINE__.
  static constexpr uint32_t kHash = 0x2b342f41;  // IPCStableHash for mojo_base::mojom::Binder::Bind
  const uint32_t hash = kHash;
  base::debug::Alias(&hash);
  return hash;
}
# endif // !BUILDFLAG(IS_FUCHSIA)

BinderProxy::BinderProxy(mojo::MessageReceiverWithResponder* receiver)
    : receiver_(receiver) {
}

void BinderProxy::Bind(
    ::mojo::GenericPendingReceiver in_receiver) {
#if BUILDFLAG(MOJO_TRACE_ENABLED)
  TRACE_EVENT1(
    "mojom", "Send mojo_base::mojom::Binder::Bind", "input_parameters",
    [&](perfetto::TracedValue context){
      auto dict = std::move(context).WriteDictionary();
      perfetto::WriteIntoTracedValueWithFallback(
           dict.AddItem("receiver"), in_receiver,
                        "<value of type ::mojo::GenericPendingReceiver>");
   });
#endif

  const bool kExpectsResponse = false;
  const bool kIsSync = false;
  const bool kAllowInterrupt = true;
  const bool is_urgent = false;

  const uint32_t kFlags =
      ((kExpectsResponse) ? mojo::Message::kFlagExpectsResponse : 0) |
      ((kIsSync) ? mojo::Message::kFlagIsSync : 0) |
      ((kAllowInterrupt) ? 0 : mojo::Message::kFlagNoInterrupt) |
      ((is_urgent) ? mojo::Message::kFlagIsUrgent : 0);

  const size_t estimated_payload_size =
    0;
  mojo::Message message(
      std::to_underlying(messages::Binder::kBind), kFlags, estimated_payload_size);
  mojo::internal::MessageFragment<
      ::mojo_base::mojom::internal::Binder_Bind_Params_Data> params(
          message);
  params.Allocate();
  mojo::internal::MessageFragment<
      typename decltype(params->receiver)::BaseType> receiver_fragment(
          params.message());
  
  mojo::internal::Serialize<::mojo_base::mojom::GenericPendingReceiverDataView>(
    in_receiver,
    receiver_fragment);

  params->receiver.Set(
      receiver_fragment.is_null() ? nullptr : receiver_fragment.data());

  
  MOJO_INTERNAL_CHECK_SERIALIZATION(
    mojo::internal::SendValidation::kDefault,
    !(params->receiver.is_null()),
    mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
    "null receiver in Binder.Bind request");

#if defined(ENABLE_IPC_FUZZER)
  message.set_interface_name(Binder::Name_);
  message.set_method_name("Bind");
#endif
  // This return value may be ignored as false implies the Connector has
  // encountered an error, which will be visible through other means.
  ::mojo::internal::SendMojoMessage(*receiver_, message);
}

// static
bool BinderStubDispatch::Accept(
    Binder* impl,
    mojo::Message* message) {
  switch (static_cast<messages::Binder>(message->header()->name)) {
    case messages::Binder::kBind: {
      DCHECK(message->is_serialized());
      internal::Binder_Bind_Params_Data* params =
          reinterpret_cast<internal::Binder_Bind_Params_Data*>(
              message->mutable_payload());
      

      // Validation for Binder.0
      bool success = true;
      ::mojo::GenericPendingReceiver p_receiver{};
      Binder_Bind_ParamsDataView input_data_view(params, message);
      
      if (success && !input_data_view.ReadReceiver(&p_receiver))
        success = false;
      if (!success) {
        ReportValidationErrorForMessage(
            message,
            mojo::internal::VALIDATION_ERROR_DESERIALIZATION_FAILED,
            Binder::Name_, 0, false);
        return false;
      }
      // A null |impl| means no implementation was bound.
      DCHECK(impl);
      impl->Bind(        
        std::move(p_receiver));
      return true;
    }
  }
  return false;
}

// static
bool BinderStubDispatch::AcceptWithResponder(
    Binder* impl,
    mojo::Message* message,
    std::unique_ptr<mojo::MessageReceiverWithStatus> responder) {
  [[maybe_unused]] const bool message_is_sync =
      message->has_flag(mojo::Message::kFlagIsSync);
  [[maybe_unused]] const uint64_t request_id = message->request_id();
  switch (static_cast<messages::Binder>(message->header()->name)) {
    case messages::Binder::kBind: {
      break;
    }
  }
  return false;
}
namespace {
}  // namespace
static const mojo::internal::GenericValidationInfo kBinderValidationInfo[] = {
    { &internal::Binder_Bind_Params_Data::Validate,
     nullptr /* no response */},
};

bool BinderRequestValidator::Accept(mojo::Message* message) {
  return mojo::internal::ValidateRequestGenericPacked(
    message,
    ::mojo_base::mojom::Binder::Name_,
    kBinderValidationInfo);
}



}  // mojo_base::mojom


namespace mojo {

}  // namespace mojo


// Symbols declared in the -test-utils.h header are defined here instead of a
// separate .cc file to save compile time.


namespace mojo_base::mojom {


void BinderInterceptorForTesting::Bind(::mojo::GenericPendingReceiver receiver) {
  GetForwardingInterface()->Bind(
    std::move(receiver)
    );
}
BinderAsyncWaiter::BinderAsyncWaiter(
    Binder* proxy) : proxy_(proxy) {}

BinderAsyncWaiter::~BinderAsyncWaiter() = default;







}  // mojo_base::mojom


#if defined(__clang__)
#pragma clang diagnostic pop
#endif