[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 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | 5ac950b | 2010-12-09 21:34:25 | [diff] [blame] | 9 | #include <string> |
[email protected] | b7b4beb | 2013-07-09 14:06:50 | [diff] [blame] | 10 | #include <utility> |
[email protected] | 5ac950b | 2010-12-09 21:34:25 | [diff] [blame] | 11 | |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 13 | #include "base/files/file_enumerator.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 14 | #include "base/files/file_util.h" |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
[email protected] | 1988e1c | 2013-02-28 20:27:42 | [diff] [blame] | 16 | #include "base/strings/string_split.h" |
[email protected] | d883056 | 2013-06-10 22:01:54 | [diff] [blame] | 17 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
fdoray | 9e26976 | 2016-12-21 20:21:07 | [diff] [blame] | 19 | #include "base/task_scheduler/post_task.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 20 | #include "build/build_config.h" |
hirono | a4a32af2 | 2014-11-07 03:17:29 | [diff] [blame] | 21 | #include "chrome/browser/browser_process.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 22 | #include "chrome/browser/platform_util.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 23 | #include "chrome/browser/profiles/profile.h" |
hirono | a4a32af2 | 2014-11-07 03:17:29 | [diff] [blame] | 24 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | d989891 | 2011-04-15 21:10:00 | [diff] [blame] | 25 | #include "chrome/browser/ui/browser.h" |
| 26 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 6e1fcd1 | 2012-07-02 17:14:20 | [diff] [blame] | 27 | #include "chrome/browser/ui/chrome_select_file_policy.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 28 | #include "chrome/grit/generated_resources.h" |
[email protected] | 6a1c98e0 | 2012-10-24 21:49:43 | [diff] [blame] | 29 | #include "content/public/browser/browser_thread.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 30 | #include "content/public/browser/notification_details.h" |
| 31 | #include "content/public/browser/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 32 | #include "content/public/browser/notification_types.h" |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 33 | #include "content/public/browser/render_frame_host.h" |
| 34 | #include "content/public/browser/render_process_host.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 35 | #include "content/public/browser/render_view_host.h" |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 36 | #include "content/public/browser/render_widget_host.h" |
[email protected] | 5626b089 | 2012-02-20 14:46:58 | [diff] [blame] | 37 | #include "content/public/browser/render_widget_host_view.h" |
rdevlin.cronin | bfbc11d9 | 2015-06-12 23:26:08 | [diff] [blame] | 38 | #include "content/public/browser/storage_partition.h" |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 39 | #include "content/public/browser/web_contents.h" |
hirono | 570357bd | 2014-10-08 12:39:27 | [diff] [blame] | 40 | #include "content/public/common/file_chooser_file_info.h" |
[email protected] | 8caadeb | 2011-11-22 02:45:23 | [diff] [blame] | 41 | #include "content/public/common/file_chooser_params.h" |
asanka | ee261f3 | 2015-10-27 21:04:28 | [diff] [blame] | 42 | #include "net/base/filename_util.h" |
[email protected] | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 43 | #include "net/base/mime_util.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 44 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 4344a3c | 2013-01-17 23:49:20 | [diff] [blame] | 45 | #include "ui/shell_dialogs/selected_file_info.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 46 | |
hirono | 275bfbe | 2014-10-21 01:46:17 | [diff] [blame] | 47 | #if defined(OS_CHROMEOS) |
| 48 | #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
| 49 | #include "content/public/browser/site_instance.h" |
| 50 | #endif |
| 51 | |
asanka | ee261f3 | 2015-10-27 21:04:28 | [diff] [blame] | 52 | #if defined(FULL_SAFE_BROWSING) |
asanka | ef1f3872 | 2016-05-20 16:17:41 | [diff] [blame] | 53 | #include "chrome/browser/safe_browsing/download_protection_service.h" |
| 54 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
asanka | ee261f3 | 2015-10-27 21:04:28 | [diff] [blame] | 55 | #endif |
| 56 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 57 | using content::BrowserThread; |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 58 | using content::FileChooserParams; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 59 | using content::RenderViewHost; |
| 60 | using content::RenderWidgetHost; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 61 | using content::WebContents; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 62 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 63 | namespace { |
| 64 | |
| 65 | // There is only one file-selection happening at any given time, |
| 66 | // so we allocate an enumeration ID for that purpose. All IDs from |
| 67 | // the renderer must start at 0 and increase. |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 68 | const int kFileSelectEnumerationId = -1; |
| 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] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 72 | const std::vector<base::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 | |
erikchen | 22de64a3 | 2014-10-10 18:27:43 | [diff] [blame] | 81 | void DeleteFiles(const std::vector<base::FilePath>& paths) { |
| 82 | for (auto& file_path : paths) |
| 83 | base::DeleteFile(file_path, false); |
| 84 | } |
| 85 | |
hirono | a4a32af2 | 2014-11-07 03:17:29 | [diff] [blame] | 86 | bool IsValidProfile(Profile* profile) { |
| 87 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
jsbell | 0d496dc | 2016-10-28 22:23:57 | [diff] [blame] | 88 | // No profile manager in unit tests. |
| 89 | if (!g_browser_process->profile_manager()) |
| 90 | return true; |
hirono | a4a32af2 | 2014-11-07 03:17:29 | [diff] [blame] | 91 | return g_browser_process->profile_manager()->IsValidProfile(profile); |
| 92 | } |
| 93 | |
asanka | ef1f3872 | 2016-05-20 16:17:41 | [diff] [blame] | 94 | #if defined(FULL_SAFE_BROWSING) |
| 95 | |
| 96 | bool IsDownloadAllowedBySafeBrowsing( |
| 97 | safe_browsing::DownloadProtectionService::DownloadCheckResult result) { |
| 98 | using Result = safe_browsing::DownloadProtectionService::DownloadCheckResult; |
| 99 | switch (result) { |
| 100 | // Only allow downloads that are marked as SAFE or UNKNOWN by SafeBrowsing. |
| 101 | // All other types are going to be blocked. UNKNOWN could be the result of a |
| 102 | // failed safe browsing ping. |
| 103 | case Result::UNKNOWN: |
| 104 | case Result::SAFE: |
| 105 | return true; |
| 106 | |
| 107 | case Result::DANGEROUS: |
| 108 | case Result::UNCOMMON: |
| 109 | case Result::DANGEROUS_HOST: |
| 110 | case Result::POTENTIALLY_UNWANTED: |
| 111 | return false; |
| 112 | } |
| 113 | NOTREACHED(); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | void InterpretSafeBrowsingVerdict( |
| 118 | const base::Callback<void(bool)>& recipient, |
| 119 | safe_browsing::DownloadProtectionService::DownloadCheckResult result) { |
| 120 | recipient.Run(IsDownloadAllowedBySafeBrowsing(result)); |
| 121 | } |
| 122 | |
| 123 | #endif |
| 124 | |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 125 | } // namespace |
| 126 | |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 127 | struct FileSelectHelper::ActiveDirectoryEnumeration { |
[email protected] | d45f751 | 2011-06-21 21:18:27 | [diff] [blame] | 128 | ActiveDirectoryEnumeration() : rvh_(NULL) {} |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 129 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 130 | std::unique_ptr<DirectoryListerDispatchDelegate> delegate_; |
| 131 | std::unique_ptr<net::DirectoryLister> lister_; |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 132 | RenderViewHost* rvh_; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 133 | std::vector<base::FilePath> results_; |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 134 | }; |
| 135 | |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 136 | FileSelectHelper::FileSelectHelper(Profile* profile) |
| 137 | : profile_(profile), |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 138 | render_frame_host_(nullptr), |
| 139 | web_contents_(nullptr), |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 140 | select_file_dialog_(), |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 141 | select_file_types_(), |
[email protected] | bfcf1e9 | 2013-07-11 04:37:25 | [diff] [blame] | 142 | dialog_type_(ui::SelectFileDialog::SELECT_OPEN_FILE), |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 143 | dialog_mode_(FileChooserParams::Open) {} |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 144 | |
| 145 | FileSelectHelper::~FileSelectHelper() { |
| 146 | // There may be pending file dialogs, we need to tell them that we've gone |
| 147 | // away so they don't try and call back to us. |
| 148 | if (select_file_dialog_.get()) |
| 149 | select_file_dialog_->ListenerDestroyed(); |
| 150 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 151 | // Stop any pending directory enumeration, prevent a callback, and free |
| 152 | // allocated memory. |
| 153 | std::map<int, ActiveDirectoryEnumeration*>::iterator iter; |
| 154 | for (iter = directory_enumerations_.begin(); |
| 155 | iter != directory_enumerations_.end(); |
| 156 | ++iter) { |
[email protected] | 05a81418 | 2011-04-27 19:50:34 | [diff] [blame] | 157 | iter->second->lister_.reset(); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 158 | delete iter->second; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
[email protected] | 23827ec | 2012-08-10 22:08:08 | [diff] [blame] | 162 | void FileSelectHelper::DirectoryListerDispatchDelegate::OnListFile( |
| 163 | const net::DirectoryLister::DirectoryListerData& data) { |
| 164 | parent_->OnListFile(id_, data); |
| 165 | } |
| 166 | |
| 167 | void FileSelectHelper::DirectoryListerDispatchDelegate::OnListDone(int error) { |
| 168 | parent_->OnListDone(id_, error); |
| 169 | } |
| 170 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 171 | void FileSelectHelper::FileSelected(const base::FilePath& path, |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 172 | int index, void* params) { |
[email protected] | 53f04c8 | 2012-07-26 02:31:09 | [diff] [blame] | 173 | FileSelectedWithExtraInfo(ui::SelectedFileInfo(path, path), index, params); |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void FileSelectHelper::FileSelectedWithExtraInfo( |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 177 | const ui::SelectedFileInfo& file, |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 178 | int index, |
| 179 | void* params) { |
jsbell | 0d496dc | 2016-10-28 22:23:57 | [diff] [blame] | 180 | if (IsValidProfile(profile_)) { |
| 181 | base::FilePath path = file.file_path; |
| 182 | if (dialog_mode_ != FileChooserParams::UploadFolder) |
| 183 | path = path.DirName(); |
| 184 | profile_->set_last_selected_directory(path); |
| 185 | } |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 186 | |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 187 | if (!render_frame_host_) { |
erikchen | 22de64a3 | 2014-10-10 18:27:43 | [diff] [blame] | 188 | RunFileChooserEnd(); |
| 189 | return; |
| 190 | } |
| 191 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 192 | const base::FilePath& path = file.local_path; |
[email protected] | 6bedbef | 2013-07-31 06:33:49 | [diff] [blame] | 193 | if (dialog_type_ == ui::SelectFileDialog::SELECT_UPLOAD_FOLDER) { |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 194 | StartNewEnumeration(path, kFileSelectEnumerationId, |
| 195 | render_frame_host_->GetRenderViewHost()); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 199 | std::vector<ui::SelectedFileInfo> files; |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 200 | files.push_back(file); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 201 | |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 202 | #if defined(OS_MACOSX) |
erikchen | 22de64a3 | 2014-10-10 18:27:43 | [diff] [blame] | 203 | content::BrowserThread::PostTask( |
| 204 | content::BrowserThread::FILE_USER_BLOCKING, |
| 205 | FROM_HERE, |
| 206 | base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files)); |
| 207 | #else |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 208 | NotifyRenderFrameHostAndEnd(files); |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 209 | #endif // defined(OS_MACOSX) |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 210 | } |
| 211 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 212 | void FileSelectHelper::MultiFilesSelected( |
| 213 | const std::vector<base::FilePath>& files, |
| 214 | void* params) { |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 215 | std::vector<ui::SelectedFileInfo> selected_files = |
[email protected] | 53f04c8 | 2012-07-26 02:31:09 | [diff] [blame] | 216 | FilePathListToSelectedFileInfoList(files); |
| 217 | |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 218 | MultiFilesSelectedWithExtraInfo(selected_files, params); |
| 219 | } |
| 220 | |
| 221 | void FileSelectHelper::MultiFilesSelectedWithExtraInfo( |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 222 | const std::vector<ui::SelectedFileInfo>& files, |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 223 | void* params) { |
jsbell | 0d496dc | 2016-10-28 22:23:57 | [diff] [blame] | 224 | if (!files.empty() && IsValidProfile(profile_)) { |
| 225 | base::FilePath path = files[0].file_path; |
| 226 | if (dialog_mode_ != FileChooserParams::UploadFolder) |
| 227 | path = path.DirName(); |
| 228 | profile_->set_last_selected_directory(path); |
| 229 | } |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 230 | #if defined(OS_MACOSX) |
erikchen | 22de64a3 | 2014-10-10 18:27:43 | [diff] [blame] | 231 | content::BrowserThread::PostTask( |
| 232 | content::BrowserThread::FILE_USER_BLOCKING, |
| 233 | FROM_HERE, |
| 234 | base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files)); |
| 235 | #else |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 236 | NotifyRenderFrameHostAndEnd(files); |
jam | 1c5a9149 | 2016-02-24 20:47:53 | [diff] [blame] | 237 | #endif // defined(OS_MACOSX) |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void FileSelectHelper::FileSelectionCanceled(void* params) { |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 241 | NotifyRenderFrameHostAndEnd(std::vector<ui::SelectedFileInfo>()); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 244 | void FileSelectHelper::StartNewEnumeration(const base::FilePath& path, |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 245 | int request_id, |
| 246 | RenderViewHost* render_view_host) { |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 247 | std::unique_ptr<ActiveDirectoryEnumeration> entry( |
| 248 | new ActiveDirectoryEnumeration); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 249 | entry->rvh_ = render_view_host; |
| 250 | entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id)); |
olli.syrjala | 3396ad3 | 2015-04-24 07:56:41 | [diff] [blame] | 251 | entry->lister_.reset(new net::DirectoryLister( |
| 252 | path, net::DirectoryLister::NO_SORT_RECURSIVE, entry->delegate_.get())); |
fdoray | 9e26976 | 2016-12-21 20:21:07 | [diff] [blame] | 253 | if (!entry->lister_->Start( |
| 254 | base::CreateTaskRunnerWithTraits( |
| 255 | base::TaskTraits() |
| 256 | .WithShutdownBehavior( |
| 257 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
| 258 | .MayBlock()) |
| 259 | .get())) { |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 260 | if (request_id == kFileSelectEnumerationId) |
| 261 | FileSelectionCanceled(NULL); |
| 262 | else |
| 263 | render_view_host->DirectoryEnumerationFinished(request_id, |
| 264 | entry->results_); |
| 265 | } else { |
| 266 | directory_enumerations_[request_id] = entry.release(); |
| 267 | } |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void FileSelectHelper::OnListFile( |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 271 | int id, |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 272 | const net::DirectoryLister::DirectoryListerData& data) { |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 273 | ActiveDirectoryEnumeration* entry = directory_enumerations_[id]; |
| 274 | |
[email protected] | 2c718a0d | 2014-03-24 02:42:22 | [diff] [blame] | 275 | // Directory upload only cares about files. |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 276 | if (data.info.IsDirectory()) |
[email protected] | 2c718a0d | 2014-03-24 02:42:22 | [diff] [blame] | 277 | return; |
| 278 | |
| 279 | entry->results_.push_back(data.path); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 280 | } |
| 281 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 282 | void FileSelectHelper::OnListDone(int id, int error) { |
| 283 | // This entry needs to be cleaned up when this function is done. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 284 | std::unique_ptr<ActiveDirectoryEnumeration> entry( |
| 285 | directory_enumerations_[id]); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 286 | directory_enumerations_.erase(id); |
| 287 | if (!entry->rvh_) |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 288 | return; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 289 | if (error) { |
| 290 | FileSelectionCanceled(NULL); |
| 291 | return; |
| 292 | } |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 293 | |
[email protected] | ddb034b | 2012-06-26 20:31:39 | [diff] [blame] | 294 | std::vector<ui::SelectedFileInfo> selected_files = |
[email protected] | 53f04c8 | 2012-07-26 02:31:09 | [diff] [blame] | 295 | FilePathListToSelectedFileInfoList(entry->results_); |
[email protected] | fb11b6a4 | 2012-03-14 07:25:12 | [diff] [blame] | 296 | |
hirono | 275bfbe | 2014-10-21 01:46:17 | [diff] [blame] | 297 | if (id == kFileSelectEnumerationId) { |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 298 | NotifyRenderFrameHostAndEnd(selected_files); |
hirono | 275bfbe | 2014-10-21 01:46:17 | [diff] [blame] | 299 | } else { |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 300 | entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); |
hirono | 275bfbe | 2014-10-21 01:46:17 | [diff] [blame] | 301 | EnumerateDirectoryEnd(); |
| 302 | } |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 303 | } |
| 304 | |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 305 | void FileSelectHelper::NotifyRenderFrameHostAndEnd( |
erikchen | 22de64a3 | 2014-10-10 18:27:43 | [diff] [blame] | 306 | const std::vector<ui::SelectedFileInfo>& files) { |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 307 | if (!render_frame_host_) { |
hirono | 275bfbe | 2014-10-21 01:46:17 | [diff] [blame] | 308 | RunFileChooserEnd(); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | #if defined(OS_CHROMEOS) |
| 313 | if (!files.empty()) { |
hirono | a4a32af2 | 2014-11-07 03:17:29 | [diff] [blame] | 314 | if (!IsValidProfile(profile_)) { |
| 315 | RunFileChooserEnd(); |
| 316 | return; |
| 317 | } |
hirono | 275bfbe | 2014-10-21 01:46:17 | [diff] [blame] | 318 | // Converts |files| into FileChooserFileInfo with handling of non-native |
| 319 | // files. |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 320 | content::SiteInstance* site_instance = |
| 321 | render_frame_host_->GetSiteInstance(); |
rdevlin.cronin | bfbc11d9 | 2015-06-12 23:26:08 | [diff] [blame] | 322 | storage::FileSystemContext* file_system_context = |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 323 | content::BrowserContext::GetStoragePartition(profile_, site_instance) |
| 324 | ->GetFileSystemContext(); |
hirono | 275bfbe | 2014-10-21 01:46:17 | [diff] [blame] | 325 | file_manager::util::ConvertSelectedFileInfoListToFileChooserFileInfoList( |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 326 | file_system_context, site_instance->GetSiteURL(), files, |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 327 | base::Bind( |
| 328 | &FileSelectHelper::NotifyRenderFrameHostAndEndAfterConversion, |
| 329 | this)); |
hirono | 275bfbe | 2014-10-21 01:46:17 | [diff] [blame] | 330 | return; |
| 331 | } |
| 332 | #endif // defined(OS_CHROMEOS) |
| 333 | |
| 334 | std::vector<content::FileChooserFileInfo> chooser_files; |
| 335 | for (const auto& file : files) { |
| 336 | content::FileChooserFileInfo chooser_file; |
| 337 | chooser_file.file_path = file.local_path; |
| 338 | chooser_file.display_name = file.display_name; |
| 339 | chooser_files.push_back(chooser_file); |
| 340 | } |
hirono | cf350cb | 2014-10-23 08:37:18 | [diff] [blame] | 341 | |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 342 | NotifyRenderFrameHostAndEndAfterConversion(chooser_files); |
hirono | cf350cb | 2014-10-23 08:37:18 | [diff] [blame] | 343 | } |
| 344 | |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 345 | void FileSelectHelper::NotifyRenderFrameHostAndEndAfterConversion( |
hirono | cf350cb | 2014-10-23 08:37:18 | [diff] [blame] | 346 | const std::vector<content::FileChooserFileInfo>& list) { |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 347 | if (render_frame_host_) |
| 348 | render_frame_host_->FilesSelectedInChooser(list, dialog_mode_); |
erikchen | 22de64a3 | 2014-10-10 18:27:43 | [diff] [blame] | 349 | |
| 350 | // No members should be accessed from here on. |
| 351 | RunFileChooserEnd(); |
| 352 | } |
| 353 | |
| 354 | void FileSelectHelper::DeleteTemporaryFiles() { |
| 355 | BrowserThread::PostTask(BrowserThread::FILE, |
| 356 | FROM_HERE, |
| 357 | base::Bind(&DeleteFiles, temporary_files_)); |
| 358 | temporary_files_.clear(); |
| 359 | } |
| 360 | |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 361 | void FileSelectHelper::CleanUp() { |
lazyboy | 1cf60af | 2015-05-15 22:17:05 | [diff] [blame] | 362 | if (!temporary_files_.empty()) { |
| 363 | DeleteTemporaryFiles(); |
| 364 | |
| 365 | // Now that the temporary files have been scheduled for deletion, there |
| 366 | // is no longer any reason to keep this instance around. |
| 367 | Release(); |
| 368 | } |
| 369 | } |
| 370 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 371 | std::unique_ptr<ui::SelectFileDialog::FileTypeInfo> |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 372 | FileSelectHelper::GetFileTypesFromAcceptType( |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 373 | const std::vector<base::string16>& accept_types) { |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 374 | std::unique_ptr<ui::SelectFileDialog::FileTypeInfo> base_file_type( |
[email protected] | 479cce78 | 2012-09-15 20:15:53 | [diff] [blame] | 375 | new ui::SelectFileDialog::FileTypeInfo()); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 376 | if (accept_types.empty()) |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 377 | return base_file_type; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 378 | |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 379 | // Create FileTypeInfo and pre-allocate for the first extension list. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 380 | std::unique_ptr<ui::SelectFileDialog::FileTypeInfo> file_type( |
[email protected] | 479cce78 | 2012-09-15 20:15:53 | [diff] [blame] | 381 | new ui::SelectFileDialog::FileTypeInfo(*base_file_type)); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 382 | file_type->include_all_files = true; |
| 383 | file_type->extensions.resize(1); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 384 | std::vector<base::FilePath::StringType>* extensions = |
| 385 | &file_type->extensions.back(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 386 | |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 387 | // Find the corresponding extensions. |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 388 | int valid_type_count = 0; |
| 389 | int description_id = 0; |
[email protected] | 3314c2b1 | 2011-11-02 08:05:46 | [diff] [blame] | 390 | for (size_t i = 0; i < accept_types.size(); ++i) { |
[email protected] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 391 | std::string ascii_type = base::UTF16ToASCII(accept_types[i]); |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 392 | if (!IsAcceptTypeValid(ascii_type)) |
| 393 | continue; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 394 | |
| 395 | size_t old_extension_size = extensions->size(); |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 396 | if (ascii_type[0] == '.') { |
| 397 | // If the type starts with a period it is assumed to be a file extension |
| 398 | // so we just have to add it to the list. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 399 | base::FilePath::StringType ext(ascii_type.begin(), ascii_type.end()); |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 400 | extensions->push_back(ext.substr(1)); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 401 | } else { |
[email protected] | 4a66fa0e | 2012-09-10 06:45:20 | [diff] [blame] | 402 | if (ascii_type == "image/*") |
| 403 | description_id = IDS_IMAGE_FILES; |
| 404 | else if (ascii_type == "audio/*") |
| 405 | description_id = IDS_AUDIO_FILES; |
| 406 | else if (ascii_type == "video/*") |
| 407 | description_id = IDS_VIDEO_FILES; |
| 408 | |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 409 | net::GetExtensionsForMimeType(ascii_type, extensions); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | if (extensions->size() > old_extension_size) |
| 413 | valid_type_count++; |
| 414 | } |
| 415 | |
[email protected] | cbcd12ed | 2010-12-16 23:42:57 | [diff] [blame] | 416 | // If no valid extension is added, bail out. |
| 417 | if (valid_type_count == 0) |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 418 | return base_file_type; |
[email protected] | cbcd12ed | 2010-12-16 23:42:57 | [diff] [blame] | 419 | |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 420 | // Use a generic description "Custom Files" if either of the following is |
| 421 | // true: |
| 422 | // 1) There're multiple types specified, like "audio/*,video/*" |
| 423 | // 2) There're multiple extensions for a MIME type without parameter, like |
| 424 | // "ehtml,shtml,htm,html" for "text/html". On Windows, the select file |
| 425 | // dialog uses the first extension in the list to form the description, |
| 426 | // like "EHTML Files". This is not what we want. |
| 427 | if (valid_type_count > 1 || |
| 428 | (valid_type_count == 1 && description_id == 0 && extensions->size() > 1)) |
| 429 | description_id = IDS_CUSTOM_FILES; |
| 430 | |
| 431 | if (description_id) { |
| 432 | file_type->extension_description_overrides.push_back( |
| 433 | l10n_util::GetStringUTF16(description_id)); |
| 434 | } |
| 435 | |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 436 | return file_type; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 439 | // static |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 440 | void FileSelectHelper::RunFileChooser( |
| 441 | content::RenderFrameHost* render_frame_host, |
| 442 | const FileChooserParams& params) { |
| 443 | Profile* profile = Profile::FromBrowserContext( |
| 444 | render_frame_host->GetProcess()->GetBrowserContext()); |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 445 | // FileSelectHelper will keep itself alive until it sends the result message. |
| 446 | scoped_refptr<FileSelectHelper> file_select_helper( |
| 447 | new FileSelectHelper(profile)); |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 448 | file_select_helper->RunFileChooser( |
ricea | 86fa1dd | 2016-09-13 05:59:45 | [diff] [blame] | 449 | render_frame_host, base::MakeUnique<content::FileChooserParams>(params)); |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // static |
| 453 | void FileSelectHelper::EnumerateDirectory(content::WebContents* tab, |
| 454 | int request_id, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 455 | const base::FilePath& path) { |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 456 | Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext()); |
| 457 | // FileSelectHelper will keep itself alive until it sends the result message. |
| 458 | scoped_refptr<FileSelectHelper> file_select_helper( |
| 459 | new FileSelectHelper(profile)); |
| 460 | file_select_helper->EnumerateDirectory( |
| 461 | request_id, tab->GetRenderViewHost(), path); |
| 462 | } |
| 463 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 464 | void FileSelectHelper::RunFileChooser( |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 465 | content::RenderFrameHost* render_frame_host, |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 466 | std::unique_ptr<FileChooserParams> params) { |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 467 | DCHECK(!render_frame_host_); |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 468 | DCHECK(!web_contents_); |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 469 | DCHECK(params->default_file_name.empty() || |
| 470 | params->mode == FileChooserParams::Save) |
asanka | ee261f3 | 2015-10-27 21:04:28 | [diff] [blame] | 471 | << "The default_file_name parameter should only be specified for Save " |
| 472 | "file choosers"; |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 473 | DCHECK(params->default_file_name == params->default_file_name.BaseName()) |
asanka | ee261f3 | 2015-10-27 21:04:28 | [diff] [blame] | 474 | << "The default_file_name parameter should not contain path separators"; |
| 475 | |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 476 | render_frame_host_ = render_frame_host; |
| 477 | web_contents_ = WebContents::FromRenderFrameHost(render_frame_host); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 478 | notification_registrar_.RemoveAll(); |
lazyboy | 1cf60af | 2015-05-15 22:17:05 | [diff] [blame] | 479 | content::WebContentsObserver::Observe(web_contents_); |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 480 | notification_registrar_.Add( |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 481 | this, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 482 | content::Source<RenderWidgetHost>( |
| 483 | render_frame_host_->GetRenderViewHost()->GetWidget())); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 484 | |
| 485 | BrowserThread::PostTask( |
| 486 | BrowserThread::FILE, FROM_HERE, |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 487 | base::Bind(&FileSelectHelper::GetFileTypesOnFileThread, this, |
| 488 | base::Passed(¶ms))); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 489 | |
| 490 | // Because this class returns notifications to the RenderViewHost, it is |
| 491 | // difficult for callers to know how long to keep a reference to this |
| 492 | // instance. We AddRef() here to keep the instance alive after we return |
| 493 | // to the caller, until the last callback is received from the file dialog. |
| 494 | // At that point, we must call RunFileChooserEnd(). |
| 495 | AddRef(); |
| 496 | } |
| 497 | |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 498 | void FileSelectHelper::GetFileTypesOnFileThread( |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 499 | std::unique_ptr<FileChooserParams> params) { |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 500 | select_file_types_ = GetFileTypesFromAcceptType(params->accept_types); |
hirono | 4d4d339 | 2016-02-04 05:03:50 | [diff] [blame] | 501 | select_file_types_->allowed_paths = |
| 502 | params->need_local_path ? ui::SelectFileDialog::FileTypeInfo::NATIVE_PATH |
| 503 | : ui::SelectFileDialog::FileTypeInfo::ANY_PATH; |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 504 | |
| 505 | BrowserThread::PostTask( |
| 506 | BrowserThread::UI, FROM_HERE, |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 507 | base::Bind(&FileSelectHelper::GetSanitizedFilenameOnUIThread, this, |
| 508 | base::Passed(¶ms))); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 509 | } |
| 510 | |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 511 | void FileSelectHelper::GetSanitizedFilenameOnUIThread( |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 512 | std::unique_ptr<FileChooserParams> params) { |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 513 | base::FilePath default_file_path = profile_->last_selected_directory().Append( |
| 514 | GetSanitizedFileName(params->default_file_name)); |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 515 | #if defined(FULL_SAFE_BROWSING) |
asanka | ef1f3872 | 2016-05-20 16:17:41 | [diff] [blame] | 516 | CheckDownloadRequestWithSafeBrowsing(default_file_path, std::move(params)); |
| 517 | #else |
dcheng | e73d8520c | 2015-12-27 01:19:09 | [diff] [blame] | 518 | RunFileChooserOnUIThread(default_file_path, std::move(params)); |
asanka | ef1f3872 | 2016-05-20 16:17:41 | [diff] [blame] | 519 | #endif |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | #if defined(FULL_SAFE_BROWSING) |
asanka | ef1f3872 | 2016-05-20 16:17:41 | [diff] [blame] | 523 | void FileSelectHelper::CheckDownloadRequestWithSafeBrowsing( |
| 524 | const base::FilePath& default_file_path, |
| 525 | std::unique_ptr<FileChooserParams> params) { |
| 526 | safe_browsing::SafeBrowsingService* sb_service = |
| 527 | g_browser_process->safe_browsing_service(); |
| 528 | |
| 529 | if (!sb_service || !sb_service->download_protection_service() || |
| 530 | !sb_service->download_protection_service()->enabled()) { |
| 531 | RunFileChooserOnUIThread(default_file_path, std::move(params)); |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | |
asanka | ef1f3872 | 2016-05-20 16:17:41 | [diff] [blame] | 535 | std::vector<base::FilePath::StringType> alternate_extensions; |
| 536 | if (select_file_types_) { |
| 537 | for (const auto& extensions_list : select_file_types_->extensions) { |
| 538 | for (const auto& extension_in_list : extensions_list) { |
| 539 | base::FilePath::StringType extension = |
| 540 | default_file_path.ReplaceExtension(extension_in_list) |
| 541 | .FinalExtension(); |
| 542 | alternate_extensions.push_back(extension); |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | GURL requestor_url = params->requestor; |
| 548 | sb_service->download_protection_service()->CheckPPAPIDownloadRequest( |
jialiul | af664d30 | 2017-01-06 01:34:49 | [diff] [blame] | 549 | requestor_url, |
| 550 | render_frame_host_? render_frame_host_->GetLastCommittedURL() : GURL(), |
| 551 | WebContents::FromRenderFrameHost(render_frame_host_), |
| 552 | default_file_path, alternate_extensions, profile_, |
asanka | ef1f3872 | 2016-05-20 16:17:41 | [diff] [blame] | 553 | base::Bind(&InterpretSafeBrowsingVerdict, |
| 554 | base::Bind(&FileSelectHelper::ProceedWithSafeBrowsingVerdict, |
| 555 | this, default_file_path, base::Passed(¶ms)))); |
| 556 | } |
| 557 | |
| 558 | void FileSelectHelper::ProceedWithSafeBrowsingVerdict( |
| 559 | const base::FilePath& default_file_path, |
| 560 | std::unique_ptr<content::FileChooserParams> params, |
| 561 | bool allowed_by_safe_browsing) { |
| 562 | if (!allowed_by_safe_browsing) { |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 563 | NotifyRenderFrameHostAndEnd(std::vector<ui::SelectedFileInfo>()); |
asanka | ef1f3872 | 2016-05-20 16:17:41 | [diff] [blame] | 564 | return; |
| 565 | } |
| 566 | RunFileChooserOnUIThread(default_file_path, std::move(params)); |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 567 | } |
| 568 | #endif |
| 569 | |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 570 | void FileSelectHelper::RunFileChooserOnUIThread( |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 571 | const base::FilePath& default_file_path, |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 572 | std::unique_ptr<FileChooserParams> params) { |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 573 | DCHECK(params); |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 574 | if (!render_frame_host_ || !web_contents_ || !IsValidProfile(profile_) || |
| 575 | !web_contents_->GetNativeView()) { |
[email protected] | b95b08d | 2011-12-15 20:23:16 | [diff] [blame] | 576 | // If the renderer was destroyed before we started, just cancel the |
| 577 | // operation. |
| 578 | RunFileChooserEnd(); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 579 | return; |
[email protected] | b95b08d | 2011-12-15 20:23:16 | [diff] [blame] | 580 | } |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 581 | |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 582 | select_file_dialog_ = ui::SelectFileDialog::Create( |
[email protected] | 6e1fcd1 | 2012-07-02 17:14:20 | [diff] [blame] | 583 | this, new ChromeSelectFilePolicy(web_contents_)); |
dcheng | 319a2195 | 2014-08-26 22:52:40 | [diff] [blame] | 584 | if (!select_file_dialog_.get()) |
[email protected] | 6910cb24 | 2014-03-20 14:33:01 | [diff] [blame] | 585 | return; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 586 | |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 587 | dialog_mode_ = params->mode; |
| 588 | switch (params->mode) { |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 589 | case FileChooserParams::Open: |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 590 | dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 591 | break; |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 592 | case FileChooserParams::OpenMultiple: |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 593 | dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 594 | break; |
[email protected] | 6bedbef | 2013-07-31 06:33:49 | [diff] [blame] | 595 | case FileChooserParams::UploadFolder: |
| 596 | dialog_type_ = ui::SelectFileDialog::SELECT_UPLOAD_FOLDER; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 597 | break; |
[email protected] | 33f8ad5 | 2012-05-22 18:10:13 | [diff] [blame] | 598 | case FileChooserParams::Save: |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 599 | dialog_type_ = ui::SelectFileDialog::SELECT_SAVEAS_FILE; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 600 | break; |
| 601 | default: |
[email protected] | 92f5408 | 2012-07-31 01:43:14 | [diff] [blame] | 602 | // Prevent warning. |
| 603 | dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 604 | NOTREACHED(); |
| 605 | } |
[email protected] | 4e9149a | 2012-08-15 20:43:59 | [diff] [blame] | 606 | |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 607 | gfx::NativeWindow owning_window = |
| 608 | platform_util::GetTopLevel(web_contents_->GetNativeView()); |
[email protected] | d989891 | 2011-04-15 21:10:00 | [diff] [blame] | 609 | |
[email protected] | 2d02a200 | 2012-09-18 21:47:56 | [diff] [blame] | 610 | #if defined(OS_ANDROID) |
| 611 | // Android needs the original MIME types and an additional capture value. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 612 | std::pair<std::vector<base::string16>, bool> accept_types = |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 613 | std::make_pair(params->accept_types, params->capture); |
[email protected] | 2d02a200 | 2012-09-18 21:47:56 | [diff] [blame] | 614 | #endif |
| 615 | |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 616 | select_file_dialog_->SelectFile( |
asanka | 1cbf4981 | 2015-11-03 18:28:15 | [diff] [blame] | 617 | dialog_type_, params->title, default_file_path, select_file_types_.get(), |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 618 | select_file_types_.get() && !select_file_types_->extensions.empty() |
| 619 | ? 1 |
| 620 | : 0, // 1-based index of default extension to show. |
| 621 | base::FilePath::StringType(), |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 622 | owning_window, |
[email protected] | b8452fa | 2012-06-15 01:41:41 | [diff] [blame] | 623 | #if defined(OS_ANDROID) |
[email protected] | 2d02a200 | 2012-09-18 21:47:56 | [diff] [blame] | 624 | &accept_types); |
[email protected] | b8452fa | 2012-06-15 01:41:41 | [diff] [blame] | 625 | #else |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 626 | NULL); |
[email protected] | b8452fa | 2012-06-15 01:41:41 | [diff] [blame] | 627 | #endif |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 628 | |
| 629 | select_file_types_.reset(); |
| 630 | } |
| 631 | |
| 632 | // This method is called when we receive the last callback from the file |
| 633 | // chooser dialog. Perform any cleanup and release the reference we added |
| 634 | // in RunFileChooser(). |
| 635 | void FileSelectHelper::RunFileChooserEnd() { |
erikchen | 22de64a3 | 2014-10-10 18:27:43 | [diff] [blame] | 636 | // If there are temporary files, then this instance needs to stick around |
| 637 | // until web_contents_ is destroyed, so that this instance can delete the |
| 638 | // temporary files. |
| 639 | if (!temporary_files_.empty()) |
| 640 | return; |
| 641 | |
nasko | 35169af | 2016-06-10 18:47:13 | [diff] [blame] | 642 | render_frame_host_ = nullptr; |
| 643 | web_contents_ = nullptr; |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 644 | Release(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 645 | } |
| 646 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 647 | void FileSelectHelper::EnumerateDirectory(int request_id, |
| 648 | RenderViewHost* render_view_host, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 649 | const base::FilePath& path) { |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 650 | // Because this class returns notifications to the RenderViewHost, it is |
| 651 | // difficult for callers to know how long to keep a reference to this |
| 652 | // instance. We AddRef() here to keep the instance alive after we return |
| 653 | // to the caller, until the last callback is received from the enumeration |
| 654 | // code. At that point, we must call EnumerateDirectoryEnd(). |
| 655 | AddRef(); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 656 | StartNewEnumeration(path, request_id, render_view_host); |
| 657 | } |
| 658 | |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 659 | // This method is called when we receive the last callback from the enumeration |
| 660 | // code. Perform any cleanup and release the reference we added in |
| 661 | // EnumerateDirectory(). |
| 662 | void FileSelectHelper::EnumerateDirectoryEnd() { |
| 663 | Release(); |
| 664 | } |
| 665 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 666 | void FileSelectHelper::Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 667 | const content::NotificationSource& source, |
| 668 | const content::NotificationDetails& details) { |
thestig | 5283579 | 2016-06-13 21:08:09 | [diff] [blame] | 669 | DCHECK_EQ(content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, type); |
thestig | 5283579 | 2016-06-13 21:08:09 | [diff] [blame] | 670 | render_frame_host_ = nullptr; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 671 | } |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 672 | |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 673 | void FileSelectHelper::RenderFrameHostChanged( |
| 674 | content::RenderFrameHost* old_host, |
| 675 | content::RenderFrameHost* new_host) { |
nasko | 5e61b75f | 2016-07-01 22:35:05 | [diff] [blame] | 676 | if (old_host == render_frame_host_) |
| 677 | render_frame_host_ = nullptr; |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | void FileSelectHelper::RenderFrameDeleted( |
| 681 | content::RenderFrameHost* render_frame_host) { |
nasko | 5e61b75f | 2016-07-01 22:35:05 | [diff] [blame] | 682 | if (render_frame_host == render_frame_host_) |
| 683 | render_frame_host_ = nullptr; |
lazyboy | 1cf60af | 2015-05-15 22:17:05 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | void FileSelectHelper::WebContentsDestroyed() { |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 687 | render_frame_host_ = nullptr; |
lazyboy | 1cf60af | 2015-05-15 22:17:05 | [diff] [blame] | 688 | web_contents_ = nullptr; |
nasko | f6a80ac | 2016-06-29 02:37:05 | [diff] [blame] | 689 | CleanUp(); |
lazyboy | 1cf60af | 2015-05-15 22:17:05 | [diff] [blame] | 690 | } |
| 691 | |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 692 | // static |
| 693 | bool FileSelectHelper::IsAcceptTypeValid(const std::string& accept_type) { |
| 694 | // TODO(raymes): This only does some basic checks, extend to test more cases. |
| 695 | // A 1 character accept type will always be invalid (either a "." in the case |
| 696 | // of an extension or a "/" in the case of a MIME type). |
| 697 | std::string unused; |
| 698 | if (accept_type.length() <= 1 || |
brettw | fce8d19 | 2015-08-10 19:07:51 | [diff] [blame] | 699 | base::ToLowerASCII(accept_type) != accept_type || |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 700 | base::TrimWhitespaceASCII(accept_type, base::TRIM_ALL, &unused) != |
| 701 | base::TRIM_NONE) { |
[email protected] | f9a4c41a | 2012-05-30 00:05:32 | [diff] [blame] | 702 | return false; |
| 703 | } |
| 704 | return true; |
| 705 | } |
asanka | ee261f3 | 2015-10-27 21:04:28 | [diff] [blame] | 706 | |
| 707 | // static |
| 708 | base::FilePath FileSelectHelper::GetSanitizedFileName( |
| 709 | const base::FilePath& suggested_filename) { |
| 710 | if (suggested_filename.empty()) |
| 711 | return base::FilePath(); |
| 712 | return net::GenerateFileName( |
| 713 | GURL(), std::string(), std::string(), suggested_filename.AsUTF8Unsafe(), |
| 714 | std::string(), l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
| 715 | } |