2016-11-15 18:56:02 +01:00
|
|
|
// Copyright 2016 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 "include/cef_file_util.h"
|
|
|
|
|
|
|
|
#include "include/cef_task.h"
|
|
|
|
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/files/file_util.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "third_party/zlib/google/zip.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool AllowFileIO() {
|
|
|
|
if (CefCurrentlyOn(TID_UI) || CefCurrentlyOn(TID_IO)) {
|
2023-05-08 17:19:38 +02:00
|
|
|
DCHECK(false) << "file IO is not allowed on the current thread";
|
2016-11-15 18:56:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool CefCreateDirectory(const CefString& full_path) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!AllowFileIO()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
return base::CreateDirectory(full_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefGetTempDirectory(CefString& temp_dir) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!AllowFileIO()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
base::FilePath result;
|
|
|
|
if (base::GetTempDir(&result)) {
|
|
|
|
temp_dir = result.value();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefCreateNewTempDirectory(const CefString& prefix,
|
|
|
|
CefString& new_temp_path) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!AllowFileIO()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
base::FilePath result;
|
|
|
|
if (base::CreateNewTempDirectory(prefix, &result)) {
|
|
|
|
new_temp_path = result.value();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefCreateTempDirectoryInDirectory(const CefString& base_dir,
|
|
|
|
const CefString& prefix,
|
|
|
|
CefString& new_dir) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!AllowFileIO()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
base::FilePath result;
|
|
|
|
if (base::CreateTemporaryDirInDir(base_dir, prefix, &result)) {
|
|
|
|
new_dir = result.value();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDirectoryExists(const CefString& path) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!AllowFileIO()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
return base::DirectoryExists(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDeleteFile(const CefString& path, bool recursive) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!AllowFileIO()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-08-29 00:39:23 +02:00
|
|
|
if (recursive) {
|
|
|
|
return base::DeletePathRecursively(path);
|
|
|
|
} else {
|
|
|
|
return base::DeleteFile(path);
|
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefZipDirectory(const CefString& src_dir,
|
|
|
|
const CefString& dest_file,
|
|
|
|
bool include_hidden_files) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!AllowFileIO()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
return zip::Zip(src_dir, dest_file, include_hidden_files);
|
|
|
|
}
|