// Copyright 2024 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_NEW_FRAME_CATCHER_H_
#define COMPONENTS_AUTOFILL_IOS_BROWSER_NEW_FRAME_CATCHER_H_

#import <optional>
#import <string>

#import "base/scoped_observation.h"
#import "ios/web/public/js_messaging/web_frame.h"
#import "ios/web/public/js_messaging/web_frames_manager.h"

namespace autofill::test {

// Catcher that captures the latest new frame reported by `manager`.
class NewFrameCatcher : public web::WebFramesManager::Observer {
 public:
  explicit NewFrameCatcher(web::WebFramesManager* manager);
  ~NewFrameCatcher() override;

  // Returns the latest new frame that was observed. Returns nullptr if nothing
  // was seen.
  const std::optional<std::string>& latest_new_frame_id() {
    return latest_new_frame_id_;
  }

 private:
  void WebFrameBecameAvailable(web::WebFramesManager* web_frames_manager,
                               web::WebFrame* web_frame) override;

  std::optional<std::string> latest_new_frame_id_;
  base::ScopedObservation<web::WebFramesManager,
                          web::WebFramesManager::Observer>
      scoped_observer_{this};
};

}  // namespace autofill::test

#endif  // COMPONENTS_AUTOFILL_IOS_BROWSER_NEW_FRAME_CATCHER_H_
