2014-10-30 16:37:14 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/web_dialog_helper.h"
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
#include <string>
|
2014-10-31 09:37:32 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-07-13 08:19:30 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
|
|
|
#include "atom/browser/native_window.h"
|
2014-10-30 16:37:14 +00:00
|
|
|
#include "atom/browser/ui/file_dialog.h"
|
|
|
|
#include "base/bind.h"
|
2014-10-31 09:37:32 +00:00
|
|
|
#include "base/files/file_enumerator.h"
|
2015-07-13 05:51:18 +00:00
|
|
|
#include "base/files/file_path.h"
|
2014-10-31 11:27:01 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-07-26 08:08:29 +00:00
|
|
|
#include "chrome/common/pref_names.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "components/prefs/pref_service.h"
|
2016-09-06 08:24:37 +00:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2014-10-31 09:37:32 +00:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
2015-01-10 02:02:44 +00:00
|
|
|
#include "content/public/common/file_chooser_file_info.h"
|
2016-09-06 08:24:37 +00:00
|
|
|
#include "content/public/common/file_chooser_params.h"
|
2015-07-13 05:51:18 +00:00
|
|
|
#include "net/base/mime_util.h"
|
2014-10-31 11:27:01 +00:00
|
|
|
#include "ui/shell_dialogs/selected_file_info.h"
|
2014-10-30 16:37:14 +00:00
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
file_dialog::Filters GetFileTypesFromAcceptType(
|
|
|
|
const std::vector<base::string16>& accept_types) {
|
|
|
|
file_dialog::Filters filters;
|
|
|
|
if (accept_types.empty())
|
|
|
|
return filters;
|
|
|
|
|
|
|
|
std::vector<base::FilePath::StringType> extensions;
|
|
|
|
|
|
|
|
for (const auto& accept_type : accept_types) {
|
|
|
|
std::string ascii_type = base::UTF16ToASCII(accept_type);
|
|
|
|
if (ascii_type[0] == '.') {
|
|
|
|
// If the type starts with a period it is assumed to be a file extension,
|
|
|
|
// like `.txt`, // so we just have to add it to the list.
|
|
|
|
base::FilePath::StringType extension(
|
|
|
|
ascii_type.begin(), ascii_type.end());
|
|
|
|
// Skip the first character.
|
|
|
|
extensions.push_back(extension.substr(1));
|
|
|
|
} else {
|
2015-07-28 02:30:36 +00:00
|
|
|
// For MIME Type, `audio/*, vidio/*, image/*
|
|
|
|
net::GetExtensionsForMimeType(ascii_type, &extensions);
|
2015-07-13 05:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:30:36 +00:00
|
|
|
// If no valid exntesion is added, return empty filters.
|
|
|
|
if (extensions.empty())
|
|
|
|
return filters;
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
filters.push_back(file_dialog::Filter());
|
|
|
|
for (const auto& extension : extensions) {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
filters[0].second.push_back(base::UTF16ToASCII(extension));
|
|
|
|
#else
|
|
|
|
filters[0].second.push_back(extension);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-10-30 16:37:14 +00:00
|
|
|
namespace atom {
|
|
|
|
|
2014-10-31 09:37:32 +00:00
|
|
|
WebDialogHelper::WebDialogHelper(NativeWindow* window)
|
|
|
|
: window_(window),
|
2014-10-30 16:37:14 +00:00
|
|
|
weak_factory_(this) {
|
|
|
|
}
|
|
|
|
|
|
|
|
WebDialogHelper::~WebDialogHelper() {
|
|
|
|
}
|
|
|
|
|
2015-07-13 05:51:18 +00:00
|
|
|
|
2016-09-06 08:24:37 +00:00
|
|
|
void WebDialogHelper::RunFileChooser(
|
|
|
|
content::RenderFrameHost* render_frame_host,
|
|
|
|
const content::FileChooserParams& params) {
|
2015-01-10 02:02:44 +00:00
|
|
|
std::vector<content::FileChooserFileInfo> result;
|
2015-07-13 05:51:18 +00:00
|
|
|
file_dialog::Filters filters = GetFileTypesFromAcceptType(
|
|
|
|
params.accept_types);
|
2014-10-31 11:27:01 +00:00
|
|
|
if (params.mode == content::FileChooserParams::Save) {
|
|
|
|
base::FilePath path;
|
|
|
|
if (file_dialog::ShowSaveDialog(window_,
|
|
|
|
base::UTF16ToUTF8(params.title),
|
2016-05-15 06:01:52 +00:00
|
|
|
"",
|
2014-10-31 11:27:01 +00:00
|
|
|
params.default_file_name,
|
2015-07-13 05:51:18 +00:00
|
|
|
filters,
|
2017-02-01 15:34:21 +00:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
false,
|
2015-01-10 02:02:44 +00:00
|
|
|
&path)) {
|
|
|
|
content::FileChooserFileInfo info;
|
|
|
|
info.file_path = path;
|
|
|
|
info.display_name = path.BaseName().value();
|
|
|
|
result.push_back(info);
|
|
|
|
}
|
2014-10-31 11:27:01 +00:00
|
|
|
} else {
|
|
|
|
int flags = file_dialog::FILE_DIALOG_CREATE_DIRECTORY;
|
|
|
|
switch (params.mode) {
|
|
|
|
case content::FileChooserParams::OpenMultiple:
|
|
|
|
flags |= file_dialog::FILE_DIALOG_MULTI_SELECTIONS;
|
|
|
|
case content::FileChooserParams::Open:
|
|
|
|
flags |= file_dialog::FILE_DIALOG_OPEN_FILE;
|
|
|
|
break;
|
|
|
|
case content::FileChooserParams::UploadFolder:
|
|
|
|
flags |= file_dialog::FILE_DIALOG_OPEN_DIRECTORY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<base::FilePath> paths;
|
2015-07-13 08:19:30 +00:00
|
|
|
AtomBrowserContext* browser_context = static_cast<AtomBrowserContext*>(
|
|
|
|
window_->web_contents()->GetBrowserContext());
|
|
|
|
base::FilePath default_file_path = browser_context->prefs()->GetFilePath(
|
2015-07-26 08:08:29 +00:00
|
|
|
prefs::kSelectFileLastDirectory).Append(params.default_file_name);
|
2014-10-31 11:27:01 +00:00
|
|
|
if (file_dialog::ShowOpenDialog(window_,
|
|
|
|
base::UTF16ToUTF8(params.title),
|
2016-05-15 06:01:52 +00:00
|
|
|
"",
|
2015-07-13 08:19:30 +00:00
|
|
|
default_file_path,
|
2015-07-13 05:51:18 +00:00
|
|
|
filters,
|
2014-10-31 11:27:01 +00:00
|
|
|
flags,
|
2017-02-01 16:01:01 +00:00
|
|
|
"",
|
2015-01-13 00:53:43 +00:00
|
|
|
&paths)) {
|
2015-01-10 02:02:44 +00:00
|
|
|
for (auto& path : paths) {
|
|
|
|
content::FileChooserFileInfo info;
|
|
|
|
info.file_path = path;
|
|
|
|
info.display_name = path.BaseName().value();
|
|
|
|
result.push_back(info);
|
|
|
|
}
|
2015-07-13 08:19:30 +00:00
|
|
|
if (!paths.empty()) {
|
2015-07-26 08:08:29 +00:00
|
|
|
browser_context->prefs()->SetFilePath(prefs::kSelectFileLastDirectory,
|
2015-07-13 08:19:30 +00:00
|
|
|
paths[0].DirName());
|
|
|
|
}
|
2015-01-13 00:53:43 +00:00
|
|
|
}
|
2014-10-31 11:27:01 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 08:24:37 +00:00
|
|
|
render_frame_host->FilesSelectedInChooser(result, params.mode);
|
2014-10-30 16:37:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 09:37:32 +00:00
|
|
|
void WebDialogHelper::EnumerateDirectory(content::WebContents* web_contents,
|
|
|
|
int request_id,
|
|
|
|
const base::FilePath& dir) {
|
|
|
|
int types = base::FileEnumerator::FILES |
|
|
|
|
base::FileEnumerator::DIRECTORIES |
|
|
|
|
base::FileEnumerator::INCLUDE_DOT_DOT;
|
|
|
|
base::FileEnumerator file_enum(dir, false, types);
|
|
|
|
|
|
|
|
base::FilePath path;
|
|
|
|
std::vector<base::FilePath> paths;
|
|
|
|
while (!(path = file_enum.Next()).empty())
|
|
|
|
paths.push_back(path);
|
|
|
|
|
|
|
|
web_contents->GetRenderViewHost()->DirectoryEnumerationFinished(
|
|
|
|
request_id, paths);
|
2014-10-30 16:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|