blob: 87c1499b8c1885556fe80badac5bfdc273466f39 [file] [log] [blame]
[email protected]5626b0892012-02-20 14:46:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ba70d082010-09-10 16:54:492// 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]5ac950b2010-12-09 21:34:257#include <string>
8
[email protected]9f054aa12011-09-29 19:13:459#include "base/bind.h"
[email protected]ba70d082010-09-10 16:54:4910#include "base/file_util.h"
[email protected]25a4c1c2013-06-08 04:53:3611#include "base/files/file_enumerator.h"
[email protected]459fba82011-10-13 02:48:5012#include "base/platform_file.h"
[email protected]ba70d082010-09-10 16:54:4913#include "base/string_util.h"
[email protected]1988e1c2013-02-28 20:27:4214#include "base/strings/string_split.h"
[email protected]112158af2013-06-07 23:46:1815#include "base/strings/utf_string_conversions.h"
[email protected]ba70d082010-09-10 16:54:4916#include "chrome/browser/platform_util.h"
[email protected]8ecad5e2010-12-02 21:18:3317#include "chrome/browser/profiles/profile.h"
[email protected]d9898912011-04-15 21:10:0018#include "chrome/browser/ui/browser.h"
19#include "chrome/browser/ui/browser_list.h"
[email protected]6e1fcd12012-07-02 17:14:2020#include "chrome/browser/ui/chrome_select_file_policy.h"
[email protected]6a1c98e02012-10-24 21:49:4321#include "content/public/browser/browser_thread.h"
[email protected]6c2381d2011-10-19 02:52:5322#include "content/public/browser/notification_details.h"
23#include "content/public/browser/notification_source.h"
[email protected]0d6e9bd2011-10-18 04:29:1624#include "content/public/browser/notification_types.h"
[email protected]9c1662b2012-03-06 15:44:3325#include "content/public/browser/render_view_host.h"
[email protected]5626b0892012-02-20 14:46:5826#include "content/public/browser/render_widget_host_view.h"
[email protected]33f8ad52012-05-22 18:10:1327#include "content/public/browser/web_contents.h"
[email protected]8caadeb2011-11-22 02:45:2328#include "content/public/common/file_chooser_params.h"
[email protected]ba70d082010-09-10 16:54:4929#include "grit/generated_resources.h"
[email protected]b3841c502011-03-09 01:21:3130#include "net/base/mime_util.h"
[email protected]c051a1b2011-01-21 23:30:1731#include "ui/base/l10n/l10n_util.h"
[email protected]4344a3c2013-01-17 23:49:2032#include "ui/shell_dialogs/selected_file_info.h"
[email protected]ba70d082010-09-10 16:54:4933
[email protected]631bb742011-11-02 11:29:3934using content::BrowserThread;
[email protected]33f8ad52012-05-22 18:10:1335using content::FileChooserParams;
[email protected]eaabba22012-03-07 15:02:1136using content::RenderViewHost;
37using content::RenderWidgetHost;
[email protected]ea049a02011-12-25 21:37:0938using content::WebContents;
[email protected]631bb742011-11-02 11:29:3939
[email protected]600ea402011-04-12 00:01:5140namespace {
41
42// There is only one file-selection happening at any given time,
43// so we allocate an enumeration ID for that purpose. All IDs from
44// the renderer must start at 0 and increase.
[email protected]459fba82011-10-13 02:48:5045const int kFileSelectEnumerationId = -1;
46
47void NotifyRenderViewHost(RenderViewHost* render_view_host,
[email protected]ddb034b2012-06-26 20:31:3948 const std::vector<ui::SelectedFileInfo>& files,
[email protected]92f54082012-07-31 01:43:1449 ui::SelectFileDialog::Type dialog_type) {
[email protected]459fba82011-10-13 02:48:5050 const int kReadFilePermissions =
51 base::PLATFORM_FILE_OPEN |
52 base::PLATFORM_FILE_READ |
53 base::PLATFORM_FILE_EXCLUSIVE_READ |
54 base::PLATFORM_FILE_ASYNC;
55
56 const int kWriteFilePermissions =
[email protected]3c688fac2011-10-14 02:29:1457 base::PLATFORM_FILE_CREATE |
58 base::PLATFORM_FILE_CREATE_ALWAYS |
59 base::PLATFORM_FILE_OPEN |
[email protected]459fba82011-10-13 02:48:5060 base::PLATFORM_FILE_OPEN_ALWAYS |
[email protected]3c688fac2011-10-14 02:29:1461 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]459fba82011-10-13 02:48:5062 base::PLATFORM_FILE_WRITE |
63 base::PLATFORM_FILE_WRITE_ATTRIBUTES |
64 base::PLATFORM_FILE_ASYNC;
65
66 int permissions = kReadFilePermissions;
[email protected]92f54082012-07-31 01:43:1467 if (dialog_type == ui::SelectFileDialog::SELECT_SAVEAS_FILE)
[email protected]459fba82011-10-13 02:48:5068 permissions = kWriteFilePermissions;
69 render_view_host->FilesSelectedInChooser(files, permissions);
70}
[email protected]fb11b6a42012-03-14 07:25:1271
[email protected]53f04c82012-07-26 02:31:0972// Converts a list of FilePaths to a list of ui::SelectedFileInfo.
73std::vector<ui::SelectedFileInfo> FilePathListToSelectedFileInfoList(
[email protected]650b2d52013-02-10 03:41:4574 const std::vector<base::FilePath>& paths) {
[email protected]ddb034b2012-06-26 20:31:3975 std::vector<ui::SelectedFileInfo> selected_files;
[email protected]fb11b6a42012-03-14 07:25:1276 for (size_t i = 0; i < paths.size(); ++i) {
77 selected_files.push_back(
[email protected]53f04c82012-07-26 02:31:0978 ui::SelectedFileInfo(paths[i], paths[i]));
[email protected]fb11b6a42012-03-14 07:25:1279 }
80 return selected_files;
[email protected]600ea402011-04-12 00:01:5181}
82
[email protected]fb11b6a42012-03-14 07:25:1283} // namespace
84
[email protected]485a5272011-04-12 00:49:2985struct FileSelectHelper::ActiveDirectoryEnumeration {
[email protected]d45f7512011-06-21 21:18:2786 ActiveDirectoryEnumeration() : rvh_(NULL) {}
[email protected]485a5272011-04-12 00:49:2987
88 scoped_ptr<DirectoryListerDispatchDelegate> delegate_;
[email protected]05a814182011-04-27 19:50:3489 scoped_ptr<net::DirectoryLister> lister_;
[email protected]485a5272011-04-12 00:49:2990 RenderViewHost* rvh_;
[email protected]650b2d52013-02-10 03:41:4591 std::vector<base::FilePath> results_;
[email protected]485a5272011-04-12 00:49:2992};
93
[email protected]ba70d082010-09-10 16:54:4994FileSelectHelper::FileSelectHelper(Profile* profile)
95 : profile_(profile),
96 render_view_host_(NULL),
[email protected]ea049a02011-12-25 21:37:0997 web_contents_(NULL),
[email protected]ba70d082010-09-10 16:54:4998 select_file_dialog_(),
[email protected]9f054aa12011-09-29 19:13:4599 select_file_types_(),
[email protected]92f54082012-07-31 01:43:14100 dialog_type_(ui::SelectFileDialog::SELECT_OPEN_FILE) {
[email protected]ba70d082010-09-10 16:54:49101}
102
103FileSelectHelper::~FileSelectHelper() {
104 // There may be pending file dialogs, we need to tell them that we've gone
105 // away so they don't try and call back to us.
106 if (select_file_dialog_.get())
107 select_file_dialog_->ListenerDestroyed();
108
[email protected]600ea402011-04-12 00:01:51109 // Stop any pending directory enumeration, prevent a callback, and free
110 // allocated memory.
111 std::map<int, ActiveDirectoryEnumeration*>::iterator iter;
112 for (iter = directory_enumerations_.begin();
113 iter != directory_enumerations_.end();
114 ++iter) {
[email protected]05a814182011-04-27 19:50:34115 iter->second->lister_.reset();
[email protected]600ea402011-04-12 00:01:51116 delete iter->second;
[email protected]ba70d082010-09-10 16:54:49117 }
118}
119
[email protected]23827ec2012-08-10 22:08:08120void FileSelectHelper::DirectoryListerDispatchDelegate::OnListFile(
121 const net::DirectoryLister::DirectoryListerData& data) {
122 parent_->OnListFile(id_, data);
123}
124
125void FileSelectHelper::DirectoryListerDispatchDelegate::OnListDone(int error) {
126 parent_->OnListDone(id_, error);
127}
128
[email protected]650b2d52013-02-10 03:41:45129void FileSelectHelper::FileSelected(const base::FilePath& path,
[email protected]ba70d082010-09-10 16:54:49130 int index, void* params) {
[email protected]53f04c82012-07-26 02:31:09131 FileSelectedWithExtraInfo(ui::SelectedFileInfo(path, path), index, params);
[email protected]fb11b6a42012-03-14 07:25:12132}
133
134void FileSelectHelper::FileSelectedWithExtraInfo(
[email protected]ddb034b2012-06-26 20:31:39135 const ui::SelectedFileInfo& file,
[email protected]fb11b6a42012-03-14 07:25:12136 int index,
137 void* params) {
[email protected]ba70d082010-09-10 16:54:49138 if (!render_view_host_)
139 return;
140
[email protected]53f04c82012-07-26 02:31:09141 profile_->set_last_selected_directory(file.file_path.DirName());
[email protected]ba70d082010-09-10 16:54:49142
[email protected]650b2d52013-02-10 03:41:45143 const base::FilePath& path = file.local_path;
[email protected]92f54082012-07-31 01:43:14144 if (dialog_type_ == ui::SelectFileDialog::SELECT_FOLDER) {
[email protected]600ea402011-04-12 00:01:51145 StartNewEnumeration(path, kFileSelectEnumerationId, render_view_host_);
[email protected]ba70d082010-09-10 16:54:49146 return;
147 }
148
[email protected]ddb034b2012-06-26 20:31:39149 std::vector<ui::SelectedFileInfo> files;
[email protected]fb11b6a42012-03-14 07:25:12150 files.push_back(file);
[email protected]459fba82011-10-13 02:48:50151 NotifyRenderViewHost(render_view_host_, files, dialog_type_);
[email protected]9f054aa12011-09-29 19:13:45152
[email protected]3a29a6e2011-08-24 18:26:21153 // No members should be accessed from here on.
[email protected]9f054aa12011-09-29 19:13:45154 RunFileChooserEnd();
[email protected]ba70d082010-09-10 16:54:49155}
156
[email protected]650b2d52013-02-10 03:41:45157void FileSelectHelper::MultiFilesSelected(
158 const std::vector<base::FilePath>& files,
159 void* params) {
[email protected]ddb034b2012-06-26 20:31:39160 std::vector<ui::SelectedFileInfo> selected_files =
[email protected]53f04c82012-07-26 02:31:09161 FilePathListToSelectedFileInfoList(files);
162
[email protected]fb11b6a42012-03-14 07:25:12163 MultiFilesSelectedWithExtraInfo(selected_files, params);
164}
165
166void FileSelectHelper::MultiFilesSelectedWithExtraInfo(
[email protected]ddb034b2012-06-26 20:31:39167 const std::vector<ui::SelectedFileInfo>& files,
[email protected]fb11b6a42012-03-14 07:25:12168 void* params) {
[email protected]ba70d082010-09-10 16:54:49169 if (!files.empty())
[email protected]53f04c82012-07-26 02:31:09170 profile_->set_last_selected_directory(files[0].file_path.DirName());
[email protected]ba70d082010-09-10 16:54:49171 if (!render_view_host_)
172 return;
173
[email protected]459fba82011-10-13 02:48:50174 NotifyRenderViewHost(render_view_host_, files, dialog_type_);
[email protected]9f054aa12011-09-29 19:13:45175
[email protected]3a29a6e2011-08-24 18:26:21176 // No members should be accessed from here on.
[email protected]9f054aa12011-09-29 19:13:45177 RunFileChooserEnd();
[email protected]ba70d082010-09-10 16:54:49178}
179
180void FileSelectHelper::FileSelectionCanceled(void* params) {
181 if (!render_view_host_)
182 return;
183
184 // If the user cancels choosing a file to upload we pass back an
185 // empty vector.
[email protected]459fba82011-10-13 02:48:50186 NotifyRenderViewHost(
[email protected]ddb034b2012-06-26 20:31:39187 render_view_host_, std::vector<ui::SelectedFileInfo>(),
[email protected]fb11b6a42012-03-14 07:25:12188 dialog_type_);
[email protected]ba70d082010-09-10 16:54:49189
[email protected]3a29a6e2011-08-24 18:26:21190 // No members should be accessed from here on.
[email protected]9f054aa12011-09-29 19:13:45191 RunFileChooserEnd();
[email protected]ba70d082010-09-10 16:54:49192}
193
[email protected]650b2d52013-02-10 03:41:45194void FileSelectHelper::StartNewEnumeration(const base::FilePath& path,
[email protected]600ea402011-04-12 00:01:51195 int request_id,
196 RenderViewHost* render_view_host) {
197 scoped_ptr<ActiveDirectoryEnumeration> entry(new ActiveDirectoryEnumeration);
198 entry->rvh_ = render_view_host;
199 entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id));
[email protected]05a814182011-04-27 19:50:34200 entry->lister_.reset(new net::DirectoryLister(path,
201 true,
202 net::DirectoryLister::NO_SORT,
203 entry->delegate_.get()));
[email protected]600ea402011-04-12 00:01:51204 if (!entry->lister_->Start()) {
205 if (request_id == kFileSelectEnumerationId)
206 FileSelectionCanceled(NULL);
207 else
208 render_view_host->DirectoryEnumerationFinished(request_id,
209 entry->results_);
210 } else {
211 directory_enumerations_[request_id] = entry.release();
212 }
[email protected]ba70d082010-09-10 16:54:49213}
214
215void FileSelectHelper::OnListFile(
[email protected]600ea402011-04-12 00:01:51216 int id,
[email protected]ba70d082010-09-10 16:54:49217 const net::DirectoryLister::DirectoryListerData& data) {
[email protected]600ea402011-04-12 00:01:51218 ActiveDirectoryEnumeration* entry = directory_enumerations_[id];
219
[email protected]9897e092011-02-04 22:09:11220 // Directory upload returns directories via a "." file, so that
221 // empty directories are included. This util call just checks
[email protected]ba70d082010-09-10 16:54:49222 // the flags in the structure; there's no file I/O going on.
[email protected]25a4c1c2013-06-08 04:53:36223 if (data.info.IsDirectory())
[email protected]600ea402011-04-12 00:01:51224 entry->results_.push_back(data.path.Append(FILE_PATH_LITERAL(".")));
[email protected]9897e092011-02-04 22:09:11225 else
[email protected]600ea402011-04-12 00:01:51226 entry->results_.push_back(data.path);
[email protected]ba70d082010-09-10 16:54:49227}
228
[email protected]600ea402011-04-12 00:01:51229void FileSelectHelper::OnListDone(int id, int error) {
230 // This entry needs to be cleaned up when this function is done.
231 scoped_ptr<ActiveDirectoryEnumeration> entry(directory_enumerations_[id]);
232 directory_enumerations_.erase(id);
233 if (!entry->rvh_)
[email protected]ba70d082010-09-10 16:54:49234 return;
[email protected]ba70d082010-09-10 16:54:49235 if (error) {
236 FileSelectionCanceled(NULL);
237 return;
238 }
[email protected]fb11b6a42012-03-14 07:25:12239
[email protected]ddb034b2012-06-26 20:31:39240 std::vector<ui::SelectedFileInfo> selected_files =
[email protected]53f04c82012-07-26 02:31:09241 FilePathListToSelectedFileInfoList(entry->results_);
[email protected]fb11b6a42012-03-14 07:25:12242
[email protected]600ea402011-04-12 00:01:51243 if (id == kFileSelectEnumerationId)
[email protected]fb11b6a42012-03-14 07:25:12244 NotifyRenderViewHost(entry->rvh_, selected_files, dialog_type_);
[email protected]600ea402011-04-12 00:01:51245 else
246 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_);
[email protected]9f054aa12011-09-29 19:13:45247
248 EnumerateDirectoryEnd();
[email protected]ba70d082010-09-10 16:54:49249}
250
[email protected]479cce782012-09-15 20:15:53251scoped_ptr<ui::SelectFileDialog::FileTypeInfo>
[email protected]92f54082012-07-31 01:43:14252FileSelectHelper::GetFileTypesFromAcceptType(
[email protected]3314c2b12011-11-02 08:05:46253 const std::vector<string16>& accept_types) {
[email protected]479cce782012-09-15 20:15:53254 scoped_ptr<ui::SelectFileDialog::FileTypeInfo> base_file_type(
255 new ui::SelectFileDialog::FileTypeInfo());
[email protected]599538b2013-02-20 14:15:14256 base_file_type->support_drive = true;
[email protected]ba70d082010-09-10 16:54:49257 if (accept_types.empty())
[email protected]479cce782012-09-15 20:15:53258 return base_file_type.Pass();
[email protected]ba70d082010-09-10 16:54:49259
[email protected]ba70d082010-09-10 16:54:49260 // Create FileTypeInfo and pre-allocate for the first extension list.
[email protected]92f54082012-07-31 01:43:14261 scoped_ptr<ui::SelectFileDialog::FileTypeInfo> file_type(
[email protected]479cce782012-09-15 20:15:53262 new ui::SelectFileDialog::FileTypeInfo(*base_file_type));
[email protected]ba70d082010-09-10 16:54:49263 file_type->include_all_files = true;
264 file_type->extensions.resize(1);
[email protected]650b2d52013-02-10 03:41:45265 std::vector<base::FilePath::StringType>* extensions =
266 &file_type->extensions.back();
[email protected]ba70d082010-09-10 16:54:49267
[email protected]f9a4c41a2012-05-30 00:05:32268 // Find the corresponding extensions.
[email protected]ba70d082010-09-10 16:54:49269 int valid_type_count = 0;
270 int description_id = 0;
[email protected]3314c2b12011-11-02 08:05:46271 for (size_t i = 0; i < accept_types.size(); ++i) {
[email protected]f9a4c41a2012-05-30 00:05:32272 std::string ascii_type = UTF16ToASCII(accept_types[i]);
273 if (!IsAcceptTypeValid(ascii_type))
274 continue;
[email protected]ba70d082010-09-10 16:54:49275
276 size_t old_extension_size = extensions->size();
[email protected]f9a4c41a2012-05-30 00:05:32277 if (ascii_type[0] == '.') {
278 // If the type starts with a period it is assumed to be a file extension
279 // so we just have to add it to the list.
[email protected]650b2d52013-02-10 03:41:45280 base::FilePath::StringType ext(ascii_type.begin(), ascii_type.end());
[email protected]f9a4c41a2012-05-30 00:05:32281 extensions->push_back(ext.substr(1));
[email protected]ba70d082010-09-10 16:54:49282 } else {
[email protected]4a66fa0e2012-09-10 06:45:20283 if (ascii_type == "image/*")
284 description_id = IDS_IMAGE_FILES;
285 else if (ascii_type == "audio/*")
286 description_id = IDS_AUDIO_FILES;
287 else if (ascii_type == "video/*")
288 description_id = IDS_VIDEO_FILES;
289
[email protected]f9a4c41a2012-05-30 00:05:32290 net::GetExtensionsForMimeType(ascii_type, extensions);
[email protected]ba70d082010-09-10 16:54:49291 }
292
293 if (extensions->size() > old_extension_size)
294 valid_type_count++;
295 }
296
[email protected]cbcd12ed2010-12-16 23:42:57297 // If no valid extension is added, bail out.
298 if (valid_type_count == 0)
[email protected]479cce782012-09-15 20:15:53299 return base_file_type.Pass();
[email protected]cbcd12ed2010-12-16 23:42:57300
[email protected]ba70d082010-09-10 16:54:49301 // Use a generic description "Custom Files" if either of the following is
302 // true:
303 // 1) There're multiple types specified, like "audio/*,video/*"
304 // 2) There're multiple extensions for a MIME type without parameter, like
305 // "ehtml,shtml,htm,html" for "text/html". On Windows, the select file
306 // dialog uses the first extension in the list to form the description,
307 // like "EHTML Files". This is not what we want.
308 if (valid_type_count > 1 ||
309 (valid_type_count == 1 && description_id == 0 && extensions->size() > 1))
310 description_id = IDS_CUSTOM_FILES;
311
312 if (description_id) {
313 file_type->extension_description_overrides.push_back(
314 l10n_util::GetStringUTF16(description_id));
315 }
316
[email protected]479cce782012-09-15 20:15:53317 return file_type.Pass();
[email protected]ba70d082010-09-10 16:54:49318}
319
[email protected]33f8ad52012-05-22 18:10:13320// static
321void FileSelectHelper::RunFileChooser(content::WebContents* tab,
322 const FileChooserParams& params) {
323 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext());
324 // FileSelectHelper will keep itself alive until it sends the result message.
325 scoped_refptr<FileSelectHelper> file_select_helper(
326 new FileSelectHelper(profile));
327 file_select_helper->RunFileChooser(tab->GetRenderViewHost(), tab, params);
328}
329
330// static
331void FileSelectHelper::EnumerateDirectory(content::WebContents* tab,
332 int request_id,
[email protected]650b2d52013-02-10 03:41:45333 const base::FilePath& path) {
[email protected]33f8ad52012-05-22 18:10:13334 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext());
335 // FileSelectHelper will keep itself alive until it sends the result message.
336 scoped_refptr<FileSelectHelper> file_select_helper(
337 new FileSelectHelper(profile));
338 file_select_helper->EnumerateDirectory(
339 request_id, tab->GetRenderViewHost(), path);
340}
341
342void FileSelectHelper::RunFileChooser(RenderViewHost* render_view_host,
343 content::WebContents* web_contents,
344 const FileChooserParams& params) {
[email protected]ba70d082010-09-10 16:54:49345 DCHECK(!render_view_host_);
[email protected]ea049a02011-12-25 21:37:09346 DCHECK(!web_contents_);
[email protected]ba70d082010-09-10 16:54:49347 render_view_host_ = render_view_host;
[email protected]ea049a02011-12-25 21:37:09348 web_contents_ = web_contents;
[email protected]ba70d082010-09-10 16:54:49349 notification_registrar_.RemoveAll();
[email protected]432115822011-07-10 15:52:27350 notification_registrar_.Add(
351 this, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
[email protected]6c2381d2011-10-19 02:52:53352 content::Source<RenderWidgetHost>(render_view_host_));
[email protected]9f054aa12011-09-29 19:13:45353 notification_registrar_.Add(
[email protected]ea049a02011-12-25 21:37:09354 this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
355 content::Source<WebContents>(web_contents_));
[email protected]9f054aa12011-09-29 19:13:45356
357 BrowserThread::PostTask(
358 BrowserThread::FILE, FROM_HERE,
359 base::Bind(&FileSelectHelper::RunFileChooserOnFileThread, this, params));
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 file dialog.
365 // At that point, we must call RunFileChooserEnd().
366 AddRef();
367}
368
369void FileSelectHelper::RunFileChooserOnFileThread(
[email protected]33f8ad52012-05-22 18:10:13370 const FileChooserParams& params) {
[email protected]479cce782012-09-15 20:15:53371 select_file_types_ = GetFileTypesFromAcceptType(params.accept_types);
[email protected]9f054aa12011-09-29 19:13:45372
373 BrowserThread::PostTask(
374 BrowserThread::UI, FROM_HERE,
375 base::Bind(&FileSelectHelper::RunFileChooserOnUIThread, this, params));
376}
377
378void FileSelectHelper::RunFileChooserOnUIThread(
[email protected]33f8ad52012-05-22 18:10:13379 const FileChooserParams& params) {
[email protected]ea049a02011-12-25 21:37:09380 if (!render_view_host_ || !web_contents_) {
[email protected]b95b08d2011-12-15 20:23:16381 // If the renderer was destroyed before we started, just cancel the
382 // operation.
383 RunFileChooserEnd();
[email protected]9f054aa12011-09-29 19:13:45384 return;
[email protected]b95b08d2011-12-15 20:23:16385 }
[email protected]ba70d082010-09-10 16:54:49386
[email protected]92f54082012-07-31 01:43:14387 select_file_dialog_ = ui::SelectFileDialog::Create(
[email protected]6e1fcd12012-07-02 17:14:20388 this, new ChromeSelectFilePolicy(web_contents_));
[email protected]ba70d082010-09-10 16:54:49389
390 switch (params.mode) {
[email protected]33f8ad52012-05-22 18:10:13391 case FileChooserParams::Open:
[email protected]92f54082012-07-31 01:43:14392 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE;
[email protected]ba70d082010-09-10 16:54:49393 break;
[email protected]33f8ad52012-05-22 18:10:13394 case FileChooserParams::OpenMultiple:
[email protected]92f54082012-07-31 01:43:14395 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE;
[email protected]ba70d082010-09-10 16:54:49396 break;
[email protected]33f8ad52012-05-22 18:10:13397 case FileChooserParams::OpenFolder:
[email protected]92f54082012-07-31 01:43:14398 dialog_type_ = ui::SelectFileDialog::SELECT_FOLDER;
[email protected]ba70d082010-09-10 16:54:49399 break;
[email protected]33f8ad52012-05-22 18:10:13400 case FileChooserParams::Save:
[email protected]92f54082012-07-31 01:43:14401 dialog_type_ = ui::SelectFileDialog::SELECT_SAVEAS_FILE;
[email protected]ba70d082010-09-10 16:54:49402 break;
403 default:
[email protected]92f54082012-07-31 01:43:14404 // Prevent warning.
405 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE;
[email protected]ba70d082010-09-10 16:54:49406 NOTREACHED();
407 }
[email protected]4e9149a2012-08-15 20:43:59408
[email protected]650b2d52013-02-10 03:41:45409 base::FilePath default_file_name = params.default_file_name.IsAbsolute() ?
[email protected]4e9149a2012-08-15 20:43:59410 params.default_file_name :
411 profile_->last_selected_directory().Append(params.default_file_name);
[email protected]ba70d082010-09-10 16:54:49412
413 gfx::NativeWindow owning_window =
[email protected]9f76c1e2012-03-05 15:15:58414 platform_util::GetTopLevel(render_view_host_->GetView()->GetNativeView());
[email protected]d9898912011-04-15 21:10:00415
[email protected]2d02a2002012-09-18 21:47:56416#if defined(OS_ANDROID)
417 // Android needs the original MIME types and an additional capture value.
418 std::vector<string16> accept_types(params.accept_types);
419 accept_types.push_back(params.capture);
420#endif
421
[email protected]9f054aa12011-09-29 19:13:45422 select_file_dialog_->SelectFile(
423 dialog_type_,
424 params.title,
425 default_file_name,
426 select_file_types_.get(),
[email protected]007b3f82013-04-09 08:46:45427 select_file_types_.get() && !select_file_types_->extensions.empty()
428 ? 1
429 : 0, // 1-based index of default extension to show.
430 base::FilePath::StringType(),
[email protected]9f054aa12011-09-29 19:13:45431 owning_window,
[email protected]b8452fa2012-06-15 01:41:41432#if defined(OS_ANDROID)
[email protected]2d02a2002012-09-18 21:47:56433 &accept_types);
[email protected]b8452fa2012-06-15 01:41:41434#else
[email protected]9f054aa12011-09-29 19:13:45435 NULL);
[email protected]b8452fa2012-06-15 01:41:41436#endif
[email protected]9f054aa12011-09-29 19:13:45437
438 select_file_types_.reset();
439}
440
441// This method is called when we receive the last callback from the file
442// chooser dialog. Perform any cleanup and release the reference we added
443// in RunFileChooser().
444void FileSelectHelper::RunFileChooserEnd() {
445 render_view_host_ = NULL;
[email protected]ea049a02011-12-25 21:37:09446 web_contents_ = NULL;
[email protected]9f054aa12011-09-29 19:13:45447 Release();
[email protected]ba70d082010-09-10 16:54:49448}
449
[email protected]600ea402011-04-12 00:01:51450void FileSelectHelper::EnumerateDirectory(int request_id,
451 RenderViewHost* render_view_host,
[email protected]650b2d52013-02-10 03:41:45452 const base::FilePath& path) {
[email protected]9f054aa12011-09-29 19:13:45453
454 // Because this class returns notifications to the RenderViewHost, it is
455 // difficult for callers to know how long to keep a reference to this
456 // instance. We AddRef() here to keep the instance alive after we return
457 // to the caller, until the last callback is received from the enumeration
458 // code. At that point, we must call EnumerateDirectoryEnd().
459 AddRef();
[email protected]600ea402011-04-12 00:01:51460 StartNewEnumeration(path, request_id, render_view_host);
461}
462
[email protected]9f054aa12011-09-29 19:13:45463// This method is called when we receive the last callback from the enumeration
464// code. Perform any cleanup and release the reference we added in
465// EnumerateDirectory().
466void FileSelectHelper::EnumerateDirectoryEnd() {
467 Release();
468}
469
[email protected]432115822011-07-10 15:52:27470void FileSelectHelper::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53471 const content::NotificationSource& source,
472 const content::NotificationDetails& details) {
[email protected]9f054aa12011-09-29 19:13:45473 switch (type) {
474 case content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
[email protected]6c2381d2011-10-19 02:52:53475 DCHECK(content::Source<RenderWidgetHost>(source).ptr() ==
476 render_view_host_);
[email protected]9f054aa12011-09-29 19:13:45477 render_view_host_ = NULL;
478 break;
479 }
480
[email protected]ea049a02011-12-25 21:37:09481 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: {
482 DCHECK(content::Source<WebContents>(source).ptr() == web_contents_);
483 web_contents_ = NULL;
[email protected]9f054aa12011-09-29 19:13:45484 break;
485 }
486
487 default:
488 NOTREACHED();
489 }
[email protected]ba70d082010-09-10 16:54:49490}
[email protected]f9a4c41a2012-05-30 00:05:32491
492// static
493bool FileSelectHelper::IsAcceptTypeValid(const std::string& accept_type) {
494 // TODO(raymes): This only does some basic checks, extend to test more cases.
495 // A 1 character accept type will always be invalid (either a "." in the case
496 // of an extension or a "/" in the case of a MIME type).
497 std::string unused;
498 if (accept_type.length() <= 1 ||
499 StringToLowerASCII(accept_type) != accept_type ||
500 TrimWhitespaceASCII(accept_type, TRIM_ALL, &unused) != TRIM_NONE) {
501 return false;
502 }
503 return true;
504}