CSSImageValue.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * (C) 1999-2003 Lars Knoll (knoll@kde.org)
  3. * Copyright (C) 2004, 2005, 2006, 2008, 2013 Apple Inc. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include "config.h"
  21. #include "CSSImageValue.h"
  22. #include "CSSCursorImageValue.h"
  23. #include "CSSParser.h"
  24. #include "CSSValueKeywords.h"
  25. #include "CachedImage.h"
  26. #include "CachedResourceLoader.h"
  27. #include "CachedResourceRequest.h"
  28. #include "CachedResourceRequestInitiators.h"
  29. #include "Document.h"
  30. #include "Element.h"
  31. #include "MemoryCache.h"
  32. #include "StyleCachedImage.h"
  33. #include "StylePendingImage.h"
  34. namespace WebCore {
  35. CSSImageValue::CSSImageValue(const String& url)
  36. : CSSValue(ImageClass)
  37. , m_url(url)
  38. , m_accessedImage(false)
  39. {
  40. }
  41. CSSImageValue::CSSImageValue(const String& url, StyleImage* image)
  42. : CSSValue(ImageClass)
  43. , m_url(url)
  44. , m_image(image)
  45. , m_accessedImage(true)
  46. {
  47. }
  48. inline void CSSImageValue::detachPendingImage()
  49. {
  50. if (m_image && m_image->isPendingImage())
  51. static_cast<StylePendingImage&>(*m_image).detachFromCSSValue();
  52. }
  53. CSSImageValue::~CSSImageValue()
  54. {
  55. detachPendingImage();
  56. }
  57. StyleImage* CSSImageValue::cachedOrPendingImage()
  58. {
  59. if (!m_image)
  60. m_image = StylePendingImage::create(this);
  61. return m_image.get();
  62. }
  63. StyleCachedImage* CSSImageValue::cachedImage(CachedResourceLoader* loader)
  64. {
  65. ASSERT(loader);
  66. if (!m_accessedImage) {
  67. m_accessedImage = true;
  68. CachedResourceRequest request(ResourceRequest(loader->document()->completeURL(m_url)));
  69. if (m_initiatorName.isEmpty())
  70. request.setInitiator(cachedResourceRequestInitiators().css);
  71. else
  72. request.setInitiator(m_initiatorName);
  73. if (CachedResourceHandle<CachedImage> cachedImage = loader->requestImage(request)) {
  74. detachPendingImage();
  75. m_image = StyleCachedImage::create(cachedImage.get());
  76. }
  77. }
  78. return (m_image && m_image->isCachedImage()) ? static_cast<StyleCachedImage*>(m_image.get()) : 0;
  79. }
  80. bool CSSImageValue::hasFailedOrCanceledSubresources() const
  81. {
  82. if (!m_image || !m_image->isCachedImage())
  83. return false;
  84. CachedResource* cachedResource = static_cast<StyleCachedImage*>(m_image.get())->cachedImage();
  85. if (!cachedResource)
  86. return true;
  87. return cachedResource->loadFailedOrCanceled();
  88. }
  89. bool CSSImageValue::equals(const CSSImageValue& other) const
  90. {
  91. return m_url == other.m_url;
  92. }
  93. String CSSImageValue::customCssText() const
  94. {
  95. return "url(" + quoteCSSURLIfNeeded(m_url) + ')';
  96. }
  97. PassRefPtr<CSSValue> CSSImageValue::cloneForCSSOM() const
  98. {
  99. // NOTE: We expose CSSImageValues as URI primitive values in CSSOM to maintain old behavior.
  100. RefPtr<CSSPrimitiveValue> uriValue = CSSPrimitiveValue::create(m_url, CSSPrimitiveValue::CSS_URI);
  101. uriValue->setCSSOMSafe();
  102. return uriValue.release();
  103. }
  104. bool CSSImageValue::knownToBeOpaque(const RenderObject* renderer) const
  105. {
  106. return m_image ? m_image->knownToBeOpaque(renderer) : false;
  107. }
  108. } // namespace WebCore