• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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.
4 
5 #include "chrome/browser/ui/shell_dialogs.h"
6 
7 #include "base/logging.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/tab_contents/simple_alert_infobar_delegate.h"
11 #include "chrome/common/pref_names.h"
12 #include "content/browser/tab_contents/tab_contents.h"
13 #include "grit/generated_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
15 
FileTypeInfo()16 SelectFileDialog::FileTypeInfo::FileTypeInfo() : include_all_files(false) {}
17 
~FileTypeInfo()18 SelectFileDialog::FileTypeInfo::~FileTypeInfo() {}
19 
SelectFileDialog(Listener * listener)20 SelectFileDialog::SelectFileDialog(Listener* listener)
21     : listener_(listener) {
22   DCHECK(listener_);
23 }
24 
~SelectFileDialog()25 SelectFileDialog::~SelectFileDialog() {}
26 
CanOpenSelectFileDialog()27 bool SelectFileDialog::CanOpenSelectFileDialog() {
28   DCHECK(g_browser_process);
29 
30   // local_state() can return NULL for tests.
31   if (!g_browser_process->local_state())
32     return false;
33 
34   return !g_browser_process->local_state()->FindPreference(
35              prefs::kAllowFileSelectionDialogs) ||
36          g_browser_process->local_state()->GetBoolean(
37              prefs::kAllowFileSelectionDialogs);
38 }
39 
SelectFile(Type type,const string16 & title,const FilePath & default_path,const FileTypeInfo * file_types,int file_type_index,const FilePath::StringType & default_extension,TabContents * source_contents,gfx::NativeWindow owning_window,void * params)40 void SelectFileDialog::SelectFile(Type type,
41                                   const string16& title,
42                                   const FilePath& default_path,
43                                   const FileTypeInfo* file_types,
44                                   int file_type_index,
45                                   const FilePath::StringType& default_extension,
46                                   TabContents* source_contents,
47                                   gfx::NativeWindow owning_window,
48                                   void* params) {
49   DCHECK(listener_);
50 
51   if (!CanOpenSelectFileDialog()) {
52     // Show the InfoBar saying that file-selection dialogs are disabled.
53     if (source_contents) {
54       source_contents->AddInfoBar(new SimpleAlertInfoBarDelegate(
55           source_contents,
56           NULL,
57           l10n_util::GetStringUTF16(IDS_FILE_SELECTION_DIALOG_INFOBAR),
58           true));
59     } else {
60       LOG(WARNING) << "File-selection dialogs are disabled but no TabContents "
61                    << "is given to display the InfoBar.";
62     }
63 
64     // Inform the listener that no file was selected.
65     // Post a task rather than calling FileSelectionCanceled directly to ensure
66     // that the listener is called asynchronously.
67     MessageLoop::current()->PostTask(FROM_HERE,
68                                      NewRunnableMethod(
69                                          this,
70                                          &SelectFileDialog::CancelFileSelection,
71                                          params));
72     return;
73   }
74   // Call the platform specific implementation of the file selection dialog.
75   SelectFileImpl(type, title, default_path, file_types, file_type_index,
76                  default_extension, owning_window, params);
77 }
78 
CancelFileSelection(void * params)79 void SelectFileDialog::CancelFileSelection(void* params) {
80   if (listener_)
81     listener_->FileSelectionCanceled(params);
82 }
83