// 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 "chrome/browser/devtools/protocol/ads.h"

#include "chrome/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 protocol {
namespace Ads {

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

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

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

CRDTP_BEGIN_DESERIALIZER(AdFrameData)
    CRDTP_DESERIALIZE_FIELD("cpuTime", m_cpuTime),
    CRDTP_DESERIALIZE_FIELD("frameId", m_frameId),
    CRDTP_DESERIALIZE_FIELD_OPT("initialOrigin", m_initialOrigin),
    CRDTP_DESERIALIZE_FIELD("networkBytes", m_networkBytes),
CRDTP_END_DESERIALIZER()

CRDTP_BEGIN_SERIALIZER(AdFrameData)
    CRDTP_SERIALIZE_FIELD("frameId", m_frameId);
    CRDTP_SERIALIZE_FIELD("initialOrigin", m_initialOrigin);
    CRDTP_SERIALIZE_FIELD("networkBytes", m_networkBytes);
    CRDTP_SERIALIZE_FIELD("cpuTime", m_cpuTime);
CRDTP_END_SERIALIZER();


CRDTP_BEGIN_DESERIALIZER(AdMetrics)
    CRDTP_DESERIALIZE_FIELD("averageViewportAdCount", m_averageViewportAdCount),
    CRDTP_DESERIALIZE_FIELD("averageViewportAdDensityByArea", m_averageViewportAdDensityByArea),
    CRDTP_DESERIALIZE_FIELD("removeAdFrames", m_removeAdFrames),
    CRDTP_DESERIALIZE_FIELD("totalAdCpuTime", m_totalAdCpuTime),
    CRDTP_DESERIALIZE_FIELD("totalAdNetworkBytes", m_totalAdNetworkBytes),
    CRDTP_DESERIALIZE_FIELD("updateAdFrames", m_updateAdFrames),
    CRDTP_DESERIALIZE_FIELD("viewportAdCount", m_viewportAdCount),
    CRDTP_DESERIALIZE_FIELD("viewportAdDensityByArea", m_viewportAdDensityByArea),
CRDTP_END_DESERIALIZER()

CRDTP_BEGIN_SERIALIZER(AdMetrics)
    CRDTP_SERIALIZE_FIELD("viewportAdDensityByArea", m_viewportAdDensityByArea);
    CRDTP_SERIALIZE_FIELD("averageViewportAdDensityByArea", m_averageViewportAdDensityByArea);
    CRDTP_SERIALIZE_FIELD("viewportAdCount", m_viewportAdCount);
    CRDTP_SERIALIZE_FIELD("averageViewportAdCount", m_averageViewportAdCount);
    CRDTP_SERIALIZE_FIELD("totalAdCpuTime", m_totalAdCpuTime);
    CRDTP_SERIALIZE_FIELD("totalAdNetworkBytes", m_totalAdNetworkBytes);
    CRDTP_SERIALIZE_FIELD("updateAdFrames", m_updateAdFrames);
    CRDTP_SERIALIZE_FIELD("removeAdFrames", m_removeAdFrames);
CRDTP_END_SERIALIZER();


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


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

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 getAdMetrics(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("getAdMetrics"),
          &DomainDispatcherImpl::getAdMetrics
    },
    };
    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::getAdMetrics(crdtp::Dispatchable& dispatchable)
{
    // Prepare input parameters.
    // Declare output parameters.
    std::unique_ptr<protocol::Ads::AdMetrics> out_metrics;

    std::unique_ptr<DomainDispatcher::WeakPtr> weak = weakPtr();
    DispatchResponse response = m_backend->GetAdMetrics(&out_metrics);
    if (response.IsFallThrough()) {
        dispatchable.DispatchFallThrough(response.Message());
        return;
    }
      if (weak->get()) {
        std::unique_ptr<crdtp::Serializable> result;
        if (response.IsSuccess()) {
          crdtp::ObjectSerializer serializer;
          serializer.AddField(crdtp::MakeSpan("metrics"), out_metrics);
          result = serializer.Finish();
        } else {
          result = Serializable::From({});
        }
        weak->get()->sendResponse(dispatchable.CallId(), response, std::move(result));
      }
    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("Ads"), SortedRedirects(), std::move(dispatcher));
}

} // Ads
} // namespace protocol
