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

// This class pairs with DownloadController on Java side to forward requests
// for GET downloads to the current DownloadListener. POST downloads are
// handled on the native side.
//
// Both classes are Singleton classes. C++ object owns Java object.
//
// Call sequence
// GET downloads:
// DownloadController::CreateGETDownload() =>
// DownloadController.newHttpGetDownload() =>
// DownloadListener.onDownloadStart() /
// DownloadListener2.requestHttpGetDownload()
//

#ifndef CHROME_BROWSER_DOWNLOAD_ANDROID_DOWNLOAD_CONTROLLER_H_
#define CHROME_BROWSER_DOWNLOAD_ANDROID_DOWNLOAD_CONTROLLER_H_

#include <string>
#include <utility>

#include "base/android/scoped_java_ref.h"
#include "base/memory/singleton.h"
#include "chrome/browser/download/android/dangerous_download_dialog_bridge.h"
#include "chrome/browser/download/android/download_callback_validator.h"
#include "chrome/browser/download/android/download_controller_base.h"
#include "chrome/browser/download/android/policy_warning_download_dialog_bridge.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "components/safe_browsing/android/safe_browsing_api_handler_bridge.h"
#include "components/safe_browsing/android/safe_browsing_api_handler_util.h"

class DownloadUIModel;

class DownloadController : public DownloadControllerBase {
 public:
  static DownloadController* GetInstance();

  DownloadController(const DownloadController&) = delete;
  DownloadController& operator=(const DownloadController&) = delete;

  // DownloadControllerBase implementation.
  void CreateAndroidDownload(const content::WebContents::Getter& wc_getter,
                             const DownloadInfo& info) override;

  // UMA histogram enum for download storage permission requests. Keep this
  // in sync with MobileDownloadStoragePermission in histograms.xml. This should
  // be append only.
  enum StoragePermissionType {
    STORAGE_PERMISSION_REQUESTED = 0,
    STORAGE_PERMISSION_NO_ACTION_NEEDED,
    STORAGE_PERMISSION_GRANTED,
    STORAGE_PERMISSION_DENIED,
    STORAGE_PERMISSION_NO_WEB_CONTENTS,
    STORAGE_PERMISSION_MAX
  };
  static void RecordStoragePermission(StoragePermissionType type);

  // Close the |web_contents| for |download|. |download| could be null
  // if the download is created by Android DownloadManager.
  static void CloseTabIfEmpty(content::WebContents* web_contents,
                              download::DownloadItem* download);

  // Schedules the removal of a download item on the UI thread.
  static void ScheduleRemoveDownloadItem(download::DownloadItem* download);

  // Callback when user permission prompt finishes. Args: whether file access
  // permission is acquired, which permission to update.
  using AcquirePermissionCallback =
      base::OnceCallback<void(bool, const std::string&)>;

  DownloadCallbackValidator* validator() { return &validator_; }

  // The download item is not dangerous. Shows the DangerousDownloadDialog
  // (confirmation dialog for safe downloads). Returns whether the dialog was
  // shown.
  bool ShowDangerousDownloadDialog(download::DownloadItem* item);

 private:
  friend struct base::DefaultSingletonTraits<DownloadController>;
  friend class DownloadControllerTest;
  DownloadController();
  ~DownloadController() override;

  // DownloadControllerBase implementation.
  void OnDownloadStarted(download::DownloadItem* download_item) override;
  void StartContextMenuDownload(const GURL& url,
                                const content::ContextMenuParams& params,
                                content::WebContents* web_contents,
                                bool is_media) override;

  // DownloadItem::Observer interface.
  void OnDownloadUpdated(download::DownloadItem* item) override;
  void OnDownloadDestroyed(download::DownloadItem* item) override;

  // The download item contains dangerous file types. Shows the
  // DangerousDownloadDialog (generic dangerous filetype warning). Returns
  // whether the dialog was shown.
  bool OnDangerousDownload(download::DownloadItem* item);

  // The download item contains sensitive content. Shows the
  // PolicyWarningDownloadDialog. Returns whether the dialog was shown.
  bool OnSensitiveDownload(download::DownloadItem* item);

  // Shows the UI warnings from Safe Browsing malicious APK download check.
  // Returns whether the dialog was shown.
  bool ShowDangerousDownloadWarning(DownloadUIModel& model);

  // Helper methods to start android download on UI thread.
  void StartAndroidDownload(const content::WebContents::Getter& wc_getter,
                            const DownloadInfo& info);

  // Get profile key from download item.
  ProfileKey* GetProfileKey(download::DownloadItem* download_item);

  // Callback after we prompt the user to enable app verification.
  void EnableVerifyAppsDone(safe_browsing::VerifyAppsEnabledResult result);

  // Notify Java that download is complete, so the user can be informed.
  void OnDownloadComplete(download::DownloadItem* item);

  // Whether or not we should show an app verification prompt for `item`
  bool ShouldShowAppVerificationPrompt(download::DownloadItem* item);

  // Helper function to get the current active window.
  ui::WindowAndroid* GetWindowHelper(download::DownloadItem* item,
                                     bool should_schedule_removal,
                                     bool fallback_to_current_window);

  std::string default_file_name_;

  DownloadCallbackValidator validator_;

  std::unique_ptr<DangerousDownloadDialogBridge> dangerous_download_bridge_;

  std::unique_ptr<PolicyWarningDownloadDialogBridge>
      policy_warning_download_bridge_;

  // The item currently or previously doing an app verification
  // prompt. Because we show at most one at a time, this does not need
  // to be a set.
  raw_ptr<download::DownloadItem> app_verification_prompt_download_ = nullptr;
};

#endif  // CHROME_BROWSER_DOWNLOAD_ANDROID_DOWNLOAD_CONTROLLER_H_
