1 // Copyright 2021 The Chromium Embedded Framework Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be found
3 // in the LICENSE file.
4
5 #include "libcef/browser/alloy/alloy_dialog_util.h"
6
7 #include "libcef/browser/alloy/alloy_browser_host_impl.h"
8 #include "libcef/browser/extensions/browser_extensions_util.h"
9 #include "libcef/browser/file_dialog_runner.h"
10
11 #include "base/strings/utf_string_conversions.h"
12
13 namespace alloy {
14
RunFileChooser(content::WebContents * web_contents,const blink::mojom::FileChooserParams & params,RunFileChooserCallback callback)15 void RunFileChooser(content::WebContents* web_contents,
16 const blink::mojom::FileChooserParams& params,
17 RunFileChooserCallback callback) {
18 CefRefPtr<AlloyBrowserHostImpl> browser = static_cast<AlloyBrowserHostImpl*>(
19 extensions::GetOwnerBrowserForHost(web_contents->GetRenderViewHost(),
20 nullptr)
21 .get());
22 if (!browser) {
23 LOG(ERROR) << "Failed to identify browser; canceling file dialog";
24 std::move(callback).Run(-1, {});
25 return;
26 }
27
28 CefFileDialogRunner::FileChooserParams cef_params;
29 cef_params.mode = params.mode;
30 cef_params.default_file_name = params.default_file_name;
31 cef_params.accept_types = params.accept_types;
32
33 browser->RunFileChooser(cef_params, std::move(callback));
34 }
35
36 // Based on net/base/filename_util_internal.cc FilePathToString16().
FilePathTypeToString16(const base::FilePath::StringType & str)37 std::u16string FilePathTypeToString16(const base::FilePath::StringType& str) {
38 std::u16string result;
39 #if BUILDFLAG(IS_WIN)
40 result.assign(str.begin(), str.end());
41 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
42 if (!str.empty()) {
43 base::UTF8ToUTF16(str.c_str(), str.size(), &result);
44 }
45 #endif
46 return result;
47 }
48
49 } // namespace alloy
50