1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/crostini/crostini_file_selector.h"
#include "base/path_service.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/shell_dialogs/selected_file_info.h"
namespace crostini {
CrostiniFileSelector::CrostiniFileSelector(content::WebUI* web_ui)
: web_ui_(web_ui) {}
CrostiniFileSelector::~CrostiniFileSelector() {
if (select_file_dialog_.get()) {
select_file_dialog_->ListenerDestroyed();
}
}
void CrostiniFileSelector::SelectFile(
base::OnceCallback<void(const base::FilePath&)> selected_callback,
base::OnceCallback<void(void)> cancelled_callback) {
selected_callback_ = std::move(selected_callback);
cancelled_callback_ = std::move(cancelled_callback);
select_file_dialog_ = ui::SelectFileDialog::Create(
this,
std::make_unique<ChromeSelectFilePolicy>(web_ui_->GetWebContents()));
// TODO(b/231905716): Add some logic to start from the path of the previously
// uploaded Ansible Playbook if the user has already "uploaded" a playbook
// before.
base::FilePath downloads_path;
if (!base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &downloads_path)) {
LOG(ERROR) << "Default Downloads path does not exist, cannot open file "
"selector for create container";
return;
}
ui::SelectFileDialog::FileTypeInfo file_type_info{
// Allowed file types include:
// * Ansible playbooks (yaml)
// * Crostini backup files (tini, tar.gz, tgz)
{FILE_PATH_LITERAL("yaml"), FILE_PATH_LITERAL("tini"),
FILE_PATH_LITERAL("tar.gz"), FILE_PATH_LITERAL("tgz")},
};
select_file_dialog_->SelectFile(
ui::SelectFileDialog::SELECT_OPEN_FILE,
l10n_util::GetStringUTF16(
IDS_SETTINGS_CROSTINI_FILE_SELECTOR_DIALOG_TITLE),
downloads_path, &file_type_info, 0, FILE_PATH_LITERAL(""),
web_ui_->GetWebContents()->GetTopLevelNativeWindow());
}
void CrostiniFileSelector::FileSelected(const ui::SelectedFileInfo& file,
int index) {
std::move(selected_callback_).Run(file.path());
}
void CrostiniFileSelector::FileSelectionCanceled() {
if (cancelled_callback_) {
std::move(cancelled_callback_).Run();
}
}
} // namespace crostini
|