| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_SELECT_FILE_DIALOG_H_ | 5 #ifndef CHROME_BROWSER_UI_SELECT_FILE_DIALOG_H_ |
| 6 #define CHROME_BROWSER_UI_SELECT_FILE_DIALOG_H_ | 6 #define CHROME_BROWSER_UI_SELECT_FILE_DIALOG_H_ |
| 7 | 7 |
| 8 #include <string> | 8 // TODO(erg): Remove this redirect header. crbug.com/134529 |
| 9 #include <vector> | 9 #include "ui/base/dialogs/select_file_dialog.h" |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 using ui::SelectFileDialog; |
| 12 #include "base/callback_forward.h" | |
| 13 #include "base/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/base/dialogs/base_shell_dialog.h" | |
| 18 #include "ui/gfx/native_widget_types.h" | |
| 19 | |
| 20 namespace ui { | |
| 21 class SelectFilePolicy; | |
| 22 } | |
| 23 | |
| 24 namespace content { | |
| 25 class WebContents; | |
| 26 } | |
| 27 | |
| 28 namespace ui { | |
| 29 struct SelectedFileInfo; | |
| 30 } | |
| 31 | |
| 32 // This function is declared extern such that it is accessible for unit tests | |
| 33 // in /chrome/browser/ui/views/select_file_dialog_win_unittest.cc | |
| 34 extern std::wstring AppendExtensionIfNeeded(const std::wstring& filename, | |
| 35 const std::wstring& filter_selected, | |
| 36 const std::wstring& suggested_ext); | |
| 37 | |
| 38 // Shows a dialog box for selecting a file or a folder. | |
| 39 class SelectFileDialog | |
| 40 : public base::RefCountedThreadSafe<SelectFileDialog>, | |
| 41 public ui::BaseShellDialog { | |
| 42 public: | |
| 43 enum Type { | |
| 44 SELECT_NONE, | |
| 45 SELECT_FOLDER, | |
| 46 SELECT_SAVEAS_FILE, | |
| 47 SELECT_OPEN_FILE, | |
| 48 SELECT_OPEN_MULTI_FILE | |
| 49 }; | |
| 50 | |
| 51 // An interface implemented by a Listener object wishing to know about the | |
| 52 // the result of the Select File/Folder action. These callbacks must be | |
| 53 // re-entrant. | |
| 54 class Listener { | |
| 55 public: | |
| 56 // Notifies the Listener that a file/folder selection has been made. The | |
| 57 // file/folder path is in |path|. |params| is contextual passed to | |
| 58 // SelectFile. |index| specifies the index of the filter passed to the | |
| 59 // the initial call to SelectFile. | |
| 60 virtual void FileSelected(const FilePath& path, | |
| 61 int index, void* params) = 0; | |
| 62 | |
| 63 // Similar to FileSelected() but takes SelectedFileInfo instead of | |
| 64 // FilePath. Used for passing extra information (ex. display name). | |
| 65 // | |
| 66 // If not overridden, calls FileSelected() with path from |file|. | |
| 67 virtual void FileSelectedWithExtraInfo( | |
| 68 const ui::SelectedFileInfo& file, | |
| 69 int index, | |
| 70 void* params); | |
| 71 | |
| 72 // Notifies the Listener that many files have been selected. The | |
| 73 // files are in |files|. |params| is contextual passed to SelectFile. | |
| 74 virtual void MultiFilesSelected( | |
| 75 const std::vector<FilePath>& files, void* params) {} | |
| 76 | |
| 77 // Similar to MultiFilesSelected() but takes SelectedFileInfo instead of | |
| 78 // FilePath. Used for passing extra information (ex. display name). | |
| 79 // | |
| 80 // If not overridden, calls MultiFilesSelected() with paths from |files|. | |
| 81 virtual void MultiFilesSelectedWithExtraInfo( | |
| 82 const std::vector<ui::SelectedFileInfo>& files, | |
| 83 void* params); | |
| 84 | |
| 85 // Notifies the Listener that the file/folder selection was aborted (via | |
| 86 // the user canceling or closing the selection dialog box, for example). | |
| 87 // |params| is contextual passed to SelectFile. | |
| 88 virtual void FileSelectionCanceled(void* params) {} | |
| 89 | |
| 90 protected: | |
| 91 virtual ~Listener() {} | |
| 92 }; | |
| 93 | |
| 94 // Creates a dialog box helper. This object is ref-counted, but the returned | |
| 95 // object will have no reference (refcount is 0). |policy| is an optional | |
| 96 // class that can prevent showing a dialog. | |
| 97 static SelectFileDialog* Create(Listener* listener, | |
| 98 ui::SelectFilePolicy* policy); | |
| 99 | |
| 100 // Holds information about allowed extensions on a file save dialog. | |
| 101 // |extensions| is a list of allowed extensions. For example, it might be | |
| 102 // { { "htm", "html" }, { "txt" } }. Only pass more than one extension | |
| 103 // in the inner vector if the extensions are equivalent. Do NOT include | |
| 104 // leading periods. | |
| 105 // |extension_description_overrides| overrides the system descriptions of the | |
| 106 // specified extensions. Entries correspond to |extensions|; if left blank | |
| 107 // the system descriptions will be used. | |
| 108 // |include_all_files| specifies whether there will be a filter added for all | |
| 109 // files (i.e. *.*). | |
| 110 struct FileTypeInfo { | |
| 111 FileTypeInfo(); | |
| 112 ~FileTypeInfo(); | |
| 113 | |
| 114 std::vector<std::vector<FilePath::StringType> > extensions; | |
| 115 std::vector<string16> extension_description_overrides; | |
| 116 bool include_all_files; | |
| 117 }; | |
| 118 | |
| 119 // Selects a File. | |
| 120 // Before doing anything this function checks if FileBrowsing is forbidden | |
| 121 // by Policy. If so, it tries to show an InfoBar and behaves as though no File | |
| 122 // was selected (the user clicked `Cancel` immediately). | |
| 123 // Otherwise it will start displaying the dialog box. This will also | |
| 124 // block the calling window until the dialog box is complete. The listener | |
| 125 // associated with this object will be notified when the selection is | |
| 126 // complete. | |
| 127 // |type| is the type of file dialog to be shown, see Type enumeration above. | |
| 128 // |title| is the title to be displayed in the dialog. If this string is | |
| 129 // empty, the default title is used. | |
| 130 // |default_path| is the default path and suggested file name to be shown in | |
| 131 // the dialog. This only works for SELECT_SAVEAS_FILE and SELECT_OPEN_FILE. | |
| 132 // Can be an empty string to indicate the platform default. | |
| 133 // |file_types| holds the information about the file types allowed. Pass NULL | |
| 134 // to get no special behavior | |
| 135 // |file_type_index| is the 1-based index into the file type list in | |
| 136 // |file_types|. Specify 0 if you don't need to specify extension behavior. | |
| 137 // |default_extension| is the default extension to add to the file if the | |
| 138 // user doesn't type one. This should NOT include the '.'. On Windows, if | |
| 139 // you specify this you must also specify |file_types|. | |
| 140 // |owning_window| is the window the dialog is modal to, or NULL for a | |
| 141 // modeless dialog. | |
| 142 // |params| is data from the calling context which will be passed through to | |
| 143 // the listener. Can be NULL. | |
| 144 // NOTE: only one instance of any shell dialog can be shown per owning_window | |
| 145 // at a time (for obvious reasons). | |
| 146 void SelectFile(Type type, | |
| 147 const string16& title, | |
| 148 const FilePath& default_path, | |
| 149 const FileTypeInfo* file_types, | |
| 150 int file_type_index, | |
| 151 const FilePath::StringType& default_extension, | |
| 152 gfx::NativeWindow owning_window, | |
| 153 void* params); | |
| 154 bool HasMultipleFileTypeChoices(); | |
| 155 | |
| 156 protected: | |
| 157 friend class base::RefCountedThreadSafe<SelectFileDialog>; | |
| 158 explicit SelectFileDialog(Listener* listener, | |
| 159 ui::SelectFilePolicy* policy); | |
| 160 virtual ~SelectFileDialog(); | |
| 161 | |
| 162 // Displays the actual file-selection dialog. | |
| 163 // This is overridden in the platform-specific descendants of FileSelectDialog | |
| 164 // and gets called from SelectFile after testing the | |
| 165 // AllowFileSelectionDialogs-Policy. | |
| 166 virtual void SelectFileImpl(Type type, | |
| 167 const string16& title, | |
| 168 const FilePath& default_path, | |
| 169 const FileTypeInfo* file_types, | |
| 170 int file_type_index, | |
| 171 const FilePath::StringType& default_extension, | |
| 172 gfx::NativeWindow owning_window, | |
| 173 void* params) = 0; | |
| 174 | |
| 175 // The listener to be notified of selection completion. | |
| 176 Listener* listener_; | |
| 177 | |
| 178 private: | |
| 179 // Tests if the file selection dialog can be displayed by | |
| 180 // testing if the AllowFileSelectionDialogs-Policy is | |
| 181 // either unset or set to true. | |
| 182 bool CanOpenSelectFileDialog(); | |
| 183 | |
| 184 // Informs the |listener_| that the file selection dialog was canceled. Moved | |
| 185 // to a function for being able to post it to the message loop. | |
| 186 void CancelFileSelection(void* params); | |
| 187 | |
| 188 // Returns true if the dialog has multiple file type choices. | |
| 189 virtual bool HasMultipleFileTypeChoicesImpl() = 0; | |
| 190 | |
| 191 scoped_ptr<ui::SelectFilePolicy> select_file_policy_; | |
| 192 | |
| 193 DISALLOW_COPY_AND_ASSIGN(SelectFileDialog); | |
| 194 }; | |
| 195 | 12 |
| 196 #endif // CHROME_BROWSER_UI_SELECT_FILE_DIALOG_H_ | 13 #endif // CHROME_BROWSER_UI_SELECT_FILE_DIALOG_H_ |
| OLD | NEW |