1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/file_select_helper.h"
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/file_chooser_file_info.h"
#include "content/public/common/file_chooser_params.h"
#include "net/base/mime_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/shell_dialogs/selected_file_info.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/file_manager/fileapi_util.h"
#include "content/public/browser/site_instance.h"
#endif
using content::BrowserThread;
using content::FileChooserParams;
using content::RenderViewHost;
using content::RenderWidgetHost;
using content::WebContents;
namespace {
// There is only one file-selection happening at any given time,
// so we allocate an enumeration ID for that purpose. All IDs from
// the renderer must start at 0 and increase.
const int kFileSelectEnumerationId = -1;
// Converts a list of FilePaths to a list of ui::SelectedFileInfo.
std::vector<ui::SelectedFileInfo> FilePathListToSelectedFileInfoList(
const std::vector<base::FilePath>& paths) {
std::vector<ui::SelectedFileInfo> selected_files;
for (size_t i = 0; i < paths.size(); ++i) {
selected_files.push_back(
ui::SelectedFileInfo(paths[i], paths[i]));
}
return selected_files;
}
void DeleteFiles(const std::vector<base::FilePath>& paths) {
for (auto& file_path : paths)
base::DeleteFile(file_path, false);
}
bool IsValidProfile(Profile* profile) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return g_browser_process->profile_manager()->IsValidProfile(profile);
}
} // namespace
struct FileSelectHelper::ActiveDirectoryEnumeration {
ActiveDirectoryEnumeration() : rvh_(NULL) {}
scoped_ptr<DirectoryListerDispatchDelegate> delegate_;
scoped_ptr<net::DirectoryLister> lister_;
RenderViewHost* rvh_;
std::vector<base::FilePath> results_;
};
FileSelectHelper::FileSelectHelper(Profile* profile)
: profile_(profile),
render_view_host_(NULL),
web_contents_(NULL),
select_file_dialog_(),
select_file_types_(),
dialog_type_(ui::SelectFileDialog::SELECT_OPEN_FILE),
dialog_mode_(FileChooserParams::Open) {
}
FileSelectHelper::~FileSelectHelper() {
// There may be pending file dialogs, we need to tell them that we've gone
// away so they don't try and call back to us.
if (select_file_dialog_.get())
select_file_dialog_->ListenerDestroyed();
// Stop any pending directory enumeration, prevent a callback, and free
// allocated memory.
std::map<int, ActiveDirectoryEnumeration*>::iterator iter;
for (iter = directory_enumerations_.begin();
iter != directory_enumerations_.end();
++iter) {
iter->second->lister_.reset();
delete iter->second;
}
}
void FileSelectHelper::DirectoryListerDispatchDelegate::OnListFile(
const net::DirectoryLister::DirectoryListerData& data) {
parent_->OnListFile(id_, data);
}
void FileSelectHelper::DirectoryListerDispatchDelegate::OnListDone(int error) {
parent_->OnListDone(id_, error);
}
void FileSelectHelper::FileSelected(const base::FilePath& path,
int index, void* params) {
FileSelectedWithExtraInfo(ui::SelectedFileInfo(path, path), index, params);
}
void FileSelectHelper::FileSelectedWithExtraInfo(
const ui::SelectedFileInfo& file,
int index,
void* params) {
if (IsValidProfile(profile_))
profile_->set_last_selected_directory(file.file_path.DirName());
if (!render_view_host_) {
RunFileChooserEnd();
return;
}
const base::FilePath& path = file.local_path;
if (dialog_type_ == ui::SelectFileDialog::SELECT_UPLOAD_FOLDER) {
StartNewEnumeration(path, kFileSelectEnumerationId, render_view_host_);
return;
}
std::vector<ui::SelectedFileInfo> files;
files.push_back(file);
#if defined(OS_MACOSX) && !defined(OS_IOS)
content::BrowserThread::PostTask(
content::BrowserThread::FILE_USER_BLOCKING,
FROM_HERE,
base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files));
#else
NotifyRenderViewHostAndEnd(files);
#endif // defined(OS_MACOSX) && !defined(OS_IOS)
}
void FileSelectHelper::MultiFilesSelected(
const std::vector<base::FilePath>& files,
void* params) {
std::vector<ui::SelectedFileInfo> selected_files =
FilePathListToSelectedFileInfoList(files);
MultiFilesSelectedWithExtraInfo(selected_files, params);
}
void FileSelectHelper::MultiFilesSelectedWithExtraInfo(
const std::vector<ui::SelectedFileInfo>& files,
void* params) {
if (!files.empty() && IsValidProfile(profile_))
profile_->set_last_selected_directory(files[0].file_path.DirName());
#if defined(OS_MACOSX) && !defined(OS_IOS)
content::BrowserThread::PostTask(
content::BrowserThread::FILE_USER_BLOCKING,
FROM_HERE,
base::Bind(&FileSelectHelper::ProcessSelectedFilesMac, this, files));
#else
NotifyRenderViewHostAndEnd(files);
#endif // defined(OS_MACOSX) && !defined(OS_IOS)
}
void FileSelectHelper::FileSelectionCanceled(void* params) {
NotifyRenderViewHostAndEnd(std::vector<ui::SelectedFileInfo>());
}
void FileSelectHelper::StartNewEnumeration(const base::FilePath& path,
int request_id,
RenderViewHost* render_view_host) {
scoped_ptr<ActiveDirectoryEnumeration> entry(new ActiveDirectoryEnumeration);
entry->rvh_ = render_view_host;
entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id));
entry->lister_.reset(new net::DirectoryLister(
path, net::DirectoryLister::NO_SORT_RECURSIVE, entry->delegate_.get()));
if (!entry->lister_->Start()) {
if (request_id == kFileSelectEnumerationId)
FileSelectionCanceled(NULL);
else
render_view_host->DirectoryEnumerationFinished(request_id,
entry->results_);
} else {
directory_enumerations_[request_id] = entry.release();
}
}
void FileSelectHelper::OnListFile(
int id,
const net::DirectoryLister::DirectoryListerData& data) {
ActiveDirectoryEnumeration* entry = directory_enumerations_[id];
// Directory upload only cares about files.
if (data.info.IsDirectory())
return;
entry->results_.push_back(data.path);
}
void FileSelectHelper::OnListDone(int id, int error) {
// This entry needs to be cleaned up when this function is done.
scoped_ptr<ActiveDirectoryEnumeration> entry(directory_enumerations_[id]);
directory_enumerations_.erase(id);
if (!entry->rvh_)
return;
if (error) {
FileSelectionCanceled(NULL);
return;
}
std::vector<ui::SelectedFileInfo> selected_files =
FilePathListToSelectedFileInfoList(entry->results_);
if (id == kFileSelectEnumerationId) {
NotifyRenderViewHostAndEnd(selected_files);
} else {
entry->rvh_->DirectoryEnumerationFinished(id, entry->results_);
EnumerateDirectoryEnd();
}
}
void FileSelectHelper::NotifyRenderViewHostAndEnd(
const std::vector<ui::SelectedFileInfo>& files) {
if (!render_view_host_) {
RunFileChooserEnd();
return;
}
#if defined(OS_CHROMEOS)
if (!files.empty()) {
if (!IsValidProfile(profile_)) {
RunFileChooserEnd();
return;
}
// Converts |files| into FileChooserFileInfo with handling of non-native
// files.
storage::FileSystemContext* file_system_context =
content::BrowserContext::GetStoragePartition(
profile_, render_view_host_->GetSiteInstance())->
GetFileSystemContext();
file_manager::util::ConvertSelectedFileInfoListToFileChooserFileInfoList(
file_system_context,
web_contents_->GetSiteInstance()->GetSiteURL(),
files,
base::Bind(
&FileSelectHelper::NotifyRenderViewHostAndEndAfterConversion,
this));
return;
}
#endif // defined(OS_CHROMEOS)
std::vector<content::FileChooserFileInfo> chooser_files;
for (const auto& file : files) {
content::FileChooserFileInfo chooser_file;
chooser_file.file_path = file.local_path;
chooser_file.display_name = file.display_name;
chooser_files.push_back(chooser_file);
}
NotifyRenderViewHostAndEndAfterConversion(chooser_files);
}
void FileSelectHelper::NotifyRenderViewHostAndEndAfterConversion(
const std::vector<content::FileChooserFileInfo>& list) {
if (render_view_host_)
render_view_host_->FilesSelectedInChooser(list, dialog_mode_);
// No members should be accessed from here on.
RunFileChooserEnd();
}
void FileSelectHelper::DeleteTemporaryFiles() {
BrowserThread::PostTask(BrowserThread::FILE,
FROM_HERE,
base::Bind(&DeleteFiles, temporary_files_));
temporary_files_.clear();
}
void FileSelectHelper::CleanUpOnRenderViewHostChange() {
if (!temporary_files_.empty()) {
DeleteTemporaryFiles();
// Now that the temporary files have been scheduled for deletion, there
// is no longer any reason to keep this instance around.
Release();
}
}
scoped_ptr<ui::SelectFileDialog::FileTypeInfo>
FileSelectHelper::GetFileTypesFromAcceptType(
const std::vector<base::string16>& accept_types) {
scoped_ptr<ui::SelectFileDialog::FileTypeInfo> base_file_type(
new ui::SelectFileDialog::FileTypeInfo());
if (accept_types.empty())
return base_file_type.Pass();
// Create FileTypeInfo and pre-allocate for the first extension list.
scoped_ptr<ui::SelectFileDialog::FileTypeInfo> file_type(
new ui::SelectFileDialog::FileTypeInfo(*base_file_type));
file_type->include_all_files = true;
file_type->extensions.resize(1);
std::vector<base::FilePath::StringType>* extensions =
&file_type->extensions.back();
// Find the corresponding extensions.
int valid_type_count = 0;
int description_id = 0;
for (size_t i = 0; i < accept_types.size(); ++i) {
std::string ascii_type = base::UTF16ToASCII(accept_types[i]);
if (!IsAcceptTypeValid(ascii_type))
continue;
size_t old_extension_size = extensions->size();
if (ascii_type[0] == '.') {
// If the type starts with a period it is assumed to be a file extension
// so we just have to add it to the list.
base::FilePath::StringType ext(ascii_type.begin(), ascii_type.end());
extensions->push_back(ext.substr(1));
} else {
if (ascii_type == "image/*")
description_id = IDS_IMAGE_FILES;
else if (ascii_type == "audio/*")
description_id = IDS_AUDIO_FILES;
else if (ascii_type == "video/*")
description_id = IDS_VIDEO_FILES;
net::GetExtensionsForMimeType(ascii_type, extensions);
}
if (extensions->size() > old_extension_size)
valid_type_count++;
}
// If no valid extension is added, bail out.
if (valid_type_count == 0)
return base_file_type.Pass();
// Use a generic description "Custom Files" if either of the following is
// true:
// 1) There're multiple types specified, like "audio/*,video/*"
// 2) There're multiple extensions for a MIME type without parameter, like
// "ehtml,shtml,htm,html" for "text/html". On Windows, the select file
// dialog uses the first extension in the list to form the description,
// like "EHTML Files". This is not what we want.
if (valid_type_count > 1 ||
(valid_type_count == 1 && description_id == 0 && extensions->size() > 1))
description_id = IDS_CUSTOM_FILES;
if (description_id) {
file_type->extension_description_overrides.push_back(
l10n_util::GetStringUTF16(description_id));
}
return file_type.Pass();
}
// static
void FileSelectHelper::RunFileChooser(content::WebContents* tab,
const FileChooserParams& params) {
Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext());
// FileSelectHelper will keep itself alive until it sends the result message.
scoped_refptr<FileSelectHelper> file_select_helper(
new FileSelectHelper(profile));
file_select_helper->RunFileChooser(tab->GetRenderViewHost(), tab, params);
}
// static
void FileSelectHelper::EnumerateDirectory(content::WebContents* tab,
int request_id,
const base::FilePath& path) {
Profile* profile = Profile::FromBrowserContext(tab->GetBrowserContext());
// FileSelectHelper will keep itself alive until it sends the result message.
scoped_refptr<FileSelectHelper> file_select_helper(
new FileSelectHelper(profile));
file_select_helper->EnumerateDirectory(
request_id, tab->GetRenderViewHost(), path);
}
void FileSelectHelper::RunFileChooser(RenderViewHost* render_view_host,
content::WebContents* web_contents,
const FileChooserParams& params) {
DCHECK(!render_view_host_);
DCHECK(!web_contents_);
render_view_host_ = render_view_host;
web_contents_ = web_contents;
notification_registrar_.RemoveAll();
content::WebContentsObserver::Observe(web_contents_);
notification_registrar_.Add(
this,
content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
content::Source<RenderWidgetHost>(render_view_host_));
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&FileSelectHelper::RunFileChooserOnFileThread, this, params));
// Because this class returns notifications to the RenderViewHost, it is
// difficult for callers to know how long to keep a reference to this
// instance. We AddRef() here to keep the instance alive after we return
// to the caller, until the last callback is received from the file dialog.
// At that point, we must call RunFileChooserEnd().
AddRef();
}
void FileSelectHelper::RunFileChooserOnFileThread(
const FileChooserParams& params) {
select_file_types_ = GetFileTypesFromAcceptType(params.accept_types);
select_file_types_->support_drive = !params.need_local_path;
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&FileSelectHelper::RunFileChooserOnUIThread, this, params));
}
void FileSelectHelper::RunFileChooserOnUIThread(
const FileChooserParams& params) {
if (!render_view_host_ || !web_contents_ || !IsValidProfile(profile_)) {
// If the renderer was destroyed before we started, just cancel the
// operation.
RunFileChooserEnd();
return;
}
select_file_dialog_ = ui::SelectFileDialog::Create(
this, new ChromeSelectFilePolicy(web_contents_));
if (!select_file_dialog_.get())
return;
dialog_mode_ = params.mode;
switch (params.mode) {
case FileChooserParams::Open:
dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE;
break;
case FileChooserParams::OpenMultiple:
dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE;
break;
case FileChooserParams::UploadFolder:
dialog_type_ = ui::SelectFileDialog::SELECT_UPLOAD_FOLDER;
break;
case FileChooserParams::Save:
dialog_type_ = ui::SelectFileDialog::SELECT_SAVEAS_FILE;
break;
default:
// Prevent warning.
dialog_type_ = ui::SelectFileDialog::SELECT_OPEN_FILE;
NOTREACHED();
}
base::FilePath default_file_name = params.default_file_name.IsAbsolute() ?
params.default_file_name :
profile_->last_selected_directory().Append(params.default_file_name);
gfx::NativeWindow owning_window =
platform_util::GetTopLevel(render_view_host_->GetView()->GetNativeView());
#if defined(OS_ANDROID)
// Android needs the original MIME types and an additional capture value.
std::pair<std::vector<base::string16>, bool> accept_types =
std::make_pair(params.accept_types, params.capture);
#endif
select_file_dialog_->SelectFile(
dialog_type_,
params.title,
default_file_name,
select_file_types_.get(),
select_file_types_.get() && !select_file_types_->extensions.empty()
? 1
: 0, // 1-based index of default extension to show.
base::FilePath::StringType(),
owning_window,
#if defined(OS_ANDROID)
&accept_types);
#else
NULL);
#endif
select_file_types_.reset();
}
// This method is called when we receive the last callback from the file
// chooser dialog. Perform any cleanup and release the reference we added
// in RunFileChooser().
void FileSelectHelper::RunFileChooserEnd() {
// If there are temporary files, then this instance needs to stick around
// until web_contents_ is destroyed, so that this instance can delete the
// temporary files.
if (!temporary_files_.empty())
return;
render_view_host_ = NULL;
web_contents_ = NULL;
Release();
}
void FileSelectHelper::EnumerateDirectory(int request_id,
RenderViewHost* render_view_host,
const base::FilePath& path) {
// Because this class returns notifications to the RenderViewHost, it is
// difficult for callers to know how long to keep a reference to this
// instance. We AddRef() here to keep the instance alive after we return
// to the caller, until the last callback is received from the enumeration
// code. At that point, we must call EnumerateDirectoryEnd().
AddRef();
StartNewEnumeration(path, request_id, render_view_host);
}
// This method is called when we receive the last callback from the enumeration
// code. Perform any cleanup and release the reference we added in
// EnumerateDirectory().
void FileSelectHelper::EnumerateDirectoryEnd() {
Release();
}
void FileSelectHelper::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
DCHECK(content::Source<RenderWidgetHost>(source).ptr() ==
render_view_host_);
render_view_host_ = NULL;
break;
}
default:
NOTREACHED();
}
}
void FileSelectHelper::RenderViewHostChanged(RenderViewHost* old_host,
RenderViewHost* new_host) {
CleanUpOnRenderViewHostChange();
}
void FileSelectHelper::WebContentsDestroyed() {
web_contents_ = nullptr;
CleanUpOnRenderViewHostChange();
}
// static
bool FileSelectHelper::IsAcceptTypeValid(const std::string& accept_type) {
// TODO(raymes): This only does some basic checks, extend to test more cases.
// A 1 character accept type will always be invalid (either a "." in the case
// of an extension or a "/" in the case of a MIME type).
std::string unused;
if (accept_type.length() <= 1 ||
base::StringToLowerASCII(accept_type) != accept_type ||
base::TrimWhitespaceASCII(accept_type, base::TRIM_ALL, &unused) !=
base::TRIM_NONE) {
return false;
}
return true;
}
|