1/* 2 * Copyright (C) 2011 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#import "WebSecurityOriginInternal.h" 27#import "WebStorageManagerPrivate.h" 28#import "WebStorageManagerInternal.h" 29#import "WebStorageTrackerClient.h" 30 31#import <WebCore/PageGroup.h> 32#import <WebCore/SecurityOrigin.h> 33#import <WebCore/StorageTracker.h> 34#import <pthread.h> 35 36using namespace WebCore; 37 38NSString * const WebStorageDirectoryDefaultsKey = @"WebKitLocalStorageDatabasePathPreferenceKey"; 39NSString * const WebStorageDidModifyOriginNotification = @"WebStorageDidModifyOriginNotification"; 40 41static NSString *sLocalStoragePath; 42static void initializeLocalStoragePath(); 43static pthread_once_t registerLocalStoragePath = PTHREAD_ONCE_INIT; 44 45@implementation WebStorageManager 46 47+ (WebStorageManager *)sharedWebStorageManager 48{ 49 static WebStorageManager *sharedManager = [[WebStorageManager alloc] init]; 50 return sharedManager; 51} 52 53#if PLATFORM(IOS) 54- (id)init 55{ 56 if (!(self = [super init])) 57 return nil; 58 59 WebKitInitializeStorageIfNecessary(); 60 61 return self; 62} 63#endif 64 65- (NSArray *)origins 66{ 67 Vector<RefPtr<SecurityOrigin>> coreOrigins; 68 69 StorageTracker::tracker().origins(coreOrigins); 70 71 NSMutableArray *webOrigins = [[NSMutableArray alloc] initWithCapacity:coreOrigins.size()]; 72 73 for (size_t i = 0; i < coreOrigins.size(); ++i) { 74 WebSecurityOrigin *webOrigin = [[WebSecurityOrigin alloc] _initWithWebCoreSecurityOrigin:coreOrigins[i].get()]; 75 [webOrigins addObject:webOrigin]; 76 [webOrigin release]; 77 } 78 79 return [webOrigins autorelease]; 80} 81 82- (void)deleteAllOrigins 83{ 84 StorageTracker::tracker().deleteAllOrigins(); 85#if PLATFORM(IOS) 86 // FIXME: This needs to be removed once StorageTrackers in multiple processes 87 // are in sync: <rdar://problem/9567500> Remove Website Data pane is not kept in sync with Safari 88 [[NSFileManager defaultManager] removeItemAtPath:[WebStorageManager _storageDirectoryPath] error:NULL]; 89#endif 90} 91 92- (void)deleteOrigin:(WebSecurityOrigin *)origin 93{ 94 StorageTracker::tracker().deleteOrigin([origin _core]); 95} 96 97- (unsigned long long)diskUsageForOrigin:(WebSecurityOrigin *)origin 98{ 99 return StorageTracker::tracker().diskUsageForOrigin([origin _core]); 100} 101 102- (void)syncLocalStorage 103{ 104 PageGroup::syncLocalStorage(); 105} 106 107- (void)syncFileSystemAndTrackerDatabase 108{ 109 StorageTracker::tracker().syncFileSystemAndTrackerDatabase(); 110} 111 112+ (NSString *)_storageDirectoryPath 113{ 114 pthread_once(®isterLocalStoragePath, initializeLocalStoragePath); 115 return sLocalStoragePath; 116} 117 118+ (void)setStorageDatabaseIdleInterval:(double)interval 119{ 120 StorageTracker::tracker().setStorageDatabaseIdleInterval(interval); 121} 122 123+ (void)closeIdleLocalStorageDatabases 124{ 125 PageGroup::closeIdleLocalStorageDatabases(); 126} 127 128static void initializeLocalStoragePath() 129{ 130 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 131 sLocalStoragePath = [defaults objectForKey:WebStorageDirectoryDefaultsKey]; 132 if (!sLocalStoragePath || ![sLocalStoragePath isKindOfClass:[NSString class]]) { 133 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 134 NSString *libraryDirectory = [paths objectAtIndex:0]; 135 sLocalStoragePath = [libraryDirectory stringByAppendingPathComponent:@"WebKit/LocalStorage"]; 136 } 137 sLocalStoragePath = [[sLocalStoragePath stringByStandardizingPath] retain]; 138} 139 140void WebKitInitializeStorageIfNecessary() 141{ 142 static BOOL initialized = NO; 143 if (initialized) 144 return; 145 146 StorageTracker::initializeTracker([WebStorageManager _storageDirectoryPath], WebStorageTrackerClient::sharedWebStorageTrackerClient()); 147 148 initialized = YES; 149} 150 151@end 152