| 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 <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "content/public/browser/notification_details.h" | 25 #include "content/public/browser/notification_details.h" |
| 26 #include "content/public/browser/notification_source.h" | 26 #include "content/public/browser/notification_source.h" |
| 27 #include "content/public/browser/notification_types.h" | 27 #include "content/public/browser/notification_types.h" |
| 28 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
| 29 #include "content/public/browser/render_widget_host.h" | 29 #include "content/public/browser/render_widget_host.h" |
| 30 #include "content/public/browser/render_widget_host_view.h" | 30 #include "content/public/browser/render_widget_host_view.h" |
| 31 #include "content/public/browser/storage_partition.h" | 31 #include "content/public/browser/storage_partition.h" |
| 32 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 33 #include "content/public/common/file_chooser_file_info.h" | 33 #include "content/public/common/file_chooser_file_info.h" |
| 34 #include "content/public/common/file_chooser_params.h" | 34 #include "content/public/common/file_chooser_params.h" |
| 35 #include "net/base/filename_util.h" |
| 35 #include "net/base/mime_util.h" | 36 #include "net/base/mime_util.h" |
| 36 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
| 37 #include "ui/shell_dialogs/selected_file_info.h" | 38 #include "ui/shell_dialogs/selected_file_info.h" |
| 38 | 39 |
| 39 #if defined(OS_CHROMEOS) | 40 #if defined(OS_CHROMEOS) |
| 40 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" | 41 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
| 41 #include "content/public/browser/site_instance.h" | 42 #include "content/public/browser/site_instance.h" |
| 42 #endif | 43 #endif |
| 43 | 44 |
| 45 #if defined(FULL_SAFE_BROWSING) |
| 46 #include "chrome/browser/safe_browsing/unverified_download_policy.h" |
| 47 #endif |
| 48 |
| 44 using content::BrowserThread; | 49 using content::BrowserThread; |
| 45 using content::FileChooserParams; | 50 using content::FileChooserParams; |
| 46 using content::RenderViewHost; | 51 using content::RenderViewHost; |
| 47 using content::RenderWidgetHost; | 52 using content::RenderWidgetHost; |
| 48 using content::WebContents; | 53 using content::WebContents; |
| 49 | 54 |
| 50 namespace { | 55 namespace { |
| 51 | 56 |
| 52 // There is only one file-selection happening at any given time, | 57 // There is only one file-selection happening at any given time, |
| 53 // so we allocate an enumeration ID for that purpose. All IDs from | 58 // so we allocate an enumeration ID for that purpose. All IDs from |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 new FileSelectHelper(profile)); | 400 new FileSelectHelper(profile)); |
| 396 file_select_helper->EnumerateDirectory( | 401 file_select_helper->EnumerateDirectory( |
| 397 request_id, tab->GetRenderViewHost(), path); | 402 request_id, tab->GetRenderViewHost(), path); |
| 398 } | 403 } |
| 399 | 404 |
| 400 void FileSelectHelper::RunFileChooser(RenderViewHost* render_view_host, | 405 void FileSelectHelper::RunFileChooser(RenderViewHost* render_view_host, |
| 401 content::WebContents* web_contents, | 406 content::WebContents* web_contents, |
| 402 const FileChooserParams& params) { | 407 const FileChooserParams& params) { |
| 403 DCHECK(!render_view_host_); | 408 DCHECK(!render_view_host_); |
| 404 DCHECK(!web_contents_); | 409 DCHECK(!web_contents_); |
| 410 DCHECK(params.default_file_name.empty() || |
| 411 params.mode == FileChooserParams::Save) |
| 412 << "The default_file_name parameter should only be specified for Save " |
| 413 "file choosers"; |
| 414 DCHECK(params.default_file_name == params.default_file_name.BaseName()) |
| 415 << "The default_file_name parameter should not contain path separators"; |
| 416 |
| 405 render_view_host_ = render_view_host; | 417 render_view_host_ = render_view_host; |
| 406 web_contents_ = web_contents; | 418 web_contents_ = web_contents; |
| 407 notification_registrar_.RemoveAll(); | 419 notification_registrar_.RemoveAll(); |
| 408 content::WebContentsObserver::Observe(web_contents_); | 420 content::WebContentsObserver::Observe(web_contents_); |
| 409 notification_registrar_.Add( | 421 notification_registrar_.Add( |
| 410 this, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, | 422 this, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, |
| 411 content::Source<RenderWidgetHost>(render_view_host_->GetWidget())); | 423 content::Source<RenderWidgetHost>(render_view_host_->GetWidget())); |
| 412 | 424 |
| 413 BrowserThread::PostTask( | 425 BrowserThread::PostTask( |
| 414 BrowserThread::FILE, FROM_HERE, | 426 BrowserThread::FILE, FROM_HERE, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 434 | 446 |
| 435 void FileSelectHelper::RunFileChooserOnUIThread( | 447 void FileSelectHelper::RunFileChooserOnUIThread( |
| 436 const FileChooserParams& params) { | 448 const FileChooserParams& params) { |
| 437 if (!render_view_host_ || !web_contents_ || !IsValidProfile(profile_)) { | 449 if (!render_view_host_ || !web_contents_ || !IsValidProfile(profile_)) { |
| 438 // If the renderer was destroyed before we started, just cancel the | 450 // If the renderer was destroyed before we started, just cancel the |
| 439 // operation. | 451 // operation. |
| 440 RunFileChooserEnd(); | 452 RunFileChooserEnd(); |
| 441 return; | 453 return; |
| 442 } | 454 } |
| 443 | 455 |
| 456 base::FilePath default_file_path = profile_->last_selected_directory().Append( |
| 457 GetSanitizedFileName(params.default_file_name)); |
| 458 |
| 459 #if defined(FULL_SAFE_BROWSING) |
| 460 if (params.mode == FileChooserParams::Save && |
| 461 !params.default_file_name.empty() && |
| 462 !safe_browsing::IsUnverifiedDownloadAllowed(default_file_path)) { |
| 463 NotifyRenderViewHostAndEnd(std::vector<ui::SelectedFileInfo>()); |
| 464 return; |
| 465 } |
| 466 #endif |
| 467 |
| 444 select_file_dialog_ = ui::SelectFileDialog::Create( | 468 select_file_dialog_ = ui::SelectFileDialog::Create( |
| 445 this, new ChromeSelectFilePolicy(web_contents_)); | 469 this, new ChromeSelectFilePolicy(web_contents_)); |
| 446 if (!select_file_dialog_.get()) | 470 if (!select_file_dialog_.get()) |
| 447 return; | 471 return; |
| 448 | 472 |
| 449 dialog_mode_ = params.mode; | 473 dialog_mode_ = params.mode; |
| 450 switch (params.mode) { | 474 switch (params.mode) { |
| 451 case FileChooserParams::Open: | 475 case FileChooserParams::Open: |
| 452 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; | 476 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; |
| 453 break; | 477 break; |
| 454 case FileChooserParams::OpenMultiple: | 478 case FileChooserParams::OpenMultiple: |
| 455 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE; | 479 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE; |
| 456 break; | 480 break; |
| 457 case FileChooserParams::UploadFolder: | 481 case FileChooserParams::UploadFolder: |
| 458 dialog_type_ = ui::SelectFileDialog::SELECT_UPLOAD_FOLDER; | 482 dialog_type_ = ui::SelectFileDialog::SELECT_UPLOAD_FOLDER; |
| 459 break; | 483 break; |
| 460 case FileChooserParams::Save: | 484 case FileChooserParams::Save: |
| 461 dialog_type_ = ui::SelectFileDialog::SELECT_SAVEAS_FILE; | 485 dialog_type_ = ui::SelectFileDialog::SELECT_SAVEAS_FILE; |
| 462 break; | 486 break; |
| 463 default: | 487 default: |
| 464 // Prevent warning. | 488 // Prevent warning. |
| 465 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; | 489 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE; |
| 466 NOTREACHED(); | 490 NOTREACHED(); |
| 467 } | 491 } |
| 468 | 492 |
| 469 base::FilePath default_file_name = params.default_file_name.IsAbsolute() ? | |
| 470 params.default_file_name : | |
| 471 profile_->last_selected_directory().Append(params.default_file_name); | |
| 472 | |
| 473 gfx::NativeWindow owning_window = platform_util::GetTopLevel( | 493 gfx::NativeWindow owning_window = platform_util::GetTopLevel( |
| 474 render_view_host_->GetWidget()->GetView()->GetNativeView()); | 494 render_view_host_->GetWidget()->GetView()->GetNativeView()); |
| 475 | 495 |
| 476 #if defined(OS_ANDROID) | 496 #if defined(OS_ANDROID) |
| 477 // Android needs the original MIME types and an additional capture value. | 497 // Android needs the original MIME types and an additional capture value. |
| 478 std::pair<std::vector<base::string16>, bool> accept_types = | 498 std::pair<std::vector<base::string16>, bool> accept_types = |
| 479 std::make_pair(params.accept_types, params.capture); | 499 std::make_pair(params.accept_types, params.capture); |
| 480 #endif | 500 #endif |
| 481 | 501 |
| 482 select_file_dialog_->SelectFile( | 502 select_file_dialog_->SelectFile( |
| 483 dialog_type_, | 503 dialog_type_, params.title, default_file_path, select_file_types_.get(), |
| 484 params.title, | |
| 485 default_file_name, | |
| 486 select_file_types_.get(), | |
| 487 select_file_types_.get() && !select_file_types_->extensions.empty() | 504 select_file_types_.get() && !select_file_types_->extensions.empty() |
| 488 ? 1 | 505 ? 1 |
| 489 : 0, // 1-based index of default extension to show. | 506 : 0, // 1-based index of default extension to show. |
| 490 base::FilePath::StringType(), | 507 base::FilePath::StringType(), |
| 491 owning_window, | 508 owning_window, |
| 492 #if defined(OS_ANDROID) | 509 #if defined(OS_ANDROID) |
| 493 &accept_types); | 510 &accept_types); |
| 494 #else | 511 #else |
| 495 NULL); | 512 NULL); |
| 496 #endif | 513 #endif |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 // of an extension or a "/" in the case of a MIME type). | 582 // of an extension or a "/" in the case of a MIME type). |
| 566 std::string unused; | 583 std::string unused; |
| 567 if (accept_type.length() <= 1 || | 584 if (accept_type.length() <= 1 || |
| 568 base::ToLowerASCII(accept_type) != accept_type || | 585 base::ToLowerASCII(accept_type) != accept_type || |
| 569 base::TrimWhitespaceASCII(accept_type, base::TRIM_ALL, &unused) != | 586 base::TrimWhitespaceASCII(accept_type, base::TRIM_ALL, &unused) != |
| 570 base::TRIM_NONE) { | 587 base::TRIM_NONE) { |
| 571 return false; | 588 return false; |
| 572 } | 589 } |
| 573 return true; | 590 return true; |
| 574 } | 591 } |
| 592 |
| 593 // static |
| 594 base::FilePath FileSelectHelper::GetSanitizedFileName( |
| 595 const base::FilePath& suggested_filename) { |
| 596 if (suggested_filename.empty()) |
| 597 return base::FilePath(); |
| 598 return net::GenerateFileName( |
| 599 GURL(), std::string(), std::string(), suggested_filename.AsUTF8Unsafe(), |
| 600 std::string(), l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
| 601 } |
| OLD | NEW |