Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / select_file_dialog_extension.h
blob8d70adda9e21df32bcc783b53d9954fa2b0d146d
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 #ifndef CHROME_BROWSER_UI_VIEWS_SELECT_FILE_DIALOG_EXTENSION_H_
6 #define CHROME_BROWSER_UI_VIEWS_SELECT_FILE_DIALOG_EXTENSION_H_
8 #include <vector>
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"
16 class ExtensionDialog;
17 class Profile;
19 namespace content {
20 class RenderViewHost;
21 class WebContents;
24 namespace ui {
25 struct SelectedFileInfo;
26 class SelectFilePolicy;
29 // Shows a dialog box for selecting a file or a folder, using the
30 // file manager extension implementation.
31 class SelectFileDialogExtension
32 : public ui::SelectFileDialog,
33 public ExtensionDialogObserver {
34 public:
35 // Opaque ID type for identifying the tab spawned each dialog, unique for
36 // every WebContents.
37 typedef const void* RoutingID;
38 static RoutingID GetRoutingIDFromWebContents(
39 const content::WebContents* web_contents);
41 static SelectFileDialogExtension* Create(
42 ui::SelectFileDialog::Listener* listener,
43 ui::SelectFilePolicy* policy);
45 // BaseShellDialog implementation.
46 bool IsRunning(gfx::NativeWindow owner_window) const override;
47 void ListenerDestroyed() override;
49 // ExtensionDialog::Observer implementation.
50 void ExtensionDialogClosing(ExtensionDialog* dialog) override;
51 void ExtensionTerminated(ExtensionDialog* dialog) override;
53 // Routes callback to appropriate SelectFileDialog::Listener based on the
54 // owning |web_contents|.
55 static void OnFileSelected(RoutingID routing_id,
56 const ui::SelectedFileInfo& file,
57 int index);
58 static void OnMultiFilesSelected(
59 RoutingID routing_id,
60 const std::vector<ui::SelectedFileInfo>& files);
61 static void OnFileSelectionCanceled(RoutingID routing_id);
63 // For testing, so we can inject JavaScript into the contained view.
64 content::RenderViewHost* GetRenderViewHost();
66 protected:
67 // SelectFileDialog implementation.
68 void SelectFileImpl(Type type,
69 const base::string16& title,
70 const base::FilePath& default_path,
71 const FileTypeInfo* file_types,
72 int file_type_index,
73 const base::FilePath::StringType& default_extension,
74 gfx::NativeWindow owning_window,
75 void* params) override;
77 private:
78 friend class SelectFileDialogExtensionBrowserTest;
79 friend class SelectFileDialogExtensionTest;
81 // Object is ref-counted, use Create().
82 explicit SelectFileDialogExtension(SelectFileDialog::Listener* listener,
83 ui::SelectFilePolicy* policy);
84 ~SelectFileDialogExtension() override;
86 // Invokes the appropriate file selection callback on our listener.
87 void NotifyListener();
89 // Adds this to the list of pending dialogs, used for testing.
90 void AddPending(RoutingID routing_id);
92 // Check if the list of pending dialogs contains dialog for |routing_id|.
93 static bool PendingExists(RoutingID routing_id);
95 // Returns true if the dialog has multiple file type choices.
96 bool HasMultipleFileTypeChoicesImpl() override;
98 bool has_multiple_file_type_choices_;
100 // Host for the extension that implements this dialog.
101 scoped_refptr<ExtensionDialog> extension_dialog_;
103 // ID of the tab that spawned this dialog, used to route callbacks.
104 RoutingID routing_id_;
106 // Pointer to the profile the dialog is running in.
107 Profile* profile_;
109 // The window that created the dialog.
110 gfx::NativeWindow owner_window_;
112 // We defer the callback into SelectFileDialog::Listener until the window
113 // closes, to match the semantics of file selection on Windows and Mac.
114 // These are the data passed to the listener.
115 enum SelectionType {
116 CANCEL = 0,
117 SINGLE_FILE,
118 MULTIPLE_FILES
120 SelectionType selection_type_;
121 std::vector<ui::SelectedFileInfo> selection_files_;
122 int selection_index_;
123 void* params_;
125 DISALLOW_COPY_AND_ASSIGN(SelectFileDialogExtension);
128 #endif // CHROME_BROWSER_UI_VIEWS_SELECT_FILE_DIALOG_EXTENSION_H_