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/frame/LocalFrame.h"
36 #include "core/inspector/InspectorInstrumentation.h"
37 #include "core/inspector/InspectorTraceEvents.h"
38 #include "core/loader/DocumentLoader.h"
39 #include "core/loader/FrameLoader.h"
40 #include "core/loader/FrameLoaderClient.h"
41 #include "core/loader/ProgressTracker.h"
42 #include "core/page/Page.h"
43 #include "core/frame/Settings.h"
44 #include "platform/weborigin/SecurityPolicy.h"
48 FrameFetchContext::FrameFetchContext(LocalFrame* frame)
53 void FrameFetchContext::reportLocalLoadFailed(const KURL& url)
55 FrameLoader::reportLocalLoadFailed(m_frame, url.elidedString());
58 void FrameFetchContext::addAdditionalRequestHeaders(Document* document, ResourceRequest& request, FetchResourceType type)
60 bool isMainResource = type == FetchMainResource;
61 if (!isMainResource) {
62 String outgoingReferrer;
63 String outgoingOrigin;
64 if (request.httpReferrer().isNull()) {
65 outgoingReferrer = document->outgoingReferrer();
66 outgoingOrigin = document->outgoingOrigin();
68 outgoingReferrer = request.httpReferrer();
69 outgoingOrigin = SecurityOrigin::createFromString(outgoingReferrer)->toString();
72 outgoingReferrer = SecurityPolicy::generateReferrerHeader(document->referrerPolicy(), request.url(), outgoingReferrer);
73 if (outgoingReferrer.isEmpty())
74 request.clearHTTPReferrer();
75 else if (!request.httpReferrer())
76 request.setHTTPReferrer(Referrer(outgoingReferrer, document->referrerPolicy()));
78 request.addHTTPOriginIfNeeded(AtomicString(outgoingOrigin));
81 // The remaining modifications are only necessary for HTTP and HTTPS.
82 if (!request.url().isEmpty() && !request.url().protocolIsInHTTPFamily())
85 m_frame->loader().applyUserAgent(request);
88 void FrameFetchContext::setFirstPartyForCookies(ResourceRequest& request)
90 if (m_frame->tree().top()->isLocalFrame())
91 request.setFirstPartyForCookies(toLocalFrame(m_frame->tree().top())->document()->firstPartyForCookies());
94 CachePolicy FrameFetchContext::cachePolicy(Document* document) const
96 if (document && document->loadEventFinished())
97 return CachePolicyVerify;
99 FrameLoadType loadType = m_frame->loader().loadType();
100 if (loadType == FrameLoadTypeReloadFromOrigin)
101 return CachePolicyReload;
103 Frame* parentFrame = m_frame->tree().parent();
104 if (parentFrame && parentFrame->isLocalFrame()) {
105 CachePolicy parentCachePolicy = toLocalFrame(parentFrame)->loader().fetchContext().cachePolicy(toLocalFrame(parentFrame)->document());
106 if (parentCachePolicy != CachePolicyVerify)
107 return parentCachePolicy;
110 if (loadType == FrameLoadTypeReload)
111 return CachePolicyRevalidate;
113 DocumentLoader* loader = document ? document->loader() : 0;
114 if (loader && loader->request().cachePolicy() == ReturnCacheDataElseLoad)
115 return CachePolicyHistoryBuffer;
116 return CachePolicyVerify;
120 // FIXME(http://crbug.com/274173):
121 // |loader| can be null if the resource is loaded from imported document.
122 // This means inspector, which uses DocumentLoader as an grouping entity,
123 // cannot see imported documents.
124 inline DocumentLoader* FrameFetchContext::ensureLoader(DocumentLoader* loader)
126 return loader ? loader : m_frame->loader().documentLoader();
129 void FrameFetchContext::dispatchDidChangeResourcePriority(unsigned long identifier, ResourceLoadPriority loadPriority, int intraPriorityValue)
131 m_frame->loader().client()->dispatchDidChangeResourcePriority(identifier, loadPriority, intraPriorityValue);
134 void FrameFetchContext::dispatchWillSendRequest(DocumentLoader* loader, unsigned long identifier, ResourceRequest& request, const ResourceResponse& redirectResponse, const FetchInitiatorInfo& initiatorInfo)
136 m_frame->loader().applyUserAgent(request);
137 m_frame->loader().client()->dispatchWillSendRequest(loader, identifier, request, redirectResponse);
138 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceSendRequest", "data", InspectorSendRequestEvent::data(identifier, m_frame, request));
139 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
140 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
141 InspectorInstrumentation::willSendRequest(m_frame, identifier, ensureLoader(loader), request, redirectResponse, initiatorInfo);
144 void FrameFetchContext::dispatchDidLoadResourceFromMemoryCache(const ResourceRequest& request, const ResourceResponse& response)
146 m_frame->loader().client()->dispatchDidLoadResourceFromMemoryCache(request, response);
149 void FrameFetchContext::dispatchDidReceiveResponse(DocumentLoader* loader, unsigned long identifier, const ResourceResponse& r, ResourceLoader* resourceLoader)
151 m_frame->loader().progress().incrementProgress(identifier, r);
152 m_frame->loader().client()->dispatchDidReceiveResponse(loader, identifier, r);
153 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceReceiveResponse", "data", InspectorReceiveResponseEvent::data(identifier, m_frame, r));
154 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
155 InspectorInstrumentation::didReceiveResourceResponse(m_frame, identifier, ensureLoader(loader), r, resourceLoader);
158 void FrameFetchContext::dispatchDidReceiveData(DocumentLoader*, unsigned long identifier, const char* data, int dataLength, int encodedDataLength)
160 m_frame->loader().progress().incrementProgress(identifier, data, dataLength);
161 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceReceivedData", "data", InspectorReceiveDataEvent::data(identifier, m_frame, encodedDataLength));
162 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
163 InspectorInstrumentation::didReceiveData(m_frame, identifier, data, dataLength, encodedDataLength);
166 void FrameFetchContext::dispatchDidDownloadData(DocumentLoader*, unsigned long identifier, int dataLength, int encodedDataLength)
168 m_frame->loader().progress().incrementProgress(identifier, 0, dataLength);
169 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceReceivedData", "data", InspectorReceiveDataEvent::data(identifier, m_frame, encodedDataLength));
170 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
171 InspectorInstrumentation::didReceiveData(m_frame, identifier, 0, dataLength, encodedDataLength);
174 void FrameFetchContext::dispatchDidFinishLoading(DocumentLoader* loader, unsigned long identifier, double finishTime, int64_t encodedDataLength)
176 m_frame->loader().progress().completeProgress(identifier);
177 m_frame->loader().client()->dispatchDidFinishLoading(loader, identifier);
179 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceFinish", "data", InspectorResourceFinishEvent::data(identifier, finishTime, false));
180 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
181 InspectorInstrumentation::didFinishLoading(m_frame, identifier, ensureLoader(loader), finishTime, encodedDataLength);
184 void FrameFetchContext::dispatchDidFail(DocumentLoader* loader, unsigned long identifier, const ResourceError& error)
186 m_frame->loader().progress().completeProgress(identifier);
187 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "ResourceFinish", "data", InspectorResourceFinishEvent::data(identifier, 0, true));
188 // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
189 InspectorInstrumentation::didFailLoading(m_frame, identifier, error);
192 void FrameFetchContext::sendRemainingDelegateMessages(DocumentLoader* loader, unsigned long identifier, const ResourceResponse& response, int dataLength)
194 if (!response.isNull())
195 dispatchDidReceiveResponse(ensureLoader(loader), identifier, response);
198 dispatchDidReceiveData(ensureLoader(loader), identifier, 0, dataLength, 0);
200 dispatchDidFinishLoading(ensureLoader(loader), identifier, 0, 0);