mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-02-10 09:00:42 +01:00
File dialogs that specify mime type (e.g. "image/*") accept filters will pass those values unchanged to the OnFileDialog |accept_filters| parameter. The default dialog implementation will show those filters in addition to a combined "Custom Files" filter. This is a change from preexisting Google Chrome behavior where only the combined "Custom Files" filter is displayed, and restores CEF behavior that existed prior to 2ea7459a89. Document the fact that OnFileDialog may be called twice, once before MIME type expansion and once afterwards. Add new OnFileDialog |accept_extensions| and |accept_descriptions| parameters for MIME type extensions and descriptions. Details: This change adds a SelectFileDialog::FileTypeInfo::extension_mimetypes member and improves the logic in FileSelectHelper::GetFileTypesFromAcceptType and file_dialog_manager.cc SelectFileToFileChooserParams to support recall of the source mime type when populating the FileChooserParams structure. To test: - Run `ceftests --gtest_filter=DialogTest.*` - Run `cefclient --url=https://tests/dialogs`
104 lines
4.1 KiB
C++
104 lines
4.1 KiB
C++
// Copyright (c) 2012 The Chromium Embedded Framework Authors.
|
|
// Portions 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.
|
|
|
|
#ifndef CEF_LIBCEF_BROWSER_FILE_DIALOG_MANAGER_H_
|
|
#define CEF_LIBCEF_BROWSER_FILE_DIALOG_MANAGER_H_
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
#include <set>
|
|
|
|
#include "base/memory/raw_ptr.h"
|
|
#include "base/memory/scoped_refptr.h"
|
|
#include "cef/include/cef_browser.h"
|
|
#include "third_party/blink/public/mojom/choosers/file_chooser.mojom.h"
|
|
#include "ui/shell_dialogs/select_file_dialog.h"
|
|
|
|
namespace content {
|
|
class FileSelectListener;
|
|
} // namespace content
|
|
|
|
class CefBrowserHostBase;
|
|
class CefSelectFileDialogListener;
|
|
|
|
class CefFileDialogManager {
|
|
public:
|
|
explicit CefFileDialogManager(CefBrowserHostBase* browser);
|
|
|
|
CefFileDialogManager(const CefFileDialogManager&) = delete;
|
|
CefFileDialogManager& operator=(const CefFileDialogManager&) = delete;
|
|
|
|
~CefFileDialogManager();
|
|
|
|
// Delete the runner to free any platform constructs.
|
|
void Destroy();
|
|
|
|
// Run a file dialog with the specified parameters. See
|
|
// CefBrowserHost::RunFileDialog for usage documentation. This method should
|
|
// be called via CefBrowserHostBase::RunFileDialog.
|
|
void RunFileDialog(cef_file_dialog_mode_t mode,
|
|
const CefString& title,
|
|
const CefString& default_file_path,
|
|
const std::vector<CefString>& accept_filters,
|
|
CefRefPtr<CefRunFileDialogCallback> callback);
|
|
|
|
// The argument vector will be empty if the dialog was canceled.
|
|
using RunFileChooserCallback =
|
|
base::OnceCallback<void(const std::vector<base::FilePath>&)>;
|
|
|
|
// Run the file dialog specified by |params|. |callback| will be executed
|
|
// synchronously or asynchronously after the dialog is dismissed. This method
|
|
// should be called via CefBrowserHostBase::RunFileChooser.
|
|
void RunFileChooser(const blink::mojom::FileChooserParams& params,
|
|
RunFileChooserCallback callback);
|
|
|
|
// Run a ui::SelectFileDialog with the specified parameters. See
|
|
// ui::SelectFileDialog for usage documentation. This method should be called
|
|
// via CefBrowserHostBase::RunSelectFile. It will be called for all file
|
|
// dialogs after interception via CefSelectFileDialog::SelectFileImpl.
|
|
void RunSelectFile(ui::SelectFileDialog::Listener* listener,
|
|
std::unique_ptr<ui::SelectFilePolicy> policy,
|
|
ui::SelectFileDialog::Type type,
|
|
const std::u16string& title,
|
|
const base::FilePath& default_path,
|
|
const ui::SelectFileDialog::FileTypeInfo* file_types,
|
|
int file_type_index,
|
|
const base::FilePath::StringType& default_extension,
|
|
gfx::NativeWindow owning_window,
|
|
void* params);
|
|
|
|
// Must be called when the |listener| passed to RunSelectFile is destroyed.
|
|
void SelectFileListenerDestroyed(ui::SelectFileDialog::Listener* listener);
|
|
|
|
private:
|
|
using Extensions = std::vector<std::vector<base::FilePath::StringType>>;
|
|
using Descriptions = std::vector<std::u16string>;
|
|
[[nodiscard]] RunFileChooserCallback MaybeRunDelegate(
|
|
const blink::mojom::FileChooserParams& params,
|
|
const Extensions& extensions,
|
|
const Descriptions& descriptions,
|
|
RunFileChooserCallback callback);
|
|
|
|
void SelectFileDoneByDelegateCallback(
|
|
ui::SelectFileDialog::Listener* listener,
|
|
void* params,
|
|
const std::vector<base::FilePath>& paths);
|
|
void SelectFileDoneByListenerCallback(bool listener_destroyed);
|
|
|
|
// CefBrowserHostBase pointer is guaranteed to outlive this object.
|
|
const raw_ptr<CefBrowserHostBase> browser_;
|
|
|
|
// Used when running a platform dialog via RunSelectFile.
|
|
scoped_refptr<ui::SelectFileDialog> dialog_;
|
|
raw_ptr<CefSelectFileDialogListener> dialog_listener_ = nullptr;
|
|
|
|
// List of all currently active listeners.
|
|
std::set<raw_ptr<ui::SelectFileDialog::Listener>> active_listeners_;
|
|
|
|
base::WeakPtrFactory<CefFileDialogManager> weak_ptr_factory_{this};
|
|
};
|
|
|
|
#endif // CEF_LIBCEF_BROWSER_JAVASCRIPT_DIALOG_MANAGER_H_
|