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_
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"
27 struct SelectedFileInfo
;
30 // This class handles file-selection requests coming from WebUI elements
31 // (via the extensions::ExtensionHost class). It implements both the
32 // initialisation and listener functions for file-selection dialogs.
33 class FileSelectHelper
34 : public base::RefCountedThreadSafe
<FileSelectHelper
>,
35 public ui::SelectFileDialog::Listener
,
36 public content::NotificationObserver
{
39 // Show the file chooser dialog.
40 static void RunFileChooser(content::WebContents
* tab
,
41 const content::FileChooserParams
& params
);
43 // Enumerates all the files in directory.
44 static void EnumerateDirectory(content::WebContents
* tab
,
46 const base::FilePath
& path
);
49 friend class base::RefCountedThreadSafe
<FileSelectHelper
>;
50 FRIEND_TEST_ALL_PREFIXES(FileSelectHelperTest
, IsAcceptTypeValid
);
51 FRIEND_TEST_ALL_PREFIXES(FileSelectHelperTest
, ZipPackage
);
52 explicit FileSelectHelper(Profile
* profile
);
53 virtual ~FileSelectHelper();
55 // Utility class which can listen for directory lister events and relay
56 // them to the main object with the correct tracking id.
57 class DirectoryListerDispatchDelegate
58 : public net::DirectoryLister::DirectoryListerDelegate
{
60 DirectoryListerDispatchDelegate(FileSelectHelper
* parent
, int id
)
63 virtual ~DirectoryListerDispatchDelegate() {}
64 virtual void OnListFile(
65 const net::DirectoryLister::DirectoryListerData
& data
) override
;
66 virtual void OnListDone(int error
) override
;
68 // This FileSelectHelper owns this object.
69 FileSelectHelper
* parent_
;
72 DISALLOW_COPY_AND_ASSIGN(DirectoryListerDispatchDelegate
);
75 void RunFileChooser(content::RenderViewHost
* render_view_host
,
76 content::WebContents
* web_contents
,
77 const content::FileChooserParams
& params
);
78 void RunFileChooserOnFileThread(
79 const content::FileChooserParams
& params
);
80 void RunFileChooserOnUIThread(
81 const content::FileChooserParams
& params
);
83 // Cleans up and releases this instance. This must be called after the last
84 // callback is received from the file chooser dialog.
85 void RunFileChooserEnd();
87 // SelectFileDialog::Listener overrides.
88 virtual void FileSelected(
89 const base::FilePath
& path
, int index
, void* params
) override
;
90 virtual void FileSelectedWithExtraInfo(
91 const ui::SelectedFileInfo
& file
,
93 void* params
) override
;
94 virtual void MultiFilesSelected(const std::vector
<base::FilePath
>& files
,
95 void* params
) override
;
96 virtual void MultiFilesSelectedWithExtraInfo(
97 const std::vector
<ui::SelectedFileInfo
>& files
,
98 void* params
) override
;
99 virtual void FileSelectionCanceled(void* params
) override
;
101 // content::NotificationObserver overrides.
102 virtual void Observe(int type
,
103 const content::NotificationSource
& source
,
104 const content::NotificationDetails
& details
) override
;
106 void EnumerateDirectory(int request_id
,
107 content::RenderViewHost
* render_view_host
,
108 const base::FilePath
& path
);
110 // Kicks off a new directory enumeration.
111 void StartNewEnumeration(const base::FilePath
& path
,
113 content::RenderViewHost
* render_view_host
);
115 // Callbacks from directory enumeration.
116 virtual void OnListFile(
118 const net::DirectoryLister::DirectoryListerData
& data
);
119 virtual void OnListDone(int id
, int error
);
121 // Cleans up and releases this instance. This must be called after the last
122 // callback is received from the enumeration code.
123 void EnumerateDirectoryEnd();
125 #if defined(OS_MACOSX) && !defined(OS_IOS)
126 // Must be called on the FILE_USER_BLOCKING thread. Each selected file that is
127 // a package will be zipped, and the zip will be passed to the render view
128 // host in place of the package.
129 void ProcessSelectedFilesMac(const std::vector
<ui::SelectedFileInfo
>& files
);
131 // Saves the paths of |zipped_files| for later deletion. Passes |files| to the
133 void ProcessSelectedFilesMacOnUIThread(
134 const std::vector
<ui::SelectedFileInfo
>& files
,
135 const std::vector
<base::FilePath
>& zipped_files
);
137 // Zips the package at |path| into a temporary destination. Returns the
138 // temporary destination, if the zip was successful. Otherwise returns an
140 static base::FilePath
ZipPackage(const base::FilePath
& path
);
141 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
143 // Utility method that passes |files| to the render view host, and ends the
145 void NotifyRenderViewHostAndEnd(
146 const std::vector
<ui::SelectedFileInfo
>& files
);
148 // Schedules the deletion of the files in |temporary_files_| and clears the
150 void DeleteTemporaryFiles();
152 // Helper method to get allowed extensions for select file dialog from
153 // the specified accept types as defined in the spec:
154 // http://whatwg.org/html/number-state.html#attr-input-accept
155 // |accept_types| contains only valid lowercased MIME types or file extensions
156 // beginning with a period (.).
157 static scoped_ptr
<ui::SelectFileDialog::FileTypeInfo
>
158 GetFileTypesFromAcceptType(
159 const std::vector
<base::string16
>& accept_types
);
161 // Check the accept type is valid. It is expected to be all lower case with
163 static bool IsAcceptTypeValid(const std::string
& accept_type
);
165 // Profile used to set/retrieve the last used directory.
168 // The RenderViewHost and WebContents for the page showing a file dialog
169 // (may only be one such dialog).
170 content::RenderViewHost
* render_view_host_
;
171 content::WebContents
* web_contents_
;
173 // Dialog box used for choosing files to upload from file form fields.
174 scoped_refptr
<ui::SelectFileDialog
> select_file_dialog_
;
175 scoped_ptr
<ui::SelectFileDialog::FileTypeInfo
> select_file_types_
;
177 // The type of file dialog last shown.
178 ui::SelectFileDialog::Type dialog_type_
;
180 // The mode of file dialog last shown.
181 content::FileChooserParams::Mode dialog_mode_
;
183 // Maintain a list of active directory enumerations. These could come from
184 // the file select dialog or from drag-and-drop of directories, so there could
185 // be more than one going on at a time.
186 struct ActiveDirectoryEnumeration
;
187 std::map
<int, ActiveDirectoryEnumeration
*> directory_enumerations_
;
189 // Registrar for notifications regarding our RenderViewHost.
190 content::NotificationRegistrar notification_registrar_
;
192 // Temporary files only used on OSX. This class is responsible for deleting
193 // these files when they are no longer needed.
194 std::vector
<base::FilePath
> temporary_files_
;
196 DISALLOW_COPY_AND_ASSIGN(FileSelectHelper
);
199 #endif // CHROME_BROWSER_FILE_SELECT_HELPER_H_