[email protected] | 5626b089 | 2012-02-20 14:46:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/file_select_helper.h" |
| 6 | |
[email protected] | 5ac950b | 2010-12-09 21:34:25 | [diff] [blame] | 7 | #include <string> |
[email protected] | b7b4beb | 2013-07-09 14:06:50 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 5ac950b | 2010-12-09 21:34:25 | [diff] [blame] | 9 | |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 11 | #include "base/file_util.h" |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 12 | #include "base/files/file_enumerator.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 13 | #include "base/strings/string_split.h" |
[email protected] | d883056 | 2013-06-10 22:01:54 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 16 | #include "chrome/browser/platform_util.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile.h" |
[email protected] | d989891 | 2011-04-15 21:10:00 | [diff] [blame] | 18 | #include "chrome/browser/ui/browser.h" |
| 19 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 6e1fcd1 | 2012-07-02 17:14:20 | [diff] [blame] | 20 | #include "chrome/browser/ui/chrome_select_file_policy.h" |
[email protected] | 6a1c98e0 | 2012-10-24 21:49:43 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 22 | #include "content/public/browser/notification_details.h" |
| 23 | #include "content/public/browser/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 24 | #include "content/public/browser/notification_types.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 25 | #include "content/public/browser/render_view_host.h" |
[email protected] | 5626b089 | 2012-02-20 14:46:58 | [diff] [blame] | 26 | #include "content/public/browser/render_widget_host_view.h" |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 27 | #include "content/public/browser/web_contents.h" |
[email protected] | 8caadeb | 2011-11-22 02:45:23 | [diff] [blame] | 28 | #include "content/public/common/file_chooser_params.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 29 | #include "grit/generated_resources.h" |
[email protected] | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 30 | #include "net/base/mime_util.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 31 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 4344a3c | 2013-01-17 23:49:20 | [diff] [blame] | 32 | #include "ui/shell_dialogs/selected_file_info.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 33 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 34 | using content::BrowserThread; |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 35 | using content::FileChooserParams; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 36 | using content::RenderViewHost; |
| 37 | using content::RenderWidgetHost; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 38 | using content::WebContents; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 39 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | // There is only one file-selection happening at any given time, |
| 43 | // so we allocate an enumeration ID for that purpose. All IDs from |
| 44 | // the renderer must start at 0 and increase. |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 45 | const int kFileSelectEnumerationId = -1; |
| 46 | |
| 47 | void NotifyRenderViewHost(RenderViewHost* render_view_host, |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 48 | const std::vector<ui::SelectedFileInfo>& files, |
[email protected] | bfcf1e9 | 2013-07-11 04:37:25 | [diff] [blame] | 49 | FileChooserParams::Mode dialog_mode) { |
| 50 | render_view_host->FilesSelectedInChooser(files, dialog_mode); |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 51 | } |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 52 | |
[email protected] | 53f04c8 | 2012-07-26 02:31:09 | [diff] [blame] | 53 | // Converts a list of FilePaths to a list of ui::SelectedFileInfo. |
| 54 | std::vector<ui::SelectedFileInfo> FilePathListToSelectedFileInfoList( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 55 | const std::vector<base::FilePath>& paths) { |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 56 | std::vector<ui::SelectedFileInfo> selected_files; |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 57 | for (size_t i = 0; i < paths.size(); ++i) { |
| 58 | selected_files.push_back( |
[email protected] | 53f04c8 | 2012-07-26 02:31:09 | [diff] [blame] | 59 | ui::SelectedFileInfo(paths[i], paths[i])); |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 60 | } |
| 61 | return selected_files; |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 64 | } // namespace |
| 65 | |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 66 | struct FileSelectHelper::ActiveDirectoryEnumeration { |
[email protected] | d45f751 | 2011-06-21 21:18:27 | [diff] [blame] | 67 | ActiveDirectoryEnumeration() : rvh_(NULL) {} |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 68 | |
| 69 | scoped_ptr<DirectoryListerDispatchDelegate> delegate_; |
[email protected] | 05a81418 | 2011-04-27 19:50:34 | [diff] [blame] | 70 | scoped_ptr<net::DirectoryLister> lister_; |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 71 | RenderViewHost* rvh_; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 72 | std::vector<base::FilePath> results_; |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 73 | }; |
| 74 | |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 75 | FileSelectHelper::FileSelectHelper(Profile* profile) |
| 76 | : profile_(profile), |
| 77 | render_view_host_(NULL), |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 78 | web_contents_(NULL), |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 79 | select_file_dialog_(), |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 80 | select_file_types_(), |
[email protected] | bfcf1e9 | 2013-07-11 04:37:25 | [diff] [blame] | 81 | dialog_type_(ui::SelectFileDialog::SELECT_OPEN_FILE), |
| 82 | dialog_mode_(FileChooserParams::Open) { |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | FileSelectHelper::~FileSelectHelper() { |
| 86 | // There may be pending file dialogs, we need to tell them that we've gone |
| 87 | // away so they don't try and call back to us. |
| 88 | if (select_file_dialog_.get()) |
| 89 | select_file_dialog_->ListenerDestroyed(); |
| 90 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 91 | // Stop any pending directory enumeration, prevent a callback, and free |
| 92 | // allocated memory. |
| 93 | std::map<int, ActiveDirectoryEnumeration*>::iterator iter; |
| 94 | for (iter = directory_enumerations_.begin(); |
| 95 | iter != directory_enumerations_.end(); |
| 96 | ++iter) { |
[email protected] | 05a81418 | 2011-04-27 19:50:34 | [diff] [blame] | 97 | iter->second->lister_.reset(); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 98 | delete iter->second; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
[email protected] | 23827ec | 2012-08-10 22:08:08 | [diff] [blame] | 102 | void FileSelectHelper::DirectoryListerDispatchDelegate::OnListFile( |
| 103 | const net::DirectoryLister::DirectoryListerData& data) { |
| 104 | parent_->OnListFile(id_, data); |
| 105 | } |
| 106 | |
| 107 | void FileSelectHelper::DirectoryListerDispatchDelegate::OnListDone(int error) { |
| 108 | parent_->OnListDone(id_, error); |
| 109 | } |
| 110 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 111 | void FileSelectHelper::FileSelected(const base::FilePath& path, |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 112 | int index, void* params) { |
[email protected] | 53f04c8 | 2012-07-26 02:31:09 | [diff] [blame] | 113 | FileSelectedWithExtraInfo(ui::SelectedFileInfo(path, path), index, params); |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void FileSelectHelper::FileSelectedWithExtraInfo( |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 117 | const ui::SelectedFileInfo& file, |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 118 | int index, |
| 119 | void* params) { |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 120 | if (!render_view_host_) |
| 121 | return; |
| 122 | |
[email protected] | 53f04c8 | 2012-07-26 02:31:09 | [diff] [blame] | 123 | profile_->set_last_selected_directory(file.file_path.DirName()); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 124 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 125 | const base::FilePath& path = file.local_path; |
[email protected] | 6bedbef | 2013-07-31 06:33:49 | [diff] [blame] | 126 | if (dialog_type_ == ui::SelectFileDialog::SELECT_UPLOAD_FOLDER) { |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 127 | StartNewEnumeration(path, kFileSelectEnumerationId, render_view_host_); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 131 | std::vector<ui::SelectedFileInfo> files; |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 132 | files.push_back(file); |
[email protected] | bfcf1e9 | 2013-07-11 04:37:25 | [diff] [blame] | 133 | NotifyRenderViewHost(render_view_host_, files, dialog_mode_); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 134 | |
[email protected] | 3a29a6e | 2011-08-24 18:26:21 | [diff] [blame] | 135 | // No members should be accessed from here on. |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 136 | RunFileChooserEnd(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 139 | void FileSelectHelper::MultiFilesSelected( |
| 140 | const std::vector<base::FilePath>& files, |
| 141 | void* params) { |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 142 | std::vector<ui::SelectedFileInfo> selected_files = |
[email protected] | 53f04c8 | 2012-07-26 02:31:09 | [diff] [blame] | 143 | FilePathListToSelectedFileInfoList(files); |
| 144 | |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 145 | MultiFilesSelectedWithExtraInfo(selected_files, params); |
| 146 | } |
| 147 | |
| 148 | void FileSelectHelper::MultiFilesSelectedWithExtraInfo( |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 149 | const std::vector<ui::SelectedFileInfo>& files, |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 150 | void* params) { |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 151 | if (!files.empty()) |
[email protected] | 53f04c8 | 2012-07-26 02:31:09 | [diff] [blame] | 152 | profile_->set_last_selected_directory(files[0].file_path.DirName()); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 153 | if (!render_view_host_) |
| 154 | return; |
| 155 | |
[email protected] | bfcf1e9 | 2013-07-11 04:37:25 | [diff] [blame] | 156 | NotifyRenderViewHost(render_view_host_, files, dialog_mode_); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 157 | |
[email protected] | 3a29a6e | 2011-08-24 18:26:21 | [diff] [blame] | 158 | // No members should be accessed from here on. |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 159 | RunFileChooserEnd(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void FileSelectHelper::FileSelectionCanceled(void* params) { |
| 163 | if (!render_view_host_) |
| 164 | return; |
| 165 | |
| 166 | // If the user cancels choosing a file to upload we pass back an |
| 167 | // empty vector. |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 168 | NotifyRenderViewHost( |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 169 | render_view_host_, std::vector<ui::SelectedFileInfo>(), |
[email protected] | bfcf1e9 | 2013-07-11 04:37:25 | [diff] [blame] | 170 | dialog_mode_); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 171 | |
[email protected] | 3a29a6e | 2011-08-24 18:26:21 | [diff] [blame] | 172 | // No members should be accessed from here on. |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 173 | RunFileChooserEnd(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 176 | void FileSelectHelper::StartNewEnumeration(const base::FilePath& path, |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 177 | int request_id, |
| 178 | RenderViewHost* render_view_host) { |
| 179 | scoped_ptr<ActiveDirectoryEnumeration> entry(new ActiveDirectoryEnumeration); |
| 180 | entry->rvh_ = render_view_host; |
| 181 | entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id)); |
[email protected] | 05a81418 | 2011-04-27 19:50:34 | [diff] [blame] | 182 | entry->lister_.reset(new net::DirectoryLister(path, |
| 183 | true, |
| 184 | net::DirectoryLister::NO_SORT, |
| 185 | entry->delegate_.get())); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 186 | if (!entry->lister_->Start()) { |
| 187 | if (request_id == kFileSelectEnumerationId) |
| 188 | FileSelectionCanceled(NULL); |
| 189 | else |
| 190 | render_view_host->DirectoryEnumerationFinished(request_id, |
| 191 | entry->results_); |
| 192 | } else { |
| 193 | directory_enumerations_[request_id] = entry.release(); |
| 194 | } |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void FileSelectHelper::OnListFile( |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 198 | int id, |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 199 | const net::DirectoryLister::DirectoryListerData& data) { |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 200 | ActiveDirectoryEnumeration* entry = directory_enumerations_[id]; |
| 201 | |
[email protected] | 9897e09 | 2011-02-04 22:09:11 | [diff] [blame] | 202 | // Directory upload returns directories via a "." file, so that |
| 203 | // empty directories are included. This util call just checks |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 204 | // the flags in the structure; there's no file I/O going on. |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 205 | if (data.info.IsDirectory()) |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 206 | entry->results_.push_back(data.path.Append(FILE_PATH_LITERAL("."))); |
[email protected] | 9897e09 | 2011-02-04 22:09:11 | [diff] [blame] | 207 | else |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 208 | entry->results_.push_back(data.path); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 211 | void FileSelectHelper::OnListDone(int id, int error) { |
| 212 | // This entry needs to be cleaned up when this function is done. |
| 213 | scoped_ptr<ActiveDirectoryEnumeration> entry(directory_enumerations_[id]); |
| 214 | directory_enumerations_.erase(id); |
| 215 | if (!entry->rvh_) |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 216 | return; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 217 | if (error) { |
| 218 | FileSelectionCanceled(NULL); |
| 219 | return; |
| 220 | } |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 221 | |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 222 | std::vector<ui::SelectedFileInfo> selected_files = |
[email protected] | 53f04c8 | 2012-07-26 02:31:09 | [diff] [blame] | 223 | FilePathListToSelectedFileInfoList(entry->results_); |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 224 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 225 | if (id == kFileSelectEnumerationId) |
[email protected] | bfcf1e9 | 2013-07-11 04:37:25 | [diff] [blame] | 226 | NotifyRenderViewHost(entry->rvh_, selected_files, dialog_mode_); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 227 | else |
| 228 | entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 229 | |
| 230 | EnumerateDirectoryEnd(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | 479cce78 | 2012-09-15 20:15:53 | [diff] [blame] | 233 | scoped_ptr<ui::SelectFileDialog::FileTypeInfo> |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 234 | FileSelectHelper::GetFileTypesFromAcceptType( |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame^] | 235 | const std::vector<base::string16>& accept_types) { |
[email protected] | 479cce78 | 2012-09-15 20:15:53 | [diff] [blame] | 236 | scoped_ptr<ui::SelectFileDialog::FileTypeInfo> base_file_type( |
| 237 | new ui::SelectFileDialog::FileTypeInfo()); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 238 | if (accept_types.empty()) |
[email protected] | 479cce78 | 2012-09-15 20:15:53 | [diff] [blame] | 239 | return base_file_type.Pass(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 240 | |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 241 | // Create FileTypeInfo and pre-allocate for the first extension list. |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 242 | scoped_ptr<ui::SelectFileDialog::FileTypeInfo> file_type( |
[email protected] | 479cce78 | 2012-09-15 20:15:53 | [diff] [blame] | 243 | new ui::SelectFileDialog::FileTypeInfo(*base_file_type)); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 244 | file_type->include_all_files = true; |
| 245 | file_type->extensions.resize(1); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 246 | std::vector<base::FilePath::StringType>* extensions = |
| 247 | &file_type->extensions.back(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 248 | |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 249 | // Find the corresponding extensions. |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 250 | int valid_type_count = 0; |
| 251 | int description_id = 0; |
[email protected] | 3314c2b1 | 2011-11-02 08:05:46 | [diff] [blame] | 252 | for (size_t i = 0; i < accept_types.size(); ++i) { |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 253 | std::string ascii_type = UTF16ToASCII(accept_types[i]); |
| 254 | if (!IsAcceptTypeValid(ascii_type)) |
| 255 | continue; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 256 | |
| 257 | size_t old_extension_size = extensions->size(); |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 258 | if (ascii_type[0] == '.') { |
| 259 | // If the type starts with a period it is assumed to be a file extension |
| 260 | // so we just have to add it to the list. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 261 | base::FilePath::StringType ext(ascii_type.begin(), ascii_type.end()); |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 262 | extensions->push_back(ext.substr(1)); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 263 | } else { |
[email protected] | 4a66fa0e | 2012-09-10 06:45:20 | [diff] [blame] | 264 | if (ascii_type == "image/*") |
| 265 | description_id = IDS_IMAGE_FILES; |
| 266 | else if (ascii_type == "audio/*") |
| 267 | description_id = IDS_AUDIO_FILES; |
| 268 | else if (ascii_type == "video/*") |
| 269 | description_id = IDS_VIDEO_FILES; |
| 270 | |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 271 | net::GetExtensionsForMimeType(ascii_type, extensions); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | if (extensions->size() > old_extension_size) |
| 275 | valid_type_count++; |
| 276 | } |
| 277 | |
[email protected] | cbcd12ed | 2010-12-16 23:42:57 | [diff] [blame] | 278 | // If no valid extension is added, bail out. |
| 279 | if (valid_type_count == 0) |
[email protected] | 479cce78 | 2012-09-15 20:15:53 | [diff] [blame] | 280 | return base_file_type.Pass(); |
[email protected] | cbcd12ed | 2010-12-16 23:42:57 | [diff] [blame] | 281 | |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 282 | // Use a generic description "Custom Files" if either of the following is |
| 283 | // true: |
| 284 | // 1) There're multiple types specified, like "audio/*,video/*" |
| 285 | // 2) There're multiple extensions for a MIME type without parameter, like |
| 286 | // "ehtml,shtml,htm,html" for "text/html". On Windows, the select file |
| 287 | // dialog uses the first extension in the list to form the description, |
| 288 | // like "EHTML Files". This is not what we want. |
| 289 | if (valid_type_count > 1 || |
| 290 | (valid_type_count == 1 && description_id == 0 && extensions->size() > 1)) |
| 291 | description_id = IDS_CUSTOM_FILES; |
| 292 | |
| 293 | if (description_id) { |
| 294 | file_type->extension_description_overrides.push_back( |
| 295 | l10n_util::GetStringUTF16(description_id)); |
| 296 | } |
| 297 | |
[email protected] | 479cce78 | 2012-09-15 20:15:53 | [diff] [blame] | 298 | return file_type.Pass(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 299 | } |
| 300 | |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 301 | // static |
| 302 | void FileSelectHelper::RunFileChooser(content::WebContents* tab, |
| 303 | const FileChooserParams& params) { |
| 304 | Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); |
| 305 | // FileSelectHelper will keep itself alive until it sends the result message. |
| 306 | scoped_refptr<FileSelectHelper> file_select_helper( |
| 307 | new FileSelectHelper(profile)); |
| 308 | file_select_helper->RunFileChooser(tab->GetRenderViewHost(), tab, params); |
| 309 | } |
| 310 | |
| 311 | // static |
| 312 | void FileSelectHelper::EnumerateDirectory(content::WebContents* tab, |
| 313 | int request_id, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 314 | const base::FilePath& path) { |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 315 | Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); |
| 316 | // FileSelectHelper will keep itself alive until it sends the result message. |
| 317 | scoped_refptr<FileSelectHelper> file_select_helper( |
| 318 | new FileSelectHelper(profile)); |
| 319 | file_select_helper->EnumerateDirectory( |
| 320 | request_id, tab->GetRenderViewHost(), path); |
| 321 | } |
| 322 | |
| 323 | void FileSelectHelper::RunFileChooser(RenderViewHost* render_view_host, |
| 324 | content::WebContents* web_contents, |
| 325 | const FileChooserParams& params) { |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 326 | DCHECK(!render_view_host_); |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 327 | DCHECK(!web_contents_); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 328 | render_view_host_ = render_view_host; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 329 | web_contents_ = web_contents; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 330 | notification_registrar_.RemoveAll(); |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 331 | notification_registrar_.Add( |
| 332 | this, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 333 | content::Source<RenderWidgetHost>(render_view_host_)); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 334 | notification_registrar_.Add( |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 335 | this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 336 | content::Source<WebContents>(web_contents_)); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 337 | |
| 338 | BrowserThread::PostTask( |
| 339 | BrowserThread::FILE, FROM_HERE, |
| 340 | base::Bind(&FileSelectHelper::RunFileChooserOnFileThread, this, params)); |
| 341 | |
| 342 | // Because this class returns notifications to the RenderViewHost, it is |
| 343 | // difficult for callers to know how long to keep a reference to this |
| 344 | // instance. We AddRef() here to keep the instance alive after we return |
| 345 | // to the caller, until the last callback is received from the file dialog. |
| 346 | // At that point, we must call RunFileChooserEnd(). |
| 347 | AddRef(); |
| 348 | } |
| 349 | |
| 350 | void FileSelectHelper::RunFileChooserOnFileThread( |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 351 | const FileChooserParams& params) { |
[email protected] | 479cce78 | 2012-09-15 20:15:53 | [diff] [blame] | 352 | select_file_types_ = GetFileTypesFromAcceptType(params.accept_types); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 353 | |
| 354 | BrowserThread::PostTask( |
| 355 | BrowserThread::UI, FROM_HERE, |
| 356 | base::Bind(&FileSelectHelper::RunFileChooserOnUIThread, this, params)); |
| 357 | } |
| 358 | |
| 359 | void FileSelectHelper::RunFileChooserOnUIThread( |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 360 | const FileChooserParams& params) { |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 361 | if (!render_view_host_ || !web_contents_) { |
[email protected] | b95b08d | 2011-12-15 20:23:16 | [diff] [blame] | 362 | // If the renderer was destroyed before we started, just cancel the |
| 363 | // operation. |
| 364 | RunFileChooserEnd(); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 365 | return; |
[email protected] | b95b08d | 2011-12-15 20:23:16 | [diff] [blame] | 366 | } |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 367 | |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 368 | select_file_dialog_ = ui::SelectFileDialog::Create( |
[email protected] | 6e1fcd1 | 2012-07-02 17:14:20 | [diff] [blame] | 369 | this, new ChromeSelectFilePolicy(web_contents_)); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 370 | |
[email protected] | bfcf1e9 | 2013-07-11 04:37:25 | [diff] [blame] | 371 | dialog_mode_ = params.mode; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 372 | switch (params.mode) { |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 373 | case FileChooserParams::Open: |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 374 | dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 375 | break; |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 376 | case FileChooserParams::OpenMultiple: |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 377 | dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 378 | break; |
[email protected] | 6bedbef | 2013-07-31 06:33:49 | [diff] [blame] | 379 | case FileChooserParams::UploadFolder: |
| 380 | dialog_type_ = ui::SelectFileDialog::SELECT_UPLOAD_FOLDER; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 381 | break; |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 382 | case FileChooserParams::Save: |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 383 | dialog_type_ = ui::SelectFileDialog::SELECT_SAVEAS_FILE; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 384 | break; |
| 385 | default: |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 386 | // Prevent warning. |
| 387 | dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 388 | NOTREACHED(); |
| 389 | } |
[email protected] | 4e9149a | 2012-08-15 20:43:59 | [diff] [blame] | 390 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 391 | base::FilePath default_file_name = params.default_file_name.IsAbsolute() ? |
[email protected] | 4e9149a | 2012-08-15 20:43:59 | [diff] [blame] | 392 | params.default_file_name : |
| 393 | profile_->last_selected_directory().Append(params.default_file_name); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 394 | |
| 395 | gfx::NativeWindow owning_window = |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 396 | platform_util::GetTopLevel(render_view_host_->GetView()->GetNativeView()); |
[email protected] | d989891 | 2011-04-15 21:10:00 | [diff] [blame] | 397 | |
[email protected] | 2d02a200 | 2012-09-18 21:47:56 | [diff] [blame] | 398 | #if defined(OS_ANDROID) |
| 399 | // Android needs the original MIME types and an additional capture value. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame^] | 400 | std::pair<std::vector<base::string16>, bool> accept_types = |
[email protected] | b7b4beb | 2013-07-09 14:06:50 | [diff] [blame] | 401 | std::make_pair(params.accept_types, params.capture); |
[email protected] | 2d02a200 | 2012-09-18 21:47:56 | [diff] [blame] | 402 | #endif |
| 403 | |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 404 | select_file_dialog_->SelectFile( |
| 405 | dialog_type_, |
| 406 | params.title, |
| 407 | default_file_name, |
| 408 | select_file_types_.get(), |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 409 | select_file_types_.get() && !select_file_types_->extensions.empty() |
| 410 | ? 1 |
| 411 | : 0, // 1-based index of default extension to show. |
| 412 | base::FilePath::StringType(), |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 413 | owning_window, |
[email protected] | b8452fa | 2012-06-15 01:41:41 | [diff] [blame] | 414 | #if defined(OS_ANDROID) |
[email protected] | 2d02a200 | 2012-09-18 21:47:56 | [diff] [blame] | 415 | &accept_types); |
[email protected] | b8452fa | 2012-06-15 01:41:41 | [diff] [blame] | 416 | #else |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 417 | NULL); |
[email protected] | b8452fa | 2012-06-15 01:41:41 | [diff] [blame] | 418 | #endif |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 419 | |
| 420 | select_file_types_.reset(); |
| 421 | } |
| 422 | |
| 423 | // This method is called when we receive the last callback from the file |
| 424 | // chooser dialog. Perform any cleanup and release the reference we added |
| 425 | // in RunFileChooser(). |
| 426 | void FileSelectHelper::RunFileChooserEnd() { |
| 427 | render_view_host_ = NULL; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 428 | web_contents_ = NULL; |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 429 | Release(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 430 | } |
| 431 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 432 | void FileSelectHelper::EnumerateDirectory(int request_id, |
| 433 | RenderViewHost* render_view_host, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 434 | const base::FilePath& path) { |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 435 | |
| 436 | // Because this class returns notifications to the RenderViewHost, it is |
| 437 | // difficult for callers to know how long to keep a reference to this |
| 438 | // instance. We AddRef() here to keep the instance alive after we return |
| 439 | // to the caller, until the last callback is received from the enumeration |
| 440 | // code. At that point, we must call EnumerateDirectoryEnd(). |
| 441 | AddRef(); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 442 | StartNewEnumeration(path, request_id, render_view_host); |
| 443 | } |
| 444 | |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 445 | // This method is called when we receive the last callback from the enumeration |
| 446 | // code. Perform any cleanup and release the reference we added in |
| 447 | // EnumerateDirectory(). |
| 448 | void FileSelectHelper::EnumerateDirectoryEnd() { |
| 449 | Release(); |
| 450 | } |
| 451 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 452 | void FileSelectHelper::Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 453 | const content::NotificationSource& source, |
| 454 | const content::NotificationDetails& details) { |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 455 | switch (type) { |
| 456 | case content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: { |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 457 | DCHECK(content::Source<RenderWidgetHost>(source).ptr() == |
| 458 | render_view_host_); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 459 | render_view_host_ = NULL; |
| 460 | break; |
| 461 | } |
| 462 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 463 | case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: { |
| 464 | DCHECK(content::Source<WebContents>(source).ptr() == web_contents_); |
| 465 | web_contents_ = NULL; |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 466 | break; |
| 467 | } |
| 468 | |
| 469 | default: |
| 470 | NOTREACHED(); |
| 471 | } |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 472 | } |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 473 | |
| 474 | // static |
| 475 | bool FileSelectHelper::IsAcceptTypeValid(const std::string& accept_type) { |
| 476 | // TODO(raymes): This only does some basic checks, extend to test more cases. |
| 477 | // A 1 character accept type will always be invalid (either a "." in the case |
| 478 | // of an extension or a "/" in the case of a MIME type). |
| 479 | std::string unused; |
| 480 | if (accept_type.length() <= 1 || |
| 481 | StringToLowerASCII(accept_type) != accept_type || |
| 482 | TrimWhitespaceASCII(accept_type, TRIM_ALL, &unused) != TRIM_NONE) { |
| 483 | return false; |
| 484 | } |
| 485 | return true; |
| 486 | } |