OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_FILE_SELECT_HELPER_H_ | 5 #ifndef CHROME_BROWSER_FILE_SELECT_HELPER_H_ |
6 #define CHROME_BROWSER_FILE_SELECT_HELPER_H_ | 6 #define CHROME_BROWSER_FILE_SELECT_HELPER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
| 9 #include <map> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
12 #include "chrome/browser/ui/shell_dialogs.h" | 13 #include "chrome/browser/ui/shell_dialogs.h" |
13 #include "content/browser/tab_contents/tab_contents_observer.h" | 14 #include "content/browser/tab_contents/tab_contents_observer.h" |
14 #include "content/common/notification_observer.h" | 15 #include "content/common/notification_observer.h" |
15 #include "content/common/notification_registrar.h" | 16 #include "content/common/notification_registrar.h" |
16 #include "net/base/directory_lister.h" | 17 #include "net/base/directory_lister.h" |
17 | 18 |
18 class Profile; | 19 class Profile; |
19 class RenderViewHost; | 20 class RenderViewHost; |
20 struct ViewHostMsg_RunFileChooser_Params; | 21 struct ViewHostMsg_RunFileChooser_Params; |
21 | 22 |
22 class FileSelectHelper | 23 class FileSelectHelper |
23 : public SelectFileDialog::Listener, | 24 : public SelectFileDialog::Listener, |
24 public net::DirectoryLister::DirectoryListerDelegate, | |
25 public NotificationObserver { | 25 public NotificationObserver { |
26 public: | 26 public: |
27 explicit FileSelectHelper(Profile* profile); | 27 explicit FileSelectHelper(Profile* profile); |
28 ~FileSelectHelper(); | 28 ~FileSelectHelper(); |
29 | 29 |
30 // Show the file chooser dialog. | 30 // Show the file chooser dialog. |
31 void RunFileChooser(RenderViewHost* render_view_host, | 31 void RunFileChooser(RenderViewHost* render_view_host, |
32 const ViewHostMsg_RunFileChooser_Params& params); | 32 const ViewHostMsg_RunFileChooser_Params& params); |
33 | 33 |
| 34 // Enumerates all the files in directory. |
| 35 void EnumerateDirectory(int request_id, |
| 36 RenderViewHost* render_view_host, |
| 37 const FilePath& path); |
| 38 |
34 private: | 39 private: |
| 40 // Utility class which can listen for directory lister events and relay |
| 41 // them to the main object with the correct tracking id. |
| 42 class DirectoryListerDispatchDelegate |
| 43 : public net::DirectoryLister::DirectoryListerDelegate { |
| 44 public: |
| 45 DirectoryListerDispatchDelegate(FileSelectHelper* parent, int id) |
| 46 : parent_(parent), |
| 47 id_(id) {} |
| 48 ~DirectoryListerDispatchDelegate() {} |
| 49 virtual void OnListFile( |
| 50 const net::DirectoryLister::DirectoryListerData& data) { |
| 51 parent_->OnListFile(id_, data); |
| 52 } |
| 53 virtual void OnListDone(int error) { |
| 54 parent_->OnListDone(id_, error); |
| 55 } |
| 56 private: |
| 57 // This FileSelectHelper owns this object. |
| 58 FileSelectHelper* parent_; |
| 59 int id_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(DirectoryListerDispatchDelegate); |
| 62 }; |
| 63 |
35 // SelectFileDialog::Listener overrides. | 64 // SelectFileDialog::Listener overrides. |
36 virtual void FileSelected( | 65 virtual void FileSelected( |
37 const FilePath& path, int index, void* params) OVERRIDE; | 66 const FilePath& path, int index, void* params) OVERRIDE; |
38 virtual void MultiFilesSelected(const std::vector<FilePath>& files, | 67 virtual void MultiFilesSelected(const std::vector<FilePath>& files, |
39 void* params) OVERRIDE; | 68 void* params) OVERRIDE; |
40 virtual void FileSelectionCanceled(void* params) OVERRIDE; | 69 virtual void FileSelectionCanceled(void* params) OVERRIDE; |
41 | 70 |
42 // net::DirectoryLister::DirectoryListerDelegate overrides. | |
43 virtual void OnListFile( | |
44 const net::DirectoryLister::DirectoryListerData& data) OVERRIDE; | |
45 virtual void OnListDone(int error) OVERRIDE; | |
46 | |
47 // NotificationObserver overrides. | 71 // NotificationObserver overrides. |
48 virtual void Observe(NotificationType type, | 72 virtual void Observe(NotificationType type, |
49 const NotificationSource& source, | 73 const NotificationSource& source, |
50 const NotificationDetails& details) OVERRIDE; | 74 const NotificationDetails& details) OVERRIDE; |
51 | 75 |
52 // Helper method for handling the SelectFileDialog::Listener callbacks. | 76 // Kicks off a new directory enumeration. |
53 void DirectorySelected(const FilePath& path); | 77 void StartNewEnumeration(const FilePath& path, |
| 78 int request_id, |
| 79 RenderViewHost* render_view_host); |
| 80 |
| 81 // Callbacks from directory enumeration. |
| 82 virtual void OnListFile( |
| 83 int id, |
| 84 const net::DirectoryLister::DirectoryListerData& data); |
| 85 virtual void OnListDone(int id, int error); |
54 | 86 |
55 // Helper method to get allowed extensions for select file dialog from | 87 // Helper method to get allowed extensions for select file dialog from |
56 // the specified accept types as defined in the spec: | 88 // the specified accept types as defined in the spec: |
57 // http://whatwg.org/html/number-state.html#attr-input-accept | 89 // http://whatwg.org/html/number-state.html#attr-input-accept |
58 SelectFileDialog::FileTypeInfo* GetFileTypesFromAcceptType( | 90 SelectFileDialog::FileTypeInfo* GetFileTypesFromAcceptType( |
59 const string16& accept_types); | 91 const string16& accept_types); |
60 | 92 |
61 // Profile used to set/retrieve the last used directory. | 93 // Profile used to set/retrieve the last used directory. |
62 Profile* profile_; | 94 Profile* profile_; |
63 | 95 |
64 // The RenderViewHost for the page we are associated with. | 96 // The RenderViewHost for the page showing a file dialog (may only be one |
| 97 // such dialog). |
65 RenderViewHost* render_view_host_; | 98 RenderViewHost* render_view_host_; |
66 | 99 |
67 // Dialog box used for choosing files to upload from file form fields. | 100 // Dialog box used for choosing files to upload from file form fields. |
68 scoped_refptr<SelectFileDialog> select_file_dialog_; | 101 scoped_refptr<SelectFileDialog> select_file_dialog_; |
69 | 102 |
70 // The type of file dialog last shown. | 103 // The type of file dialog last shown. |
71 SelectFileDialog::Type dialog_type_; | 104 SelectFileDialog::Type dialog_type_; |
72 | 105 |
73 // The current directory lister (runs on a separate thread). | 106 // Maintain a list of active directory enumerations. These could come from |
74 scoped_refptr<net::DirectoryLister> directory_lister_; | 107 // the file select dialog or from drag-and-drop of directories, so there could |
75 | 108 // be more than one going on at a time. |
76 // The current directory lister results, which may update incrementally | 109 struct ActiveDirectoryEnumeration { |
77 // as the listing proceeds. | 110 scoped_ptr<DirectoryListerDispatchDelegate> delegate_; |
78 std::vector<FilePath> directory_lister_results_; | 111 scoped_refptr<net::DirectoryLister> lister_; |
| 112 RenderViewHost* rvh_; |
| 113 std::vector<FilePath> results_; |
| 114 }; |
| 115 std::map<int, ActiveDirectoryEnumeration*> directory_enumerations_; |
79 | 116 |
80 // Registrar for notifications regarding our RenderViewHost. | 117 // Registrar for notifications regarding our RenderViewHost. |
81 NotificationRegistrar notification_registrar_; | 118 NotificationRegistrar notification_registrar_; |
82 | 119 |
83 DISALLOW_COPY_AND_ASSIGN(FileSelectHelper); | 120 DISALLOW_COPY_AND_ASSIGN(FileSelectHelper); |
84 }; | 121 }; |
85 | 122 |
86 class FileSelectObserver : public TabContentsObserver { | 123 class FileSelectObserver : public TabContentsObserver { |
87 public: | 124 public: |
88 explicit FileSelectObserver(TabContents* tab_contents); | 125 explicit FileSelectObserver(TabContents* tab_contents); |
89 ~FileSelectObserver(); | 126 ~FileSelectObserver(); |
90 | 127 |
91 private: | 128 private: |
92 // TabContentsObserver overrides. | 129 // TabContentsObserver overrides. |
93 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 130 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
94 | 131 |
95 // Called when a file selection is to be done. | 132 // Called when a file selection is to be done. |
96 void OnRunFileChooser(const ViewHostMsg_RunFileChooser_Params& params); | 133 void OnRunFileChooser(const ViewHostMsg_RunFileChooser_Params& params); |
97 | 134 |
| 135 // Called when a direction enumeration is to be done. |
| 136 void OnEnumerateDirectory(int request_id, const FilePath& path); |
| 137 |
98 // FileSelectHelper, lazily created. | 138 // FileSelectHelper, lazily created. |
99 scoped_ptr<FileSelectHelper> file_select_helper_; | 139 scoped_ptr<FileSelectHelper> file_select_helper_; |
100 | 140 |
101 DISALLOW_COPY_AND_ASSIGN(FileSelectObserver); | 141 DISALLOW_COPY_AND_ASSIGN(FileSelectObserver); |
102 }; | 142 }; |
103 | 143 |
104 #endif // CHROME_BROWSER_FILE_SELECT_HELPER_H_ | 144 #endif // CHROME_BROWSER_FILE_SELECT_HELPER_H_ |
OLD | NEW |