WebDatabaseManager.mm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #import "WebDatabaseManagerPrivate.h"
  29. #if ENABLE(SQL_DATABASE)
  30. #import "WebDatabaseManagerClient.h"
  31. #import "WebPlatformStrategies.h"
  32. #import "WebSecurityOriginInternal.h"
  33. #import <WebCore/DatabaseManager.h>
  34. #import <WebCore/SecurityOrigin.h>
  35. using namespace WebCore;
  36. NSString *WebDatabaseDirectoryDefaultsKey = @"WebDatabaseDirectory";
  37. NSString *WebDatabaseDisplayNameKey = @"WebDatabaseDisplayNameKey";
  38. NSString *WebDatabaseExpectedSizeKey = @"WebDatabaseExpectedSizeKey";
  39. NSString *WebDatabaseUsageKey = @"WebDatabaseUsageKey";
  40. NSString *WebDatabaseDidModifyOriginNotification = @"WebDatabaseDidModifyOriginNotification";
  41. NSString *WebDatabaseDidModifyDatabaseNotification = @"WebDatabaseDidModifyDatabaseNotification";
  42. NSString *WebDatabaseIdentifierKey = @"WebDatabaseIdentifierKey";
  43. static NSString *databasesDirectoryPath();
  44. @implementation WebDatabaseManager
  45. + (WebDatabaseManager *) sharedWebDatabaseManager
  46. {
  47. static WebDatabaseManager *sharedManager = [[WebDatabaseManager alloc] init];
  48. return sharedManager;
  49. }
  50. - (id)init
  51. {
  52. if (!(self = [super init]))
  53. return nil;
  54. WebPlatformStrategies::initializeIfNecessary();
  55. DatabaseManager& dbManager = DatabaseManager::manager();
  56. // Set the database root path in WebCore
  57. dbManager.initialize(databasesDirectoryPath());
  58. // Set the DatabaseManagerClient
  59. dbManager.setClient(WebDatabaseManagerClient::sharedWebDatabaseManagerClient());
  60. return self;
  61. }
  62. - (NSArray *)origins
  63. {
  64. Vector<RefPtr<SecurityOrigin> > coreOrigins;
  65. DatabaseManager::manager().origins(coreOrigins);
  66. NSMutableArray *webOrigins = [[NSMutableArray alloc] initWithCapacity:coreOrigins.size()];
  67. for (unsigned i = 0; i < coreOrigins.size(); ++i) {
  68. WebSecurityOrigin *webOrigin = [[WebSecurityOrigin alloc] _initWithWebCoreSecurityOrigin:coreOrigins[i].get()];
  69. [webOrigins addObject:webOrigin];
  70. [webOrigin release];
  71. }
  72. return [webOrigins autorelease];
  73. }
  74. - (NSArray *)databasesWithOrigin:(WebSecurityOrigin *)origin
  75. {
  76. Vector<String> nameVector;
  77. if (!DatabaseManager::manager().databaseNamesForOrigin([origin _core], nameVector))
  78. return nil;
  79. NSMutableArray *names = [[NSMutableArray alloc] initWithCapacity:nameVector.size()];
  80. for (unsigned i = 0; i < nameVector.size(); ++i)
  81. [names addObject:(NSString *)nameVector[i]];
  82. return [names autorelease];
  83. }
  84. - (NSDictionary *)detailsForDatabase:(NSString *)databaseIdentifier withOrigin:(WebSecurityOrigin *)origin
  85. {
  86. static id keys[3] = {WebDatabaseDisplayNameKey, WebDatabaseExpectedSizeKey, WebDatabaseUsageKey};
  87. DatabaseDetails details = DatabaseManager::manager().detailsForNameAndOrigin(databaseIdentifier, [origin _core]);
  88. if (details.name().isNull())
  89. return nil;
  90. id objects[3];
  91. objects[0] = details.displayName().isEmpty() ? databaseIdentifier : (NSString *)details.displayName();
  92. objects[1] = [NSNumber numberWithUnsignedLongLong:details.expectedUsage()];
  93. objects[2] = [NSNumber numberWithUnsignedLongLong:details.currentUsage()];
  94. return [[[NSDictionary alloc] initWithObjects:objects forKeys:keys count:3] autorelease];
  95. }
  96. - (void)deleteAllDatabases
  97. {
  98. DatabaseManager::manager().deleteAllDatabases();
  99. }
  100. - (BOOL)deleteOrigin:(WebSecurityOrigin *)origin
  101. {
  102. return DatabaseManager::manager().deleteOrigin([origin _core]);
  103. }
  104. - (BOOL)deleteDatabase:(NSString *)databaseIdentifier withOrigin:(WebSecurityOrigin *)origin
  105. {
  106. return DatabaseManager::manager().deleteDatabase([origin _core], databaseIdentifier);
  107. }
  108. @end
  109. static NSString *databasesDirectoryPath()
  110. {
  111. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  112. NSString *databasesDirectory = [defaults objectForKey:WebDatabaseDirectoryDefaultsKey];
  113. if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
  114. databasesDirectory = @"~/Library/WebKit/Databases";
  115. return [databasesDirectory stringByStandardizingPath];
  116. }
  117. #endif