WebSecurityOrigin.mm 5.3 KB

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