1/*
2 * Copyright (C) 2007 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 "WebDocumentLoader.h"
28
29#include "WebKitDLL.h"
30
31using namespace WebCore;
32
33WebDocumentLoader::WebDocumentLoader(const ResourceRequest& request, const SubstituteData& substituteData)
34    : DocumentLoader(request, substituteData)
35    , m_dataSource(0)
36    , m_detachedDataSource(0)
37{
38    gClassCount++;
39    gClassNameCount.add("WebDocumentLoader");
40}
41
42PassRefPtr<WebDocumentLoader> WebDocumentLoader::create(const ResourceRequest& req, const SubstituteData& data)
43{
44    return adoptRef(new WebDocumentLoader(req, data));
45}
46
47WebDocumentLoader::~WebDocumentLoader()
48{
49    gClassCount--;
50    gClassNameCount.remove("WebDocumentLoader");
51    if (m_dataSource) {
52        ASSERT(!m_detachedDataSource);
53        m_dataSource->Release();
54    }
55}
56
57void WebDocumentLoader::setDataSource(WebDataSource *dataSource)
58{
59    ASSERT(!m_dataSource);
60    m_dataSource = dataSource;
61    if (m_dataSource)
62        m_dataSource->AddRef();
63}
64
65WebDataSource* WebDocumentLoader::dataSource() const
66{
67    return m_dataSource;
68}
69
70void WebDocumentLoader::detachDataSource()
71{
72    // we only call detachDataSource when the WebDataSource is freed - and we won't get there if m_dataSource is not
73    // null (because that would mean the loader still has a reference to the data source)
74    ASSERT(!m_dataSource);
75    m_detachedDataSource = 0;
76}
77
78void WebDocumentLoader::attachToFrame()
79{
80    DocumentLoader::attachToFrame();
81    if (m_detachedDataSource) {
82        ASSERT(!m_dataSource);
83        setDataSource(m_detachedDataSource);
84        m_detachedDataSource = 0;
85    }
86}
87
88void WebDocumentLoader::detachFromFrame()
89{
90    DocumentLoader::detachFromFrame();
91    m_detachedDataSource = m_dataSource;
92    if (m_dataSource) {
93        WebDataSource* dataSourceToBeReleased = m_dataSource;
94        // It's important to null out m_dataSource before calling release on the data source.  That release can cause the data
95        // source to be deleted - which ends up calling loader->detachDataSource() which makes the assumption that the loader no
96        // longer holds a reference to the data source.
97        m_dataSource = 0;
98        dataSourceToBeReleased->Release();
99    }
100}
101