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
3 // found in the LICENSE file.
5 #include "ui/shell_dialogs/select_file_dialog.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "build/build_config.h"
11 #include "ui/shell_dialogs/select_file_dialog_factory.h"
12 #include "ui/shell_dialogs/select_file_policy.h"
13 #include "ui/shell_dialogs/selected_file_info.h"
14 #include "ui/shell_dialogs/shell_dialogs_delegate.h"
17 #include "ui/shell_dialogs/select_file_dialog_win.h"
18 #elif defined(OS_MACOSX)
19 #include "ui/shell_dialogs/select_file_dialog_mac.h"
20 #elif defined(OS_ANDROID)
21 #include "ui/shell_dialogs/select_file_dialog_android.h"
22 #elif defined(USE_AURA) && defined(OS_LINUX) && !defined(OS_CHROMEOS)
23 #include "ui/shell_dialogs/linux_shell_dialog.h"
28 // Optional dialog factory. Leaked.
29 ui::SelectFileDialogFactory
* dialog_factory_
= NULL
;
31 // The global shell dialogs delegate.
32 ui::ShellDialogsDelegate
* g_shell_dialogs_delegate_
= NULL
;
38 SelectFileDialog::FileTypeInfo::FileTypeInfo()
39 : include_all_files(false),
40 support_drive(false) {}
42 SelectFileDialog::FileTypeInfo::~FileTypeInfo() {}
44 void SelectFileDialog::Listener::FileSelectedWithExtraInfo(
45 const ui::SelectedFileInfo
& file
,
48 // Most of the dialogs need actual local path, so default to it.
49 // If local path is empty, use file_path instead.
50 FileSelected(file
.local_path
.empty() ? file
.file_path
: file
.local_path
,
55 void SelectFileDialog::Listener::MultiFilesSelectedWithExtraInfo(
56 const std::vector
<ui::SelectedFileInfo
>& files
,
58 std::vector
<base::FilePath
> file_paths
;
59 for (size_t i
= 0; i
< files
.size(); ++i
)
60 file_paths
.push_back(files
[i
].local_path
.empty() ? files
[i
].file_path
61 : files
[i
].local_path
);
63 MultiFilesSelected(file_paths
, params
);
67 void SelectFileDialog::SetFactory(ui::SelectFileDialogFactory
* factory
) {
68 delete dialog_factory_
;
69 dialog_factory_
= factory
;
73 scoped_refptr
<SelectFileDialog
> SelectFileDialog::Create(
75 ui::SelectFilePolicy
* policy
) {
76 if (dialog_factory_
) {
77 SelectFileDialog
* dialog
= dialog_factory_
->Create(listener
, policy
);
82 #if defined(USE_AURA) && defined(OS_LINUX) && !defined(OS_CHROMEOS)
83 const ui::LinuxShellDialog
* shell_dialogs
= ui::LinuxShellDialog::instance();
85 return shell_dialogs
->CreateSelectFileDialog(listener
, policy
);
90 // Fix this for Chrome ASH on Windows.
91 return CreateDefaultWinSelectFileDialog(listener
, policy
);
92 #elif defined(OS_MACOSX) && !defined(USE_AURA)
93 return CreateMacSelectFileDialog(listener
, policy
);
94 #elif defined(OS_ANDROID)
95 return CreateAndroidSelectFileDialog(listener
, policy
);
102 void SelectFileDialog::SelectFile(
104 const base::string16
& title
,
105 const base::FilePath
& default_path
,
106 const FileTypeInfo
* file_types
,
108 const base::FilePath::StringType
& default_extension
,
109 gfx::NativeWindow owning_window
,
113 if (select_file_policy_
.get() &&
114 !select_file_policy_
->CanOpenSelectFileDialog()) {
115 select_file_policy_
->SelectFileDenied();
117 // Inform the listener that no file was selected.
118 // Post a task rather than calling FileSelectionCanceled directly to ensure
119 // that the listener is called asynchronously.
120 base::MessageLoop::current()->PostTask(
122 base::Bind(&SelectFileDialog::CancelFileSelection
, this, params
));
126 // Call the platform specific implementation of the file selection dialog.
127 SelectFileImpl(type
, title
, default_path
, file_types
, file_type_index
,
128 default_extension
, owning_window
, params
);
131 bool SelectFileDialog::HasMultipleFileTypeChoices() {
132 return HasMultipleFileTypeChoicesImpl();
136 void SelectFileDialog::SetShellDialogsDelegate(ShellDialogsDelegate
* delegate
) {
137 g_shell_dialogs_delegate_
= delegate
;
140 SelectFileDialog::SelectFileDialog(Listener
* listener
,
141 ui::SelectFilePolicy
* policy
)
142 : listener_(listener
),
143 select_file_policy_(policy
) {
147 SelectFileDialog::~SelectFileDialog() {}
149 void SelectFileDialog::CancelFileSelection(void* params
) {
151 listener_
->FileSelectionCanceled(params
);
154 ShellDialogsDelegate
* SelectFileDialog::GetShellDialogsDelegate() {
155 return g_shell_dialogs_delegate_
;