| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "content/public/browser/notification_types.h" | 25 #include "content/public/browser/notification_types.h" |
| 26 #include "content/public/browser/render_view_host.h" | 26 #include "content/public/browser/render_view_host.h" |
| 27 #include "content/public/browser/render_widget_host_view.h" | 27 #include "content/public/browser/render_widget_host_view.h" |
| 28 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 29 #include "content/public/common/file_chooser_file_info.h" | 29 #include "content/public/common/file_chooser_file_info.h" |
| 30 #include "content/public/common/file_chooser_params.h" | 30 #include "content/public/common/file_chooser_params.h" |
| 31 #include "net/base/mime_util.h" | 31 #include "net/base/mime_util.h" |
| 32 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
| 33 #include "ui/shell_dialogs/selected_file_info.h" | 33 #include "ui/shell_dialogs/selected_file_info.h" |
| 34 | 34 |
| 35 #if defined(OS_CHROMEOS) |
| 36 #include "chrome/browser/chromeos/file_manager/fileapi_util.h" |
| 37 #include "content/public/browser/site_instance.h" |
| 38 #endif |
| 39 |
| 35 using content::BrowserThread; | 40 using content::BrowserThread; |
| 36 using content::FileChooserParams; | 41 using content::FileChooserParams; |
| 37 using content::RenderViewHost; | 42 using content::RenderViewHost; |
| 38 using content::RenderWidgetHost; | 43 using content::RenderWidgetHost; |
| 39 using content::WebContents; | 44 using content::WebContents; |
| 40 | 45 |
| 41 namespace { | 46 namespace { |
| 42 | 47 |
| 43 // There is only one file-selection happening at any given time, | 48 // There is only one file-selection happening at any given time, |
| 44 // so we allocate an enumeration ID for that purpose. All IDs from | 49 // so we allocate an enumeration ID for that purpose. All IDs from |
| 45 // the renderer must start at 0 and increase. | 50 // the renderer must start at 0 and increase. |
| 46 const int kFileSelectEnumerationId = -1; | 51 const int kFileSelectEnumerationId = -1; |
| 47 | 52 |
| 48 void NotifyRenderViewHost( | |
| 49 RenderViewHost* render_view_host, | |
| 50 const std::vector<ui::SelectedFileInfo>& files, | |
| 51 FileChooserParams::Mode dialog_mode) { | |
| 52 std::vector<content::FileChooserFileInfo> chooser_files; | |
| 53 for (size_t i = 0; i < files.size(); ++i) { | |
| 54 content::FileChooserFileInfo chooser_file; | |
| 55 chooser_file.file_path = files[i].local_path; | |
| 56 chooser_file.display_name = files[i].display_name; | |
| 57 chooser_files.push_back(chooser_file); | |
| 58 } | |
| 59 render_view_host->FilesSelectedInChooser(chooser_files, dialog_mode); | |
| 60 } | |
| 61 | |
| 62 // Converts a list of FilePaths to a list of ui::SelectedFileInfo. | 53 // Converts a list of FilePaths to a list of ui::SelectedFileInfo. |
| 63 std::vector<ui::SelectedFileInfo> FilePathListToSelectedFileInfoList( | 54 std::vector<ui::SelectedFileInfo> FilePathListToSelectedFileInfoList( |
| 64 const std::vector<base::FilePath>& paths) { | 55 const std::vector<base::FilePath>& paths) { |
| 65 std::vector<ui::SelectedFileInfo> selected_files; | 56 std::vector<ui::SelectedFileInfo> selected_files; |
| 66 for (size_t i = 0; i < paths.size(); ++i) { | 57 for (size_t i = 0; i < paths.size(); ++i) { |
| 67 selected_files.push_back( | 58 selected_files.push_back( |
| 68 ui::SelectedFileInfo(paths[i], paths[i])); | 59 ui::SelectedFileInfo(paths[i], paths[i])); |
| 69 } | 60 } |
| 70 return selected_files; | 61 return selected_files; |
| 71 } | 62 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 } | 136 } |
| 146 | 137 |
| 147 std::vector<ui::SelectedFileInfo> files; | 138 std::vector<ui::SelectedFileInfo> files; |
| 148 files.push_back(file); | 139 files.push_back(file); |
| 149 | 140 |
| 150 #if defined(OS_MACOSX) && !defined(OS_IOS) | 141 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 151 content::BrowserThread::PostTask( | 142 content::BrowserThread::PostTask( |
| 152 content::BrowserThread::FILE_USER_BLOCKING, | 143 content::BrowserThread::FILE_USER_BLOCKING, |
| 153 FROM_HERE, | 144 FROM_HERE, |
| 154 base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files)); | 145 base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files)); |
| 146 #elif defined(OS_CHROMEOS) |
| 147 ProcessSelectedFilesChromeOS(files); |
| 155 #else | 148 #else |
| 156 NotifyRenderViewHostAndEnd(files); | 149 ProcessSelectedFiles(files); |
| 157 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 150 #endif |
| 158 } | 151 } |
| 159 | 152 |
| 160 void FileSelectHelper::MultiFilesSelected( | 153 void FileSelectHelper::MultiFilesSelected( |
| 161 const std::vector<base::FilePath>& files, | 154 const std::vector<base::FilePath>& files, |
| 162 void* params) { | 155 void* params) { |
| 163 std::vector<ui::SelectedFileInfo> selected_files = | 156 std::vector<ui::SelectedFileInfo> selected_files = |
| 164 FilePathListToSelectedFileInfoList(files); | 157 FilePathListToSelectedFileInfoList(files); |
| 165 | 158 |
| 166 MultiFilesSelectedWithExtraInfo(selected_files, params); | 159 MultiFilesSelectedWithExtraInfo(selected_files, params); |
| 167 } | 160 } |
| 168 | 161 |
| 169 void FileSelectHelper::MultiFilesSelectedWithExtraInfo( | 162 void FileSelectHelper::MultiFilesSelectedWithExtraInfo( |
| 170 const std::vector<ui::SelectedFileInfo>& files, | 163 const std::vector<ui::SelectedFileInfo>& files, |
| 171 void* params) { | 164 void* params) { |
| 172 if (!files.empty()) | 165 if (!files.empty()) |
| 173 profile_->set_last_selected_directory(files[0].file_path.DirName()); | 166 profile_->set_last_selected_directory(files[0].file_path.DirName()); |
| 174 | 167 |
| 175 #if defined(OS_MACOSX) && !defined(OS_IOS) | 168 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 176 content::BrowserThread::PostTask( | 169 content::BrowserThread::PostTask( |
| 177 content::BrowserThread::FILE_USER_BLOCKING, | 170 content::BrowserThread::FILE_USER_BLOCKING, |
| 178 FROM_HERE, | 171 FROM_HERE, |
| 179 base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files)); | 172 base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files)); |
| 173 #elif defined(OS_CHROMEOS) |
| 174 ProcessSelectedFilesChromeOS(files); |
| 180 #else | 175 #else |
| 181 NotifyRenderViewHostAndEnd(files); | 176 ProcessSelectedFiles(files); |
| 182 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 177 #endif |
| 183 } | 178 } |
| 184 | 179 |
| 185 void FileSelectHelper::FileSelectionCanceled(void* params) { | 180 void FileSelectHelper::FileSelectionCanceled(void* params) { |
| 186 NotifyRenderViewHostAndEnd(std::vector<ui::SelectedFileInfo>()); | 181 ProcessSelectedFiles(std::vector<ui::SelectedFileInfo>()); |
| 187 } | 182 } |
| 188 | 183 |
| 189 void FileSelectHelper::StartNewEnumeration(const base::FilePath& path, | 184 void FileSelectHelper::StartNewEnumeration(const base::FilePath& path, |
| 190 int request_id, | 185 int request_id, |
| 191 RenderViewHost* render_view_host) { | 186 RenderViewHost* render_view_host) { |
| 192 scoped_ptr<ActiveDirectoryEnumeration> entry(new ActiveDirectoryEnumeration); | 187 scoped_ptr<ActiveDirectoryEnumeration> entry(new ActiveDirectoryEnumeration); |
| 193 entry->rvh_ = render_view_host; | 188 entry->rvh_ = render_view_host; |
| 194 entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id)); | 189 entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id)); |
| 195 entry->lister_.reset(new net::DirectoryLister(path, | 190 entry->lister_.reset(new net::DirectoryLister(path, |
| 196 true, | 191 true, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 226 if (!entry->rvh_) | 221 if (!entry->rvh_) |
| 227 return; | 222 return; |
| 228 if (error) { | 223 if (error) { |
| 229 FileSelectionCanceled(NULL); | 224 FileSelectionCanceled(NULL); |
| 230 return; | 225 return; |
| 231 } | 226 } |
| 232 | 227 |
| 233 std::vector<ui::SelectedFileInfo> selected_files = | 228 std::vector<ui::SelectedFileInfo> selected_files = |
| 234 FilePathListToSelectedFileInfoList(entry->results_); | 229 FilePathListToSelectedFileInfoList(entry->results_); |
| 235 | 230 |
| 236 if (id == kFileSelectEnumerationId) | 231 if (id == kFileSelectEnumerationId) { |
| 237 NotifyRenderViewHost(entry->rvh_, selected_files, dialog_mode_); | 232 #if defined(OS_CHROMEOS) |
| 238 else | 233 ProcessSelectedFilesChromeOS(selected_files); |
| 234 #else |
| 235 ProcessSelectedFiles(selected_files); |
| 236 #endif // defined(OS_CHROMEOS) |
| 237 } else { |
| 239 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); | 238 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); |
| 239 EnumerateDirectoryEnd(); |
| 240 } |
| 241 } |
| 240 | 242 |
| 241 EnumerateDirectoryEnd(); | 243 #if defined(OS_CHROMEOS) |
| 244 void FileSelectHelper::ProcessSelectedFilesChromeOS( |
| 245 const std::vector<ui::SelectedFileInfo>& files) { |
| 246 if (!render_view_host_) { |
| 247 RunFileChooserEnd(); |
| 248 return; |
| 249 } |
| 250 |
| 251 file_manager::util::ConvertSelectedFileInfoListToFileChooserFileInfoList( |
| 252 file_manager::util::GetFileSystemContextForRenderViewHost( |
| 253 profile_, render_view_host_), |
| 254 web_contents_->GetSiteInstance()->GetSiteURL(), |
| 255 files, |
| 256 base::Bind(&FileSelectHelper::ProcessSelectedFilesChromeOSAfterConvresion, |
| 257 this)); |
| 258 } |
| 259 |
| 260 void FileSelectHelper::ProcessSelectedFilesChromeOSAfterConvresion( |
| 261 scoped_ptr<std::vector<content::FileChooserFileInfo>> list) { |
| 262 NotifyRenderViewHostAndEnd( |
| 263 list ? *list : std::vector<content::FileChooserFileInfo>()); |
| 264 } |
| 265 #endif // defined(OS_CHROMEOS) |
| 266 |
| 267 void FileSelectHelper::ProcessSelectedFiles( |
| 268 const std::vector<ui::SelectedFileInfo>& files) { |
| 269 std::vector<content::FileChooserFileInfo> chooser_files; |
| 270 for (size_t i = 0; i < files.size(); ++i) { |
| 271 chooser_files[i].file_path = files[i].local_path; |
| 272 chooser_files[i].display_name = files[i].display_name; |
| 273 } |
| 274 NotifyRenderViewHostAndEnd(chooser_files); |
| 242 } | 275 } |
| 243 | 276 |
| 244 void FileSelectHelper::NotifyRenderViewHostAndEnd( | 277 void FileSelectHelper::NotifyRenderViewHostAndEnd( |
| 245 const std::vector<ui::SelectedFileInfo>& files) { | 278 const std::vector<content::FileChooserFileInfo>& files) { |
| 246 if (render_view_host_) | 279 if (render_view_host_) |
| 247 NotifyRenderViewHost(render_view_host_, files, dialog_mode_); | 280 render_view_host_->FilesSelectedInChooser(files, dialog_mode_); |
| 248 | 281 |
| 249 // No members should be accessed from here on. | 282 // No members should be accessed from here on. |
| 250 RunFileChooserEnd(); | 283 RunFileChooserEnd(); |
| 251 } | 284 } |
| 252 | 285 |
| 253 void FileSelectHelper::DeleteTemporaryFiles() { | 286 void FileSelectHelper::DeleteTemporaryFiles() { |
| 254 BrowserThread::PostTask(BrowserThread::FILE, | 287 BrowserThread::PostTask(BrowserThread::FILE, |
| 255 FROM_HERE, | 288 FROM_HERE, |
| 256 base::Bind(&DeleteFiles, temporary_files_)); | 289 base::Bind(&DeleteFiles, temporary_files_)); |
| 257 temporary_files_.clear(); | 290 temporary_files_.clear(); |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 // of an extension or a "/" in the case of a MIME type). | 561 // of an extension or a "/" in the case of a MIME type). |
| 529 std::string unused; | 562 std::string unused; |
| 530 if (accept_type.length() <= 1 || | 563 if (accept_type.length() <= 1 || |
| 531 base::StringToLowerASCII(accept_type) != accept_type || | 564 base::StringToLowerASCII(accept_type) != accept_type || |
| 532 base::TrimWhitespaceASCII(accept_type, base::TRIM_ALL, &unused) != | 565 base::TrimWhitespaceASCII(accept_type, base::TRIM_ALL, &unused) != |
| 533 base::TRIM_NONE) { | 566 base::TRIM_NONE) { |
| 534 return false; | 567 return false; |
| 535 } | 568 } |
| 536 return true; | 569 return true; |
| 537 } | 570 } |
| OLD | NEW |