| 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 #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 "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/string_split.h" | 11 #include "base/string_split.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "net/base/mime_util.h" | 14 #include "net/base/mime_util.h" |
| 15 #include "net/base/net_errors.h" |
| 15 #include "chrome/browser/platform_util.h" | 16 #include "chrome/browser/platform_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/renderer_host/render_view_host.h" | 18 #include "chrome/browser/renderer_host/render_view_host.h" |
| 18 #include "chrome/browser/renderer_host/render_widget_host_view.h" | 19 #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 19 #include "chrome/browser/shell_dialogs.h" | 20 #include "chrome/browser/shell_dialogs.h" |
| 20 #include "chrome/common/notification_details.h" | 21 #include "chrome/common/notification_details.h" |
| 21 #include "chrome/common/notification_source.h" | 22 #include "chrome/common/notification_source.h" |
| 22 #include "chrome/common/render_messages_params.h" | 23 #include "chrome/common/render_messages_params.h" |
| 23 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 24 | 25 |
| 25 FileSelectHelper::FileSelectHelper(Profile* profile) | 26 FileSelectHelper::FileSelectHelper(Profile* profile) |
| 26 : profile_(profile), | 27 : profile_(profile), |
| 27 render_view_host_(NULL), | 28 render_view_host_(NULL), |
| 28 select_file_dialog_(), | 29 select_file_dialog_(), |
| 29 dialog_type_(SelectFileDialog::SELECT_OPEN_FILE) { | 30 dialog_type_(SelectFileDialog::SELECT_OPEN_FILE) { |
| 30 } | 31 } |
| 31 | 32 |
| 32 FileSelectHelper::~FileSelectHelper() { | 33 FileSelectHelper::~FileSelectHelper() { |
| 33 // There may be pending file dialogs, we need to tell them that we've gone | 34 // There may be pending file dialogs, we need to tell them that we've gone |
| 34 // away so they don't try and call back to us. | 35 // away so they don't try and call back to us. |
| 35 if (select_file_dialog_.get()) | 36 if (select_file_dialog_.get()) |
| 36 select_file_dialog_->ListenerDestroyed(); | 37 select_file_dialog_->ListenerDestroyed(); |
| 37 | |
| 38 // Stop any pending directory enumeration and prevent a callback. | |
| 39 if (directory_lister_.get()) { | |
| 40 directory_lister_->set_delegate(NULL); | |
| 41 directory_lister_->Cancel(); | |
| 42 } | |
| 43 } | 38 } |
| 44 | 39 |
| 45 void FileSelectHelper::FileSelected(const FilePath& path, | 40 void FileSelectHelper::FileSelected(const FilePath& path, |
| 46 int index, void* params) { | 41 int index, void* params) { |
| 47 if (!render_view_host_) | 42 if (!render_view_host_) |
| 48 return; | 43 return; |
| 49 | 44 |
| 50 profile_->set_last_selected_directory(path.DirName()); | 45 profile_->set_last_selected_directory(path.DirName()); |
| 51 | 46 |
| 52 if (dialog_type_ == SelectFileDialog::SELECT_FOLDER) { | 47 if (dialog_type_ == SelectFileDialog::SELECT_FOLDER) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 79 | 74 |
| 80 // If the user cancels choosing a file to upload we pass back an | 75 // If the user cancels choosing a file to upload we pass back an |
| 81 // empty vector. | 76 // empty vector. |
| 82 render_view_host_->FilesSelectedInChooser(std::vector<FilePath>()); | 77 render_view_host_->FilesSelectedInChooser(std::vector<FilePath>()); |
| 83 | 78 |
| 84 // We are done with this showing of the dialog. | 79 // We are done with this showing of the dialog. |
| 85 render_view_host_ = NULL; | 80 render_view_host_ = NULL; |
| 86 } | 81 } |
| 87 | 82 |
| 88 void FileSelectHelper::DirectorySelected(const FilePath& path) { | 83 void FileSelectHelper::DirectorySelected(const FilePath& path) { |
| 89 directory_lister_ = new net::DirectoryLister(path, | 84 directory_lister_.reset( |
| 90 true, | 85 new net::DirectoryLister(path, |
| 91 net::DirectoryLister::NO_SORT, | 86 true, |
| 92 this); | 87 net::DirectoryLister::NO_SORT, |
| 93 if (!directory_lister_->Start()) | 88 this)); |
| 94 FileSelectionCanceled(NULL); | 89 directory_lister_->Start(); |
| 95 } | 90 } |
| 96 | 91 |
| 97 void FileSelectHelper::OnListFile( | 92 void FileSelectHelper::OnListFile(const net::DirectoryLister::Data& data) { |
| 98 const net::DirectoryLister::DirectoryListerData& data) { | |
| 99 // Directory upload only cares about files. This util call just checks | 93 // Directory upload only cares about files. This util call just checks |
| 100 // the flags in the structure; there's no file I/O going on. | 94 // the flags in the structure; there's no file I/O going on. |
| 101 if (file_util::FileEnumerator::IsDirectory(data.info)) | 95 if (file_util::FileEnumerator::IsDirectory(data.info)) |
| 102 return; | 96 return; |
| 103 | 97 |
| 104 directory_lister_results_.push_back(data.path); | 98 directory_lister_results_.push_back(data.path); |
| 105 } | 99 } |
| 106 | 100 |
| 107 void FileSelectHelper::OnListDone(int error) { | 101 void FileSelectHelper::OnListDone(int error) { |
| 108 if (!render_view_host_) | 102 if (!render_view_host_) |
| 109 return; | 103 return; |
| 110 | 104 |
| 111 if (error) { | 105 if (error != net::OK) { |
| 112 FileSelectionCanceled(NULL); | 106 FileSelectionCanceled(NULL); |
| 113 return; | 107 return; |
| 114 } | 108 } |
| 115 | 109 |
| 116 render_view_host_->FilesSelectedInChooser(directory_lister_results_); | 110 render_view_host_->FilesSelectedInChooser(directory_lister_results_); |
| 117 render_view_host_ = NULL; | 111 render_view_host_ = NULL; |
| 118 directory_lister_ = NULL; | 112 directory_lister_.reset(); |
| 119 directory_lister_results_.clear(); | 113 directory_lister_results_.clear(); |
| 120 } | 114 } |
| 121 | 115 |
| 122 SelectFileDialog::FileTypeInfo* FileSelectHelper::GetFileTypesFromAcceptType( | 116 SelectFileDialog::FileTypeInfo* FileSelectHelper::GetFileTypesFromAcceptType( |
| 123 const string16& accept_types) { | 117 const string16& accept_types) { |
| 124 if (accept_types.empty()) | 118 if (accept_types.empty()) |
| 125 return NULL; | 119 return NULL; |
| 126 | 120 |
| 127 // Split the accept-type string on commas. | 121 // Split the accept-type string on commas. |
| 128 std::vector<string16> mime_types; | 122 std::vector<string16> mime_types; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 NULL); | 231 NULL); |
| 238 } | 232 } |
| 239 | 233 |
| 240 void FileSelectHelper::Observe(NotificationType type, | 234 void FileSelectHelper::Observe(NotificationType type, |
| 241 const NotificationSource& source, | 235 const NotificationSource& source, |
| 242 const NotificationDetails& details) { | 236 const NotificationDetails& details) { |
| 243 DCHECK(type == NotificationType::RENDER_WIDGET_HOST_DESTROYED); | 237 DCHECK(type == NotificationType::RENDER_WIDGET_HOST_DESTROYED); |
| 244 DCHECK(Details<RenderViewHost>(details).ptr() == render_view_host_); | 238 DCHECK(Details<RenderViewHost>(details).ptr() == render_view_host_); |
| 245 render_view_host_ = NULL; | 239 render_view_host_ = NULL; |
| 246 } | 240 } |
| OLD | NEW |