Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chrome/browser/ui/select_file_dialog.cc

Issue 10667026: Start consolidating cross-port file selection code into ui/base/dialogs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "chrome/browser/infobars/infobar_tab_helper.h"
12 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/tab_contents/simple_alert_infobar_delegate.h"
14 #include "chrome/browser/ui/tab_contents/tab_contents.h"
15 #include "chrome/common/pref_names.h"
16 #include "content/public/common/selected_file_info.h" 10 #include "content/public/common/selected_file_info.h"
17 #include "grit/generated_resources.h" 11 #include "ui/base/dialogs/select_file_policy.h"
18 #include "ui/base/l10n/l10n_util.h"
19 12
20 using content::WebContents; 13 using content::WebContents;
21 14
15 ui::SelectFilePolicy* SelectFileDialog::select_file_policy_ = NULL;
16
17 // static
18 void SelectFileDialog::SetFilePolicy(ui::SelectFilePolicy* policy) {
sky 2012/06/26 00:07:18 How come this is static and not per instance?
19 delete select_file_policy_;
20 select_file_policy_ = policy;
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 content::SelectedFileInfo& file, 28 const content::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<content::SelectedFileInfo>& files, 35 const std::vector<content::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 : listener_(listener) {
45 DCHECK(listener_); 46 DCHECK(listener_);
46 } 47 }
47 48
48 SelectFileDialog::~SelectFileDialog() {} 49 SelectFileDialog::~SelectFileDialog() {}
49 50
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, 51 void SelectFileDialog::SelectFile(Type type,
64 const string16& title, 52 const string16& title,
65 const FilePath& default_path, 53 const FilePath& default_path,
66 const FileTypeInfo* file_types, 54 const FileTypeInfo* file_types,
67 int file_type_index, 55 int file_type_index,
68 const FilePath::StringType& default_extension, 56 const FilePath::StringType& default_extension,
69 WebContents* source_contents, 57 const base::Closure& policy_denied,
70 gfx::NativeWindow owning_window, 58 gfx::NativeWindow owning_window,
71 void* params) { 59 void* params) {
72 DCHECK(listener_); 60 DCHECK(listener_);
73 61
74 if (!CanOpenSelectFileDialog()) { 62 if (select_file_policy_ && !select_file_policy_->CanOpenSelectFileDialog()) {
75 // Show the InfoBar saying that file-selection dialogs are disabled. 63 policy_denied.Run();
76 if (source_contents) {
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 64
89 // Inform the listener that no file was selected. 65 // Inform the listener that no file was selected.
90 // Post a task rather than calling FileSelectionCanceled directly to ensure 66 // Post a task rather than calling FileSelectionCanceled directly to ensure
91 // that the listener is called asynchronously. 67 // that the listener is called asynchronously.
92 MessageLoop::current()->PostTask( 68 MessageLoop::current()->PostTask(
93 FROM_HERE, base::Bind(&SelectFileDialog::CancelFileSelection, this, 69 FROM_HERE, base::Bind(&SelectFileDialog::CancelFileSelection, this,
94 params)); 70 params));
95 return; 71 return;
96 } 72 }
73
97 // Call the platform specific implementation of the file selection dialog. 74 // Call the platform specific implementation of the file selection dialog.
98 SelectFileImpl(type, title, default_path, file_types, file_type_index, 75 SelectFileImpl(type, title, default_path, file_types, file_type_index,
99 default_extension, owning_window, params); 76 default_extension, owning_window, params);
100 } 77 }
101 78
102 bool SelectFileDialog::HasMultipleFileTypeChoices() { 79 bool SelectFileDialog::HasMultipleFileTypeChoices() {
103 return HasMultipleFileTypeChoicesImpl(); 80 return HasMultipleFileTypeChoicesImpl();
104 } 81 }
105 82
106 void SelectFileDialog::CancelFileSelection(void* params) { 83 void SelectFileDialog::CancelFileSelection(void* params) {
107 if (listener_) 84 if (listener_)
108 listener_->FileSelectionCanceled(params); 85 listener_->FileSelectionCanceled(params);
109 } 86 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698