Add SPI for setting media cache and key location on _WKWebsiteDataStoreConfiguration
authorachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 23 Jul 2019 19:32:48 +0000 (19:32 +0000)
committerachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 23 Jul 2019 19:32:48 +0000 (19:32 +0000)
https://bugs.webkit.org/show_bug.cgi?id=199972

Reviewed by Jer Noble.

Source/WebKit:

This is a step towards removing WebsiteDataStore::legacyDefaultDataStoreConfiguration
I added a unit test for the media cache location,
but the media key location can't reasonably be unit tested in an open source project.

* UIProcess/API/Cocoa/WKWebsiteDataStore.mm:
(-[WKWebsiteDataStore _initWithConfiguration:]):
* UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.h:
* UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.mm:
(-[_WKWebsiteDataStoreConfiguration mediaCacheDirectory]):
(-[_WKWebsiteDataStoreConfiguration setMediaCacheDirectory:]):
(-[_WKWebsiteDataStoreConfiguration mediaKeysStorageDirectory]):
(-[_WKWebsiteDataStoreConfiguration setMediaKeysStorageDirectory:]):

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/WebsiteDataStoreCustomPaths.mm:
(TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247733 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/Cocoa/WKWebsiteDataStore.mm
Source/WebKit/UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.h
Source/WebKit/UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.mm
Tools/ChangeLog
Tools/TestWebKitAPI/TCPServer.cpp
Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsiteDataStoreCustomPaths.mm

index 9f5eef7..1887d86 100644 (file)
@@ -1,3 +1,23 @@
+2019-07-23  Alex Christensen  <achristensen@webkit.org>
+
+        Add SPI for setting media cache and key location on _WKWebsiteDataStoreConfiguration
+        https://bugs.webkit.org/show_bug.cgi?id=199972
+
+        Reviewed by Jer Noble.
+
+        This is a step towards removing WebsiteDataStore::legacyDefaultDataStoreConfiguration
+        I added a unit test for the media cache location,
+        but the media key location can't reasonably be unit tested in an open source project.
+
+        * UIProcess/API/Cocoa/WKWebsiteDataStore.mm:
+        (-[WKWebsiteDataStore _initWithConfiguration:]):
+        * UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.h:
+        * UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.mm:
+        (-[_WKWebsiteDataStoreConfiguration mediaCacheDirectory]):
+        (-[_WKWebsiteDataStoreConfiguration setMediaCacheDirectory:]):
+        (-[_WKWebsiteDataStoreConfiguration mediaKeysStorageDirectory]):
+        (-[_WKWebsiteDataStoreConfiguration setMediaKeysStorageDirectory:]):
+
 2019-07-23  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         [macOS 10.15] Web process crashes when attempting to show the font panel via Font > Show Fonts
index 528f9d9..f5fc528 100644 (file)
@@ -256,6 +256,10 @@ static Vector<WebKit::WebsiteDataRecord> toWebsiteDataRecords(NSArray *dataRecor
             config->setApplicationCacheDirectory(configuration.applicationCacheDirectory.path);
         if (configuration.applicationCacheFlatFileSubdirectoryName)
             config->setApplicationCacheFlatFileSubdirectoryName(configuration.applicationCacheFlatFileSubdirectoryName);
+        if (configuration.mediaCacheDirectory)
+            config->setMediaCacheDirectory(configuration.mediaCacheDirectory.path);
+        if (configuration.mediaKeysStorageDirectory)
+            config->setMediaKeysStorageDirectory(configuration.mediaKeysStorageDirectory.path);
     } else {
         RELEASE_ASSERT(!configuration._webStorageDirectory);
         RELEASE_ASSERT(!configuration._webSQLDatabaseDirectory);
@@ -267,6 +271,8 @@ static Vector<WebKit::WebsiteDataRecord> toWebsiteDataRecords(NSArray *dataRecor
         RELEASE_ASSERT(!configuration.networkCacheDirectory);
         RELEASE_ASSERT(!configuration.deviceIdHashSaltsStorageDirectory);
         RELEASE_ASSERT(!configuration.applicationCacheDirectory);
+        RELEASE_ASSERT(!configuration.mediaCacheDirectory);
+        RELEASE_ASSERT(!configuration.mediaKeysStorageDirectory);
     }
 
     if (configuration.sourceApplicationBundleIdentifier)
index c88fc88..c9ed512 100644 (file)
@@ -57,6 +57,8 @@ WK_CLASS_AVAILABLE(macos(10.13), ios(11.0))
 @property (nonatomic, nullable, copy) NSURL *deviceIdHashSaltsStorageDirectory WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
 @property (nonatomic, nullable, copy) NSURL *applicationCacheDirectory WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
 @property (nonatomic, nullable, copy) NSString *applicationCacheFlatFileSubdirectoryName WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
+@property (nonatomic, nullable, copy) NSURL *mediaCacheDirectory WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
+@property (nonatomic, nullable, copy) NSURL *mediaKeysStorageDirectory WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
 
 // Testing only.
 @property (nonatomic) BOOL allLoadsBlockedByDeviceManagementRestrictionsForTesting WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
index ef1ed91..825c18f 100644 (file)
@@ -248,6 +248,32 @@ static void checkURLArgument(NSURL *url)
     _configuration->setApplicationCacheFlatFileSubdirectoryName(name);
 }
 
+- (NSURL *)mediaCacheDirectory
+{
+    return [NSURL fileURLWithPath:_configuration->mediaCacheDirectory() isDirectory:YES];
+}
+
+- (void)setMediaCacheDirectory:(NSURL *)url
+{
+    if (!_configuration->isPersistent())
+        [NSException raise:NSInvalidArgumentException format:@"Cannot set mediaCacheDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
+    checkURLArgument(url);
+    _configuration->setMediaCacheDirectory(url.path);
+}
+
+- (NSURL *)mediaKeysStorageDirectory
+{
+    return [NSURL fileURLWithPath:_configuration->mediaKeysStorageDirectory() isDirectory:YES];
+}
+
+- (void)setMediaKeysStorageDirectory:(NSURL *)url
+{
+    if (!_configuration->isPersistent())
+        [NSException raise:NSInvalidArgumentException format:@"Cannot set mediaKeysStorageDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
+    checkURLArgument(url);
+    _configuration->setMediaKeysStorageDirectory(url.path);
+}
+
 - (BOOL)deviceManagementRestrictionsEnabled
 {
     return _configuration->deviceManagementRestrictionsEnabled();
index 5fc9d74..4a8c57f 100644 (file)
@@ -1,3 +1,13 @@
+2019-07-23  Alex Christensen  <achristensen@webkit.org>
+
+        Add SPI for setting media cache and key location on _WKWebsiteDataStoreConfiguration
+        https://bugs.webkit.org/show_bug.cgi?id=199972
+
+        Reviewed by Jer Noble.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WebsiteDataStoreCustomPaths.mm:
+        (TEST):
+
 2019-07-23  Alicia Boya GarcĂ­a  <aboya@igalia.com>
 
         [WTF] Add DataMutex and MainThreadData wrappers
index f3a7e01..1238257 100644 (file)
@@ -284,7 +284,8 @@ template<> Vector<uint8_t> TCPServer::read(Socket socket)
 {
     uint8_t buffer[1000];
     auto bytesRead = ::read(socket, buffer, sizeof(buffer));
-    ASSERT_UNUSED(bytesRead, bytesRead > 0);
+    if (bytesRead <= 0)
+        return { };
     ASSERT(static_cast<size_t>(bytesRead) < sizeof(buffer));
 
     Vector<uint8_t> vector;
@@ -303,7 +304,8 @@ template<> Vector<uint8_t> TCPServer::read(SSL* ssl)
 {
     uint8_t buffer[1000];
     auto bytesRead = SSL_read(ssl, buffer, sizeof(buffer));
-    ASSERT_UNUSED(bytesRead, bytesRead > 0);
+    if (bytesRead <= 0)
+        return { };
     ASSERT(static_cast<size_t>(bytesRead) < sizeof(buffer));
 
     Vector<uint8_t> vector;
index 030cdea..3e71423 100644 (file)
@@ -42,6 +42,7 @@
 #import <WebKit/_WKWebsiteDataStoreConfiguration.h>
 #import <wtf/Deque.h>
 #import <wtf/RetainPtr.h>
+#import <wtf/text/WTFString.h>
 
 static bool receivedScriptMessage;
 static Deque<RetainPtr<WKScriptMessage>> scriptMessages;
@@ -611,3 +612,78 @@ TEST(WebKit, ApplicationCacheDirectories)
     [fileManager removeItemAtPath:path error:&error];
     EXPECT_FALSE(error);
 }
+
+// FIXME: investigate why this test times out on High Sierra
+#if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101400) || PLATFORM(IOS_FAMILY)
+TEST(WebKit, MediaCache)
+{
+    std::atomic<bool> done = false;
+    using namespace TestWebKitAPI;
+    RetainPtr<NSData> data = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"mp4" subdirectory:@"TestWebKitAPI.resources"]];
+    uint64_t dataLength = [data length];
+
+    TCPServer server([&] (int socket) {
+        TCPServer::read(socket);
+        const char* firstResponse =
+        "HTTP/1.1 200 OK\r\n"
+        "Content-Type: text/html\r\n"
+        "Content-Length: 55\r\n\r\n"
+        "<video><source src='test.mp4' type='video/mp4'></video>";
+        TCPServer::write(socket, firstResponse, strlen(firstResponse));
+
+        while (!done) {
+            auto bytes = TCPServer::read(socket);
+            if (done)
+                break;
+            StringView request(static_cast<const LChar*>(bytes.data()), bytes.size());
+            String rangeBytes = "Range: bytes="_s;
+            auto begin = request.find(StringView(rangeBytes), 0);
+            ASSERT(begin != notFound);
+            auto dash = request.find('-', begin);
+            ASSERT(dash != notFound);
+            auto end = request.find('\r', dash);
+            ASSERT(end != notFound);
+
+            auto rangeBegin = *request.substring(begin + rangeBytes.length(), dash - begin - rangeBytes.length()).toUInt64Strict();
+            auto rangeEnd = *request.substring(dash + 1, end - dash - 1).toUInt64Strict();
+
+            NSString *responseHeaderString = [NSString stringWithFormat:
+                @"HTTP/1.1 206 Partial Content\r\n"
+                "Content-Range: bytes %llu-%llu/%llu\r\n"
+                "Content-Length: %llu\r\n\r\n",
+                rangeBegin, rangeEnd, dataLength, rangeEnd - rangeBegin];
+            NSData *responseHeader = [responseHeaderString dataUsingEncoding:NSUTF8StringEncoding];
+            NSData *responseBody = [data subdataWithRange:NSMakeRange(rangeBegin, rangeEnd - rangeBegin)];
+            NSMutableData *response = [NSMutableData dataWithCapacity:responseHeader.length + responseBody.length];
+            [response appendData:responseHeader];
+            [response appendData:responseBody];
+            TCPServer::write(socket, response.bytes, response.length);
+        }
+    });
+
+    NSURL *tempDir = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"CustomPathsTest"] isDirectory:YES];
+    NSString *path = tempDir.path;
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    EXPECT_FALSE([fileManager fileExistsAtPath:path]);
+
+    auto websiteDataStoreConfiguration = adoptNS([[_WKWebsiteDataStoreConfiguration alloc] init]);
+    [websiteDataStoreConfiguration setMediaCacheDirectory:tempDir];
+
+    auto webViewConfiguration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [webViewConfiguration setWebsiteDataStore:[[[WKWebsiteDataStore alloc] _initWithConfiguration:websiteDataStoreConfiguration.get()] autorelease]];
+
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:webViewConfiguration.get()]);
+    [webView synchronouslyLoadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1:%d/", server.port()]]]];
+
+    NSError *error = nil;
+    while (![fileManager contentsOfDirectoryAtPath:path error:&error].count)
+        Util::spinRunLoop();
+    EXPECT_FALSE(error);
+
+    done = true;
+    [[webView configuration].processPool _terminateNetworkProcess];
+
+    [fileManager removeItemAtPath:path error:&error];
+    EXPECT_FALSE(error);
+}
+#endif