2021-07-23 18:40:13 +02:00
|
|
|
// Copyright 2021 The Chromium Embedded Framework 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 "libcef/browser/alloy/alloy_dialog_util.h"
|
|
|
|
|
|
|
|
#include "libcef/browser/alloy/alloy_browser_host_impl.h"
|
2022-03-04 19:15:20 +01:00
|
|
|
#include "libcef/browser/extensions/browser_extensions_util.h"
|
|
|
|
#include "libcef/browser/file_dialog_runner.h"
|
2021-07-23 18:40:13 +02:00
|
|
|
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
|
|
|
|
namespace alloy {
|
|
|
|
|
|
|
|
void RunFileChooser(content::WebContents* web_contents,
|
|
|
|
const blink::mojom::FileChooserParams& params,
|
|
|
|
RunFileChooserCallback callback) {
|
2022-03-04 19:15:20 +01:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> browser = static_cast<AlloyBrowserHostImpl*>(
|
|
|
|
extensions::GetOwnerBrowserForHost(web_contents->GetRenderViewHost(),
|
|
|
|
nullptr)
|
|
|
|
.get());
|
2021-07-23 18:40:13 +02:00
|
|
|
if (!browser) {
|
2022-03-04 19:15:20 +01:00
|
|
|
LOG(ERROR) << "Failed to identify browser; canceling file dialog";
|
2021-07-23 18:40:13 +02:00
|
|
|
std::move(callback).Run(-1, {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefFileDialogRunner::FileChooserParams cef_params;
|
|
|
|
cef_params.mode = params.mode;
|
|
|
|
cef_params.default_file_name = params.default_file_name;
|
|
|
|
cef_params.accept_types = params.accept_types;
|
|
|
|
|
|
|
|
browser->RunFileChooser(cef_params, std::move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Based on net/base/filename_util_internal.cc FilePathToString16().
|
|
|
|
std::u16string FilePathTypeToString16(const base::FilePath::StringType& str) {
|
|
|
|
std::u16string result;
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2021-07-23 18:40:13 +02:00
|
|
|
result.assign(str.begin(), str.end());
|
2022-01-24 18:58:02 +01:00
|
|
|
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
|
2021-07-23 18:40:13 +02:00
|
|
|
if (!str.empty()) {
|
|
|
|
base::UTF8ToUTF16(str.c_str(), str.size(), &result);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace alloy
|