Skip to content

Commit

Permalink
Cherry-pick abde730. rdar://problem/108841865
Browse files Browse the repository at this point in the history
    ServiceWorkerStorageManager should only delete its own files when clearing registrations
    https://bugs.webkit.org/show_bug.cgi?id=256725
    rdar://108841865

    Reviewed by Chris Dumez.

    Existing SPI allows clients to set the same path for different data types, so directory of ServiceWorkerStorageManager
    might contain files for other types. Therefore, ServiceWorkerStorageManager should not remove the directory when
    clearing registrations.

    Test: WKWebsiteDataStoreConfiguration.SameCustomPathForDifferentTypes

    * Source/WebCore/workers/service/server/SWRegistrationDatabase.cpp:
    (WebCore::SWRegistrationDatabase::~SWRegistrationDatabase):
    (WebCore::SWRegistrationDatabase::close):
    (WebCore::SWRegistrationDatabase::clearAllRegistrations):
    * Source/WebCore/workers/service/server/SWRegistrationDatabase.h:
    * Source/WebKit/NetworkProcess/storage/ServiceWorkerStorageManager.cpp:
    (WebKit::ServiceWorkerStorageManager::clearAllRegistrations):
    * Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsiteDataStoreCustomPaths.mm:
    (TEST):

    Canonical link: https://commits.webkit.org/264036@main

Identifier: 263769.32@safari-7616.1.14.11-branch
  • Loading branch information
szewai authored and Dan Robson committed May 15, 2023
1 parent c55f2b9 commit 4f0beb8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Source/WebCore/workers/service/server/SWRegistrationDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,18 @@ SWRegistrationDatabase::SWRegistrationDatabase(const String& path)
}

SWRegistrationDatabase::~SWRegistrationDatabase()
{
close();
}

void SWRegistrationDatabase::close()
{
ASSERT(!isMainRunLoop());

for (size_t i = 0; i < static_cast<size_t>(StatementType::Invalid); ++i)
m_cachedStatements[i] = nullptr;
m_database = nullptr;
m_scriptStorage = nullptr;
}

SWScriptStorage& SWRegistrationDatabase::scriptStorage()
Expand Down Expand Up @@ -487,6 +493,14 @@ std::optional<Vector<ServiceWorkerScripts>> SWRegistrationDatabase::updateRegist
return result;
}

void SWRegistrationDatabase::clearAllRegistrations()
{
close();
FileSystem::deleteFile(databaseFilePath(m_directory));
FileSystem::deleteNonEmptyDirectory(scriptDirectoryPath(m_directory));
FileSystem::deleteEmptyDirectory(m_directory);
}

} // namespace WebCore

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class SWRegistrationDatabase {

WEBCORE_EXPORT std::optional<Vector<ServiceWorkerContextData>> importRegistrations();
WEBCORE_EXPORT std::optional<Vector<ServiceWorkerScripts>> updateRegistrations(const Vector<ServiceWorkerContextData>&, const Vector<ServiceWorkerRegistrationKey>&);
WEBCORE_EXPORT void clearAllRegistrations();

private:
void close();
SWScriptStorage& scriptStorage();
enum class StatementType : uint8_t {
GetAllRecords,
Expand All @@ -63,7 +65,7 @@ class SWRegistrationDatabase {
enum class ShouldCreateIfNotExists : bool { No, Yes };
bool prepareDatabase(ShouldCreateIfNotExists);
bool ensureValidRecordsTable();

String m_directory;
std::unique_ptr<SQLiteDatabase> m_database;
Vector<std::unique_ptr<SQLiteStatement>> m_cachedStatements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ void ServiceWorkerStorageManager::closeFiles()

void ServiceWorkerStorageManager::clearAllRegistrations()
{
m_database = nullptr;
FileSystem::deleteNonEmptyDirectory(m_path);
if (auto database = ensureDatabase())
database->clearAllRegistrations();
}

std::optional<Vector<WebCore::ServiceWorkerContextData>> ServiceWorkerStorageManager::importRegistrations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ HTTPServer server([&] (Connection connection) {
websiteDataStoreConfiguration.get()._webSQLDatabaseDirectory = sharedDirectory;
websiteDataStoreConfiguration.get()._webStorageDirectory = sharedDirectory;
websiteDataStoreConfiguration.get()._indexedDBDatabaseDirectory = sharedDirectory;
websiteDataStoreConfiguration.get()._serviceWorkerRegistrationDirectory = sharedDirectory;
websiteDataStoreConfiguration.get()._cookieStorageFile = [sharedDirectory URLByAppendingPathComponent:@"Cookies.binarycookies" isDirectory:NO];
websiteDataStoreConfiguration.get().generalStorageDirectory = [sharedDirectory URLByAppendingPathComponent:@"Default" isDirectory:YES];

Expand Down

0 comments on commit 4f0beb8

Please sign in to comment.