1/*
2 * Copyright (C) 2007, 2008 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 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#import "WebDatabaseManagerPrivate.h"
30#import "WebDatabaseManagerInternal.h"
31
32#if ENABLE(DATABASE)
33
34#import "WebDatabaseTrackerClient.h"
35#import "WebSecurityOriginInternal.h"
36
37#import <WebCore/DatabaseTracker.h>
38#import <WebCore/SecurityOrigin.h>
39
40using namespace WebCore;
41
42NSString *WebDatabaseDirectoryDefaultsKey = @"WebDatabaseDirectory";
43
44NSString *WebDatabaseDisplayNameKey = @"WebDatabaseDisplayNameKey";
45NSString *WebDatabaseExpectedSizeKey = @"WebDatabaseExpectedSizeKey";
46NSString *WebDatabaseUsageKey = @"WebDatabaseUsageKey";
47
48NSString *WebDatabaseDidModifyOriginNotification = @"WebDatabaseDidModifyOriginNotification";
49NSString *WebDatabaseDidModifyDatabaseNotification = @"WebDatabaseDidModifyDatabaseNotification";
50NSString *WebDatabaseIdentifierKey = @"WebDatabaseIdentifierKey";
51
52static NSString *databasesDirectoryPath();
53
54@implementation WebDatabaseManager
55
56+ (WebDatabaseManager *) sharedWebDatabaseManager
57{
58    static WebDatabaseManager *sharedManager = [[WebDatabaseManager alloc] init];
59    return sharedManager;
60}
61
62- (NSArray *)origins
63{
64    Vector<RefPtr<SecurityOrigin> > coreOrigins;
65    DatabaseTracker::tracker().origins(coreOrigins);
66    NSMutableArray *webOrigins = [[NSMutableArray alloc] initWithCapacity:coreOrigins.size()];
67
68    for (unsigned i = 0; i < coreOrigins.size(); ++i) {
69        WebSecurityOrigin *webOrigin = [[WebSecurityOrigin alloc] _initWithWebCoreSecurityOrigin:coreOrigins[i].get()];
70        [webOrigins addObject:webOrigin];
71        [webOrigin release];
72    }
73
74    return [webOrigins autorelease];
75}
76
77- (NSArray *)databasesWithOrigin:(WebSecurityOrigin *)origin
78{
79    Vector<String> nameVector;
80    if (!DatabaseTracker::tracker().databaseNamesForOrigin([origin _core], nameVector))
81        return nil;
82
83    NSMutableArray *names = [[NSMutableArray alloc] initWithCapacity:nameVector.size()];
84
85    for (unsigned i = 0; i < nameVector.size(); ++i)
86        [names addObject:(NSString *)nameVector[i]];
87
88    return [names autorelease];
89}
90
91- (NSDictionary *)detailsForDatabase:(NSString *)databaseIdentifier withOrigin:(WebSecurityOrigin *)origin
92{
93    static id keys[3] = {WebDatabaseDisplayNameKey, WebDatabaseExpectedSizeKey, WebDatabaseUsageKey};
94
95    DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(databaseIdentifier, [origin _core]);
96    if (details.name().isNull())
97        return nil;
98
99    id objects[3];
100    objects[0] = details.displayName().isEmpty() ? databaseIdentifier : (NSString *)details.displayName();
101    objects[1] = [NSNumber numberWithUnsignedLongLong:details.expectedUsage()];
102    objects[2] = [NSNumber numberWithUnsignedLongLong:details.currentUsage()];
103
104    return [[[NSDictionary alloc] initWithObjects:objects forKeys:keys count:3] autorelease];
105}
106
107- (void)deleteAllDatabases
108{
109    DatabaseTracker::tracker().deleteAllDatabases();
110}
111
112- (BOOL)deleteOrigin:(WebSecurityOrigin *)origin
113{
114    return DatabaseTracker::tracker().deleteOrigin([origin _core]);
115}
116
117- (BOOL)deleteDatabase:(NSString *)databaseIdentifier withOrigin:(WebSecurityOrigin *)origin
118{
119    return DatabaseTracker::tracker().deleteDatabase([origin _core], databaseIdentifier);
120}
121
122@end
123
124static NSString *databasesDirectoryPath()
125{
126    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
127    NSString *databasesDirectory = [defaults objectForKey:WebDatabaseDirectoryDefaultsKey];
128    if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
129        databasesDirectory = @"~/Library/WebKit/Databases";
130
131    return [databasesDirectory stringByStandardizingPath];
132}
133
134void WebKitInitializeDatabasesIfNecessary()
135{
136    static BOOL initialized = NO;
137    if (initialized)
138        return;
139
140    // Set the database root path in WebCore
141    DatabaseTracker::initializeTracker(databasesDirectoryPath());
142
143    // Set the DatabaseTrackerClient
144    DatabaseTracker::tracker().setClient(WebDatabaseTrackerClient::sharedWebDatabaseTrackerClient());
145
146    initialized = YES;
147}
148
149#endif
150