OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/file_select_helper.h" | 5 #include "chrome/browser/file_select_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
12 #include "base/string_split.h" | 12 #include "base/string_split.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "chrome/browser/platform_util.h" | 15 #include "chrome/browser/platform_util.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
19 #include "content/public/browser/notification_details.h" | 19 #include "content/public/browser/notification_details.h" |
20 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
21 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
22 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
23 #include "content/public/browser/render_widget_host_view.h" | 23 #include "content/public/browser/render_widget_host_view.h" |
24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/file_chooser_params.h" | 25 #include "content/public/common/file_chooser_params.h" |
26 #include "content/public/common/selected_file_info.h" | |
27 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
28 #include "net/base/mime_util.h" | 27 #include "net/base/mime_util.h" |
| 28 #include "ui/base/dialogs/selected_file_info.h" |
29 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
30 | 30 |
31 using content::BrowserThread; | 31 using content::BrowserThread; |
32 using content::FileChooserParams; | 32 using content::FileChooserParams; |
33 using content::RenderViewHost; | 33 using content::RenderViewHost; |
34 using content::RenderWidgetHost; | 34 using content::RenderWidgetHost; |
35 using content::WebContents; | 35 using content::WebContents; |
36 | 36 |
37 namespace { | 37 namespace { |
38 | 38 |
39 // There is only one file-selection happening at any given time, | 39 // There is only one file-selection happening at any given time, |
40 // so we allocate an enumeration ID for that purpose. All IDs from | 40 // so we allocate an enumeration ID for that purpose. All IDs from |
41 // the renderer must start at 0 and increase. | 41 // the renderer must start at 0 and increase. |
42 const int kFileSelectEnumerationId = -1; | 42 const int kFileSelectEnumerationId = -1; |
43 | 43 |
44 void NotifyRenderViewHost(RenderViewHost* render_view_host, | 44 void NotifyRenderViewHost(RenderViewHost* render_view_host, |
45 const std::vector<content::SelectedFileInfo>& files, | 45 const std::vector<ui::SelectedFileInfo>& files, |
46 SelectFileDialog::Type dialog_type) { | 46 SelectFileDialog::Type dialog_type) { |
47 const int kReadFilePermissions = | 47 const int kReadFilePermissions = |
48 base::PLATFORM_FILE_OPEN | | 48 base::PLATFORM_FILE_OPEN | |
49 base::PLATFORM_FILE_READ | | 49 base::PLATFORM_FILE_READ | |
50 base::PLATFORM_FILE_EXCLUSIVE_READ | | 50 base::PLATFORM_FILE_EXCLUSIVE_READ | |
51 base::PLATFORM_FILE_ASYNC; | 51 base::PLATFORM_FILE_ASYNC; |
52 | 52 |
53 const int kWriteFilePermissions = | 53 const int kWriteFilePermissions = |
54 base::PLATFORM_FILE_CREATE | | 54 base::PLATFORM_FILE_CREATE | |
55 base::PLATFORM_FILE_CREATE_ALWAYS | | 55 base::PLATFORM_FILE_CREATE_ALWAYS | |
56 base::PLATFORM_FILE_OPEN | | 56 base::PLATFORM_FILE_OPEN | |
57 base::PLATFORM_FILE_OPEN_ALWAYS | | 57 base::PLATFORM_FILE_OPEN_ALWAYS | |
58 base::PLATFORM_FILE_OPEN_TRUNCATED | | 58 base::PLATFORM_FILE_OPEN_TRUNCATED | |
59 base::PLATFORM_FILE_WRITE | | 59 base::PLATFORM_FILE_WRITE | |
60 base::PLATFORM_FILE_WRITE_ATTRIBUTES | | 60 base::PLATFORM_FILE_WRITE_ATTRIBUTES | |
61 base::PLATFORM_FILE_ASYNC; | 61 base::PLATFORM_FILE_ASYNC; |
62 | 62 |
63 int permissions = kReadFilePermissions; | 63 int permissions = kReadFilePermissions; |
64 if (dialog_type == SelectFileDialog::SELECT_SAVEAS_FILE) | 64 if (dialog_type == SelectFileDialog::SELECT_SAVEAS_FILE) |
65 permissions = kWriteFilePermissions; | 65 permissions = kWriteFilePermissions; |
66 render_view_host->FilesSelectedInChooser(files, permissions); | 66 render_view_host->FilesSelectedInChooser(files, permissions); |
67 } | 67 } |
68 | 68 |
69 // Converts a list of FilePaths to a list of SelectedFileInfo, with the | 69 // Converts a list of FilePaths to a list of SelectedFileInfo, with the |
70 // display name field left empty. | 70 // display name field left empty. |
71 std::vector<content::SelectedFileInfo> ConvertToSelectedFileInfoList( | 71 std::vector<ui::SelectedFileInfo> ConvertToSelectedFileInfoList( |
72 const std::vector<FilePath>& paths) { | 72 const std::vector<FilePath>& paths) { |
73 std::vector<content::SelectedFileInfo> selected_files; | 73 std::vector<ui::SelectedFileInfo> selected_files; |
74 for (size_t i = 0; i < paths.size(); ++i) { | 74 for (size_t i = 0; i < paths.size(); ++i) { |
75 selected_files.push_back( | 75 selected_files.push_back( |
76 content::SelectedFileInfo(paths[i], FilePath::StringType())); | 76 ui::SelectedFileInfo(paths[i], FilePath::StringType())); |
77 } | 77 } |
78 return selected_files; | 78 return selected_files; |
79 } | 79 } |
80 | 80 |
81 } // namespace | 81 } // namespace |
82 | 82 |
83 struct FileSelectHelper::ActiveDirectoryEnumeration { | 83 struct FileSelectHelper::ActiveDirectoryEnumeration { |
84 ActiveDirectoryEnumeration() : rvh_(NULL) {} | 84 ActiveDirectoryEnumeration() : rvh_(NULL) {} |
85 | 85 |
86 scoped_ptr<DirectoryListerDispatchDelegate> delegate_; | 86 scoped_ptr<DirectoryListerDispatchDelegate> delegate_; |
(...skipping 24 matching lines...) Expand all Loading... |
111 iter != directory_enumerations_.end(); | 111 iter != directory_enumerations_.end(); |
112 ++iter) { | 112 ++iter) { |
113 iter->second->lister_.reset(); | 113 iter->second->lister_.reset(); |
114 delete iter->second; | 114 delete iter->second; |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 void FileSelectHelper::FileSelected(const FilePath& path, | 118 void FileSelectHelper::FileSelected(const FilePath& path, |
119 int index, void* params) { | 119 int index, void* params) { |
120 FileSelectedWithExtraInfo( | 120 FileSelectedWithExtraInfo( |
121 content::SelectedFileInfo(path, FilePath::StringType()), | 121 ui::SelectedFileInfo(path, FilePath::StringType()), |
122 index, params); | 122 index, params); |
123 } | 123 } |
124 | 124 |
125 void FileSelectHelper::FileSelectedWithExtraInfo( | 125 void FileSelectHelper::FileSelectedWithExtraInfo( |
126 const content::SelectedFileInfo& file, | 126 const ui::SelectedFileInfo& file, |
127 int index, | 127 int index, |
128 void* params) { | 128 void* params) { |
129 if (!render_view_host_) | 129 if (!render_view_host_) |
130 return; | 130 return; |
131 | 131 |
132 const FilePath& path = file.path; | 132 const FilePath& path = file.path; |
133 profile_->set_last_selected_directory(path.DirName()); | 133 profile_->set_last_selected_directory(path.DirName()); |
134 | 134 |
135 if (dialog_type_ == SelectFileDialog::SELECT_FOLDER) { | 135 if (dialog_type_ == SelectFileDialog::SELECT_FOLDER) { |
136 StartNewEnumeration(path, kFileSelectEnumerationId, render_view_host_); | 136 StartNewEnumeration(path, kFileSelectEnumerationId, render_view_host_); |
137 return; | 137 return; |
138 } | 138 } |
139 | 139 |
140 std::vector<content::SelectedFileInfo> files; | 140 std::vector<ui::SelectedFileInfo> files; |
141 files.push_back(file); | 141 files.push_back(file); |
142 NotifyRenderViewHost(render_view_host_, files, dialog_type_); | 142 NotifyRenderViewHost(render_view_host_, files, dialog_type_); |
143 | 143 |
144 // No members should be accessed from here on. | 144 // No members should be accessed from here on. |
145 RunFileChooserEnd(); | 145 RunFileChooserEnd(); |
146 } | 146 } |
147 | 147 |
148 void FileSelectHelper::MultiFilesSelected(const std::vector<FilePath>& files, | 148 void FileSelectHelper::MultiFilesSelected(const std::vector<FilePath>& files, |
149 void* params) { | 149 void* params) { |
150 std::vector<content::SelectedFileInfo> selected_files = | 150 std::vector<ui::SelectedFileInfo> selected_files = |
151 ConvertToSelectedFileInfoList(files); | 151 ConvertToSelectedFileInfoList(files); |
152 MultiFilesSelectedWithExtraInfo(selected_files, params); | 152 MultiFilesSelectedWithExtraInfo(selected_files, params); |
153 } | 153 } |
154 | 154 |
155 void FileSelectHelper::MultiFilesSelectedWithExtraInfo( | 155 void FileSelectHelper::MultiFilesSelectedWithExtraInfo( |
156 const std::vector<content::SelectedFileInfo>& files, | 156 const std::vector<ui::SelectedFileInfo>& files, |
157 void* params) { | 157 void* params) { |
158 if (!files.empty()) | 158 if (!files.empty()) |
159 profile_->set_last_selected_directory(files[0].path.DirName()); | 159 profile_->set_last_selected_directory(files[0].path.DirName()); |
160 if (!render_view_host_) | 160 if (!render_view_host_) |
161 return; | 161 return; |
162 | 162 |
163 NotifyRenderViewHost(render_view_host_, files, dialog_type_); | 163 NotifyRenderViewHost(render_view_host_, files, dialog_type_); |
164 | 164 |
165 // No members should be accessed from here on. | 165 // No members should be accessed from here on. |
166 RunFileChooserEnd(); | 166 RunFileChooserEnd(); |
167 } | 167 } |
168 | 168 |
169 void FileSelectHelper::FileSelectionCanceled(void* params) { | 169 void FileSelectHelper::FileSelectionCanceled(void* params) { |
170 if (!render_view_host_) | 170 if (!render_view_host_) |
171 return; | 171 return; |
172 | 172 |
173 // If the user cancels choosing a file to upload we pass back an | 173 // If the user cancels choosing a file to upload we pass back an |
174 // empty vector. | 174 // empty vector. |
175 NotifyRenderViewHost( | 175 NotifyRenderViewHost( |
176 render_view_host_, std::vector<content::SelectedFileInfo>(), | 176 render_view_host_, std::vector<ui::SelectedFileInfo>(), |
177 dialog_type_); | 177 dialog_type_); |
178 | 178 |
179 // No members should be accessed from here on. | 179 // No members should be accessed from here on. |
180 RunFileChooserEnd(); | 180 RunFileChooserEnd(); |
181 } | 181 } |
182 | 182 |
183 void FileSelectHelper::StartNewEnumeration(const FilePath& path, | 183 void FileSelectHelper::StartNewEnumeration(const FilePath& path, |
184 int request_id, | 184 int request_id, |
185 RenderViewHost* render_view_host) { | 185 RenderViewHost* render_view_host) { |
186 scoped_ptr<ActiveDirectoryEnumeration> entry(new ActiveDirectoryEnumeration); | 186 scoped_ptr<ActiveDirectoryEnumeration> entry(new ActiveDirectoryEnumeration); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 // This entry needs to be cleaned up when this function is done. | 219 // This entry needs to be cleaned up when this function is done. |
220 scoped_ptr<ActiveDirectoryEnumeration> entry(directory_enumerations_[id]); | 220 scoped_ptr<ActiveDirectoryEnumeration> entry(directory_enumerations_[id]); |
221 directory_enumerations_.erase(id); | 221 directory_enumerations_.erase(id); |
222 if (!entry->rvh_) | 222 if (!entry->rvh_) |
223 return; | 223 return; |
224 if (error) { | 224 if (error) { |
225 FileSelectionCanceled(NULL); | 225 FileSelectionCanceled(NULL); |
226 return; | 226 return; |
227 } | 227 } |
228 | 228 |
229 std::vector<content::SelectedFileInfo> selected_files = | 229 std::vector<ui::SelectedFileInfo> selected_files = |
230 ConvertToSelectedFileInfoList(entry->results_); | 230 ConvertToSelectedFileInfoList(entry->results_); |
231 | 231 |
232 if (id == kFileSelectEnumerationId) | 232 if (id == kFileSelectEnumerationId) |
233 NotifyRenderViewHost(entry->rvh_, selected_files, dialog_type_); | 233 NotifyRenderViewHost(entry->rvh_, selected_files, dialog_type_); |
234 else | 234 else |
235 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); | 235 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); |
236 | 236 |
237 EnumerateDirectoryEnd(); | 237 EnumerateDirectoryEnd(); |
238 } | 238 } |
239 | 239 |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 // A 1 character accept type will always be invalid (either a "." in the case | 473 // A 1 character accept type will always be invalid (either a "." in the case |
474 // of an extension or a "/" in the case of a MIME type). | 474 // of an extension or a "/" in the case of a MIME type). |
475 std::string unused; | 475 std::string unused; |
476 if (accept_type.length() <= 1 || | 476 if (accept_type.length() <= 1 || |
477 StringToLowerASCII(accept_type) != accept_type || | 477 StringToLowerASCII(accept_type) != accept_type || |
478 TrimWhitespaceASCII(accept_type, TRIM_ALL, &unused) != TRIM_NONE) { | 478 TrimWhitespaceASCII(accept_type, TRIM_ALL, &unused) != TRIM_NONE) { |
479 return false; | 479 return false; |
480 } | 480 } |
481 return true; | 481 return true; |
482 } | 482 } |
OLD | NEW |