// Copyright 2020 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/renderer_host/text_input_host_impl.h"

#include "base/unguessable_token.h"
#include "content/browser/renderer_host/text_input_client_mac.h"
#include "content/public/browser/browser_thread.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"

namespace content {

TextInputHostImpl::TextInputHostImpl() = default;
TextInputHostImpl::~TextInputHostImpl() = default;

void TextInputHostImpl::Create(
    mojo::PendingReceiver<blink::mojom::TextInputHost> receiver) {
  CHECK_CURRENTLY_ON(BrowserThread::IO, base::NotFatalUntil::M154);
  mojo::MakeSelfOwnedReceiver(std::make_unique<TextInputHostImpl>(),
                              std::move(receiver));
}

void TextInputHostImpl::GotCharacterIndexAtPoint(
    const base::UnguessableToken& request_token,
    uint32_t index) {
  CHECK_CURRENTLY_ON(BrowserThread::IO, base::NotFatalUntil::M154);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  TextInputClientMac* service = TextInputClientMac::GetInstance();
  service->SetCharacterIndexAndSignal(
      TextInputClientMac::RequestToken(request_token), index);
}

void TextInputHostImpl::GotFirstRectForRange(
    const base::UnguessableToken& request_token,
    const gfx::Rect& rect) {
  CHECK_CURRENTLY_ON(BrowserThread::IO, base::NotFatalUntil::M154);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  TextInputClientMac* service = TextInputClientMac::GetInstance();
  service->SetFirstRectAndSignal(
      TextInputClientMac::RequestToken(request_token), rect);
}

}  // namespace content
