1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117

content / shell / browser / shell_file_select_helper.h [blame]

// 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 CONTENT_SHELL_BROWSER_SHELL_FILE_SELECT_HELPER_H_
#define CONTENT_SHELL_BROWSER_SHELL_FILE_SELECT_HELPER_H_

#include <memory>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/file_select_listener.h"
#include "net/base/directory_lister.h"
#include "third_party/blink/public/mojom/choosers/file_chooser.mojom-forward.h"
#include "ui/shell_dialogs/select_file_dialog.h"

namespace ui {
struct SelectedFileInfo;
}

namespace content {

class FileSelectListener;
class RenderFrameHost;
class WebContents;

// This class handles file-selection requests coming from renderer processes.
// It implements both the initialisation and listener functions for
// file-selection dialogs.
//
// Since ShellFileSelectHelper listens to observations of a widget, it needs to
// live on and be destroyed on the UI thread. References to
// ShellFileSelectHelper may be passed on to other threads.
class ShellFileSelectHelper
    : public base::RefCountedThreadSafe<ShellFileSelectHelper,
                                        BrowserThread::DeleteOnUIThread>,
      public ui::SelectFileDialog::Listener,
      private net::DirectoryLister::DirectoryListerDelegate {
 public:
  ShellFileSelectHelper(const ShellFileSelectHelper&) = delete;
  ShellFileSelectHelper& operator=(const ShellFileSelectHelper&) = delete;

  // Show the file chooser dialog.
  static void RunFileChooser(content::RenderFrameHost* render_frame_host,
                             scoped_refptr<FileSelectListener> listener,
                             const blink::mojom::FileChooserParams& params);

 private:
  friend class base::RefCountedThreadSafe<ShellFileSelectHelper>;
  friend class base::DeleteHelper<ShellFileSelectHelper>;
  friend struct content::BrowserThread::DeleteOnThread<BrowserThread::UI>;

  ShellFileSelectHelper();
  ~ShellFileSelectHelper() override;

  void RunFileChooser(content::RenderFrameHost* render_frame_host,
                      scoped_refptr<FileSelectListener> listener,
                      blink::mojom::FileChooserParamsPtr params);

  // Cleans up and releases this instance. This must be called after the last
  // callback is received from the file chooser dialog.
  void RunFileChooserEnd();

  // SelectFileDialog::Listener:
  void FileSelected(const ui::SelectedFileInfo& file, int index) override;
  void MultiFilesSelected(
      const std::vector<ui::SelectedFileInfo>& files) override;
  void FileSelectionCanceled() override;

  // Kicks off a new directory enumeration.
  void StartNewEnumeration(const base::FilePath& path);

  // net::DirectoryLister::DirectoryListerDelegate overrides.
  void OnListFile(
      const net::DirectoryLister::DirectoryListerData& data) override;
  void OnListDone(int error) override;

  // This method is called after the user has chosen the file(s) in the UI in
  // order to process and filter the list before returning the final result to
  // the caller.
  void ConvertToFileChooserFileInfoList(
      const std::vector<ui::SelectedFileInfo>& files);

  // The enumeration root directory for EnumerateDirectory() and
  // RunFileChooser with kUploadFolder.
  base::FilePath base_dir_;

  // Maintain an active directory enumeration.  These could come from the file
  // select dialog or from drag-and-drop of directories.  There could not be
  // more than one going on at a time.
  struct ActiveDirectoryEnumeration;
  std::unique_ptr<ActiveDirectoryEnumeration> directory_enumeration_;

  // A weak pointer to the WebContents of the RenderFrameHost, for life checks.
  base::WeakPtr<WebContents> web_contents_;

  // |listener_| receives the result of the ShellFileSelectHelper.
  scoped_refptr<FileSelectListener> listener_;

  // Dialog box used for choosing files to upload from file form fields.
  scoped_refptr<ui::SelectFileDialog> select_file_dialog_;

  std::unique_ptr<ui::SelectFileDialog::FileTypeInfo> select_file_types_;

  // The type of file dialog last shown.
  ui::SelectFileDialog::Type dialog_type_ =
      ui::SelectFileDialog::SELECT_OPEN_FILE;

  // The mode of file dialog last shown.
  blink::mojom::FileChooserParams::Mode dialog_mode_ =
      blink::mojom::FileChooserParams::Mode::kOpen;
};

}  // namespace content

#endif  // CONTENT_SHELL_BROWSER_SHELL_FILE_SELECT_HELPER_H_