blob: 7438a5063b83e835bc24273c2a59edca060fbd38 [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]459fba82011-10-13 02:48:5011#include "base/platform_file.h"
[email protected]ba70d082010-09-10 16:54:4912#include "base/string_split.h"
13#include "base/string_util.h"
14#include "base/utf_string_conversions.h"
[email protected]ba70d082010-09-10 16:54:4915#include "chrome/browser/platform_util.h"
[email protected]8ecad5e2010-12-02 21:18:3316#include "chrome/browser/profiles/profile.h"
[email protected]d9898912011-04-15 21:10:0017#include "chrome/browser/ui/browser.h"
18#include "chrome/browser/ui/browser_list.h"
[email protected]6e1fcd12012-07-02 17:14:2019#include "chrome/browser/ui/chrome_select_file_policy.h"
[email protected]6c2381d2011-10-19 02:52:5320#include "content/public/browser/notification_details.h"
21#include "content/public/browser/notification_source.h"
[email protected]0d6e9bd2011-10-18 04:29:1622#include "content/public/browser/notification_types.h"
[email protected]9c1662b2012-03-06 15:44:3323#include "content/public/browser/render_view_host.h"
[email protected]5626b0892012-02-20 14:46:5824#include "content/public/browser/render_widget_host_view.h"
[email protected]33f8ad52012-05-22 18:10:1325#include "content/public/browser/web_contents.h"
[email protected]8caadeb2011-11-22 02:45:2326#include "content/public/common/file_chooser_params.h"
[email protected]ba70d082010-09-10 16:54:4927#include "grit/generated_resources.h"
[email protected]b3841c502011-03-09 01:21:3128#include "net/base/mime_util.h"
[email protected]ddb034b2012-06-26 20:31:3929#include "ui/base/dialogs/selected_file_info.h"
[email protected]c051a1b2011-01-21 23:30:1730#include "ui/base/l10n/l10n_util.h"
[email protected]ba70d082010-09-10 16:54:4931
[email protected]631bb742011-11-02 11:29:3932using content::BrowserThread;
[email protected]33f8ad52012-05-22 18:10:1333using content::FileChooserParams;
[email protected]eaabba22012-03-07 15:02:1134using content::RenderViewHost;
35using content::RenderWidgetHost;
[email protected]ea049a02011-12-25 21:37:0936using content::WebContents;
[email protected]631bb742011-11-02 11:29:3937
[email protected]600ea402011-04-12 00:01:5138namespace {
39
40// There is only one file-selection happening at any given time,
41// so we allocate an enumeration ID for that purpose. All IDs from
42// the renderer must start at 0 and increase.
[email protected]459fba82011-10-13 02:48:5043const int kFileSelectEnumerationId = -1;
44
45void NotifyRenderViewHost(RenderViewHost* render_view_host,
[email protected]ddb034b2012-06-26 20:31:3946 const std::vector<ui::SelectedFileInfo>& files,
[email protected]92f54082012-07-31 01:43:1447 ui::SelectFileDialog::Type dialog_type) {
[email protected]459fba82011-10-13 02:48:5048 const int kReadFilePermissions =
49 base::PLATFORM_FILE_OPEN |
50 base::PLATFORM_FILE_READ |
51 base::PLATFORM_FILE_EXCLUSIVE_READ |
52 base::PLATFORM_FILE_ASYNC;
53
54 const int kWriteFilePermissions =
[email protected]3c688fac2011-10-14 02:29:1455 base::PLATFORM_FILE_CREATE |
56 base::PLATFORM_FILE_CREATE_ALWAYS |
57 base::PLATFORM_FILE_OPEN |
[email protected]459fba82011-10-13 02:48:5058 base::PLATFORM_FILE_OPEN_ALWAYS |
[email protected]3c688fac2011-10-14 02:29:1459 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]459fba82011-10-13 02:48:5060 base::PLATFORM_FILE_WRITE |
61 base::PLATFORM_FILE_WRITE_ATTRIBUTES |
62 base::PLATFORM_FILE_ASYNC;
63
64 int permissions = kReadFilePermissions;
[email protected]92f54082012-07-31 01:43:1465 if (dialog_type == ui::SelectFileDialog::SELECT_SAVEAS_FILE)
[email protected]459fba82011-10-13 02:48:5066 permissions = kWriteFilePermissions;
67 render_view_host->FilesSelectedInChooser(files, permissions);
68}
[email protected]fb11b6a42012-03-14 07:25:1269
[email protected]53f04c82012-07-26 02:31:0970// Converts a list of FilePaths to a list of ui::SelectedFileInfo.
71std::vector<ui::SelectedFileInfo> FilePathListToSelectedFileInfoList(
[email protected]62ce65b32012-03-20 02:15:3672 const std::vector<FilePath>& paths) {
[email protected]ddb034b2012-06-26 20:31:3973 std::vector<ui::SelectedFileInfo> selected_files;
[email protected]fb11b6a42012-03-14 07:25:1274 for (size_t i = 0; i < paths.size(); ++i) {
75 selected_files.push_back(
[email protected]53f04c82012-07-26 02:31:0976 ui::SelectedFileInfo(paths[i], paths[i]));
[email protected]fb11b6a42012-03-14 07:25:1277 }
78 return selected_files;
[email protected]600ea402011-04-12 00:01:5179}
80
[email protected]fb11b6a42012-03-14 07:25:1281} // namespace
82
[email protected]485a5272011-04-12 00:49:2983struct FileSelectHelper::ActiveDirectoryEnumeration {
[email protected]d45f7512011-06-21 21:18:2784 ActiveDirectoryEnumeration() : rvh_(NULL) {}
[email protected]485a5272011-04-12 00:49:2985
86 scoped_ptr<DirectoryListerDispatchDelegate> delegate_;
[email protected]05a814182011-04-27 19:50:3487 scoped_ptr<net::DirectoryLister> lister_;
[email protected]485a5272011-04-12 00:49:2988 RenderViewHost* rvh_;
89 std::vector<FilePath> results_;
90};
91
[email protected]ba70d082010-09-10 16:54:4992FileSelectHelper::FileSelectHelper(Profile* profile)
93 : profile_(profile),
94 render_view_host_(NULL),
[email protected]ea049a02011-12-25 21:37:0995 web_contents_(NULL),
[email protected]ba70d082010-09-10 16:54:4996 select_file_dialog_(),
[email protected]9f054aa12011-09-29 19:13:4597 select_file_types_(),
[email protected]92f54082012-07-31 01:43:1498 dialog_type_(ui::SelectFileDialog::SELECT_OPEN_FILE) {
[email protected]ba70d082010-09-10 16:54:4999}
100
101FileSelectHelper::~FileSelectHelper() {
102 // There may be pending file dialogs, we need to tell them that we've gone
103 // away so they don't try and call back to us.
104 if (select_file_dialog_.get())
105 select_file_dialog_->ListenerDestroyed();
106
[email protected]600ea402011-04-12 00:01:51107 // Stop any pending directory enumeration, prevent a callback, and free
108 // allocated memory.
109 std::map<int, ActiveDirectoryEnumeration*>::iterator iter;
110 for (iter = directory_enumerations_.begin();
111 iter != directory_enumerations_.end();
112 ++iter) {
[email protected]05a814182011-04-27 19:50:34113 iter->second->lister_.reset();
[email protected]600ea402011-04-12 00:01:51114 delete iter->second;
[email protected]ba70d082010-09-10 16:54:49115 }
116}
117
[email protected]23827ec2012-08-10 22:08:08118void FileSelectHelper::DirectoryListerDispatchDelegate::OnListFile(
119 const net::DirectoryLister::DirectoryListerData& data) {
120 parent_->OnListFile(id_, data);
121}
122
123void FileSelectHelper::DirectoryListerDispatchDelegate::OnListDone(int error) {
124 parent_->OnListDone(id_, error);
125}
126
[email protected]ba70d082010-09-10 16:54:49127void FileSelectHelper::FileSelected(const FilePath& path,
128 int index, void* params) {
[email protected]53f04c82012-07-26 02:31:09129 FileSelectedWithExtraInfo(ui::SelectedFileInfo(path, path), index, params);
[email protected]fb11b6a42012-03-14 07:25:12130}
131
132void FileSelectHelper::FileSelectedWithExtraInfo(
[email protected]ddb034b2012-06-26 20:31:39133 const ui::SelectedFileInfo& file,
[email protected]fb11b6a42012-03-14 07:25:12134 int index,
135 void* params) {
[email protected]ba70d082010-09-10 16:54:49136 if (!render_view_host_)
137 return;
138
[email protected]53f04c82012-07-26 02:31:09139 profile_->set_last_selected_directory(file.file_path.DirName());
[email protected]ba70d082010-09-10 16:54:49140
[email protected]53f04c82012-07-26 02:31:09141 const FilePath& path = file.local_path;
[email protected]92f54082012-07-31 01:43:14142 if (dialog_type_ == ui::SelectFileDialog::SELECT_FOLDER) {
[email protected]600ea402011-04-12 00:01:51143 StartNewEnumeration(path, kFileSelectEnumerationId, render_view_host_);
[email protected]ba70d082010-09-10 16:54:49144 return;
145 }
146
[email protected]ddb034b2012-06-26 20:31:39147 std::vector<ui::SelectedFileInfo> files;
[email protected]fb11b6a42012-03-14 07:25:12148 files.push_back(file);
[email protected]459fba82011-10-13 02:48:50149 NotifyRenderViewHost(render_view_host_, files, dialog_type_);
[email protected]9f054aa12011-09-29 19:13:45150
[email protected]3a29a6e2011-08-24 18:26:21151 // No members should be accessed from here on.
[email protected]9f054aa12011-09-29 19:13:45152 RunFileChooserEnd();
[email protected]ba70d082010-09-10 16:54:49153}
154
155void FileSelectHelper::MultiFilesSelected(const std::vector<FilePath>& files,
156 void* params) {
[email protected]ddb034b2012-06-26 20:31:39157 std::vector<ui::SelectedFileInfo> selected_files =
[email protected]53f04c82012-07-26 02:31:09158 FilePathListToSelectedFileInfoList(files);
159
[email protected]fb11b6a42012-03-14 07:25:12160 MultiFilesSelectedWithExtraInfo(selected_files, params);
161}
162
163void FileSelectHelper::MultiFilesSelectedWithExtraInfo(
[email protected]ddb034b2012-06-26 20:31:39164 const std::vector<ui::SelectedFileInfo>& files,
[email protected]fb11b6a42012-03-14 07:25:12165 void* params) {
[email protected]ba70d082010-09-10 16:54:49166 if (!files.empty())
[email protected]53f04c82012-07-26 02:31:09167 profile_->set_last_selected_directory(files[0].file_path.DirName());
[email protected]ba70d082010-09-10 16:54:49168 if (!render_view_host_)
169 return;
170
[email protected]459fba82011-10-13 02:48:50171 NotifyRenderViewHost(render_view_host_, files, dialog_type_);
[email protected]9f054aa12011-09-29 19:13:45172
[email protected]3a29a6e2011-08-24 18:26:21173 // No members should be accessed from here on.
[email protected]9f054aa12011-09-29 19:13:45174 RunFileChooserEnd();
[email protected]ba70d082010-09-10 16:54:49175}
176
177void FileSelectHelper::FileSelectionCanceled(void* params) {
178 if (!render_view_host_)
179 return;
180
181 // If the user cancels choosing a file to upload we pass back an
182 // empty vector.
[email protected]459fba82011-10-13 02:48:50183 NotifyRenderViewHost(
[email protected]ddb034b2012-06-26 20:31:39184 render_view_host_, std::vector<ui::SelectedFileInfo>(),
[email protected]fb11b6a42012-03-14 07:25:12185 dialog_type_);
[email protected]ba70d082010-09-10 16:54:49186
[email protected]3a29a6e2011-08-24 18:26:21187 // No members should be accessed from here on.
[email protected]9f054aa12011-09-29 19:13:45188 RunFileChooserEnd();
[email protected]ba70d082010-09-10 16:54:49189}
190
[email protected]600ea402011-04-12 00:01:51191void FileSelectHelper::StartNewEnumeration(const FilePath& path,
192 int request_id,
193 RenderViewHost* render_view_host) {
194 scoped_ptr<ActiveDirectoryEnumeration> entry(new ActiveDirectoryEnumeration);
195 entry->rvh_ = render_view_host;
196 entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id));
[email protected]05a814182011-04-27 19:50:34197 entry->lister_.reset(new net::DirectoryLister(path,
198 true,
199 net::DirectoryLister::NO_SORT,
200 entry->delegate_.get()));
[email protected]600ea402011-04-12 00:01:51201 if (!entry->lister_->Start()) {
202 if (request_id == kFileSelectEnumerationId)
203 FileSelectionCanceled(NULL);
204 else
205 render_view_host->DirectoryEnumerationFinished(request_id,
206 entry->results_);
207 } else {
208 directory_enumerations_[request_id] = entry.release();
209 }
[email protected]ba70d082010-09-10 16:54:49210}
211
212void FileSelectHelper::OnListFile(
[email protected]600ea402011-04-12 00:01:51213 int id,
[email protected]ba70d082010-09-10 16:54:49214 const net::DirectoryLister::DirectoryListerData& data) {
[email protected]600ea402011-04-12 00:01:51215 ActiveDirectoryEnumeration* entry = directory_enumerations_[id];
216
[email protected]9897e092011-02-04 22:09:11217 // Directory upload returns directories via a "." file, so that
218 // empty directories are included. This util call just checks
[email protected]ba70d082010-09-10 16:54:49219 // the flags in the structure; there's no file I/O going on.
220 if (file_util::FileEnumerator::IsDirectory(data.info))
[email protected]600ea402011-04-12 00:01:51221 entry->results_.push_back(data.path.Append(FILE_PATH_LITERAL(".")));
[email protected]9897e092011-02-04 22:09:11222 else
[email protected]600ea402011-04-12 00:01:51223 entry->results_.push_back(data.path);
[email protected]ba70d082010-09-10 16:54:49224}
225
[email protected]600ea402011-04-12 00:01:51226void FileSelectHelper::OnListDone(int id, int error) {
227 // This entry needs to be cleaned up when this function is done.
228 scoped_ptr<ActiveDirectoryEnumeration> entry(directory_enumerations_[id]);
229 directory_enumerations_.erase(id);
230 if (!entry->rvh_)
[email protected]ba70d082010-09-10 16:54:49231 return;
[email protected]ba70d082010-09-10 16:54:49232 if (error) {
233 FileSelectionCanceled(NULL);
234 return;
235 }
[email protected]fb11b6a42012-03-14 07:25:12236
[email protected]ddb034b2012-06-26 20:31:39237 std::vector<ui::SelectedFileInfo> selected_files =
[email protected]53f04c82012-07-26 02:31:09238 FilePathListToSelectedFileInfoList(entry->results_);
[email protected]fb11b6a42012-03-14 07:25:12239
[email protected]600ea402011-04-12 00:01:51240 if (id == kFileSelectEnumerationId)
[email protected]fb11b6a42012-03-14 07:25:12241 NotifyRenderViewHost(entry->rvh_, selected_files, dialog_type_);
[email protected]600ea402011-04-12 00:01:51242 else
243 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_);
[email protected]9f054aa12011-09-29 19:13:45244
245 EnumerateDirectoryEnd();
[email protected]ba70d082010-09-10 16:54:49246}
247
[email protected]92f54082012-07-31 01:43:14248ui::SelectFileDialog::FileTypeInfo*
249FileSelectHelper::GetFileTypesFromAcceptType(
[email protected]3314c2b12011-11-02 08:05:46250 const std::vector<string16>& accept_types) {
[email protected]ba70d082010-09-10 16:54:49251 if (accept_types.empty())
252 return NULL;
253
[email protected]ba70d082010-09-10 16:54:49254 // Create FileTypeInfo and pre-allocate for the first extension list.
[email protected]92f54082012-07-31 01:43:14255 scoped_ptr<ui::SelectFileDialog::FileTypeInfo> file_type(
256 new ui::SelectFileDialog::FileTypeInfo());
[email protected]ba70d082010-09-10 16:54:49257 file_type->include_all_files = true;
258 file_type->extensions.resize(1);
259 std::vector<FilePath::StringType>* extensions = &file_type->extensions.back();
260
[email protected]f9a4c41a2012-05-30 00:05:32261 // Find the corresponding extensions.
[email protected]ba70d082010-09-10 16:54:49262 int valid_type_count = 0;
263 int description_id = 0;
[email protected]3314c2b12011-11-02 08:05:46264 for (size_t i = 0; i < accept_types.size(); ++i) {
[email protected]f9a4c41a2012-05-30 00:05:32265 std::string ascii_type = UTF16ToASCII(accept_types[i]);
266 if (!IsAcceptTypeValid(ascii_type))
267 continue;
[email protected]ba70d082010-09-10 16:54:49268
269 size_t old_extension_size = extensions->size();
[email protected]f9a4c41a2012-05-30 00:05:32270 if (ascii_type[0] == '.') {
271 // If the type starts with a period it is assumed to be a file extension
272 // so we just have to add it to the list.
273 FilePath::StringType ext(ascii_type.begin(), ascii_type.end());
274 extensions->push_back(ext.substr(1));
[email protected]ba70d082010-09-10 16:54:49275 } else {
[email protected]4a66fa0e2012-09-10 06:45:20276 if (ascii_type == "image/*")
277 description_id = IDS_IMAGE_FILES;
278 else if (ascii_type == "audio/*")
279 description_id = IDS_AUDIO_FILES;
280 else if (ascii_type == "video/*")
281 description_id = IDS_VIDEO_FILES;
282
[email protected]f9a4c41a2012-05-30 00:05:32283 net::GetExtensionsForMimeType(ascii_type, extensions);
[email protected]ba70d082010-09-10 16:54:49284 }
285
286 if (extensions->size() > old_extension_size)
287 valid_type_count++;
288 }
289
[email protected]cbcd12ed2010-12-16 23:42:57290 // If no valid extension is added, bail out.
291 if (valid_type_count == 0)
292 return NULL;
293
[email protected]ba70d082010-09-10 16:54:49294 // Use a generic description "Custom Files" if either of the following is
295 // true:
296 // 1) There're multiple types specified, like "audio/*,video/*"
297 // 2) There're multiple extensions for a MIME type without parameter, like
298 // "ehtml,shtml,htm,html" for "text/html". On Windows, the select file
299 // dialog uses the first extension in the list to form the description,
300 // like "EHTML Files". This is not what we want.
301 if (valid_type_count > 1 ||
302 (valid_type_count == 1 && description_id == 0 && extensions->size() > 1))
303 description_id = IDS_CUSTOM_FILES;
304
305 if (description_id) {
306 file_type->extension_description_overrides.push_back(
307 l10n_util::GetStringUTF16(description_id));
308 }
309
310 return file_type.release();
311}
312
[email protected]33f8ad52012-05-22 18:10:13313// static
314void FileSelectHelper::RunFileChooser(content::WebContents* tab,
315 const FileChooserParams& params) {
316 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext());
317 // FileSelectHelper will keep itself alive until it sends the result message.
318 scoped_refptr<FileSelectHelper> file_select_helper(
319 new FileSelectHelper(profile));
320 file_select_helper->RunFileChooser(tab->GetRenderViewHost(), tab, params);
321}
322
323// static
324void FileSelectHelper::EnumerateDirectory(content::WebContents* tab,
325 int request_id,
326 const FilePath& path) {
327 Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext());
328 // FileSelectHelper will keep itself alive until it sends the result message.
329 scoped_refptr<FileSelectHelper> file_select_helper(
330 new FileSelectHelper(profile));
331 file_select_helper->EnumerateDirectory(
332 request_id, tab->GetRenderViewHost(), path);
333}
334
335void FileSelectHelper::RunFileChooser(RenderViewHost* render_view_host,
336 content::WebContents* web_contents,
337 const FileChooserParams& params) {
[email protected]ba70d082010-09-10 16:54:49338 DCHECK(!render_view_host_);
[email protected]ea049a02011-12-25 21:37:09339 DCHECK(!web_contents_);
[email protected]ba70d082010-09-10 16:54:49340 render_view_host_ = render_view_host;
[email protected]ea049a02011-12-25 21:37:09341 web_contents_ = web_contents;
[email protected]ba70d082010-09-10 16:54:49342 notification_registrar_.RemoveAll();
[email protected]432115822011-07-10 15:52:27343 notification_registrar_.Add(
344 this, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
[email protected]6c2381d2011-10-19 02:52:53345 content::Source<RenderWidgetHost>(render_view_host_));
[email protected]9f054aa12011-09-29 19:13:45346 notification_registrar_.Add(
[email protected]ea049a02011-12-25 21:37:09347 this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
348 content::Source<WebContents>(web_contents_));
[email protected]9f054aa12011-09-29 19:13:45349
350 BrowserThread::PostTask(
351 BrowserThread::FILE, FROM_HERE,
352 base::Bind(&FileSelectHelper::RunFileChooserOnFileThread, this, params));
353
354 // Because this class returns notifications to the RenderViewHost, it is
355 // difficult for callers to know how long to keep a reference to this
356 // instance. We AddRef() here to keep the instance alive after we return
357 // to the caller, until the last callback is received from the file dialog.
358 // At that point, we must call RunFileChooserEnd().
359 AddRef();
360}
361
362void FileSelectHelper::RunFileChooserOnFileThread(
[email protected]33f8ad52012-05-22 18:10:13363 const FileChooserParams& params) {
[email protected]9f054aa12011-09-29 19:13:45364 select_file_types_.reset(
365 GetFileTypesFromAcceptType(params.accept_types));
366
367 BrowserThread::PostTask(
368 BrowserThread::UI, FROM_HERE,
369 base::Bind(&FileSelectHelper::RunFileChooserOnUIThread, this, params));
370}
371
372void FileSelectHelper::RunFileChooserOnUIThread(
[email protected]33f8ad52012-05-22 18:10:13373 const FileChooserParams& params) {
[email protected]ea049a02011-12-25 21:37:09374 if (!render_view_host_ || !web_contents_) {
[email protected]b95b08d2011-12-15 20:23:16375 // If the renderer was destroyed before we started, just cancel the
376 // operation.
377 RunFileChooserEnd();
[email protected]9f054aa12011-09-29 19:13:45378 return;
[email protected]b95b08d2011-12-15 20:23:16379 }
[email protected]ba70d082010-09-10 16:54:49380
[email protected]92f54082012-07-31 01:43:14381 select_file_dialog_ = ui::SelectFileDialog::Create(
[email protected]6e1fcd12012-07-02 17:14:20382 this, new ChromeSelectFilePolicy(web_contents_));
[email protected]ba70d082010-09-10 16:54:49383
384 switch (params.mode) {
[email protected]33f8ad52012-05-22 18:10:13385 case FileChooserParams::Open:
[email protected]92f54082012-07-31 01:43:14386 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE;
[email protected]ba70d082010-09-10 16:54:49387 break;
[email protected]33f8ad52012-05-22 18:10:13388 case FileChooserParams::OpenMultiple:
[email protected]92f54082012-07-31 01:43:14389 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE;
[email protected]ba70d082010-09-10 16:54:49390 break;
[email protected]33f8ad52012-05-22 18:10:13391 case FileChooserParams::OpenFolder:
[email protected]92f54082012-07-31 01:43:14392 dialog_type_ = ui::SelectFileDialog::SELECT_FOLDER;
[email protected]ba70d082010-09-10 16:54:49393 break;
[email protected]33f8ad52012-05-22 18:10:13394 case FileChooserParams::Save:
[email protected]92f54082012-07-31 01:43:14395 dialog_type_ = ui::SelectFileDialog::SELECT_SAVEAS_FILE;
[email protected]ba70d082010-09-10 16:54:49396 break;
397 default:
[email protected]92f54082012-07-31 01:43:14398 // Prevent warning.
399 dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE;
[email protected]ba70d082010-09-10 16:54:49400 NOTREACHED();
401 }
[email protected]4e9149a2012-08-15 20:43:59402
403 FilePath default_file_name = params.default_file_name.IsAbsolute() ?
404 params.default_file_name :
405 profile_->last_selected_directory().Append(params.default_file_name);
[email protected]ba70d082010-09-10 16:54:49406
407 gfx::NativeWindow owning_window =
[email protected]9f76c1e2012-03-05 15:15:58408 platform_util::GetTopLevel(render_view_host_->GetView()->GetNativeView());
[email protected]d9898912011-04-15 21:10:00409
[email protected]9f054aa12011-09-29 19:13:45410 select_file_dialog_->SelectFile(
411 dialog_type_,
412 params.title,
413 default_file_name,
414 select_file_types_.get(),
415 select_file_types_.get() ? 1 : 0, // 1-based index.
416 FILE_PATH_LITERAL(""),
[email protected]9f054aa12011-09-29 19:13:45417 owning_window,
[email protected]b8452fa2012-06-15 01:41:41418#if defined(OS_ANDROID)
[email protected]c92dd3f2012-06-20 19:49:58419 const_cast<content::FileChooserParams*>(&params));
[email protected]b8452fa2012-06-15 01:41:41420#else
[email protected]9f054aa12011-09-29 19:13:45421 NULL);
[email protected]b8452fa2012-06-15 01:41:41422#endif
[email protected]9f054aa12011-09-29 19:13:45423
424 select_file_types_.reset();
425}
426
427// This method is called when we receive the last callback from the file
428// chooser dialog. Perform any cleanup and release the reference we added
429// in RunFileChooser().
430void FileSelectHelper::RunFileChooserEnd() {
431 render_view_host_ = NULL;
[email protected]ea049a02011-12-25 21:37:09432 web_contents_ = NULL;
[email protected]9f054aa12011-09-29 19:13:45433 Release();
[email protected]ba70d082010-09-10 16:54:49434}
435
[email protected]600ea402011-04-12 00:01:51436void FileSelectHelper::EnumerateDirectory(int request_id,
437 RenderViewHost* render_view_host,
438 const FilePath& path) {
[email protected]9f054aa12011-09-29 19:13:45439
440 // Because this class returns notifications to the RenderViewHost, it is
441 // difficult for callers to know how long to keep a reference to this
442 // instance. We AddRef() here to keep the instance alive after we return
443 // to the caller, until the last callback is received from the enumeration
444 // code. At that point, we must call EnumerateDirectoryEnd().
445 AddRef();
[email protected]600ea402011-04-12 00:01:51446 StartNewEnumeration(path, request_id, render_view_host);
447}
448
[email protected]9f054aa12011-09-29 19:13:45449// This method is called when we receive the last callback from the enumeration
450// code. Perform any cleanup and release the reference we added in
451// EnumerateDirectory().
452void FileSelectHelper::EnumerateDirectoryEnd() {
453 Release();
454}
455
[email protected]432115822011-07-10 15:52:27456void FileSelectHelper::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53457 const content::NotificationSource& source,
458 const content::NotificationDetails& details) {
[email protected]9f054aa12011-09-29 19:13:45459 switch (type) {
460 case content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
[email protected]6c2381d2011-10-19 02:52:53461 DCHECK(content::Source<RenderWidgetHost>(source).ptr() ==
462 render_view_host_);
[email protected]9f054aa12011-09-29 19:13:45463 render_view_host_ = NULL;
464 break;
465 }
466
[email protected]ea049a02011-12-25 21:37:09467 case content::NOTIFICATION_WEB_CONTENTS_DESTROYED: {
468 DCHECK(content::Source<WebContents>(source).ptr() == web_contents_);
469 web_contents_ = NULL;
[email protected]9f054aa12011-09-29 19:13:45470 break;
471 }
472
473 default:
474 NOTREACHED();
475 }
[email protected]ba70d082010-09-10 16:54:49476}
[email protected]f9a4c41a2012-05-30 00:05:32477
478// static
479bool FileSelectHelper::IsAcceptTypeValid(const std::string& accept_type) {
480 // TODO(raymes): This only does some basic checks, extend to test more cases.
481 // A 1 character accept type will always be invalid (either a "." in the case
482 // of an extension or a "/" in the case of a MIME type).
483 std::string unused;
484 if (accept_type.length() <= 1 ||
485 StringToLowerASCII(accept_type) != accept_type ||
486 TrimWhitespaceASCII(accept_type, TRIM_ALL, &unused) != TRIM_NONE) {
487 return false;
488 }
489 return true;
490}