Bug 1894260 - get folder contents for DLP in a simpler way r=win-reviewers,dlp-reviewers,handyman

We used to use GetDomFileOrDirectory(), but that creates a mozilla::dom::Directory which needs to be able to get a DOMWindow. Getting that DOMWindow is now failing, perhaps because of the out-of-process file picker?

Anyway, all we care about is getting the contents of the directory, so we can just use GetDirectoryEntries() to sidestep this, and it also makes the code a bit simpler.

Unfortunately there isn't a good way to test code that's in nsFilePicker.cpp right now, as MockFilePicker mocks all of this out.

Differential Revision: https://phabricator.services.mozilla.com/D209056
This commit is contained in:
Greg Stoll 2024-05-06 00:25:41 +00:00
parent 799fd3706f
commit 43e18e4034

View File

@ -801,43 +801,29 @@ nsFilePicker::CheckContentAnalysisService() {
return promise;
};
// Since getting the files to analyze might be asynchronous, use a MozPromise
// to unify the logic below.
auto getFilesToAnalyzePromise = mozilla::MakeRefPtr<mozilla::MozPromise<
nsTArray<mozilla::PathString>, nsresult, true>::Private>(__func__);
nsCOMArray<nsIFile> files;
if (mMode == modeGetFolder) {
nsCOMPtr<nsISupports> tmp;
nsresult rv = GetDomFileOrDirectory(getter_AddRefs(tmp));
nsCOMPtr<nsIFile> file;
nsresult rv = GetFile(getter_AddRefs(file));
if (NS_WARN_IF(NS_FAILED(rv))) {
getFilesToAnalyzePromise->Reject(rv, __func__);
return nsFilePicker::ContentAnalysisResponse::CreateAndReject(rv,
__func__);
}
auto* directory = static_cast<mozilla::dom::Directory*>(tmp.get());
mozilla::dom::OwningFileOrDirectory owningDirectory;
owningDirectory.SetAsDirectory() = directory;
nsTArray<mozilla::dom::OwningFileOrDirectory> directoryArray{
std::move(owningDirectory)};
mozilla::ErrorResult error;
RefPtr<mozilla::dom::GetFilesHelper> helper =
mozilla::dom::GetFilesHelper::Create(directoryArray, true, error);
rv = error.StealNSResult();
nsCOMPtr<nsIDirectoryEnumerator> iter;
rv = file->GetDirectoryEntries(getter_AddRefs(iter));
if (NS_WARN_IF(NS_FAILED(rv))) {
getFilesToAnalyzePromise->Reject(rv, __func__);
return nsFilePicker::ContentAnalysisResponse::CreateAndReject(rv,
__func__);
}
auto getFilesCallback = mozilla::MakeRefPtr<GetFilesInDirectoryCallback>(
getFilesToAnalyzePromise);
helper->AddCallback(getFilesCallback);
nsCOMPtr<nsIFile> entry;
while (NS_SUCCEEDED(iter->GetNextFile(getter_AddRefs(entry))) && entry) {
files.AppendElement(entry);
}
} else {
nsCOMArray<nsIFile> files;
if (!mUnicodeFile.IsEmpty()) {
nsCOMPtr<nsIFile> file;
rv = GetFile(getter_AddRefs(file));
if (NS_WARN_IF(NS_FAILED(rv))) {
getFilesToAnalyzePromise->Reject(rv, __func__);
return nsFilePicker::ContentAnalysisResponse::CreateAndReject(rv,
__func__);
}
@ -845,22 +831,13 @@ nsFilePicker::CheckContentAnalysisService() {
} else {
files.AppendElements(mFiles);
}
nsTArray<mozilla::PathString> paths(files.Length());
std::transform(files.begin(), files.end(), MakeBackInserter(paths),
[](auto* entry) { return entry->NativePath(); });
getFilesToAnalyzePromise->Resolve(std::move(paths), __func__);
}
nsTArray<mozilla::PathString> paths(files.Length());
std::transform(files.begin(), files.end(), MakeBackInserter(paths),
[](auto* entry) { return entry->NativePath(); });
return getFilesToAnalyzePromise->Then(
mozilla::GetMainThreadSerialEventTarget(), __func__,
[processOneItem](nsTArray<mozilla::PathString> aPaths) mutable {
return mozilla::detail::AsyncAll<mozilla::PathString>(std::move(aPaths),
processOneItem);
},
[](nsresult aError) {
return nsFilePicker::ContentAnalysisResponse::CreateAndReject(aError,
__func__);
});
return mozilla::detail::AsyncAll<mozilla::PathString>(std::move(paths),
processOneItem);
};
///////////////////////////////////////////////////////////////////////////////