1/*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2009  Jan Michael Alonzo
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 *     its contributors may be used to endorse or promote products derived
16 *     from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24    * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "DocumentLoaderGtk.h"
32
33#include "webkitwebdatasource.h"
34#include "webkitwebdatasourceprivate.h"
35#include <wtf/gobject/GRefPtr.h>
36
37using namespace WebCore;
38
39namespace WebKit {
40
41DocumentLoader::DocumentLoader(const ResourceRequest& request, const SubstituteData& substituteData)
42    : WebCore::DocumentLoader(request, substituteData)
43    , m_isDataSourceReffed(false)
44    , m_dataSource(0)
45{
46}
47
48void DocumentLoader::setDataSource(WebKitWebDataSource* dataSource)
49{
50    ASSERT(!m_dataSource);
51
52    m_dataSource = dataSource;
53    refDataSource();
54}
55
56void DocumentLoader::detachDataSource()
57{
58    unrefDataSource();
59}
60
61void DocumentLoader::attachToFrame()
62{
63    WebCore::DocumentLoader::attachToFrame();
64
65    if (m_dataSource) {
66        refDataSource();
67        return;
68    }
69
70    // We may get to here without having a datasource, when the data
71    // is coming from the page cache.
72    GRefPtr<WebKitWebDataSource> dataSource(adoptGRef(kitNew(this)));
73    setDataSource(dataSource.get());
74}
75
76void DocumentLoader::detachFromFrame()
77{
78    WebCore::DocumentLoader::detachFromFrame();
79
80    if (m_loadingResources.isEmpty())
81        unrefDataSource();
82}
83
84void DocumentLoader::increaseLoadCount(unsigned long identifier)
85{
86    ASSERT(m_dataSource);
87
88    if (m_loadingResources.contains(identifier))
89        return;
90    m_loadingResources.add(identifier);
91    refDataSource();
92}
93
94void DocumentLoader::decreaseLoadCount(unsigned long identifier)
95{
96    HashSet<unsigned long>::iterator it = m_loadingResources.find(identifier);
97
98    // It is valid for a load to be cancelled before it's started.
99    if (it == m_loadingResources.end())
100        return;
101
102    m_loadingResources.remove(it);
103
104    if (m_loadingResources.isEmpty() && !frame())
105        unrefDataSource();
106}
107
108// helper methos to avoid ref count churn
109void DocumentLoader::refDataSource()
110{
111    if (!m_dataSource || m_isDataSourceReffed)
112        return;
113    m_isDataSourceReffed = true;
114    g_object_ref(m_dataSource);
115}
116void DocumentLoader::unrefDataSource()
117{
118    if (!m_isDataSourceReffed)
119        return;
120    ASSERT(m_dataSource);
121    m_isDataSourceReffed = false;
122    g_object_unref(m_dataSource);
123    m_dataSource = 0;
124}
125
126} // end namespace WebKit
127