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