// This file is generated by TypeBuilder_cpp.template.

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

#include "content/browser/devtools/protocol/preload.h"

#include "content/browser/devtools/protocol/protocol.h"

#include "third_party/inspector_protocol/crdtp/cbor.h"
#include "third_party/inspector_protocol/crdtp/find_by_first.h"
#include "third_party/inspector_protocol/crdtp/span.h"

namespace content {
namespace protocol {
namespace Preload {

using crdtp::DeserializerState;
using crdtp::FallthroughCallback;
using crdtp::ProtocolTypeTraits;

// ------------- Enum values from types.

const char Metainfo::domainName[] = "Preload";
const char Metainfo::commandPrefix[] = "Preload.";
const char Metainfo::version[] = "1.3";



CRDTP_BEGIN_DESERIALIZER(PreloadingAttemptKey)
    CRDTP_DESERIALIZE_FIELD("action", m_action),
    CRDTP_DESERIALIZE_FIELD_OPT("formSubmission", m_formSubmission),
    CRDTP_DESERIALIZE_FIELD("loaderId", m_loaderId),
    CRDTP_DESERIALIZE_FIELD_OPT("targetHint", m_targetHint),
    CRDTP_DESERIALIZE_FIELD("url", m_url),
CRDTP_END_DESERIALIZER()

CRDTP_BEGIN_SERIALIZER(PreloadingAttemptKey)
    CRDTP_SERIALIZE_FIELD("loaderId", m_loaderId);
    CRDTP_SERIALIZE_FIELD("action", m_action);
    CRDTP_SERIALIZE_FIELD("url", m_url);
    CRDTP_SERIALIZE_FIELD("formSubmission", m_formSubmission);
    CRDTP_SERIALIZE_FIELD("targetHint", m_targetHint);
CRDTP_END_SERIALIZER();






CRDTP_BEGIN_DESERIALIZER(PrerenderMismatchedHeaders)
    CRDTP_DESERIALIZE_FIELD_OPT("activationValue", m_activationValue),
    CRDTP_DESERIALIZE_FIELD("headerName", m_headerName),
    CRDTP_DESERIALIZE_FIELD_OPT("initialValue", m_initialValue),
CRDTP_END_DESERIALIZER()

CRDTP_BEGIN_SERIALIZER(PrerenderMismatchedHeaders)
    CRDTP_SERIALIZE_FIELD("headerName", m_headerName);
    CRDTP_SERIALIZE_FIELD("initialValue", m_initialValue);
    CRDTP_SERIALIZE_FIELD("activationValue", m_activationValue);
CRDTP_END_SERIALIZER();


// ------------- Enum values from params.


// ------------- Frontend notifications.

void Frontend::PreloadEnabledStateUpdated(bool disabledByPreference, bool disabledByDataSaver, bool disabledByBatterySaver, bool disabledByHoldbackPrefetchSpeculationRules, bool disabledByHoldbackPrerenderSpeculationRules)
{
    if (!frontend_channel_)
        return;
    crdtp::ObjectSerializer serializer;
    serializer.AddField(crdtp::MakeSpan("disabledByPreference"), disabledByPreference);
    serializer.AddField(crdtp::MakeSpan("disabledByDataSaver"), disabledByDataSaver);
    serializer.AddField(crdtp::MakeSpan("disabledByBatterySaver"), disabledByBatterySaver);
    serializer.AddField(crdtp::MakeSpan("disabledByHoldbackPrefetchSpeculationRules"), disabledByHoldbackPrefetchSpeculationRules);
    serializer.AddField(crdtp::MakeSpan("disabledByHoldbackPrerenderSpeculationRules"), disabledByHoldbackPrerenderSpeculationRules);
    frontend_channel_->SendProtocolNotification(crdtp::CreateNotification("Preload.preloadEnabledStateUpdated", serializer.Finish()));
}

void Frontend::PrefetchStatusUpdated(std::unique_ptr<protocol::Preload::PreloadingAttemptKey> key, const String& pipelineId, const String& initiatingFrameId, const String& prefetchUrl, const String& status, const String& prefetchStatus, const String& requestId)
{
    if (!frontend_channel_)
        return;
    crdtp::ObjectSerializer serializer;
    serializer.AddField(crdtp::MakeSpan("key"), key);
    serializer.AddField(crdtp::MakeSpan("pipelineId"), pipelineId);
    serializer.AddField(crdtp::MakeSpan("initiatingFrameId"), initiatingFrameId);
    serializer.AddField(crdtp::MakeSpan("prefetchUrl"), prefetchUrl);
    serializer.AddField(crdtp::MakeSpan("status"), status);
    serializer.AddField(crdtp::MakeSpan("prefetchStatus"), prefetchStatus);
    serializer.AddField(crdtp::MakeSpan("requestId"), requestId);
    frontend_channel_->SendProtocolNotification(crdtp::CreateNotification("Preload.prefetchStatusUpdated", serializer.Finish()));
}

void Frontend::PrerenderStatusUpdated(std::unique_ptr<protocol::Preload::PreloadingAttemptKey> key, const String& pipelineId, const String& status, std::optional<String> prerenderStatus, std::optional<String> disallowedMojoInterface, std::unique_ptr<protocol::Array<protocol::Preload::PrerenderMismatchedHeaders>> mismatchedHeaders)
{
    if (!frontend_channel_)
        return;
    crdtp::ObjectSerializer serializer;
    serializer.AddField(crdtp::MakeSpan("key"), key);
    serializer.AddField(crdtp::MakeSpan("pipelineId"), pipelineId);
    serializer.AddField(crdtp::MakeSpan("status"), status);
    serializer.AddField(crdtp::MakeSpan("prerenderStatus"), prerenderStatus);
    serializer.AddField(crdtp::MakeSpan("disallowedMojoInterface"), disallowedMojoInterface);
    serializer.AddField(crdtp::MakeSpan("mismatchedHeaders"), mismatchedHeaders);
    frontend_channel_->SendProtocolNotification(crdtp::CreateNotification("Preload.prerenderStatusUpdated", serializer.Finish()));
}

void Frontend::flush()
{
    frontend_channel_->FlushProtocolNotifications();
}

void Frontend::sendRawNotification(std::unique_ptr<Serializable> notification)
{
    frontend_channel_->SendProtocolNotification(std::move(notification));
}

// --------------------- Dispatcher.

class DomainDispatcherImpl : public protocol::DomainDispatcher {
public:
    DomainDispatcherImpl(FrontendChannel* frontendChannel, Backend* backend)
        : DomainDispatcher(frontendChannel)
        , m_backend(backend) {}
    ~DomainDispatcherImpl() override { }

    using CallHandler = void (DomainDispatcherImpl::*)(crdtp::Dispatchable& dispatchable);

    bool Dispatch(crdtp::span<uint8_t> command_name, crdtp::Dispatchable& dispatchable) override;

    void enable(crdtp::Dispatchable& dispatchable);
    void disable(crdtp::Dispatchable& dispatchable);
 protected:
    Backend* m_backend;
};

namespace {
// This helper method with a static map of command methods (instance methods
// of DomainDispatcherImpl declared just above) by their name is used immediately below,
// in the DomainDispatcherImpl::Dispatch method.
DomainDispatcherImpl::CallHandler CommandByName(crdtp::span<uint8_t> command_name) {
  static auto* commands = [](){
    auto* commands = new std::vector<std::pair<crdtp::span<uint8_t>,
                              DomainDispatcherImpl::CallHandler>>{
    {
          crdtp::SpanFrom("disable"),
          &DomainDispatcherImpl::disable
    },
    {
          crdtp::SpanFrom("enable"),
          &DomainDispatcherImpl::enable
    },
    };
    return commands;
  }();
  return crdtp::FindByFirst<DomainDispatcherImpl::CallHandler>(*commands, command_name, nullptr);
}
}  // namespace

bool DomainDispatcherImpl::Dispatch(crdtp::span<uint8_t> command_name, crdtp::Dispatchable& dispatchable) {
  CallHandler handler = CommandByName(command_name);
  if (!handler) return false;

  (this->*handler)(dispatchable);
  return true;
}


namespace {


}  // namespace

void DomainDispatcherImpl::enable(crdtp::Dispatchable& dispatchable)
{
    // Prepare input parameters.

    std::unique_ptr<DomainDispatcher::WeakPtr> weak = weakPtr();
    DispatchResponse response = m_backend->Enable();
    if (response.IsFallThrough()) {
        dispatchable.DispatchFallThrough(response.Message());
        return;
    }
    if (weak->get())
        weak->get()->sendResponse(dispatchable.CallId(), response);
    return;
}

namespace {


}  // namespace

void DomainDispatcherImpl::disable(crdtp::Dispatchable& dispatchable)
{
    // Prepare input parameters.

    std::unique_ptr<DomainDispatcher::WeakPtr> weak = weakPtr();
    DispatchResponse response = m_backend->Disable();
    if (response.IsFallThrough()) {
        dispatchable.DispatchFallThrough(response.Message());
        return;
    }
    if (weak->get())
        weak->get()->sendResponse(dispatchable.CallId(), response);
    return;
}

namespace {
// This helper method (with a static map of redirects) is used from Dispatcher::wire
// immediately below.
const std::vector<std::pair<crdtp::span<uint8_t>, crdtp::span<uint8_t>>>& SortedRedirects() {
  static auto* redirects = [](){
    auto* redirects = new std::vector<std::pair<crdtp::span<uint8_t>, crdtp::span<uint8_t>>>{
    };
    return redirects;
  }();
  return *redirects;
}
}  // namespace

// static
void Dispatcher::wire(UberDispatcher* uber, Backend* backend)
{
    auto dispatcher = std::make_unique<DomainDispatcherImpl>(uber->channel(), backend);
    uber->WireBackend(crdtp::SpanFrom("Preload"), SortedRedirects(), std::move(dispatcher));
}

} // Preload
} // namespace content
} // namespace protocol
