Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / loader / FrameFetchContext.cpp
1 /*
2  * Copyright (C) 2013 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "core/loader/FrameFetchContext.h"
33
34 #include "core/dom/Document.h"
35 #include "core/inspector/InspectorInstrumentation.h"
36 #include "core/loader/DocumentLoader.h"
37 #include "core/loader/FrameLoader.h"
38 #include "core/loader/FrameLoaderClient.h"
39 #include "core/loader/ProgressTracker.h"
40 #include "core/frame/Frame.h"
41 #include "core/page/Page.h"
42 #include "core/frame/Settings.h"
43 #include "platform/weborigin/SecurityPolicy.h"
44
45 namespace WebCore {
46
47 static const char defaultAcceptHeader[] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
48
49 FrameFetchContext::FrameFetchContext(Frame* frame)
50     : m_frame(frame)
51 {
52 }
53
54 void FrameFetchContext::reportLocalLoadFailed(const KURL& url)
55 {
56     FrameLoader::reportLocalLoadFailed(m_frame, url.elidedString());
57 }
58
59 void FrameFetchContext::addAdditionalRequestHeaders(Document* document, ResourceRequest& request, FetchResourceType type)
60 {
61     bool isMainResource = type == FetchMainResource;
62     if (!isMainResource) {
63         String outgoingReferrer;
64         String outgoingOrigin;
65         if (request.httpReferrer().isNull()) {
66             outgoingReferrer = document->outgoingReferrer();
67             outgoingOrigin = document->outgoingOrigin();
68         } else {
69             outgoingReferrer = request.httpReferrer();
70             outgoingOrigin = SecurityOrigin::createFromString(outgoingReferrer)->toString();
71         }
72
73         outgoingReferrer = SecurityPolicy::generateReferrerHeader(document->referrerPolicy(), request.url(), outgoingReferrer);
74         if (outgoingReferrer.isEmpty())
75             request.clearHTTPReferrer();
76         else if (!request.httpReferrer())
77             request.setHTTPReferrer(Referrer(outgoingReferrer, document->referrerPolicy()));
78
79         FrameLoader::addHTTPOriginIfNeeded(request, AtomicString(outgoingOrigin));
80     }
81
82     if (isMainResource && m_frame->isMainFrame())
83         request.setFirstPartyForCookies(request.url());
84     else
85         request.setFirstPartyForCookies(m_frame->tree().top()->document()->firstPartyForCookies());
86
87     // The remaining modifications are only necessary for HTTP and HTTPS.
88     if (!request.url().isEmpty() && !request.url().protocolIsInHTTPFamily())
89         return;
90
91     m_frame->loader().applyUserAgent(request);
92
93     if (request.cachePolicy() == ReloadIgnoringCacheData) {
94         if (m_frame->loader().loadType() == FrameLoadTypeReload) {
95             request.setHTTPHeaderField("Cache-Control", "max-age=0");
96         } else if (m_frame->loader().loadType() == FrameLoadTypeReloadFromOrigin) {
97             request.setHTTPHeaderField("Cache-Control", "no-cache");
98             request.setHTTPHeaderField("Pragma", "no-cache");
99         }
100     }
101
102     if (isMainResource)
103         request.setHTTPAccept(defaultAcceptHeader);
104
105     // Default to sending an empty Origin header if one hasn't been set yet.
106     FrameLoader::addHTTPOriginIfNeeded(request, nullAtom);
107 }
108
109 CachePolicy FrameFetchContext::cachePolicy(Document* document) const
110 {
111     if (document && document->loadEventFinished())
112         return CachePolicyVerify;
113
114     FrameLoadType loadType = m_frame->loader().loadType();
115     if (loadType == FrameLoadTypeReloadFromOrigin)
116         return CachePolicyReload;
117
118     if (Frame* parentFrame = m_frame->tree().parent()) {
119         CachePolicy parentCachePolicy = parentFrame->loader().fetchContext().cachePolicy(parentFrame->document());
120         if (parentCachePolicy != CachePolicyVerify)
121             return parentCachePolicy;
122     }
123
124     if (loadType == FrameLoadTypeReload)
125         return CachePolicyRevalidate;
126
127     DocumentLoader* loader = document ? document->loader() : 0;
128     if (loader && loader->request().cachePolicy() == ReturnCacheDataElseLoad)
129         return CachePolicyHistoryBuffer;
130     return CachePolicyVerify;
131
132 }
133
134 // FIXME(http://crbug.com/274173):
135 // |loader| can be null if the resource is loaded from imported document.
136 // This means inspector, which uses DocumentLoader as an grouping entity,
137 // cannot see imported documents.
138 inline DocumentLoader* FrameFetchContext::ensureLoader(DocumentLoader* loader)
139 {
140     return loader ? loader : m_frame->loader().documentLoader();
141 }
142
143 void FrameFetchContext::dispatchDidChangeResourcePriority(unsigned long identifier, ResourceLoadPriority loadPriority)
144 {
145     m_frame->loader().client()->dispatchDidChangeResourcePriority(identifier, loadPriority);
146 }
147
148 void FrameFetchContext::dispatchWillSendRequest(DocumentLoader* loader, unsigned long identifier, ResourceRequest& request, const ResourceResponse& redirectResponse, const FetchInitiatorInfo& initiatorInfo)
149 {
150     m_frame->loader().applyUserAgent(request);
151     m_frame->loader().client()->dispatchWillSendRequest(loader, identifier, request, redirectResponse);
152     InspectorInstrumentation::willSendRequest(m_frame, identifier, ensureLoader(loader), request, redirectResponse, initiatorInfo);
153 }
154
155 void FrameFetchContext::dispatchDidLoadResourceFromMemoryCache(const ResourceRequest& request, const ResourceResponse& response)
156 {
157     m_frame->loader().client()->dispatchDidLoadResourceFromMemoryCache(request, response);
158 }
159
160 void FrameFetchContext::dispatchDidReceiveResponse(DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r, ResourceLoader* resourceLoader)
161 {
162     if (Page* page = m_frame->page())
163         page->progress().incrementProgress(identifier, r);
164     m_frame->loader().client()->dispatchDidReceiveResponse(loader, identifier, r);
165     InspectorInstrumentation::didReceiveResourceResponse(m_frame, identifier, ensureLoader(loader), r, resourceLoader);
166 }
167
168 void FrameFetchContext::dispatchDidReceiveData(DocumentLoader*, unsigned long identifier, const char* data, int dataLength, int encodedDataLength)
169 {
170     if (Page* page = m_frame->page())
171         page->progress().incrementProgress(identifier, data, dataLength);
172     InspectorInstrumentation::didReceiveData(m_frame, identifier, data, dataLength, encodedDataLength);
173 }
174
175 void FrameFetchContext::dispatchDidDownloadData(DocumentLoader*, unsigned long identifier, int dataLength, int encodedDataLength)
176 {
177     if (Page* page = m_frame->page())
178         page->progress().incrementProgress(identifier, 0, dataLength);
179     InspectorInstrumentation::didReceiveData(m_frame, identifier, 0, dataLength, encodedDataLength);
180 }
181
182 void FrameFetchContext::dispatchDidFinishLoading(DocumentLoader* loader, unsigned long identifier, double finishTime, int64_t encodedDataLength)
183 {
184     if (Page* page = m_frame->page())
185         page->progress().completeProgress(identifier);
186     m_frame->loader().client()->dispatchDidFinishLoading(loader, identifier);
187
188     InspectorInstrumentation::didFinishLoading(m_frame, identifier, ensureLoader(loader), finishTime, encodedDataLength);
189 }
190
191 void FrameFetchContext::dispatchDidFail(DocumentLoader* loader, unsigned long identifier, const ResourceError& error)
192 {
193     if (Page* page = m_frame->page())
194         page->progress().completeProgress(identifier);
195     InspectorInstrumentation::didFailLoading(m_frame, identifier, error);
196 }
197
198 void FrameFetchContext::sendRemainingDelegateMessages(DocumentLoader* loader, unsigned long identifier, const ResourceResponse& response, int dataLength)
199 {
200     if (!response.isNull())
201         dispatchDidReceiveResponse(ensureLoader(loader), identifier, response);
202
203     if (dataLength > 0)
204         dispatchDidReceiveData(ensureLoader(loader), identifier, 0, dataLength, 0);
205
206     dispatchDidFinishLoading(ensureLoader(loader), identifier, 0, 0);
207 }
208
209 } // namespace WebCore