| 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/ui/select_file_dialog.h" | 5 #include "chrome/browser/ui/select_file_dialog.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/infobars/infobar_tab_helper.h" | 11 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/tab_contents/simple_alert_infobar_delegate.h" | 13 #include "chrome/browser/tab_contents/simple_alert_infobar_delegate.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 14 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 17 #include "ui/base/dialogs/selected_file_info.h" | 17 #include "ui/base/dialogs/selected_file_info.h" |
| 18 #include "ui/base/dialogs/select_file_policy.h" | |
| 19 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 20 | 19 |
| 21 using content::WebContents; | 20 using content::WebContents; |
| 22 | 21 |
| 23 SelectFileDialog::FileTypeInfo::FileTypeInfo() : include_all_files(false) {} | 22 SelectFileDialog::FileTypeInfo::FileTypeInfo() : include_all_files(false) {} |
| 24 | 23 |
| 25 SelectFileDialog::FileTypeInfo::~FileTypeInfo() {} | 24 SelectFileDialog::FileTypeInfo::~FileTypeInfo() {} |
| 26 | 25 |
| 27 void SelectFileDialog::Listener::FileSelectedWithExtraInfo( | 26 void SelectFileDialog::Listener::FileSelectedWithExtraInfo( |
| 28 const ui::SelectedFileInfo& file, | 27 const ui::SelectedFileInfo& file, |
| 29 int index, | 28 int index, |
| 30 void* params) { | 29 void* params) { |
| 31 FileSelected(file.path, index, params); | 30 FileSelected(file.path, index, params); |
| 32 } | 31 } |
| 33 | 32 |
| 34 void SelectFileDialog::Listener::MultiFilesSelectedWithExtraInfo( | 33 void SelectFileDialog::Listener::MultiFilesSelectedWithExtraInfo( |
| 35 const std::vector<ui::SelectedFileInfo>& files, | 34 const std::vector<ui::SelectedFileInfo>& files, |
| 36 void* params) { | 35 void* params) { |
| 37 std::vector<FilePath> file_paths; | 36 std::vector<FilePath> file_paths; |
| 38 for (size_t i = 0; i < files.size(); ++i) | 37 for (size_t i = 0; i < files.size(); ++i) |
| 39 file_paths.push_back(files[i].path); | 38 file_paths.push_back(files[i].path); |
| 40 | 39 |
| 41 MultiFilesSelected(file_paths, params); | 40 MultiFilesSelected(file_paths, params); |
| 42 } | 41 } |
| 43 | 42 |
| 44 SelectFileDialog::SelectFileDialog(Listener* listener, | 43 SelectFileDialog::SelectFileDialog(Listener* listener) |
| 45 ui::SelectFilePolicy* policy) | 44 : listener_(listener) { |
| 46 : listener_(listener), | |
| 47 select_file_policy_(policy) { | |
| 48 DCHECK(listener_); | 45 DCHECK(listener_); |
| 49 } | 46 } |
| 50 | 47 |
| 51 SelectFileDialog::~SelectFileDialog() {} | 48 SelectFileDialog::~SelectFileDialog() {} |
| 52 | 49 |
| 50 bool SelectFileDialog::CanOpenSelectFileDialog() { |
| 51 DCHECK(g_browser_process); |
| 52 |
| 53 // local_state() can return NULL for tests. |
| 54 if (!g_browser_process->local_state()) |
| 55 return false; |
| 56 |
| 57 return !g_browser_process->local_state()->FindPreference( |
| 58 prefs::kAllowFileSelectionDialogs) || |
| 59 g_browser_process->local_state()->GetBoolean( |
| 60 prefs::kAllowFileSelectionDialogs); |
| 61 } |
| 62 |
| 53 void SelectFileDialog::SelectFile(Type type, | 63 void SelectFileDialog::SelectFile(Type type, |
| 54 const string16& title, | 64 const string16& title, |
| 55 const FilePath& default_path, | 65 const FilePath& default_path, |
| 56 const FileTypeInfo* file_types, | 66 const FileTypeInfo* file_types, |
| 57 int file_type_index, | 67 int file_type_index, |
| 58 const FilePath::StringType& default_extension, | 68 const FilePath::StringType& default_extension, |
| 69 WebContents* source_contents, |
| 59 gfx::NativeWindow owning_window, | 70 gfx::NativeWindow owning_window, |
| 60 void* params) { | 71 void* params) { |
| 61 DCHECK(listener_); | 72 DCHECK(listener_); |
| 62 | 73 |
| 63 if (select_file_policy_.get() && | 74 if (!CanOpenSelectFileDialog()) { |
| 64 !select_file_policy_->CanOpenSelectFileDialog()) { | 75 // Show the InfoBar saying that file-selection dialogs are disabled. |
| 65 select_file_policy_->SelectFileDenied(); | 76 if (source_contents) { |
| 77 TabContents* tab_contents = TabContents::FromWebContents(source_contents); |
| 78 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper(); |
| 79 infobar_helper->AddInfoBar(new SimpleAlertInfoBarDelegate( |
| 80 infobar_helper, |
| 81 NULL, |
| 82 l10n_util::GetStringUTF16(IDS_FILE_SELECTION_DIALOG_INFOBAR), |
| 83 true)); |
| 84 } else { |
| 85 LOG(WARNING) << "File-selection dialogs are disabled but no WebContents " |
| 86 << "is given to display the InfoBar."; |
| 87 } |
| 66 | 88 |
| 67 // Inform the listener that no file was selected. | 89 // Inform the listener that no file was selected. |
| 68 // Post a task rather than calling FileSelectionCanceled directly to ensure | 90 // Post a task rather than calling FileSelectionCanceled directly to ensure |
| 69 // that the listener is called asynchronously. | 91 // that the listener is called asynchronously. |
| 70 MessageLoop::current()->PostTask( | 92 MessageLoop::current()->PostTask( |
| 71 FROM_HERE, base::Bind(&SelectFileDialog::CancelFileSelection, this, | 93 FROM_HERE, base::Bind(&SelectFileDialog::CancelFileSelection, this, |
| 72 params)); | 94 params)); |
| 73 return; | 95 return; |
| 74 } | 96 } |
| 75 | |
| 76 // Call the platform specific implementation of the file selection dialog. | 97 // Call the platform specific implementation of the file selection dialog. |
| 77 SelectFileImpl(type, title, default_path, file_types, file_type_index, | 98 SelectFileImpl(type, title, default_path, file_types, file_type_index, |
| 78 default_extension, owning_window, params); | 99 default_extension, owning_window, params); |
| 79 } | 100 } |
| 80 | 101 |
| 81 bool SelectFileDialog::HasMultipleFileTypeChoices() { | 102 bool SelectFileDialog::HasMultipleFileTypeChoices() { |
| 82 return HasMultipleFileTypeChoicesImpl(); | 103 return HasMultipleFileTypeChoicesImpl(); |
| 83 } | 104 } |
| 84 | 105 |
| 85 void SelectFileDialog::CancelFileSelection(void* params) { | 106 void SelectFileDialog::CancelFileSelection(void* params) { |
| 86 if (listener_) | 107 if (listener_) |
| 87 listener_->FileSelectionCanceled(params); | 108 listener_->FileSelectionCanceled(params); |
| 88 } | 109 } |
| OLD | NEW |