Bug 1805294 [wpt PR 37463] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / dom / indexedDB / DatabaseFileManagerImpl.h
blob0753adf5ffddae53520691c6d3ea428ccfdb8317
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_INDEXEDDB_DATABASEFILEMANAGERIMPL_H_
8 #define DOM_INDEXEDDB_DATABASEFILEMANAGERIMPL_H_
10 #include "DatabaseFileManager.h"
12 // local includes
13 #include "ActorsParentCommon.h"
15 // global includes
16 #include "mozilla/dom/quota/Assertions.h"
17 #include "mozilla/dom/quota/QuotaCommon.h"
18 #include "nsIFile.h"
19 #include "nsString.h"
21 namespace mozilla::dom::indexedDB {
23 template <typename KnownDirEntryOp, typename UnknownDirEntryOp>
24 Result<Ok, nsresult> DatabaseFileManager::TraverseFiles(
25 nsIFile& aDirectory, KnownDirEntryOp&& aKnownDirEntryOp,
26 UnknownDirEntryOp&& aUnknownDirEntryOp) {
27 quota::AssertIsOnIOThread();
29 QM_TRY_INSPECT(const bool& exists,
30 MOZ_TO_RESULT_INVOKE_MEMBER(aDirectory, Exists));
32 if (!exists) {
33 return Ok{};
36 QM_TRY(quota::CollectEachFile(
37 aDirectory,
38 [&aKnownDirEntryOp, &aUnknownDirEntryOp](
39 const nsCOMPtr<nsIFile>& file) -> Result<Ok, nsresult> {
40 QM_TRY_INSPECT(const auto& dirEntryKind, quota::GetDirEntryKind(*file));
42 switch (dirEntryKind) {
43 case quota::nsIFileKind::ExistsAsDirectory: {
44 QM_TRY_INSPECT(
45 const auto& leafName,
46 MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsString, file, GetLeafName));
48 if (leafName.Equals(kJournalDirectoryName)) {
49 QM_TRY(std::forward<KnownDirEntryOp>(aKnownDirEntryOp)(
50 *file, /* isDirectory */ true));
52 break;
55 Unused << WARN_IF_FILE_IS_UNKNOWN(*file);
57 QM_TRY(std::forward<UnknownDirEntryOp>(aUnknownDirEntryOp)(
58 *file, /* isDirectory */ true));
60 break;
63 case quota::nsIFileKind::ExistsAsFile: {
64 QM_TRY_INSPECT(
65 const auto& leafName,
66 MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsString, file, GetLeafName));
68 nsresult rv;
69 leafName.ToInteger64(&rv);
70 if (NS_SUCCEEDED(rv)) {
71 QM_TRY(std::forward<KnownDirEntryOp>(aKnownDirEntryOp)(
72 *file, /* isDirectory */ false));
74 break;
77 Unused << WARN_IF_FILE_IS_UNKNOWN(*file);
79 QM_TRY(std::forward<UnknownDirEntryOp>(aUnknownDirEntryOp)(
80 *file, /* isDirectory */ false));
82 break;
85 case quota::nsIFileKind::DoesNotExist:
86 // Ignore files that got removed externally while iterating.
87 break;
90 return Ok{};
91 }));
93 return Ok{};
96 } // namespace mozilla::dom::indexedDB
98 #endif // DOM_INDEXEDDB_DATABASEFILEMANAGERIMPL_H_