blob: 6c3325d55fdfa5f85e77dabd44c8cd975b4a9f78 [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"
14#include "third_party/blink/public/mojom/choosers/file_chooser.mojom-forward.h"
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0615#include "ui/shell_dialogs/select_file_dialog.h"
16
17namespace ui {
18struct SelectedFileInfo;
19}
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1720
21namespace content {
22
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0623class FileSelectListener;
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1724class RenderFrameHost;
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0625class WebContents;
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1726
27// This class handles file-selection requests coming from renderer processes.
28// It implements both the initialisation and listener functions for
29// file-selection dialogs.
30//
31// Since ShellFileSelectHelper listens to observations of a widget, it needs to
32// live on and be destroyed on the UI thread. References to
33// ShellFileSelectHelper may be passed on to other threads.
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1734class ShellFileSelectHelper
35 : public base::RefCountedThreadSafe<ShellFileSelectHelper,
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0636 BrowserThread::DeleteOnUIThread>,
37 public ui::SelectFileDialog::Listener {
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1738 public:
39 ShellFileSelectHelper(const ShellFileSelectHelper&) = delete;
40 ShellFileSelectHelper& operator=(const ShellFileSelectHelper&) = delete;
41
42 // Show the file chooser dialog.
43 static void RunFileChooser(content::RenderFrameHost* render_frame_host,
44 scoped_refptr<FileSelectListener> listener,
45 const blink::mojom::FileChooserParams& params);
46
47 private:
48 friend class base::RefCountedThreadSafe<ShellFileSelectHelper>;
49 friend class base::DeleteHelper<ShellFileSelectHelper>;
50 friend struct content::BrowserThread::DeleteOnThread<BrowserThread::UI>;
51
52 ShellFileSelectHelper();
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0653 ~ShellFileSelectHelper() override;
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1754
55 void RunFileChooser(content::RenderFrameHost* render_frame_host,
56 scoped_refptr<FileSelectListener> listener,
57 blink::mojom::FileChooserParamsPtr params);
Julie Jeongeun Kim8e989dd2023-03-30 09:27:0658
59 // Cleans up and releases this instance. This must be called after the last
60 // callback is received from the file chooser dialog.
61 void RunFileChooserEnd();
62
63 // SelectFileDialog::Listener:
64 void FileSelected(const base::FilePath& path,
65 int index,
66 void* params) override;
67 void FileSelectedWithExtraInfo(const ui::SelectedFileInfo& file,
68 int index,
69 void* params) override;
70 void MultiFilesSelected(const std::vector<base::FilePath>& files,
71 void* params) override;
72 void MultiFilesSelectedWithExtraInfo(
73 const std::vector<ui::SelectedFileInfo>& files,
74 void* params) override;
75 void FileSelectionCanceled(void* params) override;
76
77 // This method is called after the user has chosen the file(s) in the UI in
78 // order to process and filter the list before returning the final result to
79 // the caller.
80 void ConvertToFileChooserFileInfoList(
81 const std::vector<ui::SelectedFileInfo>& files);
82
83 // A weak pointer to the WebContents of the RenderFrameHost, for life checks.
84 base::WeakPtr<WebContents> web_contents_;
85
86 // |listener_| receives the result of the ShellFileSelectHelper.
87 scoped_refptr<FileSelectListener> listener_;
88
89 // Dialog box used for choosing files to upload from file form fields.
90 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
91
92 // The type of file dialog last shown.
93 ui::SelectFileDialog::Type dialog_type_ =
94 ui::SelectFileDialog::SELECT_OPEN_FILE;
95
96 // The mode of file dialog last shown.
97 blink::mojom::FileChooserParams::Mode dialog_mode_ =
98 blink::mojom::FileChooserParams::Mode::kOpen;
Julie Jeongeun Kim1f306fa42023-03-30 08:10:1799};
100
101} // namespace content
102
103#endif // CONTENT_SHELL_BROWSER_SHELL_FILE_SELECT_HELPER_H_