1/* 2 * Copyright (C) 2007, 2010 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 "WebSecurityOriginInternal.h" 30 31#import "WebApplicationCacheQuotaManager.h" 32#import "WebDatabaseQuotaManager.h" 33#import "WebQuotaManager.h" 34#import <WebCore/KURL.h> 35#import <WebCore/DatabaseTracker.h> 36#import <WebCore/SecurityOrigin.h> 37 38using namespace WebCore; 39 40@implementation WebSecurityOrigin 41- (id)initWithURL:(NSURL *)url 42{ 43 self = [super init]; 44 if (!self) 45 return nil; 46 47 RefPtr<SecurityOrigin> origin = SecurityOrigin::create(KURL([url absoluteURL])); 48 origin->ref(); 49 _private = reinterpret_cast<WebSecurityOriginPrivate *>(origin.get()); 50 51 return self; 52} 53 54- (NSString *)protocol 55{ 56 return reinterpret_cast<SecurityOrigin*>(_private)->protocol(); 57} 58 59- (NSString *)host 60{ 61 return reinterpret_cast<SecurityOrigin*>(_private)->host(); 62} 63 64- (NSString *)databaseIdentifier 65{ 66 return reinterpret_cast<SecurityOrigin*>(_private)->databaseIdentifier(); 67} 68 69// Deprecated. Use host instead. This needs to stay here until we ship a new Safari. 70- (NSString *)domain 71{ 72 return [self host]; 73} 74 75- (unsigned short)port 76{ 77 return reinterpret_cast<SecurityOrigin*>(_private)->port(); 78} 79 80- (BOOL)isEqual:(id)anObject 81{ 82 if (![anObject isMemberOfClass:[WebSecurityOrigin class]]) { 83 return NO; 84 } 85 86 return [self _core]->equal([anObject _core]); 87} 88 89- (void)dealloc 90{ 91 if (_private) 92 reinterpret_cast<SecurityOrigin*>(_private)->deref(); 93 if (_applicationCacheQuotaManager) 94 [(NSObject *)_applicationCacheQuotaManager release]; 95 if (_databaseQuotaManager) 96 [(NSObject *)_databaseQuotaManager release]; 97 [super dealloc]; 98} 99 100- (void)finalize 101{ 102 if (_private) 103 reinterpret_cast<SecurityOrigin*>(_private)->deref(); 104 [super finalize]; 105} 106 107@end 108 109@implementation WebSecurityOrigin (WebInternal) 110 111- (id)_initWithWebCoreSecurityOrigin:(SecurityOrigin*)origin 112{ 113 ASSERT(origin); 114 self = [super init]; 115 if (!self) 116 return nil; 117 118 origin->ref(); 119 _private = reinterpret_cast<WebSecurityOriginPrivate *>(origin); 120 121 return self; 122} 123 124- (SecurityOrigin *)_core 125{ 126 return reinterpret_cast<SecurityOrigin*>(_private); 127} 128 129@end 130 131 132// MARK: - 133// MARK: WebQuotaManagers 134 135@implementation WebSecurityOrigin (WebQuotaManagers) 136 137- (id<WebQuotaManager>)applicationCacheQuotaManager 138{ 139 if (!_applicationCacheQuotaManager) 140 _applicationCacheQuotaManager = [[WebApplicationCacheQuotaManager alloc] initWithOrigin:self]; 141 return _applicationCacheQuotaManager; 142} 143 144- (id<WebQuotaManager>)databaseQuotaManager 145{ 146 if (!_databaseQuotaManager) 147 _databaseQuotaManager = [[WebDatabaseQuotaManager alloc] initWithOrigin:self]; 148 return _databaseQuotaManager; 149} 150 151@end 152 153 154// MARK: - 155// MARK: Deprecated 156 157// FIXME: The following methods are deprecated and should removed later. 158// Clients should instead get a WebQuotaManager, and query / set the quota via the Manager. 159// NOTE: the <WebCore/DatabaseTracker.h> #include should be removed as well. 160 161@implementation WebSecurityOrigin (Deprecated) 162 163- (unsigned long long)usage 164{ 165#if ENABLE(DATABASE) 166 return DatabaseTracker::tracker().usageForOrigin(reinterpret_cast<SecurityOrigin*>(_private)); 167#else 168 return 0; 169#endif 170} 171 172- (unsigned long long)quota 173{ 174#if ENABLE(DATABASE) 175 return DatabaseTracker::tracker().quotaForOrigin(reinterpret_cast<SecurityOrigin*>(_private)); 176#else 177 return 0; 178#endif 179} 180 181- (void)setQuota:(unsigned long long)quota 182{ 183#if ENABLE(DATABASE) 184 DatabaseTracker::tracker().setQuota(reinterpret_cast<SecurityOrigin*>(_private), quota); 185#endif 186} 187 188@end 189