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