blob: 9ef0947379cc92518ae79bed57bfe86e6d10db6a [file] [log] [blame]
Julie Jeongeun Kim1f306fa42023-03-30 08:10:171// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_SHELL_BROWSER_SHELL_FILE_SELECT_HELPER_H_
6#define CONTENT_SHELL_BROWSER_SHELL_FILE_SELECT_HELPER_H_
7
Julie Jeongeun Kim8e989dd2023-03-30 09:27:068#include <memory>
9#include <vector>
10
11#include "base/memory/weak_ptr.h"
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1712#include "content/public/browser/browser_thread.h"
13#include "content/public/browser/file_select_listener.h"
Julie Jeongeun Kim1567d9d2023-05-26 10:06:5914#include "net/base/directory_lister.h"
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1715#include "third_party/blink/public/mojom/choosers/file_chooser.mojom-forward.h"
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0616#include "ui/shell_dialogs/select_file_dialog.h"
17
18namespace ui {
19struct SelectedFileInfo;
20}
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1721
22namespace content {
23
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0624class FileSelectListener;
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1725class RenderFrameHost;
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0626class WebContents;
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1727
28// This class handles file-selection requests coming from renderer processes.
29// It implements both the initialisation and listener functions for
30// file-selection dialogs.
31//
32// Since ShellFileSelectHelper listens to observations of a widget, it needs to
33// live on and be destroyed on the UI thread. References to
34// ShellFileSelectHelper may be passed on to other threads.
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1735class ShellFileSelectHelper
36 : public base::RefCountedThreadSafe<ShellFileSelectHelper,
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0637 BrowserThread::DeleteOnUIThread>,
Julie Jeongeun Kim1567d9d2023-05-26 10:06:5938 public ui::SelectFileDialog::Listener,
39 private net::DirectoryLister::DirectoryListerDelegate {
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1740 public:
41 ShellFileSelectHelper(const ShellFileSelectHelper&) = delete;
42 ShellFileSelectHelper& operator=(const ShellFileSelectHelper&) = delete;
43
44 // Show the file chooser dialog.
45 static void RunFileChooser(content::RenderFrameHost* render_frame_host,
46 scoped_refptr<FileSelectListener> listener,
47 const blink::mojom::FileChooserParams& params);
48
49 private:
50 friend class base::RefCountedThreadSafe<ShellFileSelectHelper>;
51 friend class base::DeleteHelper<ShellFileSelectHelper>;
52 friend struct content::BrowserThread::DeleteOnThread<BrowserThread::UI>;
53
54 ShellFileSelectHelper();
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0655 ~ShellFileSelectHelper() override;
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1756
57 void RunFileChooser(content::RenderFrameHost* render_frame_host,
58 scoped_refptr<FileSelectListener> listener,
59 blink::mojom::FileChooserParamsPtr params);
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0660
61 // Cleans up and releases this instance. This must be called after the last
62 // callback is received from the file chooser dialog.
63 void RunFileChooserEnd();
64
65 // SelectFileDialog::Listener:
Gyuyoung Kime69e6b102024-07-16 15:32:5166 void FileSelected(const ui::SelectedFileInfo& file, int index) override;
67 void MultiFilesSelected(
68 const std::vector<ui::SelectedFileInfo>& files) override;
69 void FileSelectionCanceled() override;
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0670
Julie Jeongeun Kim1567d9d2023-05-26 10:06:5971 // Kicks off a new directory enumeration.
72 void StartNewEnumeration(const base::FilePath& path);
73
74 // net::DirectoryLister::DirectoryListerDelegate overrides.
75 void OnListFile(
76 const net::DirectoryLister::DirectoryListerData& data) override;
77 void OnListDone(int error) override;
78
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0679 // This method is called after the user has chosen the file(s) in the UI in
80 // order to process and filter the list before returning the final result to
81 // the caller.
82 void ConvertToFileChooserFileInfoList(
83 const std::vector<ui::SelectedFileInfo>& files);
84
Julie Jeongeun Kim1567d9d2023-05-26 10:06:5985 // The enumeration root directory for EnumerateDirectory() and
86 // RunFileChooser with kUploadFolder.
87 base::FilePath base_dir_;
88
89 // Maintain an active directory enumeration. These could come from the file
90 // select dialog or from drag-and-drop of directories. There could not be
91 // more than one going on at a time.
92 struct ActiveDirectoryEnumeration;
93 std::unique_ptr<ActiveDirectoryEnumeration> directory_enumeration_;
94
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0695 // A weak pointer to the WebContents of the RenderFrameHost, for life checks.
96 base::WeakPtr<WebContents> web_contents_;
97
98 // |listener_| receives the result of the ShellFileSelectHelper.
99 scoped_refptr<FileSelectListener> listener_;
100
101 // Dialog box used for choosing files to upload from file form fields.
102 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
103
Julie Jeongeun Kimbc022bc2023-04-12 01:40:54104 std::unique_ptr<ui::SelectFileDialog::FileTypeInfo> select_file_types_;
105
Julie Jeongeun Kim8e989dd2023-03-30 09:27:06106 // The type of file dialog last shown.
107 ui::SelectFileDialog::Type dialog_type_ =
108 ui::SelectFileDialog::SELECT_OPEN_FILE;
109
110 // The mode of file dialog last shown.
111 blink::mojom::FileChooserParams::Mode dialog_mode_ =
112 blink::mojom::FileChooserParams::Mode::kOpen;
Julie Jeongeun Kim1f306fa42023-03-30 08:10:17113};
114
115} // namespace content
116
117#endif // CONTENT_SHELL_BROWSER_SHELL_FILE_SELECT_HELPER_H_