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/base/l10n/l10n_util.h"
12 #include "ui/shell_dialogs/select_file_dialog_factory.h"
13 #include "ui/shell_dialogs/select_file_policy.h"
14 #include "ui/shell_dialogs/selected_file_info.h"
15 #include "ui/shell_dialogs/shell_dialogs_delegate.h"
18 #include "ui/shell_dialogs/select_file_dialog_win.h"
19 #elif defined(OS_MACOSX)
20 #include "ui/shell_dialogs/select_file_dialog_mac.h"
21 #elif defined(OS_ANDROID)
22 #include "ui/shell_dialogs/select_file_dialog_android.h"
23 #elif defined(USE_AURA) && defined(OS_LINUX) && !defined(OS_CHROMEOS)
24 #include "ui/shell_dialogs/linux_shell_dialog.h"
29 // Optional dialog factory. Leaked.
30 ui::SelectFileDialogFactory
* dialog_factory_
= NULL
;
32 // The global shell dialogs delegate.
33 ui::ShellDialogsDelegate
* g_shell_dialogs_delegate_
= NULL
;
39 SelectFileDialog::FileTypeInfo::FileTypeInfo()
40 : include_all_files(false),
41 support_drive(false) {}
43 SelectFileDialog::FileTypeInfo::~FileTypeInfo() {}
45 void SelectFileDialog::Listener::FileSelectedWithExtraInfo(
46 const ui::SelectedFileInfo
& file
,
49 // Most of the dialogs need actual local path, so default to it.
50 FileSelected(file
.local_path
, index
, params
);
53 void SelectFileDialog::Listener::MultiFilesSelectedWithExtraInfo(
54 const std::vector
<ui::SelectedFileInfo
>& files
,
56 std::vector
<base::FilePath
> file_paths
;
57 for (size_t i
= 0; i
< files
.size(); ++i
)
58 file_paths
.push_back(files
[i
].local_path
);
60 MultiFilesSelected(file_paths
, params
);
64 void SelectFileDialog::SetFactory(ui::SelectFileDialogFactory
* factory
) {
65 delete dialog_factory_
;
66 dialog_factory_
= factory
;
70 scoped_refptr
<SelectFileDialog
> SelectFileDialog::Create(
72 ui::SelectFilePolicy
* policy
) {
73 if (dialog_factory_
) {
74 SelectFileDialog
* dialog
= dialog_factory_
->Create(listener
, policy
);
79 #if defined(USE_AURA) && defined(OS_LINUX) && !defined(OS_CHROMEOS)
80 const ui::LinuxShellDialog
* shell_dialogs
= ui::LinuxShellDialog::instance();
82 return shell_dialogs
->CreateSelectFileDialog(listener
, policy
);
87 // Fix this for Chrome ASH on Windows.
88 return CreateWinSelectFileDialog(listener
, policy
);
89 #elif defined(OS_MACOSX) && !defined(USE_AURA)
90 return CreateMacSelectFileDialog(listener
, policy
);
91 #elif defined(OS_ANDROID)
92 return CreateAndroidSelectFileDialog(listener
, policy
);
99 void SelectFileDialog::SelectFile(
101 const base::string16
& title
,
102 const base::FilePath
& default_path
,
103 const FileTypeInfo
* file_types
,
105 const base::FilePath::StringType
& default_extension
,
106 gfx::NativeWindow owning_window
,
110 if (select_file_policy_
.get() &&
111 !select_file_policy_
->CanOpenSelectFileDialog()) {
112 select_file_policy_
->SelectFileDenied();
114 // Inform the listener that no file was selected.
115 // Post a task rather than calling FileSelectionCanceled directly to ensure
116 // that the listener is called asynchronously.
117 base::MessageLoop::current()->PostTask(
119 base::Bind(&SelectFileDialog::CancelFileSelection
, this, params
));
123 // Call the platform specific implementation of the file selection dialog.
124 SelectFileImpl(type
, title
, default_path
, file_types
, file_type_index
,
125 default_extension
, owning_window
, params
);
128 bool SelectFileDialog::HasMultipleFileTypeChoices() {
129 return HasMultipleFileTypeChoicesImpl();
133 void SelectFileDialog::SetShellDialogsDelegate(ShellDialogsDelegate
* delegate
) {
134 g_shell_dialogs_delegate_
= delegate
;
137 SelectFileDialog::SelectFileDialog(Listener
* listener
,
138 ui::SelectFilePolicy
* policy
)
139 : listener_(listener
),
140 select_file_policy_(policy
) {
144 SelectFileDialog::~SelectFileDialog() {}
146 void SelectFileDialog::CancelFileSelection(void* params
) {
148 listener_
->FileSelectionCanceled(params
);
151 ShellDialogsDelegate
* SelectFileDialog::GetShellDialogsDelegate() {
152 return g_shell_dialogs_delegate_
;