#include "base/test/android/content_uri_test_utils.h"
#include <optional>
#include "base/android/apk_info.h"
#include "base/android/path_utils.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/escape.h"
#include "base/strings/strcat.h"
namespace base::test::android {
namespace {
std::optional<FilePath> GetInMemoryContentDocumentUriFromCacheDirPath(
const FilePath& path,
bool is_tree) {
base::FilePath cache_dir;
if (!base::android::GetCacheDirectory(&cache_dir)) {
return std::nullopt;
}
base::FilePath document_id;
if (!cache_dir.AppendRelativePath(path, &document_id)) {
return std::nullopt;
}
base::FilePath uri(
base::StrCat({"content://", base::android::apk_info::package_name(),
".docprov/", is_tree ? "tree/" : "document/",
base::EscapeAllExceptUnreserved(document_id.value())}));
return uri;
}
}
std::optional<FilePath> GetContentUriFromCacheDirFilePath(
const FilePath& path) {
base::FilePath cache_dir;
if (!base::android::GetCacheDirectory(&cache_dir)) {
return std::nullopt;
}
base::FilePath uri(
base::StrCat({"content://", base::android::apk_info::package_name(),
".fileprovider/cache/"}));
if (!cache_dir.AppendRelativePath(path, &uri)) {
return std::nullopt;
}
return uri;
}
std::optional<FilePath> GetInMemoryContentUriFromCacheDirFilePath(
const FilePath& path) {
base::FilePath cache_dir;
if (!base::android::GetCacheDirectory(&cache_dir)) {
return std::nullopt;
}
base::FilePath uri(
base::StrCat({"content://", base::android::apk_info::package_name(),
".inmemory/cache/"}));
if (!cache_dir.AppendRelativePath(path, &uri)) {
return std::nullopt;
}
return uri;
}
std::optional<FilePath> GetInMemoryContentDocumentUriFromCacheDirFilePath(
const FilePath& path) {
return GetInMemoryContentDocumentUriFromCacheDirPath(path, false);
}
std::optional<FilePath> GetInMemoryContentTreeUriFromCacheDirDirectory(
const FilePath& path) {
return GetInMemoryContentDocumentUriFromCacheDirPath(path, true);
}
std::optional<FilePath> GetVirtualDocumentPathFromCacheDirDirectory(
const FilePath& path) {
std::optional<FilePath> content_url =
GetInMemoryContentTreeUriFromCacheDirDirectory(path);
if (!content_url) {
return std::nullopt;
}
return base::ResolveToVirtualDocumentPath(*content_url);
}
std::optional<FilePath> CreateCacheCopyAndGetVirtualDocumentPath(
const FilePath& source_path,
const ScopedTempDir& temp_dir) {
if (!base::CopyDirectory(source_path, temp_dir.GetPath(), true)) {
return std::nullopt;
}
return GetVirtualDocumentPathFromCacheDirDirectory(
temp_dir.GetPath().Append(source_path.BaseName()));
}
}