Skip to content

Truly independent web browser

LibWebView: Do not read the downloads folder when the browser starts

Andreas Kling84f70d18b98a
diff --git a/Libraries/LibWebView/FileDownloader.cpp b/Libraries/LibWebView/FileDownloader.cpp
index b577ed4e817..e8f3b23a6a9 100644
--- a/Libraries/LibWebView/FileDownloader.cpp
+++ b/Libraries/LibWebView/FileDownloader.cpp
@@ -15,7 +15,6 @@
#include <LibCore/ElapsedTimer.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
-#include <LibCore/StandardPaths.h>
#include <LibCore/Timer.h>
#include <LibFileSystem/FileSystem.h>
#include <LibHTTP/HeaderList.h>
@@ -979,6 +978,12 @@ u64 FileDownloader::start_download(IsPrivate is_private, URL::URL const& url, Le
active->may_replace_destination = may_replace_destination == MayReplaceDestination::Yes;
m_active_downloads.set(download_id, move(active));
+ // NB: Reading a folder such as the user's downloads folder can make the system ask the user for permission. The
+ // user has just saved a file to this folder, so this is when to look for what earlier downloads left in it,
+ // rather than when the browser starts.
+ if (m_download_store)
+ remove_orphaned_temporary_files_once(temporary_destination.dirname());
+
return download_id;
}
@@ -1401,8 +1406,6 @@ void FileDownloader::restore_persisted_downloads()
HashTable<ByteString> directories;
HashTable<ByteString> temporary_paths_in_use;
- directories.set(Core::StandardPaths::downloads_directory());
-
for (auto& record : m_download_store->resumable_downloads()) {
LexicalPath temporary_destination { record.temporary_destination.to_byte_string() };
directories.set(temporary_destination.dirname());
@@ -1416,6 +1419,23 @@ void FileDownloader::restore_persisted_downloads()
(void)FileSystem::remove(temporary_destination.string(), FileSystem::RecursionMode::Disallowed);
}
+ // NB: These folders hold downloads that we just looked at. Other folders wait for their next download.
+ remove_orphaned_temporary_files(directories, temporary_paths_in_use);
+ for (auto const& directory : directories)
+ m_directories_cleared_of_orphaned_temporary_files.set(directory);
+}
+
+void FileDownloader::remove_orphaned_temporary_files_once(ByteString const& directory)
+{
+ if (m_directories_cleared_of_orphaned_temporary_files.set(directory) != HashSetResult::InsertedNewEntry)
+ return;
+
+ HashTable<ByteString> temporary_paths_in_use;
+ for (auto const& [id, active] : m_active_downloads)
+ temporary_paths_in_use.set(active->temporary_destination.string());
+
+ HashTable<ByteString> directories;
+ directories.set(directory);
remove_orphaned_temporary_files(directories, temporary_paths_in_use);
}
diff --git a/Libraries/LibWebView/FileDownloader.h b/Libraries/LibWebView/FileDownloader.h
index 85f52390fc7..26ef0d95bd2 100644
--- a/Libraries/LibWebView/FileDownloader.h
+++ b/Libraries/LibWebView/FileDownloader.h
@@ -154,6 +154,7 @@ private:
void forget_persisted_download(u64 id);
void restore_persisted_downloads();
bool restore_persisted_download(DownloadRecord&);
+ void remove_orphaned_temporary_files_once(ByteString const& directory);
static void remove_orphaned_temporary_files(HashTable<ByteString> const& directories, HashTable<ByteString> const& temporary_paths_in_use);
void maybe_split_download(u64 id);
@@ -173,6 +174,7 @@ private:
HashMap<u64, NonnullOwnPtr<ActiveDownload>> m_active_downloads;
Vector<FileDownloaderObserver&> m_observers;
DownloadStore* m_download_store { nullptr };
+ HashTable<ByteString> m_directories_cleared_of_orphaned_temporary_files;
u64 m_next_download_id { 0 };
};