Julie Jeongeun Kim | 1f306fa4 | 2023-03-30 08:10:17 | [diff] [blame] | 1 | // 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 Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 8 | #include <memory> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/memory/weak_ptr.h" |
Julie Jeongeun Kim | 1f306fa4 | 2023-03-30 08:10:17 | [diff] [blame] | 12 | #include "content/public/browser/browser_thread.h" |
| 13 | #include "content/public/browser/file_select_listener.h" |
Julie Jeongeun Kim | 1567d9d | 2023-05-26 10:06:59 | [diff] [blame] | 14 | #include "net/base/directory_lister.h" |
Julie Jeongeun Kim | 1f306fa4 | 2023-03-30 08:10:17 | [diff] [blame] | 15 | #include "third_party/blink/public/mojom/choosers/file_chooser.mojom-forward.h" |
Julie Jeongeun Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 16 | #include "ui/shell_dialogs/select_file_dialog.h" |
| 17 | |
| 18 | namespace ui { |
| 19 | struct SelectedFileInfo; |
| 20 | } |
Julie Jeongeun Kim | 1f306fa4 | 2023-03-30 08:10:17 | [diff] [blame] | 21 | |
| 22 | namespace content { |
| 23 | |
Julie Jeongeun Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 24 | class FileSelectListener; |
Julie Jeongeun Kim | 1f306fa4 | 2023-03-30 08:10:17 | [diff] [blame] | 25 | class RenderFrameHost; |
Julie Jeongeun Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 26 | class WebContents; |
Julie Jeongeun Kim | 1f306fa4 | 2023-03-30 08:10:17 | [diff] [blame] | 27 | |
| 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 Kim | 1f306fa4 | 2023-03-30 08:10:17 | [diff] [blame] | 35 | class ShellFileSelectHelper |
| 36 | : public base::RefCountedThreadSafe<ShellFileSelectHelper, |
Julie Jeongeun Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 37 | BrowserThread::DeleteOnUIThread>, |
Julie Jeongeun Kim | 1567d9d | 2023-05-26 10:06:59 | [diff] [blame] | 38 | public ui::SelectFileDialog::Listener, |
| 39 | private net::DirectoryLister::DirectoryListerDelegate { |
Julie Jeongeun Kim | 1f306fa4 | 2023-03-30 08:10:17 | [diff] [blame] | 40 | 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 Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 55 | ~ShellFileSelectHelper() override; |
Julie Jeongeun Kim | 1f306fa4 | 2023-03-30 08:10:17 | [diff] [blame] | 56 | |
| 57 | void RunFileChooser(content::RenderFrameHost* render_frame_host, |
| 58 | scoped_refptr<FileSelectListener> listener, |
| 59 | blink::mojom::FileChooserParamsPtr params); |
Julie Jeongeun Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 60 | |
| 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 Kim | e69e6b10 | 2024-07-16 15:32:51 | [diff] [blame] | 66 | 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 Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 70 | |
Julie Jeongeun Kim | 1567d9d | 2023-05-26 10:06:59 | [diff] [blame] | 71 | // 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 Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 79 | // 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 Kim | 1567d9d | 2023-05-26 10:06:59 | [diff] [blame] | 85 | // 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 Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 95 | // 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 Kim | bc022bc | 2023-04-12 01:40:54 | [diff] [blame] | 104 | std::unique_ptr<ui::SelectFileDialog::FileTypeInfo> select_file_types_; |
| 105 | |
Julie Jeongeun Kim | 8e989dd | 2023-03-30 09:27:06 | [diff] [blame] | 106 | // 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 Kim | 1f306fa4 | 2023-03-30 08:10:17 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | } // namespace content |
| 116 | |
| 117 | #endif // CONTENT_SHELL_BROWSER_SHELL_FILE_SELECT_HELPER_H_ |