1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/test/android/content_uri_test_utils.h"
6
7 #include "base/android/build_info.h"
8 #include "base/android/path_utils.h"
9 #include "base/files/file_path.h"
10 #include "base/strings/escape.h"
11 #include "base/strings/strcat.h"
12
13 namespace base::test::android {
14
GetContentUriFromCacheDirFilePath(const FilePath & path)15 std::optional<FilePath> GetContentUriFromCacheDirFilePath(
16 const FilePath& path) {
17 base::FilePath cache_dir;
18 if (!base::android::GetCacheDirectory(&cache_dir)) {
19 return std::nullopt;
20 }
21 base::FilePath uri(base::StrCat(
22 {"content://", base::android::BuildInfo::GetInstance()->package_name(),
23 ".fileprovider/cache/"}));
24 if (!cache_dir.AppendRelativePath(path, &uri)) {
25 return std::nullopt;
26 }
27 return uri;
28 }
29
GetInMemoryContentUriFromCacheDirFilePath(const FilePath & path)30 std::optional<FilePath> GetInMemoryContentUriFromCacheDirFilePath(
31 const FilePath& path) {
32 base::FilePath cache_dir;
33 if (!base::android::GetCacheDirectory(&cache_dir)) {
34 return std::nullopt;
35 }
36 base::FilePath uri(base::StrCat(
37 {"content://", base::android::BuildInfo::GetInstance()->package_name(),
38 ".inmemory/cache/"}));
39 if (!cache_dir.AppendRelativePath(path, &uri)) {
40 return std::nullopt;
41 }
42 return uri;
43 }
44
GetInMemoryContentTreeUriFromCacheDirDirectory(const FilePath & path)45 std::optional<FilePath> GetInMemoryContentTreeUriFromCacheDirDirectory(
46 const FilePath& path) {
47 base::FilePath cache_dir;
48 if (!base::android::GetCacheDirectory(&cache_dir)) {
49 return std::nullopt;
50 }
51 base::FilePath document_id;
52 if (!cache_dir.AppendRelativePath(path, &document_id)) {
53 return std::nullopt;
54 }
55 base::FilePath uri(base::StrCat(
56 {"content://", base::android::BuildInfo::GetInstance()->package_name(),
57 ".docprov/tree/",
58 base::EscapeAllExceptUnreserved(document_id.value())}));
59 return uri;
60 }
61
62 } // namespace base::test::android
63