1 // Copyright (c) 2013 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 UI_SHELL_DIALOGS_SELECT_FILE_DIALOG_H_
6 #define UI_SHELL_DIALOGS_SELECT_FILE_DIALOG_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/string16.h"
17 #include "ui/gfx/native_widget_types.h"
18 #include "ui/shell_dialogs/base_shell_dialog.h"
19 #include "ui/shell_dialogs/shell_dialogs_export.h"
22 class SelectFileDialogFactory
;
23 class SelectFilePolicy
;
24 struct SelectedFileInfo
;
25 class ShellDialogsDelegate
;
27 // Shows a dialog box for selecting a file or a folder.
28 class SHELL_DIALOGS_EXPORT SelectFileDialog
29 : public base::RefCountedThreadSafe
<SelectFileDialog
>,
30 public ui::BaseShellDialog
{
37 SELECT_OPEN_MULTI_FILE
40 // An interface implemented by a Listener object wishing to know about the
41 // the result of the Select File/Folder action. These callbacks must be
43 class SHELL_DIALOGS_EXPORT Listener
{
45 // Notifies the Listener that a file/folder selection has been made. The
46 // file/folder path is in |path|. |params| is contextual passed to
47 // SelectFile. |index| specifies the index of the filter passed to the
48 // the initial call to SelectFile.
49 virtual void FileSelected(const base::FilePath
& path
,
50 int index
, void* params
) = 0;
52 // Similar to FileSelected() but takes SelectedFileInfo instead of
53 // base::FilePath. Used for passing extra information (ex. display name).
55 // If not overridden, calls FileSelected() with path from |file|.
56 virtual void FileSelectedWithExtraInfo(
57 const ui::SelectedFileInfo
& file
,
61 // Notifies the Listener that many files have been selected. The
62 // files are in |files|. |params| is contextual passed to SelectFile.
63 virtual void MultiFilesSelected(
64 const std::vector
<base::FilePath
>& files
, void* params
) {}
66 // Similar to MultiFilesSelected() but takes SelectedFileInfo instead of
67 // base::FilePath. Used for passing extra information (ex. display name).
69 // If not overridden, calls MultiFilesSelected() with paths from |files|.
70 virtual void MultiFilesSelectedWithExtraInfo(
71 const std::vector
<ui::SelectedFileInfo
>& files
,
74 // Notifies the Listener that the file/folder selection was aborted (via
75 // the user canceling or closing the selection dialog box, for example).
76 // |params| is contextual passed to SelectFile.
77 virtual void FileSelectionCanceled(void* params
) {}
80 virtual ~Listener() {}
83 // Sets the factory that creates SelectFileDialog objects, overriding default
86 // This is optional and should only be used by components that have to live
87 // elsewhere in the tree due to layering violations. (For example, because of
88 // a dependency on chrome's extension system.)
89 static void SetFactory(ui::SelectFileDialogFactory
* factory
);
91 // Creates a dialog box helper. This is an inexpensive wrapper around the
92 // platform-native file selection dialog. |policy| is an optional class that
93 // can prevent showing a dialog.
94 static scoped_refptr
<SelectFileDialog
> Create(Listener
* listener
,
95 ui::SelectFilePolicy
* policy
);
97 // Holds information about allowed extensions on a file save dialog.
98 struct SHELL_DIALOGS_EXPORT FileTypeInfo
{
102 // A list of allowed extensions. For example, it might be
104 // { { "htm", "html" }, { "txt" } }
106 // Only pass more than one extension in the inner vector if the extensions
107 // are equivalent. Do NOT include leading periods.
108 std::vector
<std::vector
<base::FilePath::StringType
> > extensions
;
110 // Overrides the system descriptions of the specified extensions. Entries
111 // correspond to |extensions|; if left blank the system descriptions will
113 std::vector
<string16
> extension_description_overrides
;
115 // Specifies whether there will be a filter added for all files (i.e. *.*).
116 bool include_all_files
;
118 // Specifies whether the caller can support files/folders that are on Drive.
123 // Before doing anything this function checks if FileBrowsing is forbidden
124 // by Policy. If so, it tries to show an InfoBar and behaves as though no File
125 // was selected (the user clicked `Cancel` immediately).
126 // Otherwise it will start displaying the dialog box. This will also
127 // block the calling window until the dialog box is complete. The listener
128 // associated with this object will be notified when the selection is
130 // |type| is the type of file dialog to be shown, see Type enumeration above.
131 // |title| is the title to be displayed in the dialog. If this string is
132 // empty, the default title is used.
133 // |default_path| is the default path and suggested file name to be shown in
134 // the dialog. This only works for SELECT_SAVEAS_FILE and SELECT_OPEN_FILE.
135 // Can be an empty string to indicate the platform default.
136 // |file_types| holds the information about the file types allowed. Pass NULL
137 // to get no special behavior
138 // |file_type_index| is the 1-based index into the file type list in
139 // |file_types|. Specify 0 if you don't need to specify extension behavior.
140 // |default_extension| is the default extension to add to the file if the
141 // user doesn't type one. This should NOT include the '.'. On Windows, if
142 // you specify this you must also specify |file_types|.
143 // |owning_window| is the window the dialog is modal to, or NULL for a
145 // |params| is data from the calling context which will be passed through to
146 // the listener. Can be NULL.
147 // NOTE: only one instance of any shell dialog can be shown per owning_window
148 // at a time (for obvious reasons).
149 void SelectFile(Type type
,
150 const string16
& title
,
151 const base::FilePath
& default_path
,
152 const FileTypeInfo
* file_types
,
154 const base::FilePath::StringType
& default_extension
,
155 gfx::NativeWindow owning_window
,
157 bool HasMultipleFileTypeChoices();
159 // Sets the global ShellDialogsDelegate. Defaults to NULL.
160 static void SetShellDialogsDelegate(ShellDialogsDelegate
* delegate
);
163 friend class base::RefCountedThreadSafe
<SelectFileDialog
>;
164 explicit SelectFileDialog(Listener
* listener
,
165 ui::SelectFilePolicy
* policy
);
166 virtual ~SelectFileDialog();
168 // Displays the actual file-selection dialog.
169 // This is overridden in the platform-specific descendants of FileSelectDialog
170 // and gets called from SelectFile after testing the
171 // AllowFileSelectionDialogs-Policy.
172 virtual void SelectFileImpl(
174 const string16
& title
,
175 const base::FilePath
& default_path
,
176 const FileTypeInfo
* file_types
,
178 const base::FilePath::StringType
& default_extension
,
179 gfx::NativeWindow owning_window
,
182 // Returns the global ShellDialogsDelegate instance if any.
183 ShellDialogsDelegate
* GetShellDialogsDelegate();
185 // The listener to be notified of selection completion.
189 // Tests if the file selection dialog can be displayed by
190 // testing if the AllowFileSelectionDialogs-Policy is
191 // either unset or set to true.
192 bool CanOpenSelectFileDialog();
194 // Informs the |listener_| that the file selection dialog was canceled. Moved
195 // to a function for being able to post it to the message loop.
196 void CancelFileSelection(void* params
);
198 // Returns true if the dialog has multiple file type choices.
199 virtual bool HasMultipleFileTypeChoicesImpl() = 0;
201 scoped_ptr
<ui::SelectFilePolicy
> select_file_policy_
;
203 DISALLOW_COPY_AND_ASSIGN(SelectFileDialog
);
208 #endif // UI_SHELL_DIALOGS_SELECT_FILE_DIALOG_H_