Home | History | Annotate | Download | only in WebCoreSupport
      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 "webkitprivate.h"
     34 #include "webkitwebdatasource.h"
     35 
     36 using namespace WebCore;
     37 
     38 namespace WebKit {
     39 
     40 DocumentLoader::DocumentLoader(const ResourceRequest& request, const SubstituteData& substituteData)
     41     : WebCore::DocumentLoader(request, substituteData)
     42     , m_isDataSourceReffed(false)
     43     , m_dataSource(0)
     44 {
     45 }
     46 
     47 void DocumentLoader::setDataSource(WebKitWebDataSource* dataSource)
     48 {
     49     ASSERT(!m_dataSource);
     50 
     51     m_dataSource = dataSource;
     52     refDataSource();
     53 }
     54 
     55 void DocumentLoader::detachDataSource()
     56 {
     57     unrefDataSource();
     58 }
     59 
     60 void DocumentLoader::attachToFrame()
     61 {
     62     WebCore::DocumentLoader::attachToFrame();
     63 
     64     if (m_dataSource) {
     65         refDataSource();
     66         return;
     67     }
     68 
     69     // We may get to here without having a datasource, when the data
     70     // is coming from the page cache.
     71     WebKitWebDataSource* dataSource = webkit_web_data_source_new_with_loader(this);
     72     setDataSource(dataSource);
     73     g_object_unref(dataSource);
     74 }
     75 
     76 void DocumentLoader::detachFromFrame()
     77 {
     78     WebCore::DocumentLoader::detachFromFrame();
     79 
     80     if (m_loadingResources.isEmpty())
     81         unrefDataSource();
     82 }
     83 
     84 void 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 
     94 void 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
    109 void DocumentLoader::refDataSource()
    110 {
    111     if (!m_dataSource || m_isDataSourceReffed)
    112         return;
    113     m_isDataSourceReffed = true;
    114     g_object_ref(m_dataSource);
    115 }
    116 void 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