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