/* * Copyright (C) 2013 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" #include "APIWebArchive.h" #if PLATFORM(COCOA) #include "APIArray.h" #include "APIData.h" #include "APIWebArchiveResource.h" #include #include using namespace WebCore; namespace API { Ref WebArchive::create(WebArchiveResource* mainResource, PassRefPtr subresources, PassRefPtr subframeArchives) { return adoptRef(*new WebArchive(mainResource, subresources, subframeArchives)); } Ref WebArchive::create(API::Data* data) { return adoptRef(*new WebArchive(data)); } Ref WebArchive::create(PassRefPtr legacyWebArchive) { return adoptRef(*new WebArchive(legacyWebArchive)); } Ref WebArchive::create(Range* range) { return adoptRef(*new WebArchive(LegacyWebArchive::create(range))); } WebArchive::WebArchive(WebArchiveResource* mainResource, PassRefPtr subresources, PassRefPtr subframeArchives) : m_cachedMainResource(mainResource) , m_cachedSubresources(subresources) , m_cachedSubframeArchives(subframeArchives) { RefPtr coreMainResource = m_cachedMainResource->coreArchiveResource(); Vector> coreArchiveResources; coreArchiveResources.reserveInitialCapacity(m_cachedSubresources->size()); for (size_t i = 0; i < m_cachedSubresources->size(); ++i) { RefPtr resource = m_cachedSubresources->at(i); ASSERT(resource); coreArchiveResources.uncheckedAppend(resource->coreArchiveResource()); } Vector> coreSubframeLegacyWebArchives; coreSubframeLegacyWebArchives.reserveInitialCapacity(m_cachedSubframeArchives->size()); for (size_t i = 0; i < m_cachedSubframeArchives->size(); ++i) { RefPtr subframeWebArchive = m_cachedSubframeArchives->at(i); ASSERT(subframeWebArchive); coreSubframeLegacyWebArchives.uncheckedAppend(subframeWebArchive->coreLegacyWebArchive()); } m_legacyWebArchive = LegacyWebArchive::create(coreMainResource.release(), WTF::move(coreArchiveResources), WTF::move(coreSubframeLegacyWebArchives)); } WebArchive::WebArchive(API::Data* data) { RefPtr buffer = SharedBuffer::create(data->bytes(), data->size()); m_legacyWebArchive = LegacyWebArchive::create(buffer.get()); } WebArchive::WebArchive(PassRefPtr legacyWebArchive) : m_legacyWebArchive(legacyWebArchive) { } WebArchive::~WebArchive() { } WebArchiveResource* WebArchive::mainResource() { if (!m_cachedMainResource) m_cachedMainResource = WebArchiveResource::create(m_legacyWebArchive->mainResource()); return m_cachedMainResource.get(); } API::Array* WebArchive::subresources() { if (!m_cachedSubresources) { Vector> subresources; subresources.reserveInitialCapacity(m_legacyWebArchive->subresources().size()); for (const auto& subresource : m_legacyWebArchive->subresources()) subresources.uncheckedAppend(WebArchiveResource::create(subresource)); m_cachedSubresources = API::Array::create(WTF::move(subresources)); } return m_cachedSubresources.get(); } API::Array* WebArchive::subframeArchives() { if (!m_cachedSubframeArchives) { Vector> subframeWebArchives; subframeWebArchives.reserveInitialCapacity(m_legacyWebArchive->subframeArchives().size()); for (const auto& subframeArchive : m_legacyWebArchive->subframeArchives()) subframeWebArchives.uncheckedAppend(WebArchive::create(static_cast(subframeArchive.get()))); m_cachedSubframeArchives = API::Array::create(WTF::move(subframeWebArchives)); } return m_cachedSubframeArchives.get(); } static void releaseCFData(unsigned char*, const void* data) { // Balanced by CFRetain in WebArchive::data(). CFRelease(data); } Ref WebArchive::data() { RetainPtr rawDataRepresentation = m_legacyWebArchive->rawDataRepresentation(); // Balanced by CFRelease in releaseCFData. CFRetain(rawDataRepresentation.get()); return API::Data::createWithoutCopying(CFDataGetBytePtr(rawDataRepresentation.get()), CFDataGetLength(rawDataRepresentation.get()), releaseCFData, rawDataRepresentation.get()); } LegacyWebArchive* WebArchive::coreLegacyWebArchive() { return m_legacyWebArchive.get(); } } // namespace WebKit #endif // PLATFORM(COCOA)