Home | History | Annotate | Download | only in ui
      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 #ifndef CHROME_BROWSER_UI_SHELL_DIALOGS_H_
      6 #define CHROME_BROWSER_UI_SHELL_DIALOGS_H_
      7 #pragma once
      8 
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/file_path.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/string16.h"
     15 #include "ui/gfx/native_widget_types.h"
     16 
     17 class TabContents;
     18 
     19 // This function is declared extern such that it is accessible for unit tests
     20 // in /chrome/browser/ui/views/shell_dialogs_win_unittest.cc
     21 extern std::wstring AppendExtensionIfNeeded(const std::wstring& filename,
     22                                             const std::wstring& filter_selected,
     23                                             const std::wstring& suggested_ext);
     24 
     25 // A base class for shell dialogs.
     26 class BaseShellDialog {
     27  public:
     28   // Returns true if a shell dialog box is currently being shown modally
     29   // to the specified owner.
     30   virtual bool IsRunning(gfx::NativeWindow owning_window) const = 0;
     31 
     32   // Notifies the dialog box that the listener has been destroyed and it should
     33   // no longer be sent notifications.
     34   virtual void ListenerDestroyed() = 0;
     35 
     36  protected:
     37   virtual ~BaseShellDialog() {}
     38 };
     39 
     40 // Shows a dialog box for selecting a file or a folder.
     41 class SelectFileDialog
     42     : public base::RefCountedThreadSafe<SelectFileDialog>,
     43       public BaseShellDialog {
     44  public:
     45   enum Type {
     46     SELECT_NONE,
     47     SELECT_FOLDER,
     48     SELECT_SAVEAS_FILE,
     49     SELECT_OPEN_FILE,
     50     SELECT_OPEN_MULTI_FILE
     51   };
     52 
     53   // An interface implemented by a Listener object wishing to know about the
     54   // the result of the Select File/Folder action. These callbacks must be
     55   // re-entrant.
     56   class Listener {
     57    public:
     58     // Notifies the Listener that a file/folder selection has been made. The
     59     // file/folder path is in |path|. |params| is contextual passed to
     60     // SelectFile. |index| specifies the index of the filter passed to the
     61     // the initial call to SelectFile.
     62     virtual void FileSelected(const FilePath& path,
     63                               int index, void* params) = 0;
     64 
     65     // Notifies the Listener that many files have been selected. The
     66     // files are in |files|. |params| is contextual passed to SelectFile.
     67     virtual void MultiFilesSelected(
     68       const std::vector<FilePath>& files, void* params) {}
     69 
     70     // Notifies the Listener that the file/folder selection was aborted (via
     71     // the  user canceling or closing the selection dialog box, for example).
     72     // |params| is contextual passed to SelectFile.
     73     virtual void FileSelectionCanceled(void* params) {}
     74 
     75    protected:
     76     virtual ~Listener() {}
     77   };
     78 
     79   // Creates a dialog box helper. This object is ref-counted, but the returned
     80   // object will have no reference (refcount is 0).
     81   static SelectFileDialog* Create(Listener* listener);
     82 
     83   // Holds information about allowed extensions on a file save dialog.
     84   // |extensions| is a list of allowed extensions. For example, it might be
     85   //   { { "htm", "html" }, { "txt" } }. Only pass more than one extension
     86   //   in the inner vector if the extensions are equivalent. Do NOT include
     87   //   leading periods.
     88   // |extension_description_overrides| overrides the system descriptions of the
     89   //   specified extensions. Entries correspond to |extensions|; if left blank
     90   //   the system descriptions will be used.
     91   // |include_all_files| specifies whether there will be a filter added for all
     92   //   files (i.e. *.*).
     93   struct FileTypeInfo {
     94     FileTypeInfo();
     95     ~FileTypeInfo();
     96 
     97     std::vector<std::vector<FilePath::StringType> > extensions;
     98     std::vector<string16> extension_description_overrides;
     99     bool include_all_files;
    100   };
    101 
    102   // Selects a File.
    103   // Before doing anything this function checks if FileBrowsing is forbidden
    104   // by Policy. If so, it tries to show an InfoBar and behaves as though no File
    105   // was selected (the user clicked `Cancel` immediately).
    106   // Otherwise it will start displaying the dialog box. This will also
    107   // block the calling window until the dialog box is complete. The listener
    108   // associated with this object will be notified when the selection is
    109   // complete.
    110   // |type| is the type of file dialog to be shown, see Type enumeration above.
    111   // |title| is the title to be displayed in the dialog. If this string is
    112   //   empty, the default title is used.
    113   // |default_path| is the default path and suggested file name to be shown in
    114   //   the dialog. This only works for SELECT_SAVEAS_FILE and SELECT_OPEN_FILE.
    115   //   Can be an empty string to indicate the platform default.
    116   // |file_types| holds the infomation about the file types allowed. Pass NULL
    117   //   to get no special behavior
    118   // |file_type_index| is the 1-based index into the file type list in
    119   //   |file_types|. Specify 0 if you don't need to specify extension behavior.
    120   // |default_extension| is the default extension to add to the file if the
    121   //   user doesn't type one. This should NOT include the '.'. On Windows, if
    122   //   you specify this you must also specify |file_types|.
    123   // |source_contents| is the TabContents the call is originating from, i.e.
    124   //   where the InfoBar should be shown in case file-selection dialogs are
    125   //   forbidden by policy, or NULL if no InfoBar should be shown.
    126   // |owning_window| is the window the dialog is modal to, or NULL for a
    127   //   modeless dialog.
    128   // |params| is data from the calling context which will be passed through to
    129   //   the listener. Can be NULL.
    130   // NOTE: only one instance of any shell dialog can be shown per owning_window
    131   //       at a time (for obvious reasons).
    132   void SelectFile(Type type,
    133                   const string16& title,
    134                   const FilePath& default_path,
    135                   const FileTypeInfo* file_types,
    136                   int file_type_index,
    137                   const FilePath::StringType& default_extension,
    138                   TabContents* source_contents,
    139                   gfx::NativeWindow owning_window,
    140                   void* params);
    141 
    142   // browser_mode is true when running inside the browser.
    143   virtual void set_browser_mode(bool value) {}
    144 
    145  protected:
    146   friend class base::RefCountedThreadSafe<SelectFileDialog>;
    147   explicit SelectFileDialog(Listener* listener);
    148   virtual ~SelectFileDialog();
    149 
    150   // Displays the actual file-selection dialog.
    151   // This is overridden in the platform-specific descendants of FileSelectDialog
    152   // and gets called from SelectFile after testing the
    153   // AllowFileSelectionDialogs-Policy.
    154   virtual void SelectFileImpl(Type type,
    155                               const string16& title,
    156                               const FilePath& default_path,
    157                               const FileTypeInfo* file_types,
    158                               int file_type_index,
    159                               const FilePath::StringType& default_extension,
    160                               gfx::NativeWindow owning_window,
    161                               void* params) = 0;
    162 
    163   // The listener to be notified of selection completion.
    164   Listener* listener_;
    165 
    166  private:
    167   // Tests if the file selection dialog can be displayed by
    168   // testing if the AllowFileSelectionDialogs-Policy is
    169   // either unset or set to true.
    170   bool CanOpenSelectFileDialog();
    171 
    172   // Informs the |listener_| that the file seleciton dialog was canceled. Moved
    173   // to a function for being able to post it to the message loop.
    174   void CancelFileSelection(void* params);
    175 };
    176 
    177 #endif  // CHROME_BROWSER_UI_SHELL_DIALOGS_H_
    178