WebMemoryCacheInfo.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2013 Sony Computer Entertainment Inc.
  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 SONY COMPUTER ENTERTAINMENT INC. ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SONY COMPUTER ENTERTAINMENT INC.
  17. * OR 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. #include "config.h"
  26. #include "WebMemoryCacheInfo.h"
  27. #include "MemoryCache.h"
  28. namespace {
  29. JSObjectRef s_object;
  30. WebCore::MemoryCache::Statistics s_statistics;
  31. // Define wrapper functions to get member variables of MemoryCache::Statistics.
  32. typedef const WebCore::MemoryCache::TypeStatistic& (*StatisticGetter)(void);
  33. #define DEFINE_STATISTIC_GETTER(propName) \
  34. const WebCore::MemoryCache::TypeStatistic& propName() { return s_statistics.propName; }
  35. DEFINE_STATISTIC_GETTER(images)
  36. DEFINE_STATISTIC_GETTER(cssStyleSheets)
  37. DEFINE_STATISTIC_GETTER(scripts)
  38. DEFINE_STATISTIC_GETTER(xslStyleSheets)
  39. DEFINE_STATISTIC_GETTER(fonts)
  40. // A class template to provide JS properties.
  41. #define DEFINE_PROPERTY_GETTER(propName) \
  42. static JSValueRef propName(JSContextRef ctx, JSObjectRef, JSStringRef, JSValueRef*) { return JSValueMakeNumber(ctx, stat().propName); }
  43. #define DEFINE_PROPERTY_ENTRY(propName) \
  44. { #propName, propName, 0, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
  45. template<StatisticGetter stat>
  46. class Statistic {
  47. public:
  48. static JSStaticValue properties[];
  49. DEFINE_PROPERTY_GETTER(count)
  50. DEFINE_PROPERTY_GETTER(size)
  51. DEFINE_PROPERTY_GETTER(liveSize)
  52. DEFINE_PROPERTY_GETTER(decodedSize)
  53. DEFINE_PROPERTY_GETTER(purgeableSize)
  54. DEFINE_PROPERTY_GETTER(purgedSize)
  55. private:
  56. };
  57. template <StatisticGetter stat>
  58. JSStaticValue Statistic<stat>::properties[] = {
  59. DEFINE_PROPERTY_ENTRY(count)
  60. DEFINE_PROPERTY_ENTRY(size)
  61. DEFINE_PROPERTY_ENTRY(liveSize)
  62. DEFINE_PROPERTY_ENTRY(decodedSize)
  63. DEFINE_PROPERTY_ENTRY(purgeableSize)
  64. DEFINE_PROPERTY_ENTRY(purgedSize)
  65. { 0, 0, 0, 0 }
  66. };
  67. }
  68. namespace WebKit {
  69. void WebMemoryCacheInfo::registerObject(JSGlobalContextRef ctx)
  70. {
  71. static JSStringRef protoName = JSStringCreateWithUTF8CString("__proto__");
  72. static JSStringRef performanceName = JSStringCreateWithUTF8CString("performance");
  73. static JSStringRef sceMemoryCacheInfoName = JSStringCreateWithUTF8CString("sceMemoryCacheInfo");
  74. if (s_object)
  75. JSValueUnprotect(ctx, s_object);
  76. s_object = create(ctx);
  77. JSValueProtect(ctx, s_object);
  78. JSValueRef performanceValue = JSObjectGetProperty(ctx, JSContextGetGlobalObject(ctx), performanceName, 0);
  79. if (!JSValueIsObject(ctx, performanceValue))
  80. return;
  81. JSValueRef protoValue = JSObjectGetProperty(ctx, JSValueToObject(ctx, performanceValue, 0), protoName, 0);
  82. if (!JSValueIsObject(ctx, protoValue))
  83. return;
  84. JSObjectSetProperty(ctx, JSValueToObject(ctx, protoValue, 0), sceMemoryCacheInfoName, s_object,
  85. kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, 0);
  86. }
  87. #define ADD_PROPERTY(klassName, propName) \
  88. static JSStringRef propName##Str = JSStringCreateWithUTF8CString(#propName); \
  89. JSClassDefinition klassName = { 0 }; \
  90. klassName.className = #klassName "CacheInfo"; \
  91. klassName.staticValues = Statistic<&propName>::properties; \
  92. JSObjectSetProperty(ctx, sceObj, propName##Str, JSObjectMake(ctx, JSClassCreate(&klassName), 0), \
  93. kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, 0);
  94. JSObjectRef WebMemoryCacheInfo::create(JSContextRef ctx)
  95. {
  96. JSClassDefinition jsClassDefinition = {0};
  97. jsClassDefinition.className = "SceMemoryCacheInfo";
  98. jsClassDefinition.getProperty = getProperty;
  99. JSObjectRef sceObj = JSObjectMake(ctx, JSClassCreate(&jsClassDefinition), 0);
  100. ADD_PROPERTY(Images, images);
  101. ADD_PROPERTY(CssStyleSheets, cssStyleSheets);
  102. ADD_PROPERTY(Scripts, scripts);
  103. ADD_PROPERTY(XslStyleSheets, xslStyleSheets);
  104. ADD_PROPERTY(Fonts, fonts);
  105. return sceObj;
  106. }
  107. JSValueRef WebMemoryCacheInfo::getProperty(JSContextRef ctx, JSObjectRef, JSStringRef propertyName, JSValueRef*)
  108. {
  109. doRefresh(ctx);
  110. // continue with regular property lookup mechanism (static properties, then parent chain)
  111. return 0;
  112. }
  113. void WebMemoryCacheInfo::refreshData(JSContextRef ctx)
  114. {
  115. doRefresh(ctx);
  116. }
  117. void WebMemoryCacheInfo::doRefresh(JSContextRef)
  118. {
  119. s_statistics = WebCore::memoryCache()->getStatistics();
  120. }
  121. }