WebArchive.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (C) 2013 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. * 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 INC. AND ITS CONTRIBUTORS ``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 APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "WebArchive.h"
  27. #if PLATFORM(MAC)
  28. #include "ImmutableArray.h"
  29. #include "WebArchiveResource.h"
  30. #include "WebData.h"
  31. #include <WebCore/LegacyWebArchive.h>
  32. #include <wtf/RetainPtr.h>
  33. using namespace WebCore;
  34. namespace WebKit {
  35. PassRefPtr<WebArchive> WebArchive::create(WebArchiveResource* mainResource, ImmutableArray* subresources, ImmutableArray* subframeArchives)
  36. {
  37. return adoptRef(new WebArchive(mainResource, subresources, subframeArchives));
  38. }
  39. PassRefPtr<WebArchive> WebArchive::create(WebData* data)
  40. {
  41. return adoptRef(new WebArchive(data));
  42. }
  43. PassRefPtr<WebArchive> WebArchive::create(PassRefPtr<LegacyWebArchive> legacyWebArchive)
  44. {
  45. return adoptRef(new WebArchive(legacyWebArchive));
  46. }
  47. PassRefPtr<WebArchive> WebArchive::create(Range* range)
  48. {
  49. return adoptRef(new WebArchive(LegacyWebArchive::create(range)));
  50. }
  51. WebArchive::WebArchive(WebArchiveResource* mainResource, ImmutableArray* subresources, ImmutableArray* subframeArchives)
  52. : m_cachedMainResource(mainResource)
  53. , m_cachedSubresources(subresources)
  54. , m_cachedSubframeArchives(subframeArchives)
  55. {
  56. RefPtr<ArchiveResource> coreMainResource = m_cachedMainResource->coreArchiveResource();
  57. Vector<PassRefPtr<ArchiveResource>> coreArchiveResources;
  58. for (size_t i = 0; i < m_cachedSubresources->size(); ++i) {
  59. RefPtr<WebArchiveResource> resource = m_cachedSubresources->at<WebArchiveResource>(i);
  60. ASSERT(resource);
  61. coreArchiveResources.append(resource->coreArchiveResource());
  62. }
  63. Vector<PassRefPtr<LegacyWebArchive>> coreSubframeLegacyWebArchives;
  64. for (size_t i = 0; i < m_cachedSubframeArchives->size(); ++i) {
  65. RefPtr<WebArchive> subframeWebArchive = m_cachedSubframeArchives->at<WebArchive>(i);
  66. ASSERT(subframeWebArchive);
  67. coreSubframeLegacyWebArchives.append(subframeWebArchive->coreLegacyWebArchive());
  68. }
  69. m_legacyWebArchive = LegacyWebArchive::create(coreMainResource.release(), coreArchiveResources, coreSubframeLegacyWebArchives);
  70. }
  71. WebArchive::WebArchive(WebData* data)
  72. {
  73. RefPtr<SharedBuffer> buffer = SharedBuffer::create(data->bytes(), data->size());
  74. m_legacyWebArchive = LegacyWebArchive::create(buffer.get());
  75. }
  76. WebArchive::WebArchive(PassRefPtr<LegacyWebArchive> legacyWebArchive)
  77. : m_legacyWebArchive(legacyWebArchive)
  78. {
  79. }
  80. WebArchive::~WebArchive()
  81. {
  82. }
  83. WebArchiveResource* WebArchive::mainResource()
  84. {
  85. if (!m_cachedMainResource)
  86. m_cachedMainResource = WebArchiveResource::create(m_legacyWebArchive->mainResource());
  87. return m_cachedMainResource.get();
  88. }
  89. ImmutableArray* WebArchive::subresources()
  90. {
  91. if (!m_cachedSubresources) {
  92. Vector<RefPtr<APIObject>> subresources;
  93. subresources.reserveCapacity(m_legacyWebArchive->subresources().size());
  94. for (unsigned i = 0; i < m_legacyWebArchive->subresources().size(); ++i)
  95. subresources.append(WebArchiveResource::create(m_legacyWebArchive->subresources()[i].get()));
  96. m_cachedSubresources = ImmutableArray::adopt(subresources);
  97. }
  98. return m_cachedSubresources.get();
  99. }
  100. ImmutableArray* WebArchive::subframeArchives()
  101. {
  102. if (!m_cachedSubframeArchives) {
  103. Vector<RefPtr<APIObject>> subframeWebArchives;
  104. subframeWebArchives.reserveCapacity(m_legacyWebArchive->subframeArchives().size());
  105. for (unsigned i = 0; i < m_legacyWebArchive->subframeArchives().size(); ++i)
  106. subframeWebArchives.append(WebArchive::create(static_cast<LegacyWebArchive*>(m_legacyWebArchive->subframeArchives()[i].get())));
  107. m_cachedSubframeArchives = ImmutableArray::adopt(subframeWebArchives);
  108. }
  109. return m_cachedSubframeArchives.get();
  110. }
  111. static void releaseCFData(unsigned char*, const void* data)
  112. {
  113. // Balanced by CFRetain in WebArchive::data().
  114. CFRelease(data);
  115. }
  116. PassRefPtr<WebData> WebArchive::data()
  117. {
  118. RetainPtr<CFDataRef> rawDataRepresentation = m_legacyWebArchive->rawDataRepresentation();
  119. // Balanced by CFRelease in releaseCFData.
  120. CFRetain(rawDataRepresentation.get());
  121. return WebData::createWithoutCopying(CFDataGetBytePtr(rawDataRepresentation.get()), CFDataGetLength(rawDataRepresentation.get()), releaseCFData, rawDataRepresentation.get());
  122. }
  123. LegacyWebArchive* WebArchive::coreLegacyWebArchive()
  124. {
  125. return m_legacyWebArchive.get();
  126. }
  127. } // namespace WebKit
  128. #endif // PLATFORM(MAC)