mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
- Provide default implementations of the file chooser dialogs (open, open multiple, save) on all platforms (issue #761).
- Add a new CefBrowserHost::RunFileDialog method that displays the specified file chooser dialog and returns the results asynchronously (issue #761). - Add a new CefDialogHandler::OnFileDialog callback that allows the application to provide custom UI for file chooser dialogs (issue #761). git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@862 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
@ -13,6 +13,7 @@
|
||||
|
||||
#include "libcef/browser/thread_util.h"
|
||||
|
||||
#include "base/i18n/case_conversion.h"
|
||||
#include "base/string_util.h"
|
||||
#include "base/utf_string_conversions.h"
|
||||
#include "base/win/registry.h"
|
||||
@ -20,6 +21,10 @@
|
||||
#include "content/public/browser/native_web_keyboard_event.h"
|
||||
#include "content/public/browser/web_contents_view.h"
|
||||
#include "content/public/common/file_chooser_params.h"
|
||||
#include "grit/cef_strings.h"
|
||||
#include "grit/ui_strings.h"
|
||||
#include "net/base/mime_util.h"
|
||||
#include "ui/base/l10n/l10n_util.h"
|
||||
#include "ui/base/win/hwnd_util.h"
|
||||
|
||||
#pragma comment(lib, "dwmapi.lib")
|
||||
@ -52,10 +57,171 @@ void WriteTextToFile(const std::string& data, const std::wstring& file_path) {
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
// From ui/base/dialogs/select_file_dialog_win.cc.
|
||||
|
||||
// Get the file type description from the registry. This will be "Text Document"
|
||||
// for .txt files, "JPEG Image" for .jpg files, etc. If the registry doesn't
|
||||
// have an entry for the file type, we return false, true if the description was
|
||||
// found. 'file_ext' must be in form ".txt".
|
||||
static bool GetRegistryDescriptionFromExtension(const std::wstring& file_ext,
|
||||
std::wstring* reg_description) {
|
||||
DCHECK(reg_description);
|
||||
base::win::RegKey reg_ext(HKEY_CLASSES_ROOT, file_ext.c_str(), KEY_READ);
|
||||
std::wstring reg_app;
|
||||
if (reg_ext.ReadValue(NULL, ®_app) == ERROR_SUCCESS && !reg_app.empty()) {
|
||||
base::win::RegKey reg_link(HKEY_CLASSES_ROOT, reg_app.c_str(), KEY_READ);
|
||||
if (reg_link.ReadValue(NULL, reg_description) == ERROR_SUCCESS)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set up a filter for a Save/Open dialog, which will consist of |file_ext| file
|
||||
// extensions (internally separated by semicolons), |ext_desc| as the text
|
||||
// descriptions of the |file_ext| types (optional), and (optionally) the default
|
||||
// 'All Files' view. The purpose of the filter is to show only files of a
|
||||
// particular type in a Windows Save/Open dialog box. The resulting filter is
|
||||
// returned. The filters created here are:
|
||||
// 1. only files that have 'file_ext' as their extension
|
||||
// 2. all files (only added if 'include_all_files' is true)
|
||||
// Example:
|
||||
// file_ext: { "*.txt", "*.htm;*.html" }
|
||||
// ext_desc: { "Text Document" }
|
||||
// returned: "Text Document\0*.txt\0HTML Document\0*.htm;*.html\0"
|
||||
// "All Files\0*.*\0\0" (in one big string)
|
||||
// If a description is not provided for a file extension, it will be retrieved
|
||||
// from the registry. If the file extension does not exist in the registry, it
|
||||
// will be omitted from the filter, as it is likely a bogus extension.
|
||||
std::wstring FormatFilterForExtensions(
|
||||
const std::vector<std::wstring>& file_ext,
|
||||
const std::vector<std::wstring>& ext_desc,
|
||||
bool include_all_files) {
|
||||
const std::wstring all_ext = L"*.*";
|
||||
const std::wstring all_desc =
|
||||
l10n_util::GetStringUTF16(IDS_APP_SAVEAS_ALL_FILES) +
|
||||
L" (" + all_ext + L")";
|
||||
|
||||
DCHECK(file_ext.size() >= ext_desc.size());
|
||||
|
||||
if (file_ext.empty())
|
||||
include_all_files = true;
|
||||
|
||||
std::wstring result;
|
||||
|
||||
for (size_t i = 0; i < file_ext.size(); ++i) {
|
||||
std::wstring ext = file_ext[i];
|
||||
std::wstring desc;
|
||||
if (i < ext_desc.size())
|
||||
desc = ext_desc[i];
|
||||
|
||||
if (ext.empty()) {
|
||||
// Force something reasonable to appear in the dialog box if there is no
|
||||
// extension provided.
|
||||
include_all_files = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (desc.empty()) {
|
||||
DCHECK(ext.find(L'.') != std::wstring::npos);
|
||||
std::wstring first_extension = ext.substr(ext.find(L'.'));
|
||||
size_t first_separator_index = first_extension.find(L';');
|
||||
if (first_separator_index != std::wstring::npos)
|
||||
first_extension = first_extension.substr(0, first_separator_index);
|
||||
|
||||
// Find the extension name without the preceeding '.' character.
|
||||
std::wstring ext_name = first_extension;
|
||||
size_t ext_index = ext_name.find_first_not_of(L'.');
|
||||
if (ext_index != std::wstring::npos)
|
||||
ext_name = ext_name.substr(ext_index);
|
||||
|
||||
if (!GetRegistryDescriptionFromExtension(first_extension, &desc)) {
|
||||
// The extension doesn't exist in the registry. Create a description
|
||||
// based on the unknown extension type (i.e. if the extension is .qqq,
|
||||
// the we create a description "QQQ File (.qqq)").
|
||||
include_all_files = true;
|
||||
desc = l10n_util::GetStringFUTF16(
|
||||
IDS_APP_SAVEAS_EXTENSION_FORMAT,
|
||||
base::i18n::ToUpper(WideToUTF16(ext_name)),
|
||||
ext_name);
|
||||
}
|
||||
}
|
||||
|
||||
if (!desc.empty())
|
||||
desc += L" (" + ext + L")";
|
||||
else
|
||||
desc = ext;
|
||||
|
||||
result.append(desc.c_str(), desc.size() + 1); // Append NULL too.
|
||||
result.append(ext.c_str(), ext.size() + 1);
|
||||
}
|
||||
|
||||
if (include_all_files) {
|
||||
result.append(all_desc.c_str(), all_desc.size() + 1);
|
||||
result.append(all_ext.c_str(), all_ext.size() + 1);
|
||||
}
|
||||
|
||||
result.append(1, '\0'); // Double NULL required.
|
||||
return result;
|
||||
}
|
||||
|
||||
std::wstring GetDescriptionFromMimeType(const std::string& mime_type) {
|
||||
// Check for wild card mime types and return an appropriate description.
|
||||
static const struct {
|
||||
const char* mime_type;
|
||||
int string_id;
|
||||
} kWildCardMimeTypes[] = {
|
||||
{ "audio", IDS_APP_AUDIO_FILES },
|
||||
{ "image", IDS_APP_IMAGE_FILES },
|
||||
{ "text", IDS_APP_TEXT_FILES },
|
||||
{ "video", IDS_APP_VIDEO_FILES },
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < arraysize(kWildCardMimeTypes); ++i) {
|
||||
if (mime_type == std::string(kWildCardMimeTypes[i].mime_type) + "/*")
|
||||
return l10n_util::GetStringUTF16(kWildCardMimeTypes[i].string_id);
|
||||
}
|
||||
|
||||
return std::wstring();
|
||||
}
|
||||
|
||||
std::wstring GetFilterStringFromAcceptTypes(
|
||||
const std::vector<string16>& accept_types) {
|
||||
std::vector<std::wstring> extensions;
|
||||
std::vector<std::wstring> descriptions;
|
||||
|
||||
for (size_t i = 0; i < accept_types.size(); ++i) {
|
||||
std::string ascii_type = UTF16ToASCII(accept_types[i]);
|
||||
if (ascii_type.length()) {
|
||||
// Just treat as extension if contains '.' as the first character.
|
||||
if (ascii_type[0] == '.') {
|
||||
extensions.push_back(L"*" + ASCIIToWide(ascii_type));
|
||||
descriptions.push_back(std::wstring());
|
||||
} else {
|
||||
// Otherwise convert mime type to one or more extensions.
|
||||
std::vector<FilePath::StringType> ext;
|
||||
std::wstring ext_str;
|
||||
net::GetExtensionsForMimeType(ascii_type, &ext);
|
||||
if (ext.size() > 0) {
|
||||
for (size_t x = 0; x < ext.size(); ++x) {
|
||||
if (x != 0)
|
||||
ext_str += L";";
|
||||
ext_str += L"*." + ext[x];
|
||||
}
|
||||
extensions.push_back(ext_str);
|
||||
descriptions.push_back(GetDescriptionFromMimeType(ascii_type));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return FormatFilterForExtensions(extensions, descriptions, true);
|
||||
}
|
||||
|
||||
// from chrome/browser/views/shell_dialogs_win.cc
|
||||
|
||||
bool RunOpenFileDialog(const std::wstring& filter, HWND owner, FilePath* path) {
|
||||
bool RunOpenFileDialog(const content::FileChooserParams& params,
|
||||
HWND owner,
|
||||
FilePath* path) {
|
||||
OPENFILENAME ofn;
|
||||
|
||||
// We must do this otherwise the ofn's FlagsEx may be initialized to random
|
||||
@ -64,26 +230,48 @@ bool RunOpenFileDialog(const std::wstring& filter, HWND owner, FilePath* path) {
|
||||
ofn.lStructSize = sizeof(ofn);
|
||||
ofn.hwndOwner = owner;
|
||||
|
||||
wchar_t filename[MAX_PATH];
|
||||
base::wcslcpy(filename, path->value().c_str(), arraysize(filename));
|
||||
// Consider default file name if any.
|
||||
FilePath default_file_name(params.default_file_name);
|
||||
|
||||
wchar_t filename[MAX_PATH] = {0};
|
||||
|
||||
ofn.lpstrFile = filename;
|
||||
ofn.nMaxFile = MAX_PATH;
|
||||
|
||||
std::wstring directory;
|
||||
if (!default_file_name.empty()) {
|
||||
base::wcslcpy(filename, default_file_name.value().c_str(),
|
||||
arraysize(filename));
|
||||
|
||||
directory = default_file_name.DirName().value();
|
||||
ofn.lpstrInitialDir = directory.c_str();
|
||||
}
|
||||
|
||||
std::wstring title;
|
||||
if (!params.title.empty())
|
||||
title = params.title;
|
||||
else
|
||||
title = l10n_util::GetStringUTF16(IDS_OPEN_FILE_DIALOG_TITLE);
|
||||
if (!title.empty())
|
||||
ofn.lpstrTitle = title.c_str();
|
||||
|
||||
// We use OFN_NOCHANGEDIR so that the user can rename or delete the directory
|
||||
// without having to close Chrome first.
|
||||
ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
|
||||
ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_EXPLORER |
|
||||
OFN_ENABLESIZING;
|
||||
|
||||
if (!filter.empty()) {
|
||||
std::wstring filter = GetFilterStringFromAcceptTypes(params.accept_types);
|
||||
if (!filter.empty())
|
||||
ofn.lpstrFilter = filter.c_str();
|
||||
}
|
||||
|
||||
bool success = !!GetOpenFileName(&ofn);
|
||||
if (success)
|
||||
*path = FilePath(filename);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool RunOpenMultiFileDialog(const std::wstring& filter, HWND owner,
|
||||
bool RunOpenMultiFileDialog(const content::FileChooserParams& params,
|
||||
HWND owner,
|
||||
std::vector<FilePath>* paths) {
|
||||
OPENFILENAME ofn;
|
||||
|
||||
@ -99,14 +287,23 @@ bool RunOpenMultiFileDialog(const std::wstring& filter, HWND owner,
|
||||
ofn.lpstrFile = filename.get();
|
||||
ofn.nMaxFile = UNICODE_STRING_MAX_CHARS;
|
||||
|
||||
std::wstring title;
|
||||
if (!params.title.empty())
|
||||
title = params.title;
|
||||
else
|
||||
title = l10n_util::GetStringUTF16(IDS_OPEN_FILES_DIALOG_TITLE);
|
||||
if (!title.empty())
|
||||
ofn.lpstrTitle = title.c_str();
|
||||
|
||||
// We use OFN_NOCHANGEDIR so that the user can rename or delete the directory
|
||||
// without having to close Chrome first.
|
||||
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER
|
||||
| OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT;
|
||||
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER |
|
||||
OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT | OFN_ENABLESIZING;
|
||||
|
||||
if (!filter.empty()) {
|
||||
std::wstring filter = GetFilterStringFromAcceptTypes(params.accept_types);
|
||||
if (!filter.empty())
|
||||
ofn.lpstrFilter = filter.c_str();
|
||||
}
|
||||
|
||||
bool success = !!GetOpenFileName(&ofn);
|
||||
|
||||
if (success) {
|
||||
@ -135,6 +332,57 @@ bool RunOpenMultiFileDialog(const std::wstring& filter, HWND owner,
|
||||
return success;
|
||||
}
|
||||
|
||||
bool RunSaveFileDialog(const content::FileChooserParams& params,
|
||||
HWND owner,
|
||||
FilePath* path) {
|
||||
OPENFILENAME ofn;
|
||||
|
||||
// We must do this otherwise the ofn's FlagsEx may be initialized to random
|
||||
// junk in release builds which can cause the Places Bar not to show up!
|
||||
ZeroMemory(&ofn, sizeof(ofn));
|
||||
ofn.lStructSize = sizeof(ofn);
|
||||
ofn.hwndOwner = owner;
|
||||
|
||||
// Consider default file name if any.
|
||||
FilePath default_file_name(params.default_file_name);
|
||||
|
||||
wchar_t filename[MAX_PATH] = {0};
|
||||
|
||||
ofn.lpstrFile = filename;
|
||||
ofn.nMaxFile = MAX_PATH;
|
||||
|
||||
std::wstring directory;
|
||||
if (!default_file_name.empty()) {
|
||||
base::wcslcpy(filename, default_file_name.value().c_str(),
|
||||
arraysize(filename));
|
||||
|
||||
directory = default_file_name.DirName().value();
|
||||
ofn.lpstrInitialDir = directory.c_str();
|
||||
}
|
||||
|
||||
std::wstring title;
|
||||
if (!params.title.empty())
|
||||
title = params.title;
|
||||
else
|
||||
title = l10n_util::GetStringUTF16(IDS_SAVE_AS_DIALOG_TITLE);
|
||||
if (!title.empty())
|
||||
ofn.lpstrTitle = title.c_str();
|
||||
|
||||
// We use OFN_NOCHANGEDIR so that the user can rename or delete the directory
|
||||
// without having to close Chrome first.
|
||||
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING |
|
||||
OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST;
|
||||
|
||||
std::wstring filter = GetFilterStringFromAcceptTypes(params.accept_types);
|
||||
if (!filter.empty())
|
||||
ofn.lpstrFilter = filter.c_str();
|
||||
|
||||
bool success = !!GetSaveFileName(&ofn);
|
||||
if (success)
|
||||
*path = FilePath(filename);
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
// According to Mozilla in uriloader/exthandler/win/nsOSHelperAppService.cpp:
|
||||
// "Some versions of windows (Win2k before SP3, Win XP before SP1) crash in
|
||||
@ -374,16 +622,25 @@ void CefBrowserHostImpl::PlatformHandleKeyboardEvent(
|
||||
}
|
||||
|
||||
void CefBrowserHostImpl::PlatformRunFileChooser(
|
||||
content::WebContents* contents,
|
||||
const content::FileChooserParams& params,
|
||||
std::vector<FilePath>& files) {
|
||||
if (params.mode == content::FileChooserParams::OpenMultiple) {
|
||||
RunOpenMultiFileDialog(L"", PlatformGetWindowHandle(), &files);
|
||||
RunFileChooserCallback callback) {
|
||||
std::vector<FilePath> files;
|
||||
|
||||
if (params.mode == content::FileChooserParams::Open) {
|
||||
FilePath file;
|
||||
if (RunOpenFileDialog(params, PlatformGetWindowHandle(), &file))
|
||||
files.push_back(file);
|
||||
} else if (params.mode == content::FileChooserParams::OpenMultiple) {
|
||||
RunOpenMultiFileDialog(params, PlatformGetWindowHandle(), &files);
|
||||
} else if (params.mode == content::FileChooserParams::Save) {
|
||||
FilePath file;
|
||||
if (RunSaveFileDialog(params, PlatformGetWindowHandle(), &file))
|
||||
files.push_back(file);
|
||||
} else {
|
||||
FilePath file_name;
|
||||
if (RunOpenFileDialog(L"", PlatformGetWindowHandle(), &file_name))
|
||||
files.push_back(file_name);
|
||||
NOTIMPLEMENTED();
|
||||
}
|
||||
|
||||
callback.Run(files);
|
||||
}
|
||||
|
||||
void CefBrowserHostImpl::PlatformHandleExternalProtocol(const GURL& url) {
|
||||
|
Reference in New Issue
Block a user