[email protected] | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/file_select_helper.h" |
| 6 | |
[email protected] | 5ac950b | 2010-12-09 21:34:25 | [diff] [blame] | 7 | #include <string> |
| 8 | |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 10 | #include "base/file_util.h" |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 11 | #include "base/platform_file.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 12 | #include "base/string_split.h" |
| 13 | #include "base/string_util.h" |
| 14 | #include "base/utf_string_conversions.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 15 | #include "chrome/browser/platform_util.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 16 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 17 | #include "content/browser/renderer_host/render_process_host.h" |
[email protected] | 5de63471 | 2011-03-02 00:20:19 | [diff] [blame] | 18 | #include "content/browser/renderer_host/render_view_host.h" |
| 19 | #include "content/browser/renderer_host/render_widget_host_view.h" |
[email protected] | aaed252 | 2011-03-11 18:50:54 | [diff] [blame] | 20 | #include "content/browser/tab_contents/tab_contents.h" |
[email protected] | d989891 | 2011-04-15 21:10:00 | [diff] [blame] | 21 | #include "chrome/browser/ui/browser.h" |
| 22 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 23 | #include "content/common/view_messages.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 24 | #include "content/public/browser/notification_details.h" |
| 25 | #include "content/public/browser/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 26 | #include "content/public/browser/notification_types.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 27 | #include "grit/generated_resources.h" |
[email protected] | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 28 | #include "net/base/mime_util.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 29 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 30 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | // There is only one file-selection happening at any given time, |
| 34 | // so we allocate an enumeration ID for that purpose. All IDs from |
| 35 | // the renderer must start at 0 and increase. |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 36 | const int kFileSelectEnumerationId = -1; |
| 37 | |
| 38 | void NotifyRenderViewHost(RenderViewHost* render_view_host, |
| 39 | const std::vector<FilePath>& files, |
| 40 | SelectFileDialog::Type dialog_type) { |
| 41 | const int kReadFilePermissions = |
| 42 | base::PLATFORM_FILE_OPEN | |
| 43 | base::PLATFORM_FILE_READ | |
| 44 | base::PLATFORM_FILE_EXCLUSIVE_READ | |
| 45 | base::PLATFORM_FILE_ASYNC; |
| 46 | |
| 47 | const int kWriteFilePermissions = |
[email protected] | 3c688fac | 2011-10-14 02:29:14 | [diff] [blame] | 48 | base::PLATFORM_FILE_CREATE | |
| 49 | base::PLATFORM_FILE_CREATE_ALWAYS | |
| 50 | base::PLATFORM_FILE_OPEN | |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 51 | base::PLATFORM_FILE_OPEN_ALWAYS | |
[email protected] | 3c688fac | 2011-10-14 02:29:14 | [diff] [blame] | 52 | base::PLATFORM_FILE_OPEN_TRUNCATED | |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 53 | base::PLATFORM_FILE_WRITE | |
| 54 | base::PLATFORM_FILE_WRITE_ATTRIBUTES | |
| 55 | base::PLATFORM_FILE_ASYNC; |
| 56 | |
| 57 | int permissions = kReadFilePermissions; |
| 58 | if (dialog_type == SelectFileDialog::SELECT_SAVEAS_FILE) |
| 59 | permissions = kWriteFilePermissions; |
| 60 | render_view_host->FilesSelectedInChooser(files, permissions); |
| 61 | } |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 64 | struct FileSelectHelper::ActiveDirectoryEnumeration { |
[email protected] | d45f751 | 2011-06-21 21:18:27 | [diff] [blame] | 65 | ActiveDirectoryEnumeration() : rvh_(NULL) {} |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 66 | |
| 67 | scoped_ptr<DirectoryListerDispatchDelegate> delegate_; |
[email protected] | 05a81418 | 2011-04-27 19:50:34 | [diff] [blame] | 68 | scoped_ptr<net::DirectoryLister> lister_; |
[email protected] | 485a527 | 2011-04-12 00:49:29 | [diff] [blame] | 69 | RenderViewHost* rvh_; |
| 70 | std::vector<FilePath> results_; |
| 71 | }; |
| 72 | |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 73 | FileSelectHelper::FileSelectHelper(Profile* profile) |
| 74 | : profile_(profile), |
| 75 | render_view_host_(NULL), |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 76 | tab_contents_(NULL), |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 77 | select_file_dialog_(), |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 78 | select_file_types_(), |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 79 | dialog_type_(SelectFileDialog::SELECT_OPEN_FILE) { |
| 80 | } |
| 81 | |
| 82 | FileSelectHelper::~FileSelectHelper() { |
| 83 | // There may be pending file dialogs, we need to tell them that we've gone |
| 84 | // away so they don't try and call back to us. |
| 85 | if (select_file_dialog_.get()) |
| 86 | select_file_dialog_->ListenerDestroyed(); |
| 87 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 88 | // Stop any pending directory enumeration, prevent a callback, and free |
| 89 | // allocated memory. |
| 90 | std::map<int, ActiveDirectoryEnumeration*>::iterator iter; |
| 91 | for (iter = directory_enumerations_.begin(); |
| 92 | iter != directory_enumerations_.end(); |
| 93 | ++iter) { |
[email protected] | 05a81418 | 2011-04-27 19:50:34 | [diff] [blame] | 94 | iter->second->lister_.reset(); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 95 | delete iter->second; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | void FileSelectHelper::FileSelected(const FilePath& path, |
| 100 | int index, void* params) { |
| 101 | if (!render_view_host_) |
| 102 | return; |
| 103 | |
| 104 | profile_->set_last_selected_directory(path.DirName()); |
| 105 | |
| 106 | if (dialog_type_ == SelectFileDialog::SELECT_FOLDER) { |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 107 | StartNewEnumeration(path, kFileSelectEnumerationId, render_view_host_); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
| 111 | std::vector<FilePath> files; |
| 112 | files.push_back(path); |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 113 | NotifyRenderViewHost(render_view_host_, files, dialog_type_); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 114 | |
[email protected] | 3a29a6e | 2011-08-24 18:26:21 | [diff] [blame] | 115 | // No members should be accessed from here on. |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 116 | RunFileChooserEnd(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void FileSelectHelper::MultiFilesSelected(const std::vector<FilePath>& files, |
| 120 | void* params) { |
| 121 | if (!files.empty()) |
| 122 | profile_->set_last_selected_directory(files[0].DirName()); |
| 123 | if (!render_view_host_) |
| 124 | return; |
| 125 | |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 126 | NotifyRenderViewHost(render_view_host_, files, dialog_type_); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 127 | |
[email protected] | 3a29a6e | 2011-08-24 18:26:21 | [diff] [blame] | 128 | // No members should be accessed from here on. |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 129 | RunFileChooserEnd(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void FileSelectHelper::FileSelectionCanceled(void* params) { |
| 133 | if (!render_view_host_) |
| 134 | return; |
| 135 | |
| 136 | // If the user cancels choosing a file to upload we pass back an |
| 137 | // empty vector. |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 138 | NotifyRenderViewHost( |
| 139 | render_view_host_, std::vector<FilePath>(), dialog_type_); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 140 | |
[email protected] | 3a29a6e | 2011-08-24 18:26:21 | [diff] [blame] | 141 | // No members should be accessed from here on. |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 142 | RunFileChooserEnd(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 145 | void FileSelectHelper::StartNewEnumeration(const FilePath& path, |
| 146 | int request_id, |
| 147 | RenderViewHost* render_view_host) { |
| 148 | scoped_ptr<ActiveDirectoryEnumeration> entry(new ActiveDirectoryEnumeration); |
| 149 | entry->rvh_ = render_view_host; |
| 150 | entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id)); |
[email protected] | 05a81418 | 2011-04-27 19:50:34 | [diff] [blame] | 151 | entry->lister_.reset(new net::DirectoryLister(path, |
| 152 | true, |
| 153 | net::DirectoryLister::NO_SORT, |
| 154 | entry->delegate_.get())); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 155 | if (!entry->lister_->Start()) { |
| 156 | if (request_id == kFileSelectEnumerationId) |
| 157 | FileSelectionCanceled(NULL); |
| 158 | else |
| 159 | render_view_host->DirectoryEnumerationFinished(request_id, |
| 160 | entry->results_); |
| 161 | } else { |
| 162 | directory_enumerations_[request_id] = entry.release(); |
| 163 | } |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void FileSelectHelper::OnListFile( |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 167 | int id, |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 168 | const net::DirectoryLister::DirectoryListerData& data) { |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 169 | ActiveDirectoryEnumeration* entry = directory_enumerations_[id]; |
| 170 | |
[email protected] | 9897e09 | 2011-02-04 22:09:11 | [diff] [blame] | 171 | // Directory upload returns directories via a "." file, so that |
| 172 | // empty directories are included. This util call just checks |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 173 | // the flags in the structure; there's no file I/O going on. |
| 174 | if (file_util::FileEnumerator::IsDirectory(data.info)) |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 175 | entry->results_.push_back(data.path.Append(FILE_PATH_LITERAL("."))); |
[email protected] | 9897e09 | 2011-02-04 22:09:11 | [diff] [blame] | 176 | else |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 177 | entry->results_.push_back(data.path); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 180 | void FileSelectHelper::OnListDone(int id, int error) { |
| 181 | // This entry needs to be cleaned up when this function is done. |
| 182 | scoped_ptr<ActiveDirectoryEnumeration> entry(directory_enumerations_[id]); |
| 183 | directory_enumerations_.erase(id); |
| 184 | if (!entry->rvh_) |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 185 | return; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 186 | if (error) { |
| 187 | FileSelectionCanceled(NULL); |
| 188 | return; |
| 189 | } |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 190 | if (id == kFileSelectEnumerationId) |
[email protected] | 459fba8 | 2011-10-13 02:48:50 | [diff] [blame] | 191 | NotifyRenderViewHost(entry->rvh_, entry->results_, dialog_type_); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 192 | else |
| 193 | entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 194 | |
| 195 | EnumerateDirectoryEnd(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | SelectFileDialog::FileTypeInfo* FileSelectHelper::GetFileTypesFromAcceptType( |
[email protected] | 3314c2b1 | 2011-11-02 08:05:46 | [diff] [blame^] | 199 | const std::vector<string16>& accept_types) { |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 200 | if (accept_types.empty()) |
| 201 | return NULL; |
| 202 | |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 203 | // Create FileTypeInfo and pre-allocate for the first extension list. |
| 204 | scoped_ptr<SelectFileDialog::FileTypeInfo> file_type( |
| 205 | new SelectFileDialog::FileTypeInfo()); |
| 206 | file_type->include_all_files = true; |
| 207 | file_type->extensions.resize(1); |
| 208 | std::vector<FilePath::StringType>* extensions = &file_type->extensions.back(); |
| 209 | |
| 210 | // Find the correspondinge extensions. |
| 211 | int valid_type_count = 0; |
| 212 | int description_id = 0; |
[email protected] | 3314c2b1 | 2011-11-02 08:05:46 | [diff] [blame^] | 213 | for (size_t i = 0; i < accept_types.size(); ++i) { |
| 214 | std::string ascii_mime_type = UTF16ToASCII(accept_types[i]); |
| 215 | // WebKit normalizes MIME types. See HTMLInputElement::acceptMIMETypes(). |
| 216 | DCHECK(StringToLowerASCII(ascii_mime_type) == ascii_mime_type) |
| 217 | << "A MIME type contains uppercase letter: " << ascii_mime_type; |
| 218 | DCHECK(TrimWhitespaceASCII(ascii_mime_type, TRIM_ALL, &ascii_mime_type) |
| 219 | == TRIM_NONE) |
| 220 | << "A MIME type contains whitespace: '" << ascii_mime_type << "'"; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 221 | |
| 222 | size_t old_extension_size = extensions->size(); |
| 223 | if (ascii_mime_type == "image/*") { |
| 224 | description_id = IDS_IMAGE_FILES; |
| 225 | net::GetImageExtensions(extensions); |
| 226 | } else if (ascii_mime_type == "audio/*") { |
| 227 | description_id = IDS_AUDIO_FILES; |
| 228 | net::GetAudioExtensions(extensions); |
| 229 | } else if (ascii_mime_type == "video/*") { |
| 230 | description_id = IDS_VIDEO_FILES; |
| 231 | net::GetVideoExtensions(extensions); |
| 232 | } else { |
| 233 | net::GetExtensionsForMimeType(ascii_mime_type, extensions); |
| 234 | } |
| 235 | |
| 236 | if (extensions->size() > old_extension_size) |
| 237 | valid_type_count++; |
| 238 | } |
| 239 | |
[email protected] | cbcd12ed | 2010-12-16 23:42:57 | [diff] [blame] | 240 | // If no valid extension is added, bail out. |
| 241 | if (valid_type_count == 0) |
| 242 | return NULL; |
| 243 | |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 244 | // Use a generic description "Custom Files" if either of the following is |
| 245 | // true: |
| 246 | // 1) There're multiple types specified, like "audio/*,video/*" |
| 247 | // 2) There're multiple extensions for a MIME type without parameter, like |
| 248 | // "ehtml,shtml,htm,html" for "text/html". On Windows, the select file |
| 249 | // dialog uses the first extension in the list to form the description, |
| 250 | // like "EHTML Files". This is not what we want. |
| 251 | if (valid_type_count > 1 || |
| 252 | (valid_type_count == 1 && description_id == 0 && extensions->size() > 1)) |
| 253 | description_id = IDS_CUSTOM_FILES; |
| 254 | |
| 255 | if (description_id) { |
| 256 | file_type->extension_description_overrides.push_back( |
| 257 | l10n_util::GetStringUTF16(description_id)); |
| 258 | } |
| 259 | |
| 260 | return file_type.release(); |
| 261 | } |
| 262 | |
| 263 | void FileSelectHelper::RunFileChooser( |
| 264 | RenderViewHost* render_view_host, |
[email protected] | d989891 | 2011-04-15 21:10:00 | [diff] [blame] | 265 | TabContents* tab_contents, |
[email protected] | aaed252 | 2011-03-11 18:50:54 | [diff] [blame] | 266 | const ViewHostMsg_RunFileChooser_Params& params) { |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 267 | DCHECK(!render_view_host_); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 268 | DCHECK(!tab_contents_); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 269 | render_view_host_ = render_view_host; |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 270 | tab_contents_ = tab_contents; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 271 | notification_registrar_.RemoveAll(); |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 272 | notification_registrar_.Add( |
| 273 | this, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 274 | content::Source<RenderWidgetHost>(render_view_host_)); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 275 | notification_registrar_.Add( |
| 276 | this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 277 | content::Source<TabContents>(tab_contents_)); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 278 | |
| 279 | BrowserThread::PostTask( |
| 280 | BrowserThread::FILE, FROM_HERE, |
| 281 | base::Bind(&FileSelectHelper::RunFileChooserOnFileThread, this, params)); |
| 282 | |
| 283 | // Because this class returns notifications to the RenderViewHost, it is |
| 284 | // difficult for callers to know how long to keep a reference to this |
| 285 | // instance. We AddRef() here to keep the instance alive after we return |
| 286 | // to the caller, until the last callback is received from the file dialog. |
| 287 | // At that point, we must call RunFileChooserEnd(). |
| 288 | AddRef(); |
| 289 | } |
| 290 | |
| 291 | void FileSelectHelper::RunFileChooserOnFileThread( |
| 292 | const ViewHostMsg_RunFileChooser_Params& params) { |
| 293 | select_file_types_.reset( |
| 294 | GetFileTypesFromAcceptType(params.accept_types)); |
| 295 | |
| 296 | BrowserThread::PostTask( |
| 297 | BrowserThread::UI, FROM_HERE, |
| 298 | base::Bind(&FileSelectHelper::RunFileChooserOnUIThread, this, params)); |
| 299 | } |
| 300 | |
| 301 | void FileSelectHelper::RunFileChooserOnUIThread( |
| 302 | const ViewHostMsg_RunFileChooser_Params& params) { |
| 303 | if (!render_view_host_ || !tab_contents_) |
| 304 | return; |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 305 | |
| 306 | if (!select_file_dialog_.get()) |
| 307 | select_file_dialog_ = SelectFileDialog::Create(this); |
| 308 | |
| 309 | switch (params.mode) { |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 310 | case ViewHostMsg_RunFileChooser_Mode::Open: |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 311 | dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; |
| 312 | break; |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 313 | case ViewHostMsg_RunFileChooser_Mode::OpenMultiple: |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 314 | dialog_type_ = SelectFileDialog::SELECT_OPEN_MULTI_FILE; |
| 315 | break; |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 316 | case ViewHostMsg_RunFileChooser_Mode::OpenFolder: |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 317 | dialog_type_ = SelectFileDialog::SELECT_FOLDER; |
| 318 | break; |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 319 | case ViewHostMsg_RunFileChooser_Mode::Save: |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 320 | dialog_type_ = SelectFileDialog::SELECT_SAVEAS_FILE; |
| 321 | break; |
| 322 | default: |
| 323 | dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning. |
| 324 | NOTREACHED(); |
| 325 | } |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 326 | FilePath default_file_name = params.default_file_name; |
| 327 | if (default_file_name.empty()) |
| 328 | default_file_name = profile_->last_selected_directory(); |
| 329 | |
| 330 | gfx::NativeWindow owning_window = |
| 331 | platform_util::GetTopLevel(render_view_host_->view()->GetNativeView()); |
[email protected] | d989891 | 2011-04-15 21:10:00 | [diff] [blame] | 332 | |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 333 | select_file_dialog_->SelectFile( |
| 334 | dialog_type_, |
| 335 | params.title, |
| 336 | default_file_name, |
| 337 | select_file_types_.get(), |
| 338 | select_file_types_.get() ? 1 : 0, // 1-based index. |
| 339 | FILE_PATH_LITERAL(""), |
| 340 | tab_contents_, |
| 341 | owning_window, |
| 342 | NULL); |
| 343 | |
| 344 | select_file_types_.reset(); |
| 345 | } |
| 346 | |
| 347 | // This method is called when we receive the last callback from the file |
| 348 | // chooser dialog. Perform any cleanup and release the reference we added |
| 349 | // in RunFileChooser(). |
| 350 | void FileSelectHelper::RunFileChooserEnd() { |
| 351 | render_view_host_ = NULL; |
| 352 | tab_contents_ = NULL; |
| 353 | Release(); |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 354 | } |
| 355 | |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 356 | void FileSelectHelper::EnumerateDirectory(int request_id, |
| 357 | RenderViewHost* render_view_host, |
| 358 | const FilePath& path) { |
| 359 | DCHECK_NE(kFileSelectEnumerationId, request_id); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 360 | |
| 361 | // Because this class returns notifications to the RenderViewHost, it is |
| 362 | // difficult for callers to know how long to keep a reference to this |
| 363 | // instance. We AddRef() here to keep the instance alive after we return |
| 364 | // to the caller, until the last callback is received from the enumeration |
| 365 | // code. At that point, we must call EnumerateDirectoryEnd(). |
| 366 | AddRef(); |
[email protected] | 600ea40 | 2011-04-12 00:01:51 | [diff] [blame] | 367 | StartNewEnumeration(path, request_id, render_view_host); |
| 368 | } |
| 369 | |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 370 | // This method is called when we receive the last callback from the enumeration |
| 371 | // code. Perform any cleanup and release the reference we added in |
| 372 | // EnumerateDirectory(). |
| 373 | void FileSelectHelper::EnumerateDirectoryEnd() { |
| 374 | Release(); |
| 375 | } |
| 376 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 377 | void FileSelectHelper::Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 378 | const content::NotificationSource& source, |
| 379 | const content::NotificationDetails& details) { |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 380 | switch (type) { |
| 381 | case content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: { |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 382 | DCHECK(content::Source<RenderWidgetHost>(source).ptr() == |
| 383 | render_view_host_); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 384 | render_view_host_ = NULL; |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | case content::NOTIFICATION_TAB_CONTENTS_DESTROYED: { |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 389 | DCHECK(content::Source<TabContents>(source).ptr() == tab_contents_); |
[email protected] | 9f054aa1 | 2011-09-29 19:13:45 | [diff] [blame] | 390 | tab_contents_ = NULL; |
| 391 | break; |
| 392 | } |
| 393 | |
| 394 | default: |
| 395 | NOTREACHED(); |
| 396 | } |
[email protected] | ba70d08 | 2010-09-10 16:54:49 | [diff] [blame] | 397 | } |
[email protected] | aaed252 | 2011-03-11 18:50:54 | [diff] [blame] | 398 | |