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

#ifndef CHROME_BROWSER_SUPERVISED_USER_CHROMEOS_SUPERVISED_USER_WEB_CONTENT_HANDLER_IMPL_H_
#define CHROME_BROWSER_SUPERVISED_USER_CHROMEOS_SUPERVISED_USER_WEB_CONTENT_HANDLER_IMPL_H_

#include <memory>
#include <string>

#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/supervised_user/chrome_supervised_user_web_content_handler_base.h"
#include "chrome/browser/ui/webui/ash/parent_access/parent_access_dialog.h"
#include "content/public/browser/frame_tree_node_id.h"
#include "url/gurl.h"

class Profile;

namespace favicon {
class LargeIconService;
}  // namespace favicon

class SupervisedUserFaviconRequestHandler;

// Chrome OS specific implementation of web content handler.
class SupervisedUserWebContentHandlerImpl
    : public ChromeSupervisedUserWebContentHandlerBase {
 public:
  SupervisedUserWebContentHandlerImpl(
      content::WebContents* web_contents,
      const GURL& url,
      favicon::LargeIconService& large_icon_service,
      content::FrameTreeNodeId frame_id,
      int64_t interstitial_navigation_id);

  SupervisedUserWebContentHandlerImpl(
      const SupervisedUserWebContentHandlerImpl&) = delete;
  SupervisedUserWebContentHandlerImpl& operator=(
      const SupervisedUserWebContentHandlerImpl&) = delete;
  ~SupervisedUserWebContentHandlerImpl() override;

  // ChromeSupervisedUserWebContentHandlerBase implementation:
  void RequestLocalApproval(
      const GURL& target_url,
      supervised_user::WebFilteringResult filtering_result,
      const std::u16string& child_display_name,
      ApprovalRequestInitiatedCallback callback) override;

 private:
  void OnLocalApprovalRequestCompleted(
      supervised_user::FamilyLinkSettingsService& family_link_settings_service,
      const GURL& url,
      base::TimeTicks start_time,
      std::unique_ptr<ash::ParentAccessDialog::Result> result);

  // Helpers for private method testing.
  FRIEND_TEST_ALL_PREFIXES(SupervisedUserWebContentHandlerImplTest,
                           LocalWebApprovalApprovedChromeOSTest);
  FRIEND_TEST_ALL_PREFIXES(SupervisedUserWebContentHandlerImplTest,
                           LocalWebApprovalDeclinedChromeOSTest);
  FRIEND_TEST_ALL_PREFIXES(SupervisedUserWebContentHandlerImplTest,
                           LocalWebApprovalCanceledChromeOSTest);
  FRIEND_TEST_ALL_PREFIXES(SupervisedUserWebContentHandlerImplTest,
                           LocalWebApprovalErrorChromeOSTest);

  std::unique_ptr<SupervisedUserFaviconRequestHandler> favicon_handler_;
  const raw_ref<Profile> profile_;
  std::unique_ptr<ash::ParentAccessDialogProvider> dialog_provider_;
  base::WeakPtrFactory<SupervisedUserWebContentHandlerImpl> weak_ptr_factory_{
      this};
};

#endif  // CHROME_BROWSER_SUPERVISED_USER_CHROMEOS_SUPERVISED_USER_WEB_CONTENT_HANDLER_IMPL_H_
