WebCache.mm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2006 Apple Computer, 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. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import "WebCache.h"
  26. #import "WebNSObjectExtras.h"
  27. #import "WebPreferences.h"
  28. #import "WebSystemInterface.h"
  29. #import "WebView.h"
  30. #import "WebViewInternal.h"
  31. #import <WebCore/ApplicationCacheStorage.h>
  32. #import <WebCore/CrossOriginPreflightResultCache.h>
  33. #import <WebCore/MemoryCache.h>
  34. #import <WebCore/RunLoop.h>
  35. #import <runtime/InitializeThreading.h>
  36. #import <wtf/MainThread.h>
  37. @implementation WebCache
  38. + (void)initialize
  39. {
  40. JSC::initializeThreading();
  41. WTF::initializeMainThreadToProcessMainThread();
  42. WebCore::RunLoop::initializeMainRunLoop();
  43. InitWebCoreSystemInterface();
  44. }
  45. + (NSArray *)statistics
  46. {
  47. WebCore::MemoryCache::Statistics s = WebCore::memoryCache()->getStatistics();
  48. return [NSArray arrayWithObjects:
  49. [NSDictionary dictionaryWithObjectsAndKeys:
  50. [NSNumber numberWithInt:s.images.count], @"Images",
  51. [NSNumber numberWithInt:s.cssStyleSheets.count], @"CSS",
  52. #if ENABLE(XSLT)
  53. [NSNumber numberWithInt:s.xslStyleSheets.count], @"XSL",
  54. #else
  55. [NSNumber numberWithInt:0], @"XSL",
  56. #endif
  57. [NSNumber numberWithInt:s.scripts.count], @"JavaScript",
  58. nil],
  59. [NSDictionary dictionaryWithObjectsAndKeys:
  60. [NSNumber numberWithInt:s.images.size], @"Images",
  61. [NSNumber numberWithInt:s.cssStyleSheets.size] ,@"CSS",
  62. #if ENABLE(XSLT)
  63. [NSNumber numberWithInt:s.xslStyleSheets.size], @"XSL",
  64. #else
  65. [NSNumber numberWithInt:0], @"XSL",
  66. #endif
  67. [NSNumber numberWithInt:s.scripts.size], @"JavaScript",
  68. nil],
  69. [NSDictionary dictionaryWithObjectsAndKeys:
  70. [NSNumber numberWithInt:s.images.liveSize], @"Images",
  71. [NSNumber numberWithInt:s.cssStyleSheets.liveSize] ,@"CSS",
  72. #if ENABLE(XSLT)
  73. [NSNumber numberWithInt:s.xslStyleSheets.liveSize], @"XSL",
  74. #else
  75. [NSNumber numberWithInt:0], @"XSL",
  76. #endif
  77. [NSNumber numberWithInt:s.scripts.liveSize], @"JavaScript",
  78. nil],
  79. [NSDictionary dictionaryWithObjectsAndKeys:
  80. [NSNumber numberWithInt:s.images.decodedSize], @"Images",
  81. [NSNumber numberWithInt:s.cssStyleSheets.decodedSize] ,@"CSS",
  82. #if ENABLE(XSLT)
  83. [NSNumber numberWithInt:s.xslStyleSheets.decodedSize], @"XSL",
  84. #else
  85. [NSNumber numberWithInt:0], @"XSL",
  86. #endif
  87. [NSNumber numberWithInt:s.scripts.decodedSize], @"JavaScript",
  88. nil],
  89. [NSDictionary dictionaryWithObjectsAndKeys:
  90. [NSNumber numberWithInt:s.images.purgeableSize], @"Images",
  91. [NSNumber numberWithInt:s.cssStyleSheets.purgeableSize] ,@"CSS",
  92. #if ENABLE(XSLT)
  93. [NSNumber numberWithInt:s.xslStyleSheets.purgeableSize], @"XSL",
  94. #else
  95. [NSNumber numberWithInt:0], @"XSL",
  96. #endif
  97. [NSNumber numberWithInt:s.scripts.purgeableSize], @"JavaScript",
  98. nil],
  99. [NSDictionary dictionaryWithObjectsAndKeys:
  100. [NSNumber numberWithInt:s.images.purgedSize], @"Images",
  101. [NSNumber numberWithInt:s.cssStyleSheets.purgedSize] ,@"CSS",
  102. #if ENABLE(XSLT)
  103. [NSNumber numberWithInt:s.xslStyleSheets.purgedSize], @"XSL",
  104. #else
  105. [NSNumber numberWithInt:0], @"XSL",
  106. #endif
  107. [NSNumber numberWithInt:s.scripts.purgedSize], @"JavaScript",
  108. nil],
  109. nil];
  110. }
  111. + (void)empty
  112. {
  113. // Toggling the cache model like this forces the cache to evict all its in-memory resources.
  114. WebCacheModel cacheModel = [WebView _cacheModel];
  115. [WebView _setCacheModel:WebCacheModelDocumentViewer];
  116. [WebView _setCacheModel:cacheModel];
  117. // Empty the application cache.
  118. WebCore::cacheStorage().empty();
  119. // Empty the Cross-Origin Preflight cache
  120. WebCore::CrossOriginPreflightResultCache::shared().empty();
  121. }
  122. + (void)setDisabled:(BOOL)disabled
  123. {
  124. if (!pthread_main_np())
  125. return [[self _webkit_invokeOnMainThread] setDisabled:disabled];
  126. WebCore::memoryCache()->setDisabled(disabled);
  127. }
  128. + (BOOL)isDisabled
  129. {
  130. return WebCore::memoryCache()->disabled();
  131. }
  132. @end