2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/loader/FrameFetchContext.h"
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"
47 static const char defaultAcceptHeader[] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
49 FrameFetchContext::FrameFetchContext(Frame* frame)
54 void FrameFetchContext::reportLocalLoadFailed(const KURL& url)
56 FrameLoader::reportLocalLoadFailed(m_frame, url.elidedString());
59 void FrameFetchContext::addAdditionalRequestHeaders(Document* document, ResourceRequest& request, FetchResourceType type)
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();
69 outgoingReferrer = request.httpReferrer();
70 outgoingOrigin = SecurityOrigin::createFromString(outgoingReferrer)->toString();
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()));
79 FrameLoader::addHTTPOriginIfNeeded(request, AtomicString(outgoingOrigin));
82 if (isMainResource && m_frame->isMainFrame())
83 request.setFirstPartyForCookies(request.url());
85 request.setFirstPartyForCookies(m_frame->tree().top()->document()->firstPartyForCookies());
87 // The remaining modifications are only necessary for HTTP and HTTPS.
88 if (!request.url().isEmpty() && !request.url().protocolIsInHTTPFamily())
91 m_frame->loader().applyUserAgent(request);
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");
103 request.setHTTPAccept(defaultAcceptHeader);
105 // Default to sending an empty Origin header if one hasn't been set yet.
106 FrameLoader::addHTTPOriginIfNeeded(request, nullAtom);
109 CachePolicy FrameFetchContext::cachePolicy(Document* document) const
111 if (document && document->loadEventFinished())
112 return CachePolicyVerify;
114 FrameLoadType loadType = m_frame->loader().loadType();
115 if (loadType == FrameLoadTypeReloadFromOrigin)
116 return CachePolicyReload;
118 if (Frame* parentFrame = m_frame->tree().parent()) {
119 CachePolicy parentCachePolicy = parentFrame->loader().fetchContext().cachePolicy(parentFrame->document());
120 if (parentCachePolicy != CachePolicyVerify)
121 return parentCachePolicy;
124 if (loadType == FrameLoadTypeReload)
125 return CachePolicyRevalidate;
127 DocumentLoader* loader = document ? document->loader() : 0;
128 if (loader && loader->request().cachePolicy() == ReturnCacheDataElseLoad)
129 return CachePolicyHistoryBuffer;
130 return CachePolicyVerify;
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)
140 return loader ? loader : m_frame->loader().documentLoader();
143 void FrameFetchContext::dispatchDidChangeResourcePriority(unsigned long identifier, ResourceLoadPriority loadPriority)
145 m_frame->loader().client()->dispatchDidChangeResourcePriority(identifier, loadPriority);
148 void FrameFetchContext::dispatchWillSendRequest(DocumentLoader* loader, unsigned long identifier, ResourceRequest& request, const ResourceResponse& redirectResponse, const FetchInitiatorInfo& initiatorInfo)
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);
155 void FrameFetchContext::dispatchDidLoadResourceFromMemoryCache(const ResourceRequest& request, const ResourceResponse& response)
157 m_frame->loader().client()->dispatchDidLoadResourceFromMemoryCache(request, response);
160 void FrameFetchContext::dispatchDidReceiveResponse(DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r, ResourceLoader* resourceLoader)
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);
168 void FrameFetchContext::dispatchDidReceiveData(DocumentLoader*, unsigned long identifier, const char* data, int dataLength, int encodedDataLength)
170 if (Page* page = m_frame->page())
171 page->progress().incrementProgress(identifier, data, dataLength);
172 InspectorInstrumentation::didReceiveData(m_frame, identifier, data, dataLength, encodedDataLength);
175 void FrameFetchContext::dispatchDidDownloadData(DocumentLoader*, unsigned long identifier, int dataLength, int encodedDataLength)
177 if (Page* page = m_frame->page())
178 page->progress().incrementProgress(identifier, 0, dataLength);
179 InspectorInstrumentation::didReceiveData(m_frame, identifier, 0, dataLength, encodedDataLength);
182 void FrameFetchContext::dispatchDidFinishLoading(DocumentLoader* loader, unsigned long identifier, double finishTime, int64_t encodedDataLength)
184 if (Page* page = m_frame->page())
185 page->progress().completeProgress(identifier);
186 m_frame->loader().client()->dispatchDidFinishLoading(loader, identifier);
188 InspectorInstrumentation::didFinishLoading(m_frame, identifier, ensureLoader(loader), finishTime, encodedDataLength);
191 void FrameFetchContext::dispatchDidFail(DocumentLoader* loader, unsigned long identifier, const ResourceError& error)
193 if (Page* page = m_frame->page())
194 page->progress().completeProgress(identifier);
195 InspectorInstrumentation::didFailLoading(m_frame, identifier, error);
198 void FrameFetchContext::sendRemainingDelegateMessages(DocumentLoader* loader, unsigned long identifier, const ResourceResponse& response, int dataLength)
200 if (!response.isNull())
201 dispatchDidReceiveResponse(ensureLoader(loader), identifier, response);
204 dispatchDidReceiveData(ensureLoader(loader), identifier, 0, dataLength, 0);
206 dispatchDidFinishLoading(ensureLoader(loader), identifier, 0, 0);
209 } // namespace WebCore