// 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.

#ifndef COMPONENTS_AUTOFILL_IOS_BROWSER_AUTOFILL_JAVA_SCRIPT_FEATURE_H_
#define COMPONENTS_AUTOFILL_IOS_BROWSER_AUTOFILL_JAVA_SCRIPT_FEATURE_H_

#import <Foundation/Foundation.h>

#import "base/functional/callback.h"
#import "base/no_destructor.h"
#import "base/values.h"
#import "components/autofill/core/common/unique_ids.h"
#import "ios/web/public/js_messaging/java_script_feature.h"
#import "ios/web/public/js_messaging/script_message.h"

namespace web {
class WebFrame;
}  // namespace web

namespace autofill {

// Communicates with the JavaScript file, autofill_controller.js, which contains
// form parsing and autofill functions.
class AutofillJavaScriptFeature : public web::JavaScriptFeature {
 public:
  // This feature holds no state, so only a single static instance is ever
  // needed.
  static AutofillJavaScriptFeature* GetInstance();

  // Extracts forms from a web `frame`. Only forms with at least
  // `required_fields_count` fields are extracted. `callback` is called
  // with the JSON string of forms of a web page.  `callback` cannot be nil.
  void FetchForms(web::WebFrame* frame,
                  base::OnceCallback<void(NSString*)> callback);

  // Fills `data` into the active form field in `frame`, then executes the
  // `callback`. `callback` cannot be nil.
  void FillActiveFormField(web::WebFrame* frame,
                           base::DictValue data,
                           base::OnceCallback<void(BOOL)> callback);

  // Fills `data` into the field identified by `data['renderer_id']`,
  // then executes callback. This is similar to `FillActiveFormField`, but does
  // not require that the target element be the active element.
  void FillSpecificFormField(web::WebFrame* frame,
                             base::DictValue data,
                             base::OnceCallback<void(BOOL)> callback);

  // Fills a number of fields in the same named form for full-form Autofill.
  // Applies Autofill CSS (i.e. yellow background) to filled elements.
  // Only empty fields will be filled, except the focused field which will
  // always be filled even if non-empty. Fields must be contained in
  // `frame`. `callback` is called after the forms are filled with `data`
  // which must contain pairs of unique renderer ids of filled fields and
  // corresponding filled values. `callback` cannot be nil.
  void FillForm(web::WebFrame* frame,
                base::DictValue data,
                base::OnceCallback<void(NSString*)> callback);

  // Marks up the form with autofill field prediction data (diagnostic tool).
  void FillPredictionData(web::WebFrame* frame, base::DictValue data);

  // Scrolls the form field identified by `field` into view in `frame`.
  void ScrollFieldIntoView(web::WebFrame* frame, FieldRendererId field);

  // web::JavaScriptFeature:
  std::optional<std::string> GetScriptMessageHandlerName() const override;

 protected:
  // web::JavaScriptFeature:
  void ScriptMessageReceived(web::WebState* web_state,
                             const web::ScriptMessage& message) override;

 private:
  friend class base::NoDestructor<AutofillJavaScriptFeature>;
  // TODO(crbug.com/359538514): Remove friend once isolated world for Autofill
  // is launched.
  friend class TestAutofillJavaScriptFeatureContainer;

  AutofillJavaScriptFeature();
  ~AutofillJavaScriptFeature() override;

  AutofillJavaScriptFeature(const AutofillJavaScriptFeature&) = delete;
  AutofillJavaScriptFeature& operator=(const AutofillJavaScriptFeature&) =
      delete;
};

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_IOS_BROWSER_AUTOFILL_JAVA_SCRIPT_FEATURE_H_
