Home | History | Annotate | Download | only in views
      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.
      4 
      5 #ifndef CHROME_BROWSER_UI_VIEWS_SELECT_FILE_DIALOG_EXTENSION_H_
      6 #define CHROME_BROWSER_UI_VIEWS_SELECT_FILE_DIALOG_EXTENSION_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h"
     13 #include "ui/gfx/native_widget_types.h"  // gfx::NativeWindow
     14 #include "ui/shell_dialogs/select_file_dialog.h"
     15 
     16 class ExtensionDialog;
     17 class Profile;
     18 
     19 namespace content {
     20 class RenderViewHost;
     21 }
     22 
     23 namespace ui {
     24 struct SelectedFileInfo;
     25 class SelectFilePolicy;
     26 }
     27 
     28 // Shows a dialog box for selecting a file or a folder, using the
     29 // file manager extension implementation.
     30 class SelectFileDialogExtension
     31     : public ui::SelectFileDialog,
     32       public ExtensionDialogObserver {
     33  public:
     34   static SelectFileDialogExtension* Create(
     35       ui::SelectFileDialog::Listener* listener,
     36       ui::SelectFilePolicy* policy);
     37 
     38   // BaseShellDialog implementation.
     39   virtual bool IsRunning(gfx::NativeWindow owner_window) const OVERRIDE;
     40   virtual void ListenerDestroyed() OVERRIDE;
     41 
     42   // ExtensionDialog::Observer implementation.
     43   virtual void ExtensionDialogClosing(ExtensionDialog* dialog) OVERRIDE;
     44   virtual void ExtensionTerminated(ExtensionDialog* dialog) OVERRIDE;
     45 
     46   // Routes callback to appropriate SelectFileDialog::Listener based on
     47   // the owning |tab_id|.
     48   static void OnFileSelected(int32 tab_id,
     49                              const ui::SelectedFileInfo& file,
     50                              int index);
     51   static void OnMultiFilesSelected(
     52       int32 tab_id,
     53       const std::vector<ui::SelectedFileInfo>& files);
     54   static void OnFileSelectionCanceled(int32 tab_id);
     55 
     56   // For testing, so we can inject JavaScript into the contained view.
     57   content::RenderViewHost* GetRenderViewHost();
     58 
     59  protected:
     60   // SelectFileDialog implementation.
     61   virtual void SelectFileImpl(
     62       Type type,
     63       const string16& title,
     64       const base::FilePath& default_path,
     65       const FileTypeInfo* file_types,
     66       int file_type_index,
     67       const base::FilePath::StringType& default_extension,
     68       gfx::NativeWindow owning_window,
     69       void* params) OVERRIDE;
     70 
     71  private:
     72   friend class SelectFileDialogExtensionBrowserTest;
     73   friend class SelectFileDialogExtensionTest;
     74 
     75   // Object is ref-counted, use Create().
     76   explicit SelectFileDialogExtension(SelectFileDialog::Listener* listener,
     77                                      ui::SelectFilePolicy* policy);
     78   virtual ~SelectFileDialogExtension();
     79 
     80   // Invokes the appropriate file selection callback on our listener.
     81   void NotifyListener();
     82 
     83   // Adds this to the list of pending dialogs, used for testing.
     84   void AddPending(int32 tab_id);
     85 
     86   // Check if the list of pending dialogs contains dialog for |tab_id|.
     87   static bool PendingExists(int32 tab_id);
     88 
     89   // Returns true if the dialog has multiple file type choices.
     90   virtual bool HasMultipleFileTypeChoicesImpl() OVERRIDE;
     91 
     92   bool has_multiple_file_type_choices_;
     93 
     94   // Host for the extension that implements this dialog.
     95   scoped_refptr<ExtensionDialog> extension_dialog_;
     96 
     97   // ID of the tab that spawned this dialog, used to route callbacks.
     98   int32 tab_id_;
     99 
    100   // Pointer to the profile the dialog is running in.
    101   Profile* profile_;
    102 
    103   // The window that created the dialog.
    104   gfx::NativeWindow owner_window_;
    105 
    106   // We defer the callback into SelectFileDialog::Listener until the window
    107   // closes, to match the semantics of file selection on Windows and Mac.
    108   // These are the data passed to the listener.
    109   enum SelectionType {
    110     CANCEL = 0,
    111     SINGLE_FILE,
    112     MULTIPLE_FILES
    113   };
    114   SelectionType selection_type_;
    115   std::vector<ui::SelectedFileInfo> selection_files_;
    116   int selection_index_;
    117   void* params_;
    118 
    119   DISALLOW_COPY_AND_ASSIGN(SelectFileDialogExtension);
    120 };
    121 
    122 #endif  // CHROME_BROWSER_UI_VIEWS_SELECT_FILE_DIALOG_EXTENSION_H_
    123