Fix WindowAndroid leak in Android WebView
[chromium-blink-merge.git] / ui / shell_dialogs / select_file_dialog.cc
blobd26fc36d2aa41a64c58aa59276ab524aabd446ae
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"
7 #include "base/bind.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"
16 #if defined(OS_WIN)
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"
24 #endif
26 namespace {
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;
34 } // namespace
36 namespace ui {
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,
46 int index,
47 void* params) {
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,
51 index,
52 params);
55 void SelectFileDialog::Listener::MultiFilesSelectedWithExtraInfo(
56 const std::vector<ui::SelectedFileInfo>& files,
57 void* params) {
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);
66 // static
67 void SelectFileDialog::SetFactory(ui::SelectFileDialogFactory* factory) {
68 delete dialog_factory_;
69 dialog_factory_ = factory;
72 // static
73 scoped_refptr<SelectFileDialog> SelectFileDialog::Create(
74 Listener* listener,
75 ui::SelectFilePolicy* policy) {
76 if (dialog_factory_) {
77 SelectFileDialog* dialog = dialog_factory_->Create(listener, policy);
78 if (dialog)
79 return dialog;
82 #if defined(USE_AURA) && defined(OS_LINUX) && !defined(OS_CHROMEOS)
83 const ui::LinuxShellDialog* shell_dialogs = ui::LinuxShellDialog::instance();
84 if (shell_dialogs)
85 return shell_dialogs->CreateSelectFileDialog(listener, policy);
86 #endif
88 #if defined(OS_WIN)
89 // TODO(ananta)
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);
96 #else
97 NOTIMPLEMENTED();
98 return NULL;
99 #endif
102 void SelectFileDialog::SelectFile(
103 Type type,
104 const base::string16& title,
105 const base::FilePath& default_path,
106 const FileTypeInfo* file_types,
107 int file_type_index,
108 const base::FilePath::StringType& default_extension,
109 gfx::NativeWindow owning_window,
110 void* params) {
111 DCHECK(listener_);
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(
121 FROM_HERE,
122 base::Bind(&SelectFileDialog::CancelFileSelection, this, params));
123 return;
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();
135 // static
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) {
144 DCHECK(listener_);
147 SelectFileDialog::~SelectFileDialog() {}
149 void SelectFileDialog::CancelFileSelection(void* params) {
150 if (listener_)
151 listener_->FileSelectionCanceled(params);
154 ShellDialogsDelegate* SelectFileDialog::GetShellDialogsDelegate() {
155 return g_shell_dialogs_delegate_;
158 } // namespace ui