1 // Copyright (c) 2011 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_
12 #include "base/compiler_specific.h"
13 #include "chrome/browser/ui/select_file_dialog.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "net/base/directory_lister.h"
22 struct FileChooserParams
;
25 // This class handles file-selection requests coming from WebUI elements
26 // (via the ExtensionHost class). It implements both the initialisation
27 // and listener functions for file-selection dialogs.
28 class FileSelectHelper
29 : public base::RefCountedThreadSafe
<FileSelectHelper
>,
30 public SelectFileDialog::Listener
,
31 public content::NotificationObserver
{
33 explicit FileSelectHelper(Profile
* profile
);
35 // Show the file chooser dialog.
36 void RunFileChooser(RenderViewHost
* render_view_host
,
37 content::WebContents
* tab_contents
,
38 const content::FileChooserParams
& params
);
40 // Enumerates all the files in directory.
41 void EnumerateDirectory(int request_id
,
42 RenderViewHost
* render_view_host
,
43 const FilePath
& path
);
46 friend class base::RefCountedThreadSafe
<FileSelectHelper
>;
47 virtual ~FileSelectHelper();
49 // Utility class which can listen for directory lister events and relay
50 // them to the main object with the correct tracking id.
51 class DirectoryListerDispatchDelegate
52 : public net::DirectoryLister::DirectoryListerDelegate
{
54 DirectoryListerDispatchDelegate(FileSelectHelper
* parent
, int id
)
57 virtual ~DirectoryListerDispatchDelegate() {}
58 virtual void OnListFile(
59 const net::DirectoryLister::DirectoryListerData
& data
) OVERRIDE
{
60 parent_
->OnListFile(id_
, data
);
62 virtual void OnListDone(int error
) OVERRIDE
{
63 parent_
->OnListDone(id_
, error
);
66 // This FileSelectHelper owns this object.
67 FileSelectHelper
* parent_
;
70 DISALLOW_COPY_AND_ASSIGN(DirectoryListerDispatchDelegate
);
73 void RunFileChooserOnFileThread(
74 const content::FileChooserParams
& params
);
75 void RunFileChooserOnUIThread(
76 const content::FileChooserParams
& params
);
78 // Cleans up and releases this instance. This must be called after the last
79 // callback is received from the file chooser dialog.
80 void RunFileChooserEnd();
82 // SelectFileDialog::Listener overrides.
83 virtual void FileSelected(
84 const FilePath
& path
, int index
, void* params
) OVERRIDE
;
85 virtual void MultiFilesSelected(const std::vector
<FilePath
>& files
,
86 void* params
) OVERRIDE
;
87 virtual void FileSelectionCanceled(void* params
) OVERRIDE
;
89 // content::NotificationObserver overrides.
90 virtual void Observe(int type
,
91 const content::NotificationSource
& source
,
92 const content::NotificationDetails
& details
) OVERRIDE
;
94 // Kicks off a new directory enumeration.
95 void StartNewEnumeration(const FilePath
& path
,
97 RenderViewHost
* render_view_host
);
99 // Callbacks from directory enumeration.
100 virtual void OnListFile(
102 const net::DirectoryLister::DirectoryListerData
& data
);
103 virtual void OnListDone(int id
, int error
);
105 // Cleans up and releases this instance. This must be called after the last
106 // callback is received from the enumeration code.
107 void EnumerateDirectoryEnd();
109 // Helper method to get allowed extensions for select file dialog from
110 // the specified accept types as defined in the spec:
111 // http://whatwg.org/html/number-state.html#attr-input-accept
112 // |accept_types| contains only valid lowercased MIME types.
113 SelectFileDialog::FileTypeInfo
* GetFileTypesFromAcceptType(
114 const std::vector
<string16
>& accept_types
);
116 // Profile used to set/retrieve the last used directory.
119 // The RenderViewHost and WebContents for the page showing a file dialog
120 // (may only be one such dialog).
121 RenderViewHost
* render_view_host_
;
122 content::WebContents
* web_contents_
;
124 // Dialog box used for choosing files to upload from file form fields.
125 scoped_refptr
<SelectFileDialog
> select_file_dialog_
;
126 scoped_ptr
<SelectFileDialog::FileTypeInfo
> select_file_types_
;
128 // The type of file dialog last shown.
129 SelectFileDialog::Type dialog_type_
;
131 // Maintain a list of active directory enumerations. These could come from
132 // the file select dialog or from drag-and-drop of directories, so there could
133 // be more than one going on at a time.
134 struct ActiveDirectoryEnumeration
;
135 std::map
<int, ActiveDirectoryEnumeration
*> directory_enumerations_
;
137 // Registrar for notifications regarding our RenderViewHost.
138 content::NotificationRegistrar notification_registrar_
;
140 DISALLOW_COPY_AND_ASSIGN(FileSelectHelper
);
143 #endif // CHROME_BROWSER_FILE_SELECT_HELPER_H_