2016-11-15 18:56:02 +01:00
|
|
|
// Copyright 2016 The Chromium Embedded Framework Authors. Portions copyright
|
|
|
|
// 2011 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.
|
|
|
|
|
|
|
|
#include "include/wrapper/cef_scoped_temp_dir.h"
|
|
|
|
|
|
|
|
#include "include/base/cef_logging.h"
|
|
|
|
#include "include/cef_file_util.h"
|
|
|
|
|
2024-01-20 18:34:13 +01:00
|
|
|
CefScopedTempDir::CefScopedTempDir() = default;
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
CefScopedTempDir::~CefScopedTempDir() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!path_.empty() && !Delete()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
DLOG(WARNING) << "Could not delete temp dir in dtor.";
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefScopedTempDir::CreateUniqueTempDir() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!path_.empty()) {
|
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
|
|
|
|
|
|
|
// This "scoped_dir" prefix is only used on Windows and serves as a template
|
|
|
|
// for the unique name.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!CefCreateNewTempDirectory("scoped_dir", path_)) {
|
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 true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefScopedTempDir::CreateUniqueTempDirUnderPath(
|
|
|
|
const CefString& base_path) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!path_.empty()) {
|
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
|
|
|
|
|
|
|
// If |base_path| does not exist, create it.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!CefCreateDirectory(base_path)) {
|
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
|
|
|
|
|
|
|
// Create a new, uniquely named directory under |base_path|.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!CefCreateTempDirectoryInDirectory(base_path, "scoped_dir_", path_)) {
|
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 true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefScopedTempDir::Set(const CefString& path) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!path_.empty()) {
|
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
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!CefDirectoryExists(path) && !CefCreateDirectory(path)) {
|
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
|
|
|
|
|
|
|
path_ = path;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefScopedTempDir::Delete() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (path_.empty()) {
|
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
|
|
|
|
|
|
|
bool ret = CefDeleteFile(path_, true);
|
|
|
|
if (ret) {
|
|
|
|
// We only clear the path if deleted the directory.
|
|
|
|
path_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefScopedTempDir::Take() {
|
|
|
|
CefString ret = path_;
|
|
|
|
path_.clear();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CefString& CefScopedTempDir::GetPath() const {
|
|
|
|
DCHECK(!path_.empty()) << "Did you call CreateUniqueTempDir* before?";
|
|
|
|
return path_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefScopedTempDir::IsEmpty() const {
|
|
|
|
return path_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefScopedTempDir::IsValid() const {
|
|
|
|
return !path_.empty() && CefDirectoryExists(path_);
|
|
|
|
}
|