• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
52@implementation WebDatabaseManager
53
54+ (WebDatabaseManager *) sharedWebDatabaseManager
55{
56    static WebDatabaseManager *sharedManager = [[WebDatabaseManager alloc] init];
57    return sharedManager;
58}
59
60- (NSArray *)origins
61{
62    Vector<RefPtr<SecurityOrigin> > coreOrigins;
63    DatabaseTracker::tracker().origins(coreOrigins);
64    NSMutableArray *webOrigins = [[NSMutableArray alloc] initWithCapacity:coreOrigins.size()];
65
66    for (unsigned i = 0; i < coreOrigins.size(); ++i) {
67        WebSecurityOrigin *webOrigin = [[WebSecurityOrigin alloc] _initWithWebCoreSecurityOrigin:coreOrigins[i].get()];
68        [webOrigins addObject:webOrigin];
69        [webOrigin release];
70    }
71
72    return [webOrigins autorelease];
73}
74
75- (NSArray *)databasesWithOrigin:(WebSecurityOrigin *)origin
76{
77    Vector<String> nameVector;
78    if (!DatabaseTracker::tracker().databaseNamesForOrigin([origin _core], nameVector))
79        return nil;
80
81    NSMutableArray *names = [[NSMutableArray alloc] initWithCapacity:nameVector.size()];
82
83    for (unsigned i = 0; i < nameVector.size(); ++i)
84        [names addObject:(NSString *)nameVector[i]];
85
86    return [names autorelease];
87}
88
89- (NSDictionary *)detailsForDatabase:(NSString *)databaseIdentifier withOrigin:(WebSecurityOrigin *)origin
90{
91    static id keys[3] = {WebDatabaseDisplayNameKey, WebDatabaseExpectedSizeKey, WebDatabaseUsageKey};
92
93    DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(databaseIdentifier, [origin _core]);
94    if (details.name().isNull())
95        return nil;
96
97    id objects[3];
98    objects[0] = details.displayName().isEmpty() ? databaseIdentifier : (NSString *)details.displayName();
99    objects[1] = [NSNumber numberWithUnsignedLongLong:details.expectedUsage()];
100    objects[2] = [NSNumber numberWithUnsignedLongLong:details.currentUsage()];
101
102    return [[[NSDictionary alloc] initWithObjects:objects forKeys:keys count:3] autorelease];
103}
104
105- (void)deleteAllDatabases
106{
107    DatabaseTracker::tracker().deleteAllDatabases();
108}
109
110- (void)deleteOrigin:(WebSecurityOrigin *)origin
111{
112    DatabaseTracker::tracker().deleteOrigin([origin _core]);
113}
114
115- (void)deleteDatabase:(NSString *)databaseIdentifier withOrigin:(WebSecurityOrigin *)origin
116{
117    DatabaseTracker::tracker().deleteDatabase([origin _core], databaseIdentifier);
118}
119
120@end
121
122void WebKitInitializeDatabasesIfNecessary()
123{
124    static BOOL initialized = NO;
125    if (initialized)
126        return;
127
128    // Set the database root path in WebCore
129    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
130
131    NSString *databasesDirectory = [defaults objectForKey:WebDatabaseDirectoryDefaultsKey];
132    if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
133        databasesDirectory = @"~/Library/WebKit/Databases";
134
135    DatabaseTracker::tracker().setDatabaseDirectoryPath([databasesDirectory stringByStandardizingPath]);
136
137    // Set the DatabaseTrackerClient
138    DatabaseTracker::tracker().setClient(WebDatabaseTrackerClient::sharedWebDatabaseTrackerClient());
139
140    initialized = YES;
141}
142
143#endif
144