Fixing an off by one error in screenshot manager code.
[chromium-blink-merge.git] / ui / shell_dialogs / select_file_dialog.cc
blob7897bcaa88cafa9b6b0c0243727aebee0d0bdf8e
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/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"
17 #if defined(OS_WIN)
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(TOOLKIT_GTK)
22 #include "ui/shell_dialogs/gtk/select_file_dialog_impl.h"
23 #elif defined(OS_ANDROID)
24 #include "ui/shell_dialogs/select_file_dialog_android.h"
25 #elif defined(USE_AURA) && !defined(USE_ASH) && defined(OS_LINUX)
26 #include "ui/shell_dialogs/linux_shell_dialog.h"
27 #endif
29 namespace {
31 // Optional dialog factory. Leaked.
32 ui::SelectFileDialogFactory* dialog_factory_ = NULL;
34 // The global shell dialogs delegate.
35 ui::ShellDialogsDelegate* g_shell_dialogs_delegate_ = NULL;
37 } // namespace
39 namespace ui {
41 SelectFileDialog::FileTypeInfo::FileTypeInfo()
42 : include_all_files(false),
43 support_drive(false) {}
45 SelectFileDialog::FileTypeInfo::~FileTypeInfo() {}
47 void SelectFileDialog::Listener::FileSelectedWithExtraInfo(
48 const ui::SelectedFileInfo& file,
49 int index,
50 void* params) {
51 // Most of the dialogs need actual local path, so default to it.
52 FileSelected(file.local_path, index, 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);
62 MultiFilesSelected(file_paths, params);
65 // static
66 void SelectFileDialog::SetFactory(ui::SelectFileDialogFactory* factory) {
67 delete dialog_factory_;
68 dialog_factory_ = factory;
71 // static
72 scoped_refptr<SelectFileDialog> SelectFileDialog::Create(
73 Listener* listener,
74 ui::SelectFilePolicy* policy) {
75 if (dialog_factory_) {
76 SelectFileDialog* dialog = dialog_factory_->Create(listener, policy);
77 if (dialog)
78 return dialog;
81 #if defined(USE_AURA) && !defined(USE_ASH) && defined(OS_LINUX)
82 const ui::LinuxShellDialog* shell_dialogs = ui::LinuxShellDialog::instance();
83 if (shell_dialogs)
84 return shell_dialogs->CreateSelectFileDialog(listener, policy);
85 #endif
87 #if defined(OS_WIN)
88 // TODO(ananta)
89 // Fix this for Chrome ASH on Windows.
90 return CreateWinSelectFileDialog(listener, policy);
91 #elif defined(OS_MACOSX) && !defined(USE_AURA)
92 return CreateMacSelectFileDialog(listener, policy);
93 #elif defined(TOOLKIT_GTK)
94 return CreateLinuxSelectFileDialog(listener, policy);
95 #elif defined(OS_ANDROID)
96 return CreateAndroidSelectFileDialog(listener, policy);
97 #else
98 NOTIMPLEMENTED();
99 return NULL;
100 #endif
103 void SelectFileDialog::SelectFile(
104 Type type,
105 const base::string16& title,
106 const base::FilePath& default_path,
107 const FileTypeInfo* file_types,
108 int file_type_index,
109 const base::FilePath::StringType& default_extension,
110 gfx::NativeWindow owning_window,
111 void* params) {
112 DCHECK(listener_);
114 if (select_file_policy_.get() &&
115 !select_file_policy_->CanOpenSelectFileDialog()) {
116 select_file_policy_->SelectFileDenied();
118 // Inform the listener that no file was selected.
119 // Post a task rather than calling FileSelectionCanceled directly to ensure
120 // that the listener is called asynchronously.
121 base::MessageLoop::current()->PostTask(
122 FROM_HERE,
123 base::Bind(&SelectFileDialog::CancelFileSelection, this, params));
124 return;
127 // Call the platform specific implementation of the file selection dialog.
128 SelectFileImpl(type, title, default_path, file_types, file_type_index,
129 default_extension, owning_window, params);
132 bool SelectFileDialog::HasMultipleFileTypeChoices() {
133 return HasMultipleFileTypeChoicesImpl();
136 // static
137 void SelectFileDialog::SetShellDialogsDelegate(ShellDialogsDelegate* delegate) {
138 g_shell_dialogs_delegate_ = delegate;
141 SelectFileDialog::SelectFileDialog(Listener* listener,
142 ui::SelectFilePolicy* policy)
143 : listener_(listener),
144 select_file_policy_(policy) {
145 DCHECK(listener_);
148 SelectFileDialog::~SelectFileDialog() {}
150 void SelectFileDialog::CancelFileSelection(void* params) {
151 if (listener_)
152 listener_->FileSelectionCanceled(params);
155 ShellDialogsDelegate* SelectFileDialog::GetShellDialogsDelegate() {
156 return g_shell_dialogs_delegate_;
159 } // namespace ui