blob: 4e1330805875124f78972d7fdd0e17cc6d0682b3 [file] [log] [blame]
[email protected]b3841c502011-03-09 01:21:311// Copyright (c) 2011 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]600ea402011-04-12 00:01:5117#include "content/browser/renderer_host/render_process_host.h"
[email protected]5de634712011-03-02 00:20:1918#include "content/browser/renderer_host/render_view_host.h"
19#include "content/browser/renderer_host/render_widget_host_view.h"
[email protected]aaed2522011-03-11 18:50:5420#include "content/browser/tab_contents/tab_contents.h"
[email protected]d9898912011-04-15 21:10:0021#include "chrome/browser/ui/browser.h"
22#include "chrome/browser/ui/browser_list.h"
[email protected]0aed2f52011-03-23 18:06:3623#include "content/common/view_messages.h"
[email protected]6c2381d2011-10-19 02:52:5324#include "content/public/browser/notification_details.h"
25#include "content/public/browser/notification_source.h"
[email protected]0d6e9bd2011-10-18 04:29:1626#include "content/public/browser/notification_types.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]c051a1b2011-01-21 23:30:1729#include "ui/base/l10n/l10n_util.h"
[email protected]ba70d082010-09-10 16:54:4930
[email protected]600ea402011-04-12 00:01:5131namespace {
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]459fba82011-10-13 02:48:5036const int kFileSelectEnumerationId = -1;
37
38void 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]3c688fac2011-10-14 02:29:1448 base::PLATFORM_FILE_CREATE |
49 base::PLATFORM_FILE_CREATE_ALWAYS |
50 base::PLATFORM_FILE_OPEN |
[email protected]459fba82011-10-13 02:48:5051 base::PLATFORM_FILE_OPEN_ALWAYS |
[email protected]3c688fac2011-10-14 02:29:1452 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]459fba82011-10-13 02:48:5053 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]600ea402011-04-12 00:01:5162}
63
[email protected]485a5272011-04-12 00:49:2964struct FileSelectHelper::ActiveDirectoryEnumeration {
[email protected]d45f7512011-06-21 21:18:2765 ActiveDirectoryEnumeration() : rvh_(NULL) {}
[email protected]485a5272011-04-12 00:49:2966
67 scoped_ptr<DirectoryListerDispatchDelegate> delegate_;
[email protected]05a814182011-04-27 19:50:3468 scoped_ptr<net::DirectoryLister> lister_;
[email protected]485a5272011-04-12 00:49:2969 RenderViewHost* rvh_;
70 std::vector<FilePath> results_;
71};
72
[email protected]ba70d082010-09-10 16:54:4973FileSelectHelper::FileSelectHelper(Profile* profile)
74 : profile_(profile),
75 render_view_host_(NULL),
[email protected]9f054aa12011-09-29 19:13:4576 tab_contents_(NULL),
[email protected]ba70d082010-09-10 16:54:4977 select_file_dialog_(),
[email protected]9f054aa12011-09-29 19:13:4578 select_file_types_(),
[email protected]ba70d082010-09-10 16:54:4979 dialog_type_(SelectFileDialog::SELECT_OPEN_FILE) {
80}
81
82FileSelectHelper::~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]600ea402011-04-12 00:01:5188 // 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]05a814182011-04-27 19:50:3494 iter->second->lister_.reset();
[email protected]600ea402011-04-12 00:01:5195 delete iter->second;
[email protected]ba70d082010-09-10 16:54:4996 }
97}
98
99void 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]600ea402011-04-12 00:01:51107 StartNewEnumeration(path, kFileSelectEnumerationId, render_view_host_);
[email protected]ba70d082010-09-10 16:54:49108 return;
109 }
110
111 std::vector<FilePath> files;
112 files.push_back(path);
[email protected]459fba82011-10-13 02:48:50113 NotifyRenderViewHost(render_view_host_, files, dialog_type_);
[email protected]9f054aa12011-09-29 19:13:45114
[email protected]3a29a6e2011-08-24 18:26:21115 // No members should be accessed from here on.
[email protected]9f054aa12011-09-29 19:13:45116 RunFileChooserEnd();
[email protected]ba70d082010-09-10 16:54:49117}
118
119void 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]459fba82011-10-13 02:48:50126 NotifyRenderViewHost(render_view_host_, files, dialog_type_);
[email protected]9f054aa12011-09-29 19:13:45127
[email protected]3a29a6e2011-08-24 18:26:21128 // No members should be accessed from here on.
[email protected]9f054aa12011-09-29 19:13:45129 RunFileChooserEnd();
[email protected]ba70d082010-09-10 16:54:49130}
131
132void 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]459fba82011-10-13 02:48:50138 NotifyRenderViewHost(
139 render_view_host_, std::vector<FilePath>(), dialog_type_);
[email protected]ba70d082010-09-10 16:54:49140
[email protected]3a29a6e2011-08-24 18:26:21141 // No members should be accessed from here on.
[email protected]9f054aa12011-09-29 19:13:45142 RunFileChooserEnd();
[email protected]ba70d082010-09-10 16:54:49143}
144
[email protected]600ea402011-04-12 00:01:51145void 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]05a814182011-04-27 19:50:34151 entry->lister_.reset(new net::DirectoryLister(path,
152 true,
153 net::DirectoryLister::NO_SORT,
154 entry->delegate_.get()));
[email protected]600ea402011-04-12 00:01:51155 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]ba70d082010-09-10 16:54:49164}
165
166void FileSelectHelper::OnListFile(
[email protected]600ea402011-04-12 00:01:51167 int id,
[email protected]ba70d082010-09-10 16:54:49168 const net::DirectoryLister::DirectoryListerData& data) {
[email protected]600ea402011-04-12 00:01:51169 ActiveDirectoryEnumeration* entry = directory_enumerations_[id];
170
[email protected]9897e092011-02-04 22:09:11171 // Directory upload returns directories via a "." file, so that
172 // empty directories are included. This util call just checks
[email protected]ba70d082010-09-10 16:54:49173 // the flags in the structure; there's no file I/O going on.
174 if (file_util::FileEnumerator::IsDirectory(data.info))
[email protected]600ea402011-04-12 00:01:51175 entry->results_.push_back(data.path.Append(FILE_PATH_LITERAL(".")));
[email protected]9897e092011-02-04 22:09:11176 else
[email protected]600ea402011-04-12 00:01:51177 entry->results_.push_back(data.path);
[email protected]ba70d082010-09-10 16:54:49178}
179
[email protected]600ea402011-04-12 00:01:51180void 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]ba70d082010-09-10 16:54:49185 return;
[email protected]ba70d082010-09-10 16:54:49186 if (error) {
187 FileSelectionCanceled(NULL);
188 return;
189 }
[email protected]600ea402011-04-12 00:01:51190 if (id == kFileSelectEnumerationId)
[email protected]459fba82011-10-13 02:48:50191 NotifyRenderViewHost(entry->rvh_, entry->results_, dialog_type_);
[email protected]600ea402011-04-12 00:01:51192 else
193 entry->rvh_->DirectoryEnumerationFinished(id, entry->results_);
[email protected]9f054aa12011-09-29 19:13:45194
195 EnumerateDirectoryEnd();
[email protected]ba70d082010-09-10 16:54:49196}
197
198SelectFileDialog::FileTypeInfo* FileSelectHelper::GetFileTypesFromAcceptType(
[email protected]3314c2b12011-11-02 08:05:46199 const std::vector<string16>& accept_types) {
[email protected]ba70d082010-09-10 16:54:49200 if (accept_types.empty())
201 return NULL;
202
[email protected]ba70d082010-09-10 16:54:49203 // 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]3314c2b12011-11-02 08:05:46213 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]ba70d082010-09-10 16:54:49221
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]cbcd12ed2010-12-16 23:42:57240 // If no valid extension is added, bail out.
241 if (valid_type_count == 0)
242 return NULL;
243
[email protected]ba70d082010-09-10 16:54:49244 // 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
263void FileSelectHelper::RunFileChooser(
264 RenderViewHost* render_view_host,
[email protected]d9898912011-04-15 21:10:00265 TabContents* tab_contents,
[email protected]aaed2522011-03-11 18:50:54266 const ViewHostMsg_RunFileChooser_Params& params) {
[email protected]ba70d082010-09-10 16:54:49267 DCHECK(!render_view_host_);
[email protected]9f054aa12011-09-29 19:13:45268 DCHECK(!tab_contents_);
[email protected]ba70d082010-09-10 16:54:49269 render_view_host_ = render_view_host;
[email protected]9f054aa12011-09-29 19:13:45270 tab_contents_ = tab_contents;
[email protected]ba70d082010-09-10 16:54:49271 notification_registrar_.RemoveAll();
[email protected]432115822011-07-10 15:52:27272 notification_registrar_.Add(
273 this, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
[email protected]6c2381d2011-10-19 02:52:53274 content::Source<RenderWidgetHost>(render_view_host_));
[email protected]9f054aa12011-09-29 19:13:45275 notification_registrar_.Add(
276 this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
[email protected]6c2381d2011-10-19 02:52:53277 content::Source<TabContents>(tab_contents_));
[email protected]9f054aa12011-09-29 19:13:45278
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
291void 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
301void FileSelectHelper::RunFileChooserOnUIThread(
302 const ViewHostMsg_RunFileChooser_Params& params) {
303 if (!render_view_host_ || !tab_contents_)
304 return;
[email protected]ba70d082010-09-10 16:54:49305
306 if (!select_file_dialog_.get())
307 select_file_dialog_ = SelectFileDialog::Create(this);
308
309 switch (params.mode) {
[email protected]0aed2f52011-03-23 18:06:36310 case ViewHostMsg_RunFileChooser_Mode::Open:
[email protected]ba70d082010-09-10 16:54:49311 dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE;
312 break;
[email protected]0aed2f52011-03-23 18:06:36313 case ViewHostMsg_RunFileChooser_Mode::OpenMultiple:
[email protected]ba70d082010-09-10 16:54:49314 dialog_type_ = SelectFileDialog::SELECT_OPEN_MULTI_FILE;
315 break;
[email protected]0aed2f52011-03-23 18:06:36316 case ViewHostMsg_RunFileChooser_Mode::OpenFolder:
[email protected]ba70d082010-09-10 16:54:49317 dialog_type_ = SelectFileDialog::SELECT_FOLDER;
318 break;
[email protected]0aed2f52011-03-23 18:06:36319 case ViewHostMsg_RunFileChooser_Mode::Save:
[email protected]ba70d082010-09-10 16:54:49320 dialog_type_ = SelectFileDialog::SELECT_SAVEAS_FILE;
321 break;
322 default:
323 dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
324 NOTREACHED();
325 }
[email protected]ba70d082010-09-10 16:54:49326 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]d9898912011-04-15 21:10:00332
[email protected]9f054aa12011-09-29 19:13:45333 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().
350void FileSelectHelper::RunFileChooserEnd() {
351 render_view_host_ = NULL;
352 tab_contents_ = NULL;
353 Release();
[email protected]ba70d082010-09-10 16:54:49354}
355
[email protected]600ea402011-04-12 00:01:51356void FileSelectHelper::EnumerateDirectory(int request_id,
357 RenderViewHost* render_view_host,
358 const FilePath& path) {
359 DCHECK_NE(kFileSelectEnumerationId, request_id);
[email protected]9f054aa12011-09-29 19:13:45360
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]600ea402011-04-12 00:01:51367 StartNewEnumeration(path, request_id, render_view_host);
368}
369
[email protected]9f054aa12011-09-29 19:13:45370// 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().
373void FileSelectHelper::EnumerateDirectoryEnd() {
374 Release();
375}
376
[email protected]432115822011-07-10 15:52:27377void FileSelectHelper::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53378 const content::NotificationSource& source,
379 const content::NotificationDetails& details) {
[email protected]9f054aa12011-09-29 19:13:45380 switch (type) {
381 case content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
[email protected]6c2381d2011-10-19 02:52:53382 DCHECK(content::Source<RenderWidgetHost>(source).ptr() ==
383 render_view_host_);
[email protected]9f054aa12011-09-29 19:13:45384 render_view_host_ = NULL;
385 break;
386 }
387
388 case content::NOTIFICATION_TAB_CONTENTS_DESTROYED: {
[email protected]6c2381d2011-10-19 02:52:53389 DCHECK(content::Source<TabContents>(source).ptr() == tab_contents_);
[email protected]9f054aa12011-09-29 19:13:45390 tab_contents_ = NULL;
391 break;
392 }
393
394 default:
395 NOTREACHED();
396 }
[email protected]ba70d082010-09-10 16:54:49397}
[email protected]aaed2522011-03-11 18:50:54398