Add network and application cache directories to WebsiteDataStore
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / Cocoa / APIWebsiteDataStoreCocoa.mm
1 /*
2  * Copyright (C) 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "APIWebsiteDataStore.h"
28
29 #include "SandboxUtilities.h"
30
31 namespace API {
32
33 String WebsiteDataStore::cacheDirectoryFileSystemRepresentation(const String& directoryName)
34 {
35     static dispatch_once_t onceToken;
36     static NSURL *cacheURL;
37
38     dispatch_once(&onceToken, ^{
39         NSURL *url = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nullptr create:NO error:nullptr];
40         if (!url)
41             RELEASE_ASSERT_NOT_REACHED();
42
43         if (!WebKit::processHasContainer()) {
44             NSString *bundleIdentifier = [NSBundle mainBundle].bundleIdentifier;
45             if (!bundleIdentifier)
46                 bundleIdentifier = [NSProcessInfo processInfo].processName;
47             url = [url URLByAppendingPathComponent:bundleIdentifier isDirectory:YES];
48         }
49
50         cacheURL = [[url URLByAppendingPathComponent:@"WebKit" isDirectory:YES] retain];
51     });
52
53     NSURL *url = [cacheURL URLByAppendingPathComponent:directoryName isDirectory:YES];
54     if (![[NSFileManager defaultManager] createDirectoryAtURL:url withIntermediateDirectories:YES attributes:nil error:nullptr])
55         LOG_ERROR("Failed to create directory %@", url);
56
57     return url.absoluteURL.path.fileSystemRepresentation;
58 }
59
60 String WebsiteDataStore::websiteDataDirectoryFileSystemRepresentation(const String& directoryName)
61 {
62     static dispatch_once_t onceToken;
63     static NSURL *websiteDataURL;
64
65     dispatch_once(&onceToken, ^{
66         NSURL *url = [[NSFileManager defaultManager] URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nullptr create:NO error:nullptr];
67         if (!url)
68             RELEASE_ASSERT_NOT_REACHED();
69
70         url = [url URLByAppendingPathComponent:@"WebKit" isDirectory:YES];
71
72         if (!WebKit::processHasContainer()) {
73             NSString *bundleIdentifier = [NSBundle mainBundle].bundleIdentifier;
74             if (!bundleIdentifier)
75                 bundleIdentifier = [NSProcessInfo processInfo].processName;
76             url = [url URLByAppendingPathComponent:bundleIdentifier isDirectory:YES];
77         }
78
79         websiteDataURL = [[url URLByAppendingPathComponent:@"WebsiteData" isDirectory:YES] retain];
80     });
81
82     NSURL *url = [websiteDataURL URLByAppendingPathComponent:directoryName isDirectory:YES];
83     if (![[NSFileManager defaultManager] createDirectoryAtURL:url withIntermediateDirectories:YES attributes:nil error:nullptr])
84         LOG_ERROR("Failed to create directory %@", url);
85
86     return url.absoluteURL.path.fileSystemRepresentation;
87 }
88
89 WebKit::WebsiteDataStore::Configuration WebsiteDataStore::defaultDataStoreConfiguration()
90 {
91     WebKit::WebsiteDataStore::Configuration configuration;
92
93     configuration.networkCacheDirectory = cacheDirectoryFileSystemRepresentation("NetworkCache");
94     configuration.applicationCacheDirectory = cacheDirectoryFileSystemRepresentation("OfflineWebApplicationCache");
95
96     configuration.webSQLDatabaseDirectory = websiteDataDirectoryFileSystemRepresentation("WebSQL");
97     configuration.localStorageDirectory = websiteDataDirectoryFileSystemRepresentation("LocalStorage");
98
99     return configuration;
100 }
101
102 }