| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/file_select_helper.h" | 5 #include "chrome/browser/file_select_helper.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 | 191 |
| 192 std::vector<ui::SelectedFileInfo> files; | 192 std::vector<ui::SelectedFileInfo> files; |
| 193 files.push_back(file); | 193 files.push_back(file); |
| 194 | 194 |
| 195 #if defined(OS_MACOSX) | 195 #if defined(OS_MACOSX) |
| 196 content::BrowserThread::PostTask( | 196 content::BrowserThread::PostTask( |
| 197 content::BrowserThread::FILE_USER_BLOCKING, | 197 content::BrowserThread::FILE_USER_BLOCKING, |
| 198 FROM_HERE, | 198 FROM_HERE, |
| 199 base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files)); | 199 base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files)); |
| 200 #else | 200 #else |
| 201 NotifyRenderViewHostAndEnd(files); | 201 NotifyRenderFrameHostAndEnd(files); |
| 202 #endif // defined(OS_MACOSX) | 202 #endif // defined(OS_MACOSX) |
| 203 } | 203 } |
| 204 | 204 |
| 205 void FileSelectHelper::MultiFilesSelected( | 205 void FileSelectHelper::MultiFilesSelected( |
| 206 const std::vector<base::FilePath>& files, | 206 const std::vector<base::FilePath>& files, |
| 207 void* params) { | 207 void* params) { |
| 208 std::vector<ui::SelectedFileInfo> selected_files = | 208 std::vector<ui::SelectedFileInfo> selected_files = |
| 209 FilePathListToSelectedFileInfoList(files); | 209 FilePathListToSelectedFileInfoList(files); |
| 210 | 210 |
| 211 MultiFilesSelectedWithExtraInfo(selected_files, params); | 211 MultiFilesSelectedWithExtraInfo(selected_files, params); |
| 212 } | 212 } |
| 213 | 213 |
| 214 void FileSelectHelper::MultiFilesSelectedWithExtraInfo( | 214 void FileSelectHelper::MultiFilesSelectedWithExtraInfo( |
| 215 const std::vector<ui::SelectedFileInfo>& files, | 215 const std::vector<ui::SelectedFileInfo>& files, |
| 216 void* params) { | 216 void* params) { |
| 217 if (!files.empty() && IsValidProfile(profile_)) | 217 if (!files.empty() && IsValidProfile(profile_)) |
| 218 profile_->set_last_selected_directory(files[0].file_path.DirName()); | 218 profile_->set_last_selected_directory(files[0].file_path.DirName()); |
| 219 | 219 |
| 220 #if defined(OS_MACOSX) | 220 #if defined(OS_MACOSX) |
| 221 content::BrowserThread::PostTask( | 221 content::BrowserThread::PostTask( |
| 222 content::BrowserThread::FILE_USER_BLOCKING, | 222 content::BrowserThread::FILE_USER_BLOCKING, |
| 223 FROM_HERE, | 223 FROM_HERE, |
| 224 base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files)); | 224 base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files)); |
| 225 #else | 225 #else |
| 226 NotifyRenderViewHostAndEnd(files); | 226 NotifyRenderFrameHostAndEnd(files); |
| 227 #endif // defined(OS_MACOSX) | 227 #endif // defined(OS_MACOSX) |
| 228 } | 228 } |
| 229 | 229 |
| 230 void FileSelectHelper::FileSelectionCanceled(void* params) { | 230 void FileSelectHelper::FileSelectionCanceled(void* params) { |
| 231 NotifyRenderViewHostAndEnd(std::vector<ui::SelectedFileInfo>()); | 231 NotifyRenderFrameHostAndEnd(std::vector<ui::SelectedFileInfo>()); |
| 232 } | 232 } |
| 233 | 233 |
| 234 void FileSelectHelper::StartNewEnumeration(const base::FilePath& path, | 234 void FileSelectHelper::StartNewEnumeration(const base::FilePath& path, |
| 235 int request_id, | 235 int request_id, |
| 236 RenderViewHost* render_view_host) { | 236 RenderViewHost* render_view_host) { |
| 237 std::unique_ptr<ActiveDirectoryEnumeration> entry( | 237 std::unique_ptr<ActiveDirectoryEnumeration> entry( |
| 238 new ActiveDirectoryEnumeration); | 238 new ActiveDirectoryEnumeration); |
| 239 entry->rvh_ = render_view_host; | 239 entry->rvh_ = render_view_host; |
| 240 entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id)); | 240 entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id)); |
| 241 entry->lister_.reset(new net::DirectoryLister( | 241 entry->lister_.reset(new net::DirectoryLister( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 272 return; | 272 return; |
| 273 if (error) { | 273 if (error) { |
| 274 FileSelectionCanceled(NULL); | 274 FileSelectionCanceled(NULL); |
| 275 return; | 275 return; |
| 276 } | 276 } |
| 277 | 277 |
| 278 std::vector<ui::SelectedFileInfo> selected_files = | 278 std::vector<ui::SelectedFileInfo> selected_files = |
| 279 FilePathListToSelectedFileInfoList(entry->results_); | 279 FilePathListToSelectedFileInfoList(entry->results_); |
| 280 | 280 |
| 281 if (id == kFileSelectEnumerationId) { | 281 if (id == kFileSelectEnumerationId) { |
| 282 NotifyRenderViewHostAndEnd(selected_files); | 282 NotifyRenderFrameHostAndEnd(selected_files); |
| 283 } else { | 283 } else { |
| 284 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); | 284 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); |
| 285 EnumerateDirectoryEnd(); | 285 EnumerateDirectoryEnd(); |
| 286 } | 286 } |
| 287 } | 287 } |
| 288 | 288 |
| 289 void FileSelectHelper::NotifyRenderViewHostAndEnd( | 289 void FileSelectHelper::NotifyRenderFrameHostAndEnd( |
| 290 const std::vector<ui::SelectedFileInfo>& files) { | 290 const std::vector<ui::SelectedFileInfo>& files) { |
| 291 if (!render_frame_host_) { | 291 if (!render_frame_host_) { |
| 292 RunFileChooserEnd(); | 292 RunFileChooserEnd(); |
| 293 return; | 293 return; |
| 294 } | 294 } |
| 295 | 295 |
| 296 #if defined(OS_CHROMEOS) | 296 #if defined(OS_CHROMEOS) |
| 297 if (!files.empty()) { | 297 if (!files.empty()) { |
| 298 if (!IsValidProfile(profile_)) { | 298 if (!IsValidProfile(profile_)) { |
| 299 RunFileChooserEnd(); | 299 RunFileChooserEnd(); |
| 300 return; | 300 return; |
| 301 } | 301 } |
| 302 // Converts |files| into FileChooserFileInfo with handling of non-native | 302 // Converts |files| into FileChooserFileInfo with handling of non-native |
| 303 // files. | 303 // files. |
| 304 content::SiteInstance* site_instance = | 304 content::SiteInstance* site_instance = |
| 305 render_frame_host_->GetSiteInstance(); | 305 render_frame_host_->GetSiteInstance(); |
| 306 storage::FileSystemContext* file_system_context = | 306 storage::FileSystemContext* file_system_context = |
| 307 content::BrowserContext::GetStoragePartition(profile_, site_instance) | 307 content::BrowserContext::GetStoragePartition(profile_, site_instance) |
| 308 ->GetFileSystemContext(); | 308 ->GetFileSystemContext(); |
| 309 file_manager::util::ConvertSelectedFileInfoListToFileChooserFileInfoList( | 309 file_manager::util::ConvertSelectedFileInfoListToFileChooserFileInfoList( |
| 310 file_system_context, site_instance->GetSiteURL(), files, | 310 file_system_context, site_instance->GetSiteURL(), files, |
| 311 base::Bind(&FileSelectHelper::NotifyRenderViewHostAndEndAfterConversion, | 311 base::Bind( |
| 312 this)); | 312 &FileSelectHelper::NotifyRenderFrameHostAndEndAfterConversion, |
| 313 this)); |
| 313 return; | 314 return; |
| 314 } | 315 } |
| 315 #endif // defined(OS_CHROMEOS) | 316 #endif // defined(OS_CHROMEOS) |
| 316 | 317 |
| 317 std::vector<content::FileChooserFileInfo> chooser_files; | 318 std::vector<content::FileChooserFileInfo> chooser_files; |
| 318 for (const auto& file : files) { | 319 for (const auto& file : files) { |
| 319 content::FileChooserFileInfo chooser_file; | 320 content::FileChooserFileInfo chooser_file; |
| 320 chooser_file.file_path = file.local_path; | 321 chooser_file.file_path = file.local_path; |
| 321 chooser_file.display_name = file.display_name; | 322 chooser_file.display_name = file.display_name; |
| 322 chooser_files.push_back(chooser_file); | 323 chooser_files.push_back(chooser_file); |
| 323 } | 324 } |
| 324 | 325 |
| 325 NotifyRenderViewHostAndEndAfterConversion(chooser_files); | 326 NotifyRenderFrameHostAndEndAfterConversion(chooser_files); |
| 326 } | 327 } |
| 327 | 328 |
| 328 void FileSelectHelper::NotifyRenderViewHostAndEndAfterConversion( | 329 void FileSelectHelper::NotifyRenderFrameHostAndEndAfterConversion( |
| 329 const std::vector<content::FileChooserFileInfo>& list) { | 330 const std::vector<content::FileChooserFileInfo>& list) { |
| 330 if (render_frame_host_) | 331 if (render_frame_host_) |
| 331 render_frame_host_->FilesSelectedInChooser(list, dialog_mode_); | 332 render_frame_host_->FilesSelectedInChooser(list, dialog_mode_); |
| 332 | 333 |
| 333 // No members should be accessed from here on. | 334 // No members should be accessed from here on. |
| 334 RunFileChooserEnd(); | 335 RunFileChooserEnd(); |
| 335 } | 336 } |
| 336 | 337 |
| 337 void FileSelectHelper::DeleteTemporaryFiles() { | 338 void FileSelectHelper::DeleteTemporaryFiles() { |
| 338 BrowserThread::PostTask(BrowserThread::FILE, | 339 BrowserThread::PostTask(BrowserThread::FILE, |
| 339 FROM_HERE, | 340 FROM_HERE, |
| 340 base::Bind(&DeleteFiles, temporary_files_)); | 341 base::Bind(&DeleteFiles, temporary_files_)); |
| 341 temporary_files_.clear(); | 342 temporary_files_.clear(); |
| 342 } | 343 } |
| 343 | 344 |
| 344 void FileSelectHelper::CleanUpOnRenderViewHostChange() { | 345 void FileSelectHelper::CleanUp() { |
| 345 if (!temporary_files_.empty()) { | 346 if (!temporary_files_.empty()) { |
| 346 DeleteTemporaryFiles(); | 347 DeleteTemporaryFiles(); |
| 347 | 348 |
| 348 // Now that the temporary files have been scheduled for deletion, there | 349 // Now that the temporary files have been scheduled for deletion, there |
| 349 // is no longer any reason to keep this instance around. | 350 // is no longer any reason to keep this instance around. |
| 350 Release(); | 351 Release(); |
| 351 } | 352 } |
| 352 } | 353 } |
| 353 | 354 |
| 354 std::unique_ptr<ui::SelectFileDialog::FileTypeInfo> | 355 std::unique_ptr<ui::SelectFileDialog::FileTypeInfo> |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 base::Bind(&InterpretSafeBrowsingVerdict, | 535 base::Bind(&InterpretSafeBrowsingVerdict, |
| 535 base::Bind(&FileSelectHelper::ProceedWithSafeBrowsingVerdict, | 536 base::Bind(&FileSelectHelper::ProceedWithSafeBrowsingVerdict, |
| 536 this, default_file_path, base::Passed(¶ms)))); | 537 this, default_file_path, base::Passed(¶ms)))); |
| 537 } | 538 } |
| 538 | 539 |
| 539 void FileSelectHelper::ProceedWithSafeBrowsingVerdict( | 540 void FileSelectHelper::ProceedWithSafeBrowsingVerdict( |
| 540 const base::FilePath& default_file_path, | 541 const base::FilePath& default_file_path, |
| 541 std::unique_ptr<content::FileChooserParams> params, | 542 std::unique_ptr<content::FileChooserParams> params, |
| 542 bool allowed_by_safe_browsing) { | 543 bool allowed_by_safe_browsing) { |
| 543 if (!allowed_by_safe_browsing) { | 544 if (!allowed_by_safe_browsing) { |
| 544 NotifyRenderViewHostAndEnd(std::vector<ui::SelectedFileInfo>()); | 545 NotifyRenderFrameHostAndEnd(std::vector<ui::SelectedFileInfo>()); |
| 545 return; | 546 return; |
| 546 } | 547 } |
| 547 RunFileChooserOnUIThread(default_file_path, std::move(params)); | 548 RunFileChooserOnUIThread(default_file_path, std::move(params)); |
| 548 } | 549 } |
| 549 #endif | 550 #endif |
| 550 | 551 |
| 551 void FileSelectHelper::RunFileChooserOnUIThread( | 552 void FileSelectHelper::RunFileChooserOnUIThread( |
| 552 const base::FilePath& default_file_path, | 553 const base::FilePath& default_file_path, |
| 553 std::unique_ptr<FileChooserParams> params) { | 554 std::unique_ptr<FileChooserParams> params) { |
| 554 DCHECK(params); | 555 DCHECK(params); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 // code. Perform any cleanup and release the reference we added in | 642 // code. Perform any cleanup and release the reference we added in |
| 642 // EnumerateDirectory(). | 643 // EnumerateDirectory(). |
| 643 void FileSelectHelper::EnumerateDirectoryEnd() { | 644 void FileSelectHelper::EnumerateDirectoryEnd() { |
| 644 Release(); | 645 Release(); |
| 645 } | 646 } |
| 646 | 647 |
| 647 void FileSelectHelper::Observe(int type, | 648 void FileSelectHelper::Observe(int type, |
| 648 const content::NotificationSource& source, | 649 const content::NotificationSource& source, |
| 649 const content::NotificationDetails& details) { | 650 const content::NotificationDetails& details) { |
| 650 DCHECK_EQ(content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, type); | 651 DCHECK_EQ(content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, type); |
| 651 DCHECK_EQ(content::Source<RenderWidgetHost>(source).ptr(), | |
| 652 render_frame_host_->GetRenderViewHost()->GetWidget()); | |
| 653 render_frame_host_ = nullptr; | 652 render_frame_host_ = nullptr; |
| 654 } | 653 } |
| 655 | 654 |
| 656 void FileSelectHelper::RenderViewHostChanged(RenderViewHost* old_host, | 655 void FileSelectHelper::RenderFrameHostChanged( |
| 657 RenderViewHost* new_host) { | 656 content::RenderFrameHost* old_host, |
| 658 CleanUpOnRenderViewHostChange(); | 657 content::RenderFrameHost* new_host) { |
| 658 render_frame_host_ = nullptr; |
| 659 } |
| 660 |
| 661 void FileSelectHelper::RenderFrameDeleted( |
| 662 content::RenderFrameHost* render_frame_host) { |
| 663 render_frame_host_ = nullptr; |
| 659 } | 664 } |
| 660 | 665 |
| 661 void FileSelectHelper::WebContentsDestroyed() { | 666 void FileSelectHelper::WebContentsDestroyed() { |
| 667 render_frame_host_ = nullptr; |
| 662 web_contents_ = nullptr; | 668 web_contents_ = nullptr; |
| 663 CleanUpOnRenderViewHostChange(); | 669 CleanUp(); |
| 664 } | 670 } |
| 665 | 671 |
| 666 // static | 672 // static |
| 667 bool FileSelectHelper::IsAcceptTypeValid(const std::string& accept_type) { | 673 bool FileSelectHelper::IsAcceptTypeValid(const std::string& accept_type) { |
| 668 // TODO(raymes): This only does some basic checks, extend to test more cases. | 674 // TODO(raymes): This only does some basic checks, extend to test more cases. |
| 669 // A 1 character accept type will always be invalid (either a "." in the case | 675 // A 1 character accept type will always be invalid (either a "." in the case |
| 670 // of an extension or a "/" in the case of a MIME type). | 676 // of an extension or a "/" in the case of a MIME type). |
| 671 std::string unused; | 677 std::string unused; |
| 672 if (accept_type.length() <= 1 || | 678 if (accept_type.length() <= 1 || |
| 673 base::ToLowerASCII(accept_type) != accept_type || | 679 base::ToLowerASCII(accept_type) != accept_type || |
| 674 base::TrimWhitespaceASCII(accept_type, base::TRIM_ALL, &unused) != | 680 base::TrimWhitespaceASCII(accept_type, base::TRIM_ALL, &unused) != |
| 675 base::TRIM_NONE) { | 681 base::TRIM_NONE) { |
| 676 return false; | 682 return false; |
| 677 } | 683 } |
| 678 return true; | 684 return true; |
| 679 } | 685 } |
| 680 | 686 |
| 681 // static | 687 // static |
| 682 base::FilePath FileSelectHelper::GetSanitizedFileName( | 688 base::FilePath FileSelectHelper::GetSanitizedFileName( |
| 683 const base::FilePath& suggested_filename) { | 689 const base::FilePath& suggested_filename) { |
| 684 if (suggested_filename.empty()) | 690 if (suggested_filename.empty()) |
| 685 return base::FilePath(); | 691 return base::FilePath(); |
| 686 return net::GenerateFileName( | 692 return net::GenerateFileName( |
| 687 GURL(), std::string(), std::string(), suggested_filename.AsUTF8Unsafe(), | 693 GURL(), std::string(), std::string(), suggested_filename.AsUTF8Unsafe(), |
| 688 std::string(), l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); | 694 std::string(), l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
| 689 } | 695 } |
| OLD | NEW |