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

#include "components/autofill/content/browser/bad_message.h"

#include <algorithm>

#include "components/autofill/core/common/aliases.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_field_data.h"
#include "content/public/browser/render_frame_host.h"

namespace autofill::bad_message {

namespace internal {

bool CheckFieldInForm(const FormData& form, FieldRendererId field_id) {
  if (!std::ranges::contains(form.fields(), field_id,
                             &FormFieldData::renderer_id)) {
    mojo::ReportBadMessage("Unexpected FormData/FieldRendererId pair received");
    return false;
  }
  return true;
}

}  // namespace internal

bool CheckFrameNotPrerendering(content::RenderFrameHost* frame) {
  if (frame->IsInLifecycleState(
          content::RenderFrameHost::LifecycleState::kPrerendering)) {
    mojo::ReportBadMessage("Autofill is not allowed in a prerendering frame");
    return false;
  }
  return true;
}

}  // namespace autofill::bad_message
