1/*
2 * Copyright (C) 2008 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. ``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 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
26#include "config.h"
27#include "WebKitDLL.h"
28#include "WebArchive.h"
29
30#include "DOMCoreClasses.h"
31#include "MemoryStream.h"
32#include <WebCore/LegacyWebArchive.h>
33
34using namespace WebCore;
35
36// WebArchive ----------------------------------------------------------------
37
38WebArchive* WebArchive::createInstance()
39{
40    WebArchive* instance = new WebArchive(0);
41    instance->AddRef();
42    return instance;
43}
44
45WebArchive* WebArchive::createInstance(PassRefPtr<LegacyWebArchive> coreArchive)
46{
47    WebArchive* instance = new WebArchive(coreArchive);
48
49    instance->AddRef();
50    return instance;
51}
52
53WebArchive::WebArchive(PassRefPtr<LegacyWebArchive> coreArchive)
54    : m_refCount(0)
55    , m_archive(coreArchive)
56{
57    gClassCount++;
58    gClassNameCount.add("WebArchive");
59}
60
61WebArchive::~WebArchive()
62{
63    gClassCount--;
64    gClassNameCount.remove("WebArchive");
65}
66
67HRESULT STDMETHODCALLTYPE WebArchive::QueryInterface(REFIID riid, void** ppvObject)
68{
69    *ppvObject = 0;
70    if (IsEqualGUID(riid, IID_IUnknown))
71        *ppvObject = static_cast<IWebArchive*>(this);
72    else if (IsEqualGUID(riid, __uuidof(IWebArchive)))
73        *ppvObject = static_cast<IWebArchive*>(this);
74    else
75        return E_NOINTERFACE;
76
77    AddRef();
78    return S_OK;
79}
80
81ULONG STDMETHODCALLTYPE WebArchive::AddRef()
82{
83    return ++m_refCount;
84}
85
86ULONG STDMETHODCALLTYPE WebArchive::Release()
87{
88    ULONG newRef = --m_refCount;
89    if (!newRef)
90        delete(this);
91
92    return newRef;
93}
94
95HRESULT STDMETHODCALLTYPE WebArchive::initWithMainResource(
96        /* [in] */ IWebResource*,
97        /* [in, size_is(cSubResources)] */ IWebResource**,
98        /* [in] */ int,
99        /* in, size_is(cSubFrameArchives)] */ IWebArchive**,
100        /* [in] */ int)
101{
102    return E_NOTIMPL;
103}
104
105HRESULT STDMETHODCALLTYPE WebArchive::initWithData(
106        /* [in] */ IStream*)
107{
108    return E_NOTIMPL;
109}
110
111HRESULT STDMETHODCALLTYPE WebArchive::initWithNode(
112        /* [in] */ IDOMNode* node)
113{
114    if (!node)
115        return E_POINTER;
116
117    COMPtr<DOMNode> domNode(Query, node);
118    if (!domNode)
119        return E_NOINTERFACE;
120
121    m_archive = LegacyWebArchive::create(domNode->node());
122
123    return S_OK;
124}
125
126HRESULT STDMETHODCALLTYPE WebArchive::mainResource(
127        /* [out, retval] */ IWebResource**)
128{
129    return E_NOTIMPL;
130}
131
132HRESULT STDMETHODCALLTYPE WebArchive::subResources(
133        /* [out, retval] */ IEnumVARIANT**)
134{
135    return E_NOTIMPL;
136}
137
138HRESULT STDMETHODCALLTYPE WebArchive::subframeArchives(
139        /* [out, retval] */ IEnumVARIANT**)
140{
141    return E_NOTIMPL;
142}
143
144HRESULT STDMETHODCALLTYPE WebArchive::data(
145        /* [out, retval] */ IStream** stream)
146{
147    RetainPtr<CFDataRef> cfData = m_archive->rawDataRepresentation();
148    if (!cfData)
149        return E_FAIL;
150
151    RefPtr<SharedBuffer> buffer = SharedBuffer::create(CFDataGetBytePtr(cfData.get()), CFDataGetLength(cfData.get()));
152
153    return MemoryStream::createInstance(buffer).copyRefTo(stream);
154}
155