Prevent chrome://net-internals/#export from flickering
[chromium-blink-merge.git] / chrome / browser / file_select_helper.h
blob06672ba86847110cf55ae3a0229a87958d9496e0
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_FILE_SELECT_HELPER_H_
6 #define CHROME_BROWSER_FILE_SELECT_HELPER_H_
8 #include <map>
9 #include <vector>
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/common/file_chooser_params.h"
16 #include "net/base/directory_lister.h"
17 #include "ui/shell_dialogs/select_file_dialog.h"
19 class Profile;
21 namespace content {
22 struct FileChooserFileInfo;
23 class RenderViewHost;
24 class WebContents;
27 namespace ui {
28 struct SelectedFileInfo;
31 // This class handles file-selection requests coming from WebUI elements
32 // (via the extensions::ExtensionHost class). It implements both the
33 // initialisation and listener functions for file-selection dialogs.
34 class FileSelectHelper
35 : public base::RefCountedThreadSafe<FileSelectHelper>,
36 public ui::SelectFileDialog::Listener,
37 public content::NotificationObserver {
38 public:
40 // Show the file chooser dialog.
41 static void RunFileChooser(content::WebContents* tab,
42 const content::FileChooserParams& params);
44 // Enumerates all the files in directory.
45 static void EnumerateDirectory(content::WebContents* tab,
46 int request_id,
47 const base::FilePath& path);
49 private:
50 friend class base::RefCountedThreadSafe<FileSelectHelper>;
51 FRIEND_TEST_ALL_PREFIXES(FileSelectHelperTest, IsAcceptTypeValid);
52 FRIEND_TEST_ALL_PREFIXES(FileSelectHelperTest, ZipPackage);
53 explicit FileSelectHelper(Profile* profile);
54 ~FileSelectHelper() override;
56 // Utility class which can listen for directory lister events and relay
57 // them to the main object with the correct tracking id.
58 class DirectoryListerDispatchDelegate
59 : public net::DirectoryLister::DirectoryListerDelegate {
60 public:
61 DirectoryListerDispatchDelegate(FileSelectHelper* parent, int id)
62 : parent_(parent),
63 id_(id) {}
64 ~DirectoryListerDispatchDelegate() override {}
65 void OnListFile(
66 const net::DirectoryLister::DirectoryListerData& data) override;
67 void OnListDone(int error) override;
69 private:
70 // This FileSelectHelper owns this object.
71 FileSelectHelper* parent_;
72 int id_;
74 DISALLOW_COPY_AND_ASSIGN(DirectoryListerDispatchDelegate);
77 void RunFileChooser(content::RenderViewHost* render_view_host,
78 content::WebContents* web_contents,
79 const content::FileChooserParams& params);
80 void RunFileChooserOnFileThread(
81 const content::FileChooserParams& params);
82 void RunFileChooserOnUIThread(
83 const content::FileChooserParams& params);
85 // Cleans up and releases this instance. This must be called after the last
86 // callback is received from the file chooser dialog.
87 void RunFileChooserEnd();
89 // SelectFileDialog::Listener overrides.
90 void FileSelected(const base::FilePath& path,
91 int index,
92 void* params) override;
93 void FileSelectedWithExtraInfo(const ui::SelectedFileInfo& file,
94 int index,
95 void* params) override;
96 void MultiFilesSelected(const std::vector<base::FilePath>& files,
97 void* params) override;
98 void MultiFilesSelectedWithExtraInfo(
99 const std::vector<ui::SelectedFileInfo>& files,
100 void* params) override;
101 void FileSelectionCanceled(void* params) override;
103 // content::NotificationObserver overrides.
104 void Observe(int type,
105 const content::NotificationSource& source,
106 const content::NotificationDetails& details) override;
108 void EnumerateDirectory(int request_id,
109 content::RenderViewHost* render_view_host,
110 const base::FilePath& path);
112 // Kicks off a new directory enumeration.
113 void StartNewEnumeration(const base::FilePath& path,
114 int request_id,
115 content::RenderViewHost* render_view_host);
117 // Callbacks from directory enumeration.
118 virtual void OnListFile(
119 int id,
120 const net::DirectoryLister::DirectoryListerData& data);
121 virtual void OnListDone(int id, int error);
123 // Cleans up and releases this instance. This must be called after the last
124 // callback is received from the enumeration code.
125 void EnumerateDirectoryEnd();
127 #if defined(OS_MACOSX) && !defined(OS_IOS)
128 // Must be called on the FILE_USER_BLOCKING thread. Each selected file that is
129 // a package will be zipped, and the zip will be passed to the render view
130 // host in place of the package.
131 void ProcessSelectedFilesMac(const std::vector<ui::SelectedFileInfo>& files);
133 // Saves the paths of |zipped_files| for later deletion. Passes |files| to the
134 // render view host.
135 void ProcessSelectedFilesMacOnUIThread(
136 const std::vector<ui::SelectedFileInfo>& files,
137 const std::vector<base::FilePath>& zipped_files);
139 // Zips the package at |path| into a temporary destination. Returns the
140 // temporary destination, if the zip was successful. Otherwise returns an
141 // empty path.
142 static base::FilePath ZipPackage(const base::FilePath& path);
143 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
145 // Utility method that passes |files| to the render view host, and ends the
146 // file chooser.
147 void NotifyRenderViewHostAndEnd(
148 const std::vector<ui::SelectedFileInfo>& files);
150 // Sends the result to the render process, and call |RunFileChooserEnd|.
151 void NotifyRenderViewHostAndEndAfterConversion(
152 const std::vector<content::FileChooserFileInfo>& list);
154 // Schedules the deletion of the files in |temporary_files_| and clears the
155 // vector.
156 void DeleteTemporaryFiles();
158 // Helper method to get allowed extensions for select file dialog from
159 // the specified accept types as defined in the spec:
160 // http://whatwg.org/html/number-state.html#attr-input-accept
161 // |accept_types| contains only valid lowercased MIME types or file extensions
162 // beginning with a period (.).
163 static scoped_ptr<ui::SelectFileDialog::FileTypeInfo>
164 GetFileTypesFromAcceptType(
165 const std::vector<base::string16>& accept_types);
167 // Check the accept type is valid. It is expected to be all lower case with
168 // no whitespace.
169 static bool IsAcceptTypeValid(const std::string& accept_type);
171 // Profile used to set/retrieve the last used directory.
172 Profile* profile_;
174 // The RenderViewHost and WebContents for the page showing a file dialog
175 // (may only be one such dialog).
176 content::RenderViewHost* render_view_host_;
177 content::WebContents* web_contents_;
179 // Dialog box used for choosing files to upload from file form fields.
180 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
181 scoped_ptr<ui::SelectFileDialog::FileTypeInfo> select_file_types_;
183 // The type of file dialog last shown.
184 ui::SelectFileDialog::Type dialog_type_;
186 // The mode of file dialog last shown.
187 content::FileChooserParams::Mode dialog_mode_;
189 // Maintain a list of active directory enumerations. These could come from
190 // the file select dialog or from drag-and-drop of directories, so there could
191 // be more than one going on at a time.
192 struct ActiveDirectoryEnumeration;
193 std::map<int, ActiveDirectoryEnumeration*> directory_enumerations_;
195 // Registrar for notifications regarding our RenderViewHost.
196 content::NotificationRegistrar notification_registrar_;
198 // Temporary files only used on OSX. This class is responsible for deleting
199 // these files when they are no longer needed.
200 std::vector<base::FilePath> temporary_files_;
202 DISALLOW_COPY_AND_ASSIGN(FileSelectHelper);
205 #endif // CHROME_BROWSER_FILE_SELECT_HELPER_H_