initial import
[vuplus_webkit] / Source / WebKit / mac / WebView / WebDocumentLoaderMac.mm
1 /*
2  * Copyright (C) 2006, 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23     * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import "WebDocumentLoaderMac.h"
30
31 #import "WebKitVersionChecks.h"
32 #import "WebView.h"
33
34 using namespace WebCore;
35
36 WebDocumentLoaderMac::WebDocumentLoaderMac(const ResourceRequest& request, const SubstituteData& substituteData)
37     : DocumentLoader(request, substituteData)
38     , m_dataSource(nil)
39     , m_isDataSourceRetained(false)
40 {
41 }
42
43 static inline bool needsDataLoadWorkaround(WebView *webView)
44 {
45     static bool needsWorkaround = !WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITHOUT_ADOBE_INSTALLER_QUIRK) 
46                                   && [[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.adobe.Installers.Setup"];
47     return needsWorkaround;
48 }
49
50 void WebDocumentLoaderMac::setDataSource(WebDataSource *dataSource, WebView *webView)
51 {
52     ASSERT(!m_dataSource);
53     ASSERT(!m_isDataSourceRetained);
54
55     m_dataSource = dataSource;
56     retainDataSource();
57
58     m_resourceLoadDelegate = [webView resourceLoadDelegate];
59     m_downloadDelegate = [webView downloadDelegate];
60     
61     // Some clients run the run loop in a way that prevents the data load timer
62     // from firing. We work around that issue here. See <rdar://problem/5266289>
63     // and <rdar://problem/5049509>.
64     if (needsDataLoadWorkaround(webView))
65         m_deferMainResourceDataLoad = false;
66 }
67
68 WebDataSource *WebDocumentLoaderMac::dataSource() const
69 {
70     return m_dataSource;
71 }
72
73 void WebDocumentLoaderMac::attachToFrame()
74 {
75     DocumentLoader::attachToFrame();
76
77     retainDataSource();
78 }
79
80 void WebDocumentLoaderMac::detachFromFrame()
81 {
82     DocumentLoader::detachFromFrame();
83
84     if (m_loadingResources.isEmpty())
85         releaseDataSource();
86
87     // FIXME: What prevents the data source from getting deallocated while the
88     // frame is not attached?
89 }
90
91 void WebDocumentLoaderMac::increaseLoadCount(unsigned long identifier)
92 {
93     ASSERT(m_dataSource);
94
95     if (m_loadingResources.contains(identifier))
96         return;
97     m_loadingResources.add(identifier);
98
99     retainDataSource();
100 }
101
102 void WebDocumentLoaderMac::decreaseLoadCount(unsigned long identifier)
103 {
104     HashSet<unsigned long>::iterator it = m_loadingResources.find(identifier);
105     
106     // It is valid for a load to be cancelled before it's started.
107     if (it == m_loadingResources.end())
108         return;
109     
110     m_loadingResources.remove(it);
111     
112     if (m_loadingResources.isEmpty()) {
113         m_resourceLoadDelegate = 0;
114         m_downloadDelegate = 0;
115         if (!frame())
116             releaseDataSource();
117     }
118 }
119
120 void WebDocumentLoaderMac::retainDataSource()
121 {
122     if (m_isDataSourceRetained || !m_dataSource)
123         return;
124     m_isDataSourceRetained = true;
125     CFRetain(m_dataSource);
126 }
127
128 void WebDocumentLoaderMac::releaseDataSource()
129 {
130     if (!m_isDataSourceRetained)
131         return;
132     ASSERT(m_dataSource);
133     m_isDataSourceRetained = false;
134     CFRelease(m_dataSource);
135 }
136
137 void WebDocumentLoaderMac::detachDataSource()
138 {
139     ASSERT(!m_isDataSourceRetained);
140     m_dataSource = nil;
141 }